1 require File.join( File.dirname(__FILE__), '..', 'test_helper' )
2 require 'globalize/locale/language_tag'
4 include Globalize::Locale
6 class LanguageTagTest < ActiveSupport::TestCase
7 test "given a valid tag 'de' returns an LanguageTag from #tag" do
8 assert_instance_of LanguageTag, LanguageTag.tag('de')
11 test "given a valid tag 'de' returns an array of subtags" do
12 assert_equal ['de', nil, nil, nil, nil, nil, nil], LanguageTag::SimpleParser.match('de')
15 test "given a valid tag 'de-DE' returns an array of subtags" do
16 assert_equal ['de', nil, 'DE', nil, nil, nil, nil], LanguageTag::SimpleParser.match('de-DE')
19 test "given a valid lowercase tag 'de-latn-de-variant-x-phonebk' returns an array of subtags" do
20 assert_equal ['de', 'latn', 'de', 'variant', nil, 'x-phonebk', nil],
21 LanguageTag::SimpleParser.match('de-latn-de-variant-x-phonebk')
24 test "given a valid uppercase tag 'DE-LATN-DE-VARIANT-X-PHONEBK' returns an array of subtags" do
25 assert_equal ['DE', 'LATN', 'DE', 'VARIANT', nil, 'X-PHONEBK', nil],
26 LanguageTag::SimpleParser.match('DE-LATN-DE-VARIANT-X-PHONEBK')
29 test "given an invalid tag 'a-DE' test returns false" do
30 assert !LanguageTag::SimpleParser.match('a-DE')
33 test "given an invalid tag 'de-419-DE' test returns false" do
34 assert !LanguageTag::SimpleParser.match('de-419-DE')
38 class DeLatnLanguageTagTest < ActiveSupport::TestCase
40 subtags = %w(de Latn DE variant a-ext x-phonebk i-klingon)
41 @tag = LanguageTag.new *subtags
44 test "returns 'de' as the language subtag in lowercase" do
45 assert_equal 'de', @tag.language
48 test "returns 'Latn' as the script subtag in titlecase" do
49 assert_equal 'Latn', @tag.script
52 test "returns 'DE' as the region subtag in uppercase" do
53 assert_equal 'DE', @tag.region
56 test "returns 'variant' as the variant subtag in lowercase" do
57 assert_equal 'variant', @tag.variant
60 test "returns 'a-ext' as the extension subtag" do
61 assert_equal 'a-ext', @tag.extension
64 test "returns 'x-phonebk' as the privateuse subtag" do
65 assert_equal 'x-phonebk', @tag.privateuse
68 test "returns 'i-klingon' as the grandfathered subtag" do
69 assert_equal 'i-klingon', @tag.grandfathered
72 test "returns a formatted tag string from #to_s" do
73 assert_equal 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon', @tag.to_s
76 test "returns an array containing the formatted subtags from #to_a" do
77 assert_equal %w(de Latn DE variant a-ext x-phonebk i-klingon), @tag.to_a
81 class InheritanceLanguageTagTest < ActiveSupport::TestCase
82 test "returns 'de-Latn-DE-variant-a-ext-x-phonebk' as the parent of 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon'" do
83 tag = LanguageTag.new *%w(de Latn DE variant a-ext x-phonebk i-klingon)
84 assert_equal 'de-Latn-DE-variant-a-ext-x-phonebk', tag.parent.to_s
87 test "returns 'de-Latn-DE-variant-a-ext' as the parent of 'de-Latn-DE-variant-a-ext-x-phonebk'" do
88 tag = LanguageTag.new *%w(de Latn DE variant a-ext x-phonebk)
89 assert_equal 'de-Latn-DE-variant-a-ext', tag.parent.to_s
92 test "returns 'de-Latn-DE-variant' as the parent of 'de-Latn-DE-variant-a-ext'" do
93 tag = LanguageTag.new *%w(de Latn DE variant a-ext)
94 assert_equal 'de-Latn-DE-variant', tag.parent.to_s
97 test "returns 'de-Latn-DE' as the parent of 'de-Latn-DE-variant'" do
98 tag = LanguageTag.new *%w(de Latn DE variant)
99 assert_equal 'de-Latn-DE', tag.parent.to_s
102 test "returns 'de-Latn' as the parent of 'de-Latn-DE'" do
103 tag = LanguageTag.new *%w(de Latn DE)
104 assert_equal 'de-Latn', tag.parent.to_s
107 test "returns 'de' as the parent of 'de-Latn'" do
108 tag = LanguageTag.new *%w(de Latn)
109 assert_equal 'de', tag.parent.to_s
112 # TODO RFC4647 says: "If no language tag matches the request, the "default" value is returned."
113 # where should we set the default language?
114 # test "returns '' as the parent of 'de'" do
115 # tag = LanguageTag.new *%w(de)
116 # assert_equal '', tag.parent.to_s
119 test "returns an array of 5 parents for 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon'" do
120 parents = %w(de-Latn-DE-variant-a-ext-x-phonebk-i-klingon
121 de-Latn-DE-variant-a-ext-x-phonebk
122 de-Latn-DE-variant-a-ext
127 tag = LanguageTag.new *%w(de Latn DE variant a-ext x-phonebk i-klingon)
128 assert_equal parents, tag.parents.map{|tag| tag.to_s}