From 932fe8dd8334020581914a66647558ffbfe93297 Mon Sep 17 00:00:00 2001 From: Kenny Hung Date: Sun, 13 Jan 2013 22:22:24 +0000 Subject: [PATCH] Some collection and string helper functions --- CsharpUtils/CollectionHelper.cs | 5 +++ CsharpUtils/StringHelper.cs | 32 +++++++++++++++- CsharpUtilsTest/CollectionHelperTest.cs | 22 +++++++++++ CsharpUtilsTest/StringHelperTest.cs | 49 +++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 1 deletion(-) diff --git a/CsharpUtils/CollectionHelper.cs b/CsharpUtils/CollectionHelper.cs index 06f0102..768127e 100644 --- a/CsharpUtils/CollectionHelper.cs +++ b/CsharpUtils/CollectionHelper.cs @@ -55,5 +55,10 @@ public static bool EquivalentTo(this List left, List right) } return true; } + + public static bool IsNullOrEmpty(this List list) + { + return list == null || list.Count == 0; + } } } diff --git a/CsharpUtils/StringHelper.cs b/CsharpUtils/StringHelper.cs index f552138..486a742 100644 --- a/CsharpUtils/StringHelper.cs +++ b/CsharpUtils/StringHelper.cs @@ -1,10 +1,40 @@ -namespace CsharpUtils +using System; +using System.Linq; + +namespace CsharpUtils { public static class StringHelper { + #region Truncate + public static string TruncateLastChars(this string source, int number) { return source.Substring(0, source.Length - number); } + + public static string TruncateLastChar(this string source) + { + return source.TruncateLastChars(1); + } + + public static string TruncateEnding(this string source, string ending) + { + if (!source.EndsWith(ending)) + { + throw new InvalidOperationException(string.Format("'{0}' does not end with '{1}'", source, ending)); + } + return source.TruncateLastChars(ending.Length); + } + + #endregion + + #region Ends with + + public static bool EndsWithOneOf(this string source, params string[] endings) + { + return endings.Any(source.EndsWith); + } + + #endregion } } diff --git a/CsharpUtilsTest/CollectionHelperTest.cs b/CsharpUtilsTest/CollectionHelperTest.cs index 168431a..2628d05 100644 --- a/CsharpUtilsTest/CollectionHelperTest.cs +++ b/CsharpUtilsTest/CollectionHelperTest.cs @@ -95,5 +95,27 @@ public void IsEquivalentWorksWhenEquivalent() } #endregion + + #region IsNullOrEmpty + + [TestMethod] + public void IsNullOrEmptyWorksForNull() + { + Assert.IsTrue((null as List).IsNullOrEmpty()); + } + + [TestMethod] + public void IsNullOrEmptyWorksForEmpty() + { + Assert.IsTrue(new List().IsNullOrEmpty()); + } + + [TestMethod] + public void IsNullOrEmptyWorksForFullList() + { + Assert.IsFalse(new List{"hello", "goodbye"}.IsNullOrEmpty()); + } + + #endregion } } diff --git a/CsharpUtilsTest/StringHelperTest.cs b/CsharpUtilsTest/StringHelperTest.cs index de68e9b..5e0aa9d 100644 --- a/CsharpUtilsTest/StringHelperTest.cs +++ b/CsharpUtilsTest/StringHelperTest.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using CsharpUtils; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -7,6 +8,8 @@ namespace CsharpUtilsTest [TestClass] public class StringHelperTest { + #region Truncate last chars + [TestMethod] public void TruncateLastCharsWorks() { @@ -19,5 +22,51 @@ public void TruncateLastThrowsWhenNumberOfCharsExceedsLength() { "hell".TruncateLastChars(50); } + + [TestMethod] + public void TruncateLastCharWorks() + { + var test = new List {"a", "ab", "abcde"}; + test.ForEach(s => Assert.AreEqual(s.TruncateLastChars(1), s.TruncateLastChar())); + } + + [TestMethod] + public void TruncateEndingWorks() + { + Assert.AreEqual("SomeRandomString", "SomeRandomStringWithEnding".TruncateEnding("WithEnding")); + } + + [TestMethod] + [ExpectedException(typeof(InvalidOperationException))] + public void TruncateEndingThrowsWhenSourceDoesNotHaveEnding() + { + "SomeRandomString".TruncateEnding("TheEnding"); + } + + #endregion + + #region Ends with + + [TestMethod] + public void EndsWithOneOfWorksWithNoArgs() + { + Assert.IsFalse("blah".EndsWithOneOf()); + } + + [TestMethod] + public void EndsWithOneOfWorksWithOneArg() + { + Assert.IsTrue("blahDeBlah".EndsWithOneOf("Blah")); + Assert.IsFalse("blahDeBlah".EndsWithOneOf("blah")); + } + + [TestMethod] + public void EndsWithOneOfWorksWithMultipleArgs() + { + Assert.IsTrue("amo".EndsWithOneOf("o", "a")); + Assert.IsFalse("amor".EndsWithOneOf("o", "a")); + } + + #endregion } }