Skip to content

Commit

Permalink
Some collection and string helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
skewwhiffy authored and Kenny Hung committed Jan 13, 2013
1 parent 66f40ce commit 932fe8d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CsharpUtils/CollectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@ public static bool EquivalentTo<T>(this List<T> left, List<T> right)
}
return true;
}

public static bool IsNullOrEmpty<T>(this List<T> list)
{
return list == null || list.Count == 0;
}
}
}
32 changes: 31 additions & 1 deletion CsharpUtils/StringHelper.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
22 changes: 22 additions & 0 deletions CsharpUtilsTest/CollectionHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,27 @@ public void IsEquivalentWorksWhenEquivalent()
}

#endregion

#region IsNullOrEmpty

[TestMethod]
public void IsNullOrEmptyWorksForNull()
{
Assert.IsTrue((null as List<string>).IsNullOrEmpty());
}

[TestMethod]
public void IsNullOrEmptyWorksForEmpty()
{
Assert.IsTrue(new List<string>().IsNullOrEmpty());
}

[TestMethod]
public void IsNullOrEmptyWorksForFullList()
{
Assert.IsFalse(new List<string>{"hello", "goodbye"}.IsNullOrEmpty());
}

#endregion
}
}
49 changes: 49 additions & 0 deletions CsharpUtilsTest/StringHelperTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using CsharpUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand All @@ -7,6 +8,8 @@ namespace CsharpUtilsTest
[TestClass]
public class StringHelperTest
{
#region Truncate last chars

[TestMethod]
public void TruncateLastCharsWorks()
{
Expand All @@ -19,5 +22,51 @@ public void TruncateLastThrowsWhenNumberOfCharsExceedsLength()
{
"hell".TruncateLastChars(50);
}

[TestMethod]
public void TruncateLastCharWorks()
{
var test = new List<string> {"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
}
}

0 comments on commit 932fe8d

Please sign in to comment.