Skip to content

Instantly share code, notes, and snippets.

@jamesrcounts
Created June 12, 2012 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesrcounts/2920631 to your computer and use it in GitHub Desktop.
Save jamesrcounts/2920631 to your computer and use it in GitHub Desktop.
A patch that updates ScrubPath to handle when the "text" parameter is null.
// I expected scrub path to handle null input. There is no reason to let the
// NullReferenceException to propogate, since Verify can handle nulls.
// This test will pass or fail based on the contents of approved.txt
[TestMethod]
public void MyTestMethod()
{
string file = null;
ApprovalTests.Approvals.Verify(file);
}
// This test always fails with a NullReferenceException.
[TestMethod]
public void MyTestMethod1()
{
var dir = PathUtilities.GetDirectoryForCaller();
string file = null;
ApprovalTests.Approvals.Verify(file.ScrubPath(dir));
}
// This test will pass or fail based on the contents of approved.txt
[TestMethod]
public void MyTestMethod3()
{
var dir = PathUtilities.GetDirectoryForCaller();
string file = string.Empty;
ApprovalTests.Approvals.Verify(file.ScrubPath(dir));
}
Index: ApprovalUtilities.Tests/Utilities/PathUtilitiesTest.cs
===================================================================
--- ApprovalUtilities.Tests/Utilities/PathUtilitiesTest.cs (revision 411)
+++ ApprovalUtilities.Tests/Utilities/PathUtilitiesTest.cs (working copy)
@@ -17,5 +17,13 @@
var file = dir + "PathUtilitiesTest.cs";
Assert.AreEqual(@"...\PathUtilitiesTest.cs", PathUtilities.ScrubPath(file, dir));
}
+
+ [TestMethod]
+ public void ReturnNullForNullTest()
+ {
+ var dir = PathUtilities.GetDirectoryForCaller();
+ string file = null;
+ Assert.IsNull(file.ScrubPath(dir));
+ }
}
}
Index: ApprovalUtilities/Utilities/PathUtilities.cs
===================================================================
--- ApprovalUtilities/Utilities/PathUtilities.cs (revision 411)
+++ ApprovalUtilities/Utilities/PathUtilities.cs (working copy)
@@ -23,6 +23,11 @@
public static string ScrubPath(this string text, string path)
{
+ if (text == null)
+ {
+ return null;
+ }
+
return text.Replace(path, @"...\");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment