C#: Unit Test to Differentiate CurrentCulture from InvariantCulture
我有一个方法采用了StringComparison参数,它确实需要一个单元测试来证明它正在执行正确的比较类型。
这基本上就是我目前所拥有的。它通过比较"oe"和"?"来区分顺序和其余的顺序。(参见system.string)。然而,我还没有找到区分当前文化和不变文化的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | using System; using NUnit.Framework; using System.Threading; using System.Globalization; namespace NUnitTests { [TestFixture] public class IndexOfTests { [Test] public void IndexOf_uses_specified_comparison_type() { StringComparison comparisonTypePerformed; result = TestComparisonType(StringComparison.CurrentCulture); Assert.AreEqual(StringComparison.CurrentCulture, comparisonTypePerformed); result = TestComparisonType(StringComparison.InvariantCulture); Assert.AreEqual(StringComparison.CurrentCulture, comparisonTypePerformed); result = TestComparisonType(StringComparison.Ordinal); Assert.AreEqual(StringComparison.CurrentCulture, comparisonTypePerformed); } bool TestComparisonType(StringComparison comparisonType) { int result; // Ensure the current culture is consistent for test var prevCulture = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false); try { result = IndexOf("oe","?", comparisonType); if (result == 0) { // The comparison type performed was either CurrentCulture, // InvariantCulture, or one of the case-insensitive variants. // TODO: Add test to differentiate between CurrentCulture and InvariantCulture throw new NotImplementedException(); result = IndexOf("???????","???????", StringComparison.CurrentCulture); //... } else // result == -1 { // The comparison type performed was either Ordinal or OrdinalIgnoreCase result = IndexOf("a","A", StringComparison.CurrentCulture); if (result) Console.WriteLine("Comparison type was OrdinalIgnoreCase"); else Console.WriteLine("Comparison type was Ordinal"); } } finally { Thread.CurrentThread.CurrentCulture = prevCulture; } } } } |
我考虑通过比较"i"和"i"来使用土耳其问题,但它只适用于不区分大小写的比较。
我环顾四周,在字符串相等性测试(仅排序顺序和解析/序列化)中找不到InvariantCulture与其他区域性不同的情况。例如,根据文化,"?"而"o"的分类不同,但所有文化在平等测试中返回相同的结果。
是否有一个平等测试来区分不变文化和其他文化?
编辑:德语"?"而"ss"对所有文化都给出了相同的结果,所以它们不会起作用。
编辑:实际上,所需要的只是两个在一种文化中被认为是平等的,在另一种文化中被认为是不平等的字符串。
MSDN的例子已在cultureinfo样品与不同的订单的安全取决于你的文化。我认为这是最有可能的BET的,对于一个开始的地方。然而你可能能把下面的一些的信息优势,如果这并不工作。
它看起来像是被一个类似的测试集进行比较对弦乐。MSDN页。虽然在这个例子中,它是美国文化的地区。
如果你看看在cultureinfo.invarientculture页,你会注意,invarientculture是基于英文的,但没有任何encoding区域或国家。和stringcomparison mentions hyphens页,特别是有一个不同的重量,取决于语言。你可能能把优势的发现,由一个语言,英语hyphens differently煤量。
有一些有趣的sorting笔记对维基百科的文章,在collation是幸运的,也有用的。特别是通过sorting alphabetical,看看,然后看看在西班牙的笔记。
For example, the 29-letter alphabet of
Spanish treats ? as a basic letter
following n, and formerly treated ch
and ll as basic letters following c
and l, respectively. Ch and ll are
still considered letters, but are now
alphabetized as two-letter
combinations.
我不知道如何helpful这将是,但它可能导致你的回答。
谢谢大blacksphere,我是能找到一个多文化之间的一些差异和invariantculture,作为显示在以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | void Demonstration_For_LL() { int foundPos; foundPos ="ll".IndexOf("l", StringComparison.InvariantCulture); Assert.AreEqual(0, foundPos); Thread.CurrentThread.CurrentCulture = new CultureInfo("sq", false); foundPos ="ll".IndexOf("l", StringComparison.CurrentCulture); Assert.AreEqual(-1, foundPos); Thread.CurrentThread.CurrentCulture = new CultureInfo("sq-AL", false); foundPos ="ll".IndexOf("l", StringComparison.CurrentCulture); Assert.AreEqual(-1, foundPos); } void Demonstration_For_CH() { int foundPos; foundPos ="ch".IndexOf("c", StringComparison.InvariantCulture); Assert.AreEqual(0, foundPos); Thread.CurrentThread.CurrentCulture = new CultureInfo("cs", false); foundPos ="ch".IndexOf("c", StringComparison.InvariantCulture); Assert.AreEqual(-1, foundPos); Thread.CurrentThread.CurrentCulture = new CultureInfo("cs-CZ", false); foundPos ="ch".IndexOf("c", StringComparison.InvariantCulture); Assert.AreEqual(-1, foundPos); Thread.CurrentThread.CurrentCulture = new CultureInfo("sk", false); foundPos ="ch".IndexOf("c", StringComparison.InvariantCulture); Assert.AreEqual(-1, foundPos); Thread.CurrentThread.CurrentCulture = new CultureInfo("sk-SK", false); foundPos ="ch".IndexOf("c", StringComparison.InvariantCulture); Assert.AreEqual(-1, foundPos); Thread.CurrentThread.CurrentCulture = new CultureInfo("vi", false); foundPos ="ch".IndexOf("c", StringComparison.InvariantCulture); Assert.AreEqual(-1, foundPos); Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN", false); foundPos ="ch".IndexOf("c", StringComparison.InvariantCulture); Assert.AreEqual(-1, foundPos); } |
不幸的是,《弦乐。比较方法不似乎表明,这些差异。无论如何,我的测试方法现在看起来像这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | class Example { public static void Main() { var comparisonTypePerformed = GetComparisonTypePerformed( new TestedMethod(string.IndexOf)); Console.WriteLine("Comparison type performed was:" + comparisonTypePerformed); } public static StringComparison GetComparisonTypePerformed(TestedMethod testedMethod) { StringComparison comparisonTypePerformed; var prevCulture = Thread.CurrentThread.CurrentCulture; try { Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false); // Check if method performs an Ordinal search int result = testedMethod("?","oe", 0, 1, comparisonType); if (result == StringHelper.NPOS) { // Check if method performs a case-sensitive search result = testedMethod("a","A", 0, 1, comparisonType); if (result == StringHelper.NPOS) comparisonTypePerformed = StringComparison.Ordinal; else comparisonTypePerformed = StringComparison.OrdinalIgnoreCase; } else { Thread.CurrentThread.CurrentCulture = new CultureInfo("sq-AL", false); // Check if method uses CurrentCulture or InvariantCulture result = testedMethod("ll", new string[] {"l" }, 0, 2, comparisonType); if (result == StringHelper.NPOS) { // Check if method performs a case-sensitive search result = testedMethod("a", new string[] {"A" }, 0, 1, comparisonType); if (result == StringHelper.NPOS) comparisonTypePerformed = StringComparison.CurrentCulture; else comparisonTypePerformed = StringComparison.CurrentCultureIgnoreCase; } else { // Check if method performs a case-sensitive search result = testedMethod("a", new string[] {"A" }, 0, 1, comparisonType); if (result == StringHelper.NPOS) comparisonTypePerformed = StringComparison.InvariantCulture; else comparisonTypePerformed = StringComparison.InvariantCultureIgnoreCase; } } } finally { Thread.CurrentThread.CurrentCulture = prevCulture; } return comparisonTypePerformed; } delegate int TestedMethod(string source, string value, int startIndex, int count, StringComparison comparisonType); } |
尝试coté和C?的Te与FR FR -为你当前的文化。
或使用MSDN资源这个大发现其他的例子。
I've looked around and couldn't find a case where InvariantCulture differs from other cultures by string comparison (only sort order and parsing/serialization).
我不理解那些评论-当然这一stringcomparison的顺序使用。