public void TryParse(string text, char singleton, string[] subtags) { var pu1 = ExtensionSubtag.TryParse(text); Assert.IsTrue(pu1.HasValue); Assert.That(pu1 !.Value.Singleton, Is.EqualTo(singleton)); Assert.That(pu1, Is.EquivalentTo(subtags)); Assert.That(ExtensionSubtag.TryParse(text, out var pu2), Is.True); Assert.That(pu2.Singleton, Is.EqualTo(singleton)); Assert.That(pu2, Is.EquivalentTo(subtags)); Assert.That(pu1, Is.EqualTo(pu2)); foreach (var language in TestContent.GetLanguages()) { var languageTag = LanguageTag.TryParse($"{language}-{text}"); Assert.IsNotNull(languageTag); if (singleton == 'x') { Assert.AreEqual( languageTag !.Value.PrivateUse, new ExtensionSubtag(singleton, subtags)); } else { CollectionAssert.AreEqual( languageTag !.Value.Extensions, new[] { new ExtensionSubtag(singleton, subtags) }); } } }
public void Empty() { var empty = new ExtensionSubtag(); Assert.Throws <InvalidOperationException>(() => empty.ToString()); Assert.Throws <InvalidOperationException>(() => empty.Singleton.GetHashCode()); }
public void Playground() { var en = Language.EN; // English language var zh = Language.ZH; // Chinese language var latn = Script.Latn; // Latin script var hans = Script.Hans; // Han (Simplified variant) var tw = Region.TW; // Taiwan, Province of China // tag zh-Hans-TW var tag1 = new LanguageTag(zh, hans, tw); // tag zh-Latn-TW-Pinyin var tag2 = new LanguageTag(zh, latn, tw, new [] { Variant.Pinyin }); // tag en-US-x-twain var tag3 = new LanguageTag(en, Region.US, ExtensionSubtag.ForPrivateUse("twain")); // tag de-DE-u-co-phonebk var tag4 = new LanguageTag(Language.DE, Region.DE, new [] { new ExtensionSubtag('u', "co", "phonebk") }); var tag5 = LanguageTag.Parse("ru"); var tag6 = LanguageTag.Parse("zh-Hans-TW"); var tag7 = LanguageTag.Parse("en-GB-scotland"); Assert.True(tag1 == tag6); // zh-Hans-TW -> zh-Hans var tag8 = tag6.Take(LanguageTag.Field.Language | LanguageTag.Field.Script); // zh-Hans is subset of zh-Hans-TW Assert.True(tag8.IsSubsetOf(tag6)); }
public void Empty() { var empty = new ExtensionSubtag(); Assert.IsTrue(empty.IsEmpty); Assert.AreEqual("", empty.ToString()); Assert.That(empty.ToArray(), Is.Empty); Assert.Throws <InvalidOperationException>(() => empty.Singleton.GetHashCode()); }
public void Constructor_LangVarPr() { var tag1 = new LanguageTag(Language.RU, new [] { Variant.Petr1708 }, ExtensionSubtag.ForPrivateUse("aaa")); var tag2 = new LanguageTag(Language.RU, null, null, new [] { Variant.Petr1708 }, new ExtensionSubtag[] {}, ExtensionSubtag.ForPrivateUse("aaa")); var expected = LanguageTag.Parse("ru-Petr1708-x-aaa"); Assert.That(tag1, Is.EqualTo(expected)); Assert.That(tag2, Is.EqualTo(expected)); }
public void Constructor_LangScrRegPr() { var tag1 = new LanguageTag(Language.RU, Script.Latn, Region.RU, ExtensionSubtag.ForPrivateUse("aaa")); var tag2 = new LanguageTag(Language.RU, Script.Latn, Region.RU, new Variant[] {}, new ExtensionSubtag[] {}, ExtensionSubtag.ForPrivateUse("aaa")); var expected = LanguageTag.Parse("ru-Latn-RU-x-aaa"); Assert.That(tag1, Is.EqualTo(expected)); Assert.That(tag2, Is.EqualTo(expected)); }
public void ParseFail(string text) { var pu1 = ExtensionSubtag.TryParse(text); Assert.That(pu1.HasValue, Is.False); Assert.That(ExtensionSubtag.TryParse(text, out _), Is.False); Assert.Throws <FormatException>(() => ExtensionSubtag.Parse(text)); }
public void Equals() { var ext1 = new ExtensionSubtag('a', "aaa"); var ext2 = new ExtensionSubtag('a', "aaa"); var ext3 = new ExtensionSubtag('b', "aaa"); var ext4 = new ExtensionSubtag('b', "bbb"); Assert.IsTrue(ext1.Equals(ext1)); Assert.IsTrue(ext1.Equals(ext2)); Assert.IsFalse(ext2.Equals(ext3)); Assert.IsFalse(ext3.Equals(ext4)); Assert.IsTrue(ext1 != ext3); Assert.IsFalse(ext1 == ext3); }
public void Equals() { var e1 = new ExtensionSubtag(); var e2 = (object)e1; var ext1 = new ExtensionSubtag('a', "aaa"); var ext2 = new ExtensionSubtag('a', "aaa"); var ext3 = new ExtensionSubtag('b', "aaa"); var ext4 = new ExtensionSubtag('x', "bbb"); Assert.IsFalse(e1.Equals(null)); Assert.IsTrue(e1.Equals(e2)); Assert.AreEqual(e1, e1); Assert.AreNotEqual(e1, ext1); Assert.IsTrue(ext1.Equals(ext1)); Assert.IsTrue(ext1.Equals(ext2)); Assert.IsFalse(ext2.Equals(ext3)); Assert.IsFalse(ext3.Equals(ext4)); Assert.IsTrue(ext1 != ext3); Assert.IsFalse(ext1 == ext3); }
public void Contains(string?subtagText, string tag, bool expected) { var pu = subtagText == null ? new ExtensionSubtag() : ExtensionSubtag.Parse(subtagText); Assert.That(pu.Contains(tag), Is.EqualTo(expected)); }
public void Enumerate(string[] subtags) { var ext = new ExtensionSubtag('a', subtags); Assert.That(ext, Is.EquivalentTo(subtags)); }
public void PrivateUse() { var ext = ExtensionSubtag.ForPrivateUse("aaa"); Assert.IsTrue(ext.PrivateUse); }
public void ToString(char singleton, string[] subtags, string expected) { var ext = new ExtensionSubtag(singleton, subtags); Assert.AreEqual(expected, ext.ToString()); }
public void Constructor_LangExrPr() { var tag1 = new LanguageTag(Language.RU, new [] { new ExtensionSubtag('a', "aaa") }, ExtensionSubtag.ForPrivateUse("aaa")); var tag2 = new LanguageTag(Language.RU, null, null, new Variant[] {}, new [] { new ExtensionSubtag('a', "aaa") }, ExtensionSubtag.ForPrivateUse("aaa")); var expected = LanguageTag.Parse("ru-a-aaa-x-aaa"); Assert.That(tag1, Is.EqualTo(expected)); Assert.That(tag2, Is.EqualTo(expected)); }
public void Constructor_LangScrRegVarExrPr() { var tag1 = new LanguageTag(Language.RU, Script.Latn, Region.RU, new [] { Variant.Petr1708 }, new [] { new ExtensionSubtag('a', "aaa") }, ExtensionSubtag.ForPrivateUse("aaa")); var tag2 = new LanguageTag(Language.RU, Script.Latn, Region.RU, new [] { Variant.Petr1708 }, new [] { new ExtensionSubtag('a', "aaa") }, ExtensionSubtag.ForPrivateUse("aaa")); var expected = LanguageTag.Parse("ru-Latn-RU-Petr1708-a-aaa-x-aaa"); Assert.That(tag1, Is.EqualTo(expected)); Assert.That(tag2, Is.EqualTo(expected)); }