Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Most optimal way to check specific characters only appear in a string
- // https://stackoverflow.com/questions/58786021/most-optimal-way-to-check-specific-characters-only-appear-in-a-string/58786086#58786086
- Every class, and call exactly in the business logic.
- // My original code
- public static bool ContainsOnlyChars(string strValue, params char[] charValues)
- {
- if (string.IsNullOrEmpty(strValue))
- throw new ArgumentNullException("String cannot be null or empty.");
- for (int i = 0; i < strValue.Length; i++)
- {
- if (!charValues.Any(c=> c == strValue[i]))
- {
- return false;
- }
- }
- return true;
- }
- // A function thats called from my test class.
- public class SRFIsStringIgnorableUtility
- {
- public static char[] IgnorableCharacters { get; }
- static SRFIsStringIgnorableUtility()
- {
- IgnorableCharacters = new char[] { ' ', '\f', '\n', '\r', '\t', '\v', };
- }
- public static bool IsStringIgnorable(string strValue)
- {
- return SRFContainsUtility.ContainsOnlyChars(strValue, IgnorableCharacters);
- }
- }
- // Test class cases
- [TestCase(" ")]
- [TestCase("\f")]
- [TestCase("\n")]
- [TestCase("\r")]
- [TestCase("\t")]
- [TestCase("\v")]
- [TestCase(" ")]
- [TestCase("\f\f")]
- [TestCase("\n\n")]
- [TestCase("\r\r")]
- [TestCase("\t\t")]
- [TestCase("\v\v")]
- [TestCase(" \f")]
- [TestCase(" \n")]
- [TestCase(" \r")]
- [TestCase(" \t")]
- [TestCase(" \v")]
- [TestCase(" \f")]
- [TestCase(" \n")]
- [TestCase(" \r")]
- [TestCase(" \t")]
- [TestCase(" \v")]
- [TestCase("\f\f ")]
- [TestCase("\n\n ")]
- [TestCase("\r\r ")]
- [TestCase("\t\t ")]
- [TestCase("\v\v ")]
- [TestCase("\f \f")]
- [TestCase("\n \n")]
- [TestCase("\r \r")]
- [TestCase("\t \t")]
- [TestCase("\v \v")]
- [TestCase(" \f\f")]
- [TestCase(" \n\n")]
- [TestCase(" \r\r")]
- [TestCase(" \t\t")]
- [TestCase(" \v\v")] // all should return true because only chars in the string appear in the char array
- public void ShouldReturnTrueGivenIgnorableString(string strValue)
- {
- Assert.IsTrue(
- SRFIsStringIgnorableUtility.IsStringIgnorable(strValue)
- );
- }
- [TestCase(" ;")]
- [TestCase("\f;")]
- [TestCase("\n;")]
- [TestCase("\r;")]
- [TestCase("\t;")]
- [TestCase("\v;")]
- [TestCase(" ;")]
- [TestCase("\f\f;")]
- [TestCase("\n\n;")]
- [TestCase("\r\r;")]
- [TestCase("\t\t;")]
- [TestCase("\v\v;")]
- [TestCase(" \f;")]
- [TestCase(" \n;")]
- [TestCase(" \r;")]
- [TestCase(" \t;")]
- [TestCase(" \v;")]
- [TestCase(" \f;")]
- [TestCase(" \n;")]
- [TestCase(" \r;")]
- [TestCase(" \t;")]
- [TestCase(" \v;")]
- [TestCase("\f\f ;")]
- [TestCase("\n\n ;")]
- [TestCase("\r\r ;")]
- [TestCase("\t\t ;")]
- [TestCase("\v\v ;")]
- [TestCase("\f \f;")]
- [TestCase("\n \n;")]
- [TestCase("\r \r;")]
- [TestCase("\t \t;")]
- [TestCase("\v \v;")]
- [TestCase(" \f\f;")]
- [TestCase(" \n\n;")]
- [TestCase(" \r\r;")]
- [TestCase(" \t\t;")]
- [TestCase(" \v\v;")] // all should return false because of the selicolon
- public void ShouldReturnFalseGivenNonIgnorableString(string strValue)
- {
- Assert.IsFalse(
- SRFIsStringIgnorableUtility.IsStringIgnorable(strValue)
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement