require 'test/unit' require 'vendor/plugins/pluralize_for_gettext/lib/pluralize_for_gettext.rb' class PluralizeForGettextTest < Test::Unit::TestCase def test_simple_pluralization assert_plural("{single|plural}", 0 => 'plural', 1 => 'single', 2 => 'plural') end def test_amount_replacement assert_equal "1", "{$N}".pluralize_for(1) assert_equal "1 1", "{$N} {$N}".pluralize_for(1) assert_equal "2", "{$N}".pluralize_for(2) end def test_simple_and_amount_together assert_plural("{{$N} post|{$N} posts}", 0 => '0 posts', 1 => '1 post', 2 => '2 posts') end def test_simple_and_amount_seperated assert_plural("{$N} {post|posts}", 0 => '0 posts', 1 => '1 post', 2 => '2 posts') end def test_two_part_pluralization assert_plural("single~~plural", 0 => 'plural', 1 => 'single', 2 => 'plural') end def test_three_part_pluralization assert_plural("none~~single~~plural", 0 => 'none', 1 => 'single', 2 => 'plural') end def test_conditional_pluralization assert_plural("$N==1:single~~$N>10:many~~else:plural", 0 => 'plural', 1 => 'single', 2 => 'plural', 10 => 'plural', 11 => 'many', 100 => 'many') end def test_partially_conditional_pluralization assert_plural("single~~$N<=6:plural~~$N==8:eight~~$N>6:many but eight", 0 => 'plural', 1 => 'single', 2 => 'plural', 7 => 'many but eight', 8 => 'eight', 9 => 'many but eight') end def test_spaces assert_plural(" single ~~ plural ", 0 => 'plural ', 1 => 'single ', 2 => 'plural ') end def test_variable_substitution test = "$N==0:No posts, {1:write one}.~~$N<6:You have {$N} {post|posts}.~~else:You have {$N} posts. {2:Click here to view more}." assert_equal "No posts, >>write one<<.", try_substitution(0, test) assert_equal "You have 1 post.", try_substitution(1, test) assert_equal "You have 5 posts.", try_substitution(5, test) assert_equal "You have 6 posts. >>Click here to view more<<.", try_substitution(6, test) end def test_variable_substitution_nested test = "$N==0:No posts. {1:create one}.~~else:You have {1:{$N} new {post|posts}}..." assert_equal "No posts. >>create one<<.", try_substitution(0, test) assert_equal "You have >>1 new post<<...", try_substitution(1, test) assert_equal "You have >>2 new posts<<...", try_substitution(2, test) end private def assert_plural(test, response) response.each do |n, expected| assert_equal expected, test.pluralize_for(n) end end def try_substitution(count, test) test.pluralize_for(count) do |i| i[1] = ">>"+i[1]+"<<" unless i[1].nil? i[2] = ">>"+i[2]+"<<" unless i[2].nil? end end end