|
| 1 | +require "spec_helper" |
| 2 | +require "locale" |
| 3 | + |
| 4 | +describe Locale do |
| 5 | + describe "::parse" do |
| 6 | + it "parses a string in the correct format" do |
| 7 | + expect(described_class.parse("zh")).to eql(described_class.new("zh", nil, nil)) |
| 8 | + expect(described_class.parse("zh-CN")).to eql(described_class.new("zh", "CN", nil)) |
| 9 | + expect(described_class.parse("zh-Hans")).to eql(described_class.new("zh", nil, "Hans")) |
| 10 | + expect(described_class.parse("zh-CN-Hans")).to eql(described_class.new("zh", "CN", "Hans")) |
| 11 | + end |
| 12 | + |
| 13 | + context "raises a ParserError when given" do |
| 14 | + it "an empty string" do |
| 15 | + expect{ described_class.parse("") }.to raise_error(Locale::ParserError) |
| 16 | + end |
| 17 | + |
| 18 | + it "a string in a wrong format" do |
| 19 | + expect { described_class.parse("zh_CN_Hans") }.to raise_error(Locale::ParserError) |
| 20 | + expect { described_class.parse("zhCNHans") }.to raise_error(Locale::ParserError) |
| 21 | + expect { described_class.parse("zh-CN_Hans") }.to raise_error(Locale::ParserError) |
| 22 | + expect { described_class.parse("zhCN") }.to raise_error(Locale::ParserError) |
| 23 | + expect { described_class.parse("zh_Hans") }.to raise_error(Locale::ParserError) |
| 24 | + end |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + describe "::new" do |
| 29 | + it "raises an ArgumentError when all arguments are nil" do |
| 30 | + expect { described_class.new(nil, nil, nil) }.to raise_error(ArgumentError) |
| 31 | + end |
| 32 | + |
| 33 | + it "raises a ParserError when one of the arguments does not match the locale format" do |
| 34 | + expect { described_class.new("ZH", nil, nil) }.to raise_error(Locale::ParserError) |
| 35 | + expect { described_class.new(nil, "cn", nil) }.to raise_error(Locale::ParserError) |
| 36 | + expect { described_class.new(nil, nil, "hans") }.to raise_error(Locale::ParserError) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + subject { described_class.new("zh", "CN", "Hans") } |
| 41 | + |
| 42 | + describe "#include?" do |
| 43 | + it { is_expected.to include("zh") } |
| 44 | + it { is_expected.to include("zh-CN") } |
| 45 | + it { is_expected.to include("CN") } |
| 46 | + it { is_expected.to include("CN-Hans") } |
| 47 | + it { is_expected.to include("Hans") } |
| 48 | + it { is_expected.to include("zh-CN-Hans") } |
| 49 | + end |
| 50 | + |
| 51 | + describe "#eql?" do |
| 52 | + subject { described_class.new("zh", "CN", "Hans") } |
| 53 | + |
| 54 | + context "all parts match" do |
| 55 | + it { is_expected.to eql("zh-CN-Hans") } |
| 56 | + it { is_expected.to eql(subject) } |
| 57 | + end |
| 58 | + |
| 59 | + context "only some parts match" do |
| 60 | + it { is_expected.to_not eql("zh") } |
| 61 | + it { is_expected.to_not eql("zh-CN") } |
| 62 | + it { is_expected.to_not eql("CN") } |
| 63 | + it { is_expected.to_not eql("CN-Hans") } |
| 64 | + it { is_expected.to_not eql("Hans") } |
| 65 | + end |
| 66 | + |
| 67 | + it "does not raise if 'other' cannot be parsed" do |
| 68 | + expect { subject.eql?("zh_CN_Hans") }.not_to raise_error |
| 69 | + expect(subject.eql?("zh_CN_Hans")).to be false |
| 70 | + end |
| 71 | + end |
| 72 | +end |
0 commit comments