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

88 lines
2.5 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 'confirm language engine cant detect' do
  23. result = CLD.detect_language('')
  24. expect(result[:reliable]).to be false
  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 = CLD.detect_language(string)[:code]
  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. user = double(:user, locale: 'fr')
  38. account = double(:account, user: user)
  39. result = described_class.new('', account).to_iso_s
  40. expect(result).to eq :fr
  41. end
  42. it 'uses default locale when account is present but has no locale' do
  43. user = double(:user, locale: nil)
  44. account = double(:accunt, user: user)
  45. result = described_class.new('', account).to_iso_s
  46. expect(result).to eq :en
  47. end
  48. end
  49. describe 'with an `en` default locale' do
  50. it 'uses the default locale' do
  51. string = ''
  52. result = described_class.new(string).to_iso_s
  53. expect(result).to eq :en
  54. end
  55. end
  56. describe 'with a non-`en` default locale' do
  57. around(:each) do |example|
  58. before = I18n.default_locale
  59. I18n.default_locale = :ja
  60. example.run
  61. I18n.default_locale = before
  62. end
  63. it 'uses the default locale' do
  64. string = ''
  65. result = described_class.new(string).to_iso_s
  66. expect(result).to eq :ja
  67. end
  68. end
  69. end
  70. end
  71. end