闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.3 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe LanguageDetector do
  4. describe 'to_iso_s' do
  5. it 'detects english language' do
  6. string = 'Hello and welcome to mastodon'
  7. result = described_class.new(string).to_iso_s
  8. expect(result).to eq :en
  9. end
  10. it 'detects spanish language' do
  11. string = 'Obtener un Hola y bienvenidos a Mastodon'
  12. result = described_class.new(string).to_iso_s
  13. expect(result).to eq :es
  14. end
  15. describe 'when language can\'t be detected' do
  16. it 'confirm language engine cant detect' do
  17. result = WhatLanguage.new(:all).language_iso('')
  18. expect(result).to be_nil
  19. end
  20. describe 'because of a URL' do
  21. it 'uses default locale when sent just a URL' do
  22. string = 'http://example.com/media/2kFTgOJLXhQf0g2nKB4'
  23. wl_result = WhatLanguage.new(:all).language_iso(string)
  24. expect(wl_result).not_to eq :en
  25. result = described_class.new(string).to_iso_s
  26. expect(result).to eq :en
  27. end
  28. end
  29. describe 'with an account' do
  30. it 'uses the account locale when present' do
  31. user = double(:user, locale: 'fr')
  32. account = double(:account, user: user)
  33. result = described_class.new('', account).to_iso_s
  34. expect(result).to eq :fr
  35. end
  36. it 'uses default locale when account is present but has no locale' do
  37. user = double(:user, locale: nil)
  38. account = double(:accunt, user: user)
  39. result = described_class.new('', account).to_iso_s
  40. expect(result).to eq :en
  41. end
  42. end
  43. describe 'with an `en` default locale' do
  44. it 'uses the default locale' do
  45. string = ''
  46. result = described_class.new(string).to_iso_s
  47. expect(result).to eq :en
  48. end
  49. end
  50. describe 'with a non-`en` default locale' do
  51. around(:each) do |example|
  52. before = I18n.default_locale
  53. I18n.default_locale = :ja
  54. example.run
  55. I18n.default_locale = before
  56. end
  57. it 'uses the default locale' do
  58. string = ''
  59. result = described_class.new(string).to_iso_s
  60. expect(result).to eq :ja
  61. end
  62. end
  63. end
  64. end
  65. end