| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  | require "locale" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-18 15:11:11 -08:00
										 |  |  | RSpec.describe Locale do | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  |   describe "::parse" do | 
					
						
							|  |  |  |     it "parses a string in the correct format" do | 
					
						
							|  |  |  |       expect(described_class.parse("zh")).to eql(described_class.new("zh", nil, nil)) | 
					
						
							| 
									
										
										
										
											2023-04-13 11:20:28 +08:00
										 |  |  |       expect(described_class.parse("zh-CN")).to eql(described_class.new("zh", nil, "CN")) | 
					
						
							|  |  |  |       expect(described_class.parse("zh-Hans")).to eql(described_class.new("zh", "Hans", nil)) | 
					
						
							|  |  |  |       expect(described_class.parse("zh-Hans-CN")).to eql(described_class.new("zh", "Hans", "CN")) | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 11:37:44 +02:00
										 |  |  |     it "correctly parses a string with a UN M.49 region code" do | 
					
						
							| 
									
										
										
										
											2023-04-13 11:20:28 +08:00
										 |  |  |       expect(described_class.parse("es-419")).to eql(described_class.new("es", nil, "419")) | 
					
						
							| 
									
										
										
										
											2017-09-29 11:37:44 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     describe "raises a ParserError when given" do | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  |       it "an empty string" do | 
					
						
							| 
									
										
										
										
											2016-10-14 20:33:16 +02:00
										 |  |  |         expect { described_class.parse("") }.to raise_error(Locale::ParserError) | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "a string in a wrong format" do | 
					
						
							| 
									
										
										
										
											2023-03-17 20:15:42 +08:00
										 |  |  |         expect { described_class.parse("zh-CN-Hans") }.to raise_error(Locale::ParserError) | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  |         expect { described_class.parse("zh_CN_Hans") }.to raise_error(Locale::ParserError) | 
					
						
							|  |  |  |         expect { described_class.parse("zhCNHans") }.to raise_error(Locale::ParserError) | 
					
						
							|  |  |  |         expect { described_class.parse("zh-CN_Hans") }.to raise_error(Locale::ParserError) | 
					
						
							|  |  |  |         expect { described_class.parse("zhCN") }.to raise_error(Locale::ParserError) | 
					
						
							|  |  |  |         expect { described_class.parse("zh_Hans") }.to raise_error(Locale::ParserError) | 
					
						
							| 
									
										
										
										
											2020-08-12 00:04:20 +02:00
										 |  |  |         expect { described_class.parse("zh-") }.to raise_error(Locale::ParserError) | 
					
						
							|  |  |  |         expect { described_class.parse("ZH-CN") }.to raise_error(Locale::ParserError) | 
					
						
							|  |  |  |         expect { described_class.parse("zh-cn") }.to raise_error(Locale::ParserError) | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "::new" do | 
					
						
							|  |  |  |     it "raises an ArgumentError when all arguments are nil" do | 
					
						
							|  |  |  |       expect { described_class.new(nil, nil, nil) }.to raise_error(ArgumentError) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises a ParserError when one of the arguments does not match the locale format" do | 
					
						
							|  |  |  |       expect { described_class.new("ZH", nil, nil) }.to raise_error(Locale::ParserError) | 
					
						
							| 
									
										
										
										
											2023-04-13 11:20:28 +08:00
										 |  |  |       expect { described_class.new(nil, "hans", nil) }.to raise_error(Locale::ParserError) | 
					
						
							|  |  |  |       expect { described_class.new(nil, nil, "cn") }.to raise_error(Locale::ParserError) | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#include?" do | 
					
						
							| 
									
										
										
										
											2023-04-13 11:20:28 +08:00
										 |  |  |     subject { described_class.new("zh", "Hans", "CN") } | 
					
						
							| 
									
										
										
										
											2017-02-28 16:33:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  |     it { is_expected.to include("zh") } | 
					
						
							|  |  |  |     it { is_expected.to include("zh-CN") } | 
					
						
							|  |  |  |     it { is_expected.to include("CN") } | 
					
						
							| 
									
										
										
										
											2023-03-17 20:35:17 +08:00
										 |  |  |     it { is_expected.to include("Hans-CN") } | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  |     it { is_expected.to include("Hans") } | 
					
						
							| 
									
										
										
										
											2023-03-17 20:15:42 +08:00
										 |  |  |     it { is_expected.to include("zh-Hans-CN") } | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#eql?" do | 
					
						
							| 
									
										
										
										
											2023-04-13 11:20:28 +08:00
										 |  |  |     subject(:locale) { described_class.new("zh", "Hans", "CN") } | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "when all parts match" do | 
					
						
							| 
									
										
										
										
											2023-03-17 20:15:42 +08:00
										 |  |  |       it { is_expected.to eql("zh-Hans-CN") } | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |       it { is_expected.to eql(locale) } | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "when only some parts match" do | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  |       it { is_expected.not_to eql("zh") } | 
					
						
							|  |  |  |       it { is_expected.not_to eql("zh-CN") } | 
					
						
							|  |  |  |       it { is_expected.not_to eql("CN") } | 
					
						
							| 
									
										
										
										
											2023-03-17 20:15:42 +08:00
										 |  |  |       it { is_expected.not_to eql("Hans-CN") } | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  |       it { is_expected.not_to eql("Hans") } | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "does not raise if 'other' cannot be parsed" do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |       expect { locale.eql?("zh_CN_Hans") }.not_to raise_error | 
					
						
							|  |  |  |       expect(locale.eql?("zh_CN_Hans")).to be false | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2018-06-03 00:54:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "#detect" do | 
					
						
							|  |  |  |     let(:locale_groups) { [["zh"], ["zh-TW"]] } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "finds best matching language code, independent of order" do | 
					
						
							| 
									
										
										
										
											2023-04-13 11:20:28 +08:00
										 |  |  |       expect(described_class.new("zh", nil, "TW").detect(locale_groups)).to eql(["zh-TW"]) | 
					
						
							|  |  |  |       expect(described_class.new("zh", nil, "TW").detect(locale_groups.reverse)).to eql(["zh-TW"]) | 
					
						
							|  |  |  |       expect(described_class.new("zh", "Hans", "CN").detect(locale_groups)).to eql(["zh"]) | 
					
						
							| 
									
										
										
										
											2018-06-03 00:54:52 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2016-09-27 22:23:13 +02:00
										 |  |  | end |