Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Unit Test to Differentiate CurrentCulture from InvariantCulture

I have a method which takes a StringComparison argument, and it really needs a unit test to prove it is performing the right comparison type.

This is basically what I have so far. It differentiates Ordinal from the rest by comparing "oe" and "œ" (see System.String). I haven't, however, found a way to differentiate CurrentCulture from InvariantCulture.

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 considered using the Turkish problem by comparing "i" and "I", but it only works for case-insensitive comparisons.

I've looked around and couldn't find a case where InvariantCulture differs from other cultures in a string equality test (only sort order and parsing/serialization). For example, depending on the culture, "ô" and "o" will sort differently, but all cultures return the same result in equality tests.

Is there an equality test to differentiate InvariantCulture from any other culture?

EDIT: The german "ß" and "ss" give the same result for all cultures so they won't work.

EDIT: Really, all that is needed is two strings considered equal in one culture, and unequal in another.

like image 701
drifter Avatar asked Apr 24 '11 05:04

drifter


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.


1 Answers

The MSDN Sample on CultureInfo has an example with different sort orders depending on culture. I think that's your most likely bet for a starting place. However you may be able to take advantage of some of the information below if that doesn't work.

It looks like a similar set of tests is being performed on the String.Compare MSDN page. In this example though, it is in the en-US culture.

If you look at the CultureInfo.InvarientCulture page, you'll notice that the InvarientCulture is based on English, but without any regional or country encoding. And the StringComparison page mentions that hyphens specifically have a different weight, depending on language. You may be able to take advantage of that by finding a language that weights hyphens differently than English.

Wikipedia has some interesting notes on sorting in the collation article, that may also be useful. Look specifically through Sorting, alphabetical, and then look at the Spanish notes.

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.

I'm not sure how helpful this will be, but it may lead you to an answer.

like image 159
blackSphere Avatar answered Oct 04 '22 13:10

blackSphere