OWL Library part 1

3 minutesWhen we have the solution the first step to create a library. Our TDD approach forces to you create an empty class which contains the methods then we write the tests for them. We will create the beginning StringUtility class because it is the static class we don’t need create an interface. Our class:
 
static class StringUtility {}
When we have our class we must create the first test. In project TooExe.Owl.Library.UnitTests we create TestStringUtility class and  ToWordList_DirtyString_ExpectedWord method.  Our ToWordList() method is very complex so we must first present “Business requirements”:
  1. Eliminate words which contain:
    1. Undesirable char
       bool IsContainUnneceseryCharacter(this string text, string charsCharacter = @"~`1234567890@#$%^&*()_=+{[}]\|<>/")
    2. Two or more upper letters
      bool HaveTwoOrMoreUpperCaseLetter()
  2. Remove last char such as “.,;:’?!
    string RemoveLastCharByLists(this string text, string unnecessaryCharacters = "\".,;:'?!")
  3. All words we will be stored in infinitive form. This condition imposes additional requirements on us:
    1. plural form noun (regular)
      string ReplaceRegularPluralWords(this string text)
    2. plural form noun (irregular)
       string ReplaceIrregularPluralWords(this string text)
    3. verb regular form
       static string ReplcaceRegularVerbForm(this string text)
    4. verb irregular form
       static string ReplcaceIrregularVerbForm(this string text)
    5. the third person singular is partly implemented by plural noun form we implement it in regular noun form.
  4. All capital letters will be ignored.
Our class:
static class StringUtility
    {

        static StringUtility()
        {

        }

        public static IEnumerable<string> ToWordList(this string text)
        {
            return null;
        }
        public static string ReplaceWordToInfinitive(this string text)
        {
            throw new NotImplementedException();
        }
        public static bool IsBadWord(this string text)
        {
            throw new NotImplementedException();
        }
        public static string RemoveLastCharByLists(this string text, string unnecessaryCharacters = "\".,;:'?!")
        {
            throw new NotImplementedException();
        }
        public static string ReplcaceRegularVerbForm(this string text)
        {
            throw new NotImplementedException();
        }
        public static string ReplacePluralWords(this string text)
        {
            throw new NotImplementedException();
        }
        public static string ReplcaceIrregularVerbForm(this string text)
        {
            throw new NotImplementedException();
        }
        public static bool IsContainUnneceseryCharacter(this string text, string charsCharacter = @"~`1234567890@#$%^&*()_=+{[}]\|<>/")
        {
            throw new NotImplementedException();
        }
        public static bool HaveTwoOrMoreUpperCaseLetter(this string text)
        {
            throw new NotImplementedException();
        }
    }
This is the very simple algorithm, but It enough for our using. Irregular verb form and plural words and irregular third person singular form can be obtained from the file. (temporarily used later we will use a database or a web API connected to dictionary database). First test:
 [Theory]
 [InlineData(@"12312312312")]
 [InlineData(@"mark@gmail.com")]
 [InlineData(@"Ma#$t")]
 public void IsContainUnneceseryCharacter_InsertUnneceseryCharacter_True(string input)
 {
 // Arrange all necessary preconditions and inputs.
 var expected = true;

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

 // Assert that the expected results have occurred.
 // ReSharper disable once ConditionIsAlwaysTrueOrFalse
 Assert.Equal(expected, actual);
 }
Run it and it fails. (It is ok we expected that.) Let’s write the method:
 public static bool IsContainUnneceseryCharacter(this string text, string charsCharacter = @"~`1234567890@#$%^&*()_=+{[}]\|<>/")
 {
 var result = text.IndexOfAny(charsCharacter.ToCharArray()) != -1;
 return result;
 }
Run the test. We get the true result. The code covered is 100%. This method passed the test. Next tested method is HaveTwoOrMoreUpperCaseLetter(). Our tests:
 [Theory]
 [InlineData(@"Abcdef123")]
 [InlineData(@"Abcdef123jk")]
 [InlineData(@"Abcdef123j")]
 [InlineData(@"Abcdef123dfgdfg")]
 [InlineData(@"abcd123!@#234sdfgdgerge2342!$@%#^^#")]
 [InlineData(@"abcd")]
 [InlineData(@"ab")]
 [InlineData(@"a")]
 public void HaveTwoOrMoreCapitalLetter_StringDontContainTwoOrMoreUpperCaseLetter_True(string input)
 {
 // Arrange all necessary preconditions and inputs.
 var expected = false;

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

 // Assert that the expected results have occurred.
 // ReSharper disable once ConditionIsAlwaysTrueOrFalse
 Assert.Equal(expected, actual);
 }

 [Theory]
 [InlineData(@"AB")]
 [InlineData(@"ABC")]
 [InlineData(@"AbcDef123")]
 [InlineData(@"Abcdef123G")]
 [InlineData(@"AAbcdef123G")]
 [InlineData(@"AAbcdef123GF")]
 [InlineData(@"abcd123!@#KLsdfgdgerge2342!$@%#^^#")]
 public void HaveTwoOrMoreCapitalLetter_StringContainTwoOrMoreUpperCaseLetter_True(string input)
 {
 // Arrange all necessary preconditions and inputs.
 var expected = true;

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

 // Assert that the expected results have occurred.
 // ReSharper disable once ConditionIsAlwaysTrueOrFalse
 Assert.Equal(expected, actual);
 }
We created two type of tests. The first must return true (the entering data contain expected char(s) in this case two or more capital letter), the second test returns fail (the entry data don’t contain expected char(s) two or more capital letter). Run it and it fails. (It is ok we expected that.) Let’s write the method:
 public static bool HaveTwoOrMoreCapitalLetter(this string text)
 {
 int numberCapitalLetter = 0;

 foreach (var item in text.ToCharArray())
 {
 if ((item >= 65) && (item <= 90)) { numberCapitalLetter++; } if (numberCapitalLetter > 1)
 {
 return true;
 }
 }

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