闭社主体 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.

86 lines
2.4 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 for basic strings' do
  6. strings = [
  7. "Hello and welcome to mastodon",
  8. "I'd rather not!",
  9. "a lot of people just want to feel righteous all the time and that's all that matters",
  10. ]
  11. strings.each do |string|
  12. result = described_class.new(string).to_iso_s
  13. expect(result).to eq(:en), string
  14. end
  15. end
  16. it 'detects spanish language' do
  17. string = 'Obtener un Hola y bienvenidos a Mastodon'
  18. result = described_class.new(string).to_iso_s
  19. expect(result).to eq :es
  20. end
  21. describe 'when language can\'t be detected' do
  22. it 'uses default locale when sent an empty document' do
  23. result = described_class.new('').to_iso_s
  24. expect(result).to eq :en
  25. end
  26. describe 'because of a URL' do
  27. it 'uses default locale when sent just a URL' do
  28. string = 'http://example.com/media/2kFTgOJLXhQf0g2nKB4'
  29. cld_result = CLD3::NNetLanguageIdentifier.new(0, 2048).find_language(string)
  30. expect(cld_result).not_to eq :en
  31. result = described_class.new(string).to_iso_s
  32. expect(result).to eq :en
  33. end
  34. end
  35. describe 'with an account' do
  36. it 'uses the account locale when present' do
  37. account = double(user_locale: 'fr')
  38. result = described_class.new('', account).to_iso_s
  39. expect(result).to eq :fr
  40. end
  41. it 'uses default locale when account is present but has no locale' do
  42. account = double(user_locale: nil)
  43. result = described_class.new('', account).to_iso_s
  44. expect(result).to eq :en
  45. end
  46. end
  47. describe 'with an `en` default locale' do
  48. it 'uses the default locale' do
  49. string = ''
  50. result = described_class.new(string).to_iso_s
  51. expect(result).to eq :en
  52. end
  53. end
  54. describe 'with a non-`en` default locale' do
  55. around(:each) do |example|
  56. before = I18n.default_locale
  57. I18n.default_locale = :ja
  58. example.run
  59. I18n.default_locale = before
  60. end
  61. it 'uses the default locale' do
  62. string = ''
  63. result = described_class.new(string).to_iso_s
  64. expect(result).to eq :ja
  65. end
  66. end
  67. end
  68. end
  69. end