OWL Library part 2

2 minutesOWL Library part 2 Now we will be tested method RemoveLastCharByLists. Our test:
[Theory]
[InlineData("Forest\"")]
[InlineData("Forest.")]
[InlineData("Forest,")]
[InlineData("Forest;")]
[InlineData("Forest:")]
[InlineData("Forest'")]
[InlineData("Forest?")]
[InlineData("Forest????,:;!!!??")]
[InlineData("Forest......")]
[InlineData("Forest,,,,,,,,,,,")]
public void RemoveLastCharByLists_ExpectedCharToEndString_RemoveLastChar(string input)
{
    // Arrange all necessary preconditions and inputs.
    var expected = "Forest";

    // Act on the object or method under test.
    string actual = input.RemoveLastCharByLists();

    // Assert that the expected results have occurred.
    Assert.Equal(expected, actual);
}
Run it and it fails. (It is ok we expected that.) Let’s write the method:
public static string RemoveLastCharByLists(this string text, string unnecessaryCharacters = "\".,;:'?!")
{
    while (text[text.Length - 1].ToString().IndexOfAny(unnecessaryCharacters.ToCharArray()) != -1)
    {
        foreach (var c in unnecessaryCharacters.ToCharArray())
        {
            if (text[text.Length - 1] == c)
            {
                text = text.Remove(text.Length - 1);
            }
        }
    }
    return text;
}
Run the test. We get the true result. The code covered is 100%. This method passed the test. Now we will be tested method ReplaceRegularPluralWords. Our test:
[Theory]
[InlineData("dresses", "dress")]
[InlineData("boxes", "box")]
[InlineData("bushes", "bush")]
[InlineData("buses", "bus")]
[InlineData("watches", "watch")]
[InlineData("stomachs", "stomach")]
[InlineData("epochs", "epoch")]
[InlineData("spoofs", "spoof")]
[InlineData("solos", "solo")]
[InlineData("zoos", "zoo")]
[InlineData("bureaus", "bureau")]
[InlineData("berries", "berry")]
[InlineData("abbeys", "abbey")]
public void ReplaceRegularPluralWords_ExpectedPluralWord_ReplaceToSingular(string input, string expected)
{
    // Arrange all necessary preconditions and inputs.

    // Act on the object or method under test.
    string actual = input.ReplaceRegularPluralWords();

    // Assert that the expected results have occurred.
    Assert.Equal(expected, actual);
}
Run it and it fails. (It is ok we expected that.) Let’s write the method:
public static string ReplaceRegularPluralWords(this string text)
{
    if (
         text.EndsWith("ches") ||
         text.EndsWith("ses") ||
         text.EndsWith("shes") ||
         text.EndsWith("sses") ||
         text.EndsWith("xes")
        )
    {
        text = text.Remove(text.Length - 2);
    }
    else
    {
        if (text.EndsWith("ies"))
        {
            text = text.Remove(text.Length - 3) + "y";
        }
        else
        {
            if ((text.EndsWith("es")) || (text.EndsWith("ys")))
            {
                text = text.Remove(text.Length - 1);
            }
            else
            {
                if ((text.EndsWith("s")) && (!text.EndsWith("is")) && (!text.EndsWith("ss")))
                {
                    text = text.Remove(text.Length - 1);
                }
            }
        }
    }
   
    return text;
}
Run the test. We get the true result. The code covered is 100%. This method passed the test. Now we will be tested method ReplaceIrregularPluralWords. Our test:
[Theory]
[InlineData("mice", "mouse")]
[InlineData("children", "child")]
[InlineData("chiefs", "chief")]
[InlineData("feet", "foot")]
[InlineData("jeans", "jeans")]
[InlineData("data", "datum")]
[InlineData("knives", "knife")]
[InlineData("buffaloes", "buffalo")]
[InlineData("babies", "baby")]
public void ReplaceIrregularPluralWords_ExpectedIrregularPluralWords_ReplaceToFirstForm(string input, string expected)
{
    // Arrange all necessary preconditions and inputs.

    // Act on the object or method under test.
    string actual = input.ReplaceIrregularPluralWords();

    // Assert that the expected results have occurred.
    Assert.Equal(expected, actual);
}
Run it and it fails. (It is ok we expected that.) Let’s write the method:
public static string ReplaceIrregularPluralWords(this string text)
{
    foreach (var noun in ListIrregularNouns)
    {
        if ((text == noun.Plural) || (text == noun.Singular))
        {
            return noun.Singular;
        }
    }
    return text;
}
Run the test. We get the true result. The code covered is 100%. This method passed the test. Now we will be tested method ReplcaceRegularVerbForm. Our test:
[Fact]
public void ReplcaceRegularVerbForm_InputReguralVerb_GetFirstFormVerb()
{
    // Arrange all necessary preconditions and inputs.
    string input = "finished";
    var expected = "finish";

    // Act on the object or method under test.
    var actual = input.ReplcaceRegularVerbForm();

    // Assert that the expected results have occurred.
    Assert.Equal(expected, actual);
}
Run it and it fails. (It is ok we expected that.) Let’s write the method:
public static string ReplcaceRegularVerbForm(this string text)
{
    if ((text[text.Length - 1] == 'd') && (text[text.Length - 2] == 'e'))
    {
        text = text.Remove(text.Length - 2);
    }

    return text;
}
Run the test. We get the true result. The code covered is 100%. This method passed the test. Now we will be tested method ReplcaceIrregularVerbForm. Our test:
[Theory]
[InlineData("bend", "bend")]
[InlineData("bent", "bend")]
[InlineData("bite", "bite")]
[InlineData("bit", "bite")]
[InlineData("bitten", "bite")]
public void ReplcaceIrregularVerbForm_ExpectedIrregularVerb_ReplaceToFirstForm(string input, string expected)
{
    // Arrange all necessary preconditions and inputs.

    // Act on the object or method under test.
    string actual = input.ReplcaceIrregularVerbForm();

    // Assert that the expected results have occurred.
    Assert.Equal(expected, actual);
}
Run it and it fails. (It is ok we expected that.) Let’s write the method:
public static string ReplcaceIrregularVerbForm(this string text)
{
    foreach (var verb in ListIrregularVerbForms)
    {
        if ((text == verb.FirstForm) || (text == verb.SecondForm) || (text == verb.ThirdForm))
        {
            return verb.FirstForm;
        }
    }

    return text;
}
Run the test. We get the true result. The code covered is 100%. This method passed the test.  

Cover unit tests.

TestStringUtility

covercode

All unit tests.