PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/scratch/deliciolytics.co.uk/vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/test/simple_backend_test.rb

http://chrisroos.googlecode.com/
Ruby | 568 lines | 442 code | 107 blank | 19 comment | 1 complexity | 1419027a60d68de9861c61464feca3d4 MD5 | raw file
  1. # encoding: utf-8
  2. $:.unshift "lib"
  3. require 'rubygems'
  4. require 'test/unit'
  5. require 'mocha'
  6. require 'i18n'
  7. require 'time'
  8. require 'yaml'
  9. module I18nSimpleBackendTestSetup
  10. def setup_backend
  11. # backend_reset_translations!
  12. @backend = I18n::Backend::Simple.new
  13. @backend.store_translations 'en', :foo => {:bar => 'bar', :baz => 'baz'}
  14. @locale_dir = File.dirname(__FILE__) + '/locale'
  15. end
  16. alias :setup :setup_backend
  17. # def backend_reset_translations!
  18. # I18n::Backend::Simple::ClassMethods.send :class_variable_set, :@@translations, {}
  19. # end
  20. def backend_get_translations
  21. # I18n::Backend::Simple::ClassMethods.send :class_variable_get, :@@translations
  22. @backend.instance_variable_get :@translations
  23. end
  24. def add_datetime_translations
  25. @backend.store_translations :'de', {
  26. :date => {
  27. :formats => {
  28. :default => "%d.%m.%Y",
  29. :short => "%d. %b",
  30. :long => "%d. %B %Y",
  31. },
  32. :day_names => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag),
  33. :abbr_day_names => %w(So Mo Di Mi Do Fr Sa),
  34. :month_names => %w(Januar Februar März April Mai Juni Juli August September Oktober November Dezember).unshift(nil),
  35. :abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil),
  36. :order => [:day, :month, :year]
  37. },
  38. :time => {
  39. :formats => {
  40. :default => "%a, %d. %b %Y %H:%M:%S %z",
  41. :short => "%d. %b %H:%M",
  42. :long => "%d. %B %Y %H:%M",
  43. },
  44. :am => 'am',
  45. :pm => 'pm'
  46. },
  47. :datetime => {
  48. :distance_in_words => {
  49. :half_a_minute => 'half a minute',
  50. :less_than_x_seconds => {
  51. :one => 'less than 1 second',
  52. :other => 'less than {{count}} seconds'
  53. },
  54. :x_seconds => {
  55. :one => '1 second',
  56. :other => '{{count}} seconds'
  57. },
  58. :less_than_x_minutes => {
  59. :one => 'less than a minute',
  60. :other => 'less than {{count}} minutes'
  61. },
  62. :x_minutes => {
  63. :one => '1 minute',
  64. :other => '{{count}} minutes'
  65. },
  66. :about_x_hours => {
  67. :one => 'about 1 hour',
  68. :other => 'about {{count}} hours'
  69. },
  70. :x_days => {
  71. :one => '1 day',
  72. :other => '{{count}} days'
  73. },
  74. :about_x_months => {
  75. :one => 'about 1 month',
  76. :other => 'about {{count}} months'
  77. },
  78. :x_months => {
  79. :one => '1 month',
  80. :other => '{{count}} months'
  81. },
  82. :about_x_years => {
  83. :one => 'about 1 year',
  84. :other => 'about {{count}} year'
  85. },
  86. :over_x_years => {
  87. :one => 'over 1 year',
  88. :other => 'over {{count}} years'
  89. }
  90. }
  91. }
  92. }
  93. end
  94. end
  95. class I18nSimpleBackendTranslationsTest < Test::Unit::TestCase
  96. include I18nSimpleBackendTestSetup
  97. def test_store_translations_adds_translations # no, really :-)
  98. @backend.store_translations :'en', :foo => 'bar'
  99. assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
  100. end
  101. def test_store_translations_deep_merges_translations
  102. @backend.store_translations :'en', :foo => {:bar => 'bar'}
  103. @backend.store_translations :'en', :foo => {:baz => 'baz'}
  104. assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
  105. end
  106. def test_store_translations_forces_locale_to_sym
  107. @backend.store_translations 'en', :foo => 'bar'
  108. assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
  109. end
  110. def test_store_translations_converts_keys_to_symbols
  111. # backend_reset_translations!
  112. @backend.store_translations 'en', 'foo' => {'bar' => 'bar', 'baz' => 'baz'}
  113. assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
  114. end
  115. end
  116. class I18nSimpleBackendAvailableLocalesTest < Test::Unit::TestCase
  117. def test_available_locales
  118. @backend = I18n::Backend::Simple.new
  119. @backend.store_translations 'de', :foo => 'bar'
  120. @backend.store_translations 'en', :foo => 'foo'
  121. assert_equal ['de', 'en'], @backend.available_locales.map{|locale| locale.to_s }.sort
  122. end
  123. end
  124. class I18nSimpleBackendTranslateTest < Test::Unit::TestCase
  125. include I18nSimpleBackendTestSetup
  126. def test_translate_calls_lookup_with_locale_given
  127. @backend.expects(:lookup).with('de', :bar, [:foo]).returns 'bar'
  128. @backend.translate 'de', :bar, :scope => [:foo]
  129. end
  130. def test_given_no_keys_it_returns_the_default
  131. assert_equal 'default', @backend.translate('en', nil, :default => 'default')
  132. end
  133. def test_translate_given_a_symbol_as_a_default_translates_the_symbol
  134. assert_equal 'bar', @backend.translate('en', nil, :scope => [:foo], :default => :bar)
  135. end
  136. def test_translate_given_an_array_as_default_uses_the_first_match
  137. assert_equal 'bar', @backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :bar])
  138. end
  139. def test_translate_given_an_array_of_inexistent_keys_it_raises_missing_translation_data
  140. assert_raise I18n::MissingTranslationData do
  141. @backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :does_not_exist_3])
  142. end
  143. end
  144. def test_translate_an_array_of_keys_translates_all_of_them
  145. assert_equal %w(bar baz), @backend.translate('en', [:bar, :baz], :scope => [:foo])
  146. end
  147. def test_translate_calls_pluralize
  148. @backend.expects(:pluralize).with 'en', 'bar', 1
  149. @backend.translate 'en', :bar, :scope => [:foo], :count => 1
  150. end
  151. def test_translate_calls_interpolate
  152. @backend.expects(:interpolate).with 'en', 'bar', {}
  153. @backend.translate 'en', :bar, :scope => [:foo]
  154. end
  155. def test_translate_calls_interpolate_including_count_as_a_value
  156. @backend.expects(:interpolate).with 'en', 'bar', {:count => 1}
  157. @backend.translate 'en', :bar, :scope => [:foo], :count => 1
  158. end
  159. def test_translate_given_nil_as_a_locale_raises_an_argument_error
  160. assert_raise(I18n::InvalidLocale){ @backend.translate nil, :bar }
  161. end
  162. def test_translate_with_a_bogus_key_and_no_default_raises_missing_translation_data
  163. assert_raise(I18n::MissingTranslationData){ @backend.translate 'de', :bogus }
  164. end
  165. end
  166. class I18nSimpleBackendLookupTest < Test::Unit::TestCase
  167. include I18nSimpleBackendTestSetup
  168. # useful because this way we can use the backend with no key for interpolation/pluralization
  169. def test_lookup_given_nil_as_a_key_returns_nil
  170. assert_nil @backend.send(:lookup, 'en', nil)
  171. end
  172. def test_lookup_given_nested_keys_looks_up_a_nested_hash_value
  173. assert_equal 'bar', @backend.send(:lookup, 'en', :bar, [:foo])
  174. end
  175. end
  176. class I18nSimpleBackendPluralizeTest < Test::Unit::TestCase
  177. include I18nSimpleBackendTestSetup
  178. def test_pluralize_given_nil_returns_the_given_entry
  179. entry = {:one => 'bar', :other => 'bars'}
  180. assert_equal entry, @backend.send(:pluralize, nil, entry, nil)
  181. end
  182. def test_pluralize_given_0_returns_zero_string_if_zero_key_given
  183. assert_equal 'zero', @backend.send(:pluralize, nil, {:zero => 'zero', :one => 'bar', :other => 'bars'}, 0)
  184. end
  185. def test_pluralize_given_0_returns_plural_string_if_no_zero_key_given
  186. assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 0)
  187. end
  188. def test_pluralize_given_1_returns_singular_string
  189. assert_equal 'bar', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 1)
  190. end
  191. def test_pluralize_given_2_returns_plural_string
  192. assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 2)
  193. end
  194. def test_pluralize_given_3_returns_plural_string
  195. assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 3)
  196. end
  197. def test_interpolate_given_incomplete_pluralization_data_raises_invalid_pluralization_data
  198. assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, {:one => 'bar'}, 2) }
  199. end
  200. # def test_interpolate_given_a_string_raises_invalid_pluralization_data
  201. # assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, 'bar', 2) }
  202. # end
  203. #
  204. # def test_interpolate_given_an_array_raises_invalid_pluralization_data
  205. # assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, ['bar'], 2) }
  206. # end
  207. end
  208. class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase
  209. include I18nSimpleBackendTestSetup
  210. def test_interpolate_given_a_value_hash_interpolates_the_values_to_the_string
  211. assert_equal 'Hi David!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'David')
  212. end
  213. def test_interpolate_given_a_value_hash_interpolates_into_unicode_string
  214. assert_equal 'Häi David!', @backend.send(:interpolate, nil, 'Häi {{name}}!', :name => 'David')
  215. end
  216. def test_interpolate_given_an_unicode_value_hash_interpolates_to_the_string
  217. assert_equal 'Hi ????!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => '????')
  218. end
  219. def test_interpolate_given_an_unicode_value_hash_interpolates_into_unicode_string
  220. assert_equal '????????????!', @backend.send(:interpolate, nil, '??????{{name}}??!', :name => '????')
  221. end
  222. if Kernel.const_defined?(:Encoding)
  223. def test_interpolate_given_a_non_unicode_multibyte_value_hash_interpolates_into_a_string_with_the_same_encoding
  224. assert_equal euc_jp('Hi ????!'), @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => euc_jp('????'))
  225. end
  226. def test_interpolate_given_an_unicode_value_hash_into_a_non_unicode_multibyte_string_raises_encoding_compatibility_error
  227. assert_raise(Encoding::CompatibilityError) do
  228. @backend.send(:interpolate, nil, euc_jp('??????{{name}}??!'), :name => '????')
  229. end
  230. end
  231. def test_interpolate_given_a_non_unicode_multibyte_value_hash_into_an_unicode_string_raises_encoding_compatibility_error
  232. assert_raise(Encoding::CompatibilityError) do
  233. @backend.send(:interpolate, nil, '??????{{name}}??!', :name => euc_jp('????'))
  234. end
  235. end
  236. end
  237. def test_interpolate_given_nil_as_a_string_returns_nil
  238. assert_nil @backend.send(:interpolate, nil, nil, :name => 'David')
  239. end
  240. def test_interpolate_given_an_non_string_as_a_string_returns_nil
  241. assert_equal [], @backend.send(:interpolate, nil, [], :name => 'David')
  242. end
  243. def test_interpolate_given_a_values_hash_with_nil_values_interpolates_the_string
  244. assert_equal 'Hi !', @backend.send(:interpolate, nil, 'Hi {{name}}!', {:name => nil})
  245. end
  246. def test_interpolate_given_an_empty_values_hash_raises_missing_interpolation_argument
  247. assert_raise(I18n::MissingInterpolationArgument) { @backend.send(:interpolate, nil, 'Hi {{name}}!', {}) }
  248. end
  249. def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
  250. assert_raise(I18n::ReservedInterpolationKey) { @backend.send(:interpolate, nil, '{{default}}', {:default => nil}) }
  251. end
  252. private
  253. def euc_jp(string)
  254. string.encode!(Encoding::EUC_JP)
  255. end
  256. end
  257. class I18nSimpleBackendLocalizeDateTest < Test::Unit::TestCase
  258. include I18nSimpleBackendTestSetup
  259. def setup
  260. @backend = I18n::Backend::Simple.new
  261. add_datetime_translations
  262. @date = Date.new 2008, 1, 1
  263. end
  264. def test_translate_given_the_short_format_it_uses_it
  265. assert_equal '01. Jan', @backend.localize('de', @date, :short)
  266. end
  267. def test_translate_given_the_long_format_it_uses_it
  268. assert_equal '01. Januar 2008', @backend.localize('de', @date, :long)
  269. end
  270. def test_translate_given_the_default_format_it_uses_it
  271. assert_equal '01.01.2008', @backend.localize('de', @date, :default)
  272. end
  273. def test_translate_given_a_day_name_format_it_returns_a_day_name
  274. assert_equal 'Dienstag', @backend.localize('de', @date, '%A')
  275. end
  276. def test_translate_given_an_abbr_day_name_format_it_returns_an_abbrevated_day_name
  277. assert_equal 'Di', @backend.localize('de', @date, '%a')
  278. end
  279. def test_translate_given_a_month_name_format_it_returns_a_month_name
  280. assert_equal 'Januar', @backend.localize('de', @date, '%B')
  281. end
  282. def test_translate_given_an_abbr_month_name_format_it_returns_an_abbrevated_month_name
  283. assert_equal 'Jan', @backend.localize('de', @date, '%b')
  284. end
  285. def test_translate_given_no_format_it_does_not_fail
  286. assert_nothing_raised{ @backend.localize 'de', @date }
  287. end
  288. def test_translate_given_an_unknown_format_it_does_not_fail
  289. assert_nothing_raised{ @backend.localize 'de', @date, '%x' }
  290. end
  291. def test_localize_nil_raises_argument_error
  292. assert_raise(I18n::ArgumentError) { @backend.localize 'de', nil }
  293. end
  294. def test_localize_object_raises_argument_error
  295. assert_raise(I18n::ArgumentError) { @backend.localize 'de', Object.new }
  296. end
  297. end
  298. class I18nSimpleBackendLocalizeDateTimeTest < Test::Unit::TestCase
  299. include I18nSimpleBackendTestSetup
  300. def setup
  301. @backend = I18n::Backend::Simple.new
  302. add_datetime_translations
  303. @morning = DateTime.new 2008, 1, 1, 6
  304. @evening = DateTime.new 2008, 1, 1, 18
  305. end
  306. def test_translate_given_the_short_format_it_uses_it
  307. assert_equal '01. Jan 06:00', @backend.localize('de', @morning, :short)
  308. end
  309. def test_translate_given_the_long_format_it_uses_it
  310. assert_equal '01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
  311. end
  312. def test_translate_given_the_default_format_it_uses_it
  313. assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
  314. end
  315. def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
  316. assert_equal 'Dienstag', @backend.localize('de', @morning, '%A')
  317. end
  318. def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
  319. assert_equal 'Di', @backend.localize('de', @morning, '%a')
  320. end
  321. def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
  322. assert_equal 'Januar', @backend.localize('de', @morning, '%B')
  323. end
  324. def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
  325. assert_equal 'Jan', @backend.localize('de', @morning, '%b')
  326. end
  327. def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
  328. assert_equal 'am', @backend.localize('de', @morning, '%p')
  329. assert_equal 'pm', @backend.localize('de', @evening, '%p')
  330. end
  331. def test_translate_given_no_format_it_does_not_fail
  332. assert_nothing_raised{ @backend.localize 'de', @morning }
  333. end
  334. def test_translate_given_an_unknown_format_it_does_not_fail
  335. assert_nothing_raised{ @backend.localize 'de', @morning, '%x' }
  336. end
  337. end
  338. class I18nSimpleBackendLocalizeTimeTest < Test::Unit::TestCase
  339. include I18nSimpleBackendTestSetup
  340. def setup
  341. @old_timezone, ENV['TZ'] = ENV['TZ'], 'UTC'
  342. @backend = I18n::Backend::Simple.new
  343. add_datetime_translations
  344. @morning = Time.parse '2008-01-01 6:00 UTC'
  345. @evening = Time.parse '2008-01-01 18:00 UTC'
  346. end
  347. def teardown
  348. @old_timezone ? ENV['TZ'] = @old_timezone : ENV.delete('TZ')
  349. end
  350. def test_translate_given_the_short_format_it_uses_it
  351. assert_equal '01. Jan 06:00', @backend.localize('de', @morning, :short)
  352. end
  353. def test_translate_given_the_long_format_it_uses_it
  354. assert_equal '01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
  355. end
  356. # TODO Seems to break on Windows because ENV['TZ'] is ignored. What's a better way to do this?
  357. # def test_translate_given_the_default_format_it_uses_it
  358. # assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
  359. # end
  360. def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
  361. assert_equal 'Dienstag', @backend.localize('de', @morning, '%A')
  362. end
  363. def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
  364. assert_equal 'Di', @backend.localize('de', @morning, '%a')
  365. end
  366. def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
  367. assert_equal 'Januar', @backend.localize('de', @morning, '%B')
  368. end
  369. def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
  370. assert_equal 'Jan', @backend.localize('de', @morning, '%b')
  371. end
  372. def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
  373. assert_equal 'am', @backend.localize('de', @morning, '%p')
  374. assert_equal 'pm', @backend.localize('de', @evening, '%p')
  375. end
  376. def test_translate_given_no_format_it_does_not_fail
  377. assert_nothing_raised{ @backend.localize 'de', @morning }
  378. end
  379. def test_translate_given_an_unknown_format_it_does_not_fail
  380. assert_nothing_raised{ @backend.localize 'de', @morning, '%x' }
  381. end
  382. end
  383. class I18nSimpleBackendHelperMethodsTest < Test::Unit::TestCase
  384. def setup
  385. @backend = I18n::Backend::Simple.new
  386. end
  387. def test_deep_symbolize_keys_works
  388. result = @backend.send :deep_symbolize_keys, 'foo' => {'bar' => {'baz' => 'bar'}}
  389. expected = {:foo => {:bar => {:baz => 'bar'}}}
  390. assert_equal expected, result
  391. end
  392. end
  393. class I18nSimpleBackendLoadTranslationsTest < Test::Unit::TestCase
  394. include I18nSimpleBackendTestSetup
  395. def test_load_translations_with_unknown_file_type_raises_exception
  396. assert_raise(I18n::UnknownFileType) { @backend.load_translations "#{@locale_dir}/en.xml" }
  397. end
  398. def test_load_translations_with_ruby_file_type_does_not_raise_exception
  399. assert_nothing_raised { @backend.load_translations "#{@locale_dir}/en.rb" }
  400. end
  401. def test_load_rb_loads_data_from_ruby_file
  402. data = @backend.send :load_rb, "#{@locale_dir}/en.rb"
  403. assert_equal({:'en-Ruby' => {:foo => {:bar => "baz"}}}, data)
  404. end
  405. def test_load_rb_loads_data_from_yaml_file
  406. data = @backend.send :load_yml, "#{@locale_dir}/en.yml"
  407. assert_equal({'en-Yaml' => {'foo' => {'bar' => 'baz'}}}, data)
  408. end
  409. def test_load_translations_loads_from_different_file_formats
  410. @backend = I18n::Backend::Simple.new
  411. @backend.load_translations "#{@locale_dir}/en.rb", "#{@locale_dir}/en.yml"
  412. expected = {
  413. :'en-Ruby' => {:foo => {:bar => "baz"}},
  414. :'en-Yaml' => {:foo => {:bar => "baz"}}
  415. }
  416. assert_equal expected, backend_get_translations
  417. end
  418. end
  419. class I18nSimpleBackendLoadPathTest < Test::Unit::TestCase
  420. include I18nSimpleBackendTestSetup
  421. def teardown
  422. I18n.load_path = []
  423. end
  424. def test_nested_load_paths_do_not_break_locale_loading
  425. @backend = I18n::Backend::Simple.new
  426. I18n.load_path = [[File.dirname(__FILE__) + '/locale/en.yml']]
  427. assert_nil backend_get_translations
  428. assert_nothing_raised { @backend.send :init_translations }
  429. assert_not_nil backend_get_translations
  430. end
  431. def test_adding_arrays_of_filenames_to_load_path_do_not_break_locale_loading
  432. @backend = I18n::Backend::Simple.new
  433. I18n.load_path << Dir[File.dirname(__FILE__) + '/locale/*.{rb,yml}']
  434. assert_nil backend_get_translations
  435. assert_nothing_raised { @backend.send :init_translations }
  436. assert_not_nil backend_get_translations
  437. end
  438. end
  439. class I18nSimpleBackendReloadTranslationsTest < Test::Unit::TestCase
  440. include I18nSimpleBackendTestSetup
  441. def setup
  442. @backend = I18n::Backend::Simple.new
  443. I18n.load_path = [File.dirname(__FILE__) + '/locale/en.yml']
  444. assert_nil backend_get_translations
  445. @backend.send :init_translations
  446. end
  447. def teardown
  448. I18n.load_path = []
  449. end
  450. def test_setup
  451. assert_not_nil backend_get_translations
  452. end
  453. def test_reload_translations_unloads_translations
  454. @backend.reload!
  455. assert_nil backend_get_translations
  456. end
  457. def test_reload_translations_uninitializes_translations
  458. @backend.reload!
  459. assert_equal @backend.initialized?, false
  460. end
  461. end