Permalink
Browse files

added the IfNotNull method for nullable types

  • Loading branch information...
1 parent 5a15a5d commit ea941c123509569266bf9069cb6b445dbc708b8d @jeremydmiller jeremydmiller committed Oct 14, 2011
Showing with 31 additions and 1 deletion.
  1. +23 −1 src/FubuCore.Testing/BasicExtensionsTester.cs
  2. +8 −0 src/FubuCore/BasicExtensions.cs
@@ -1,11 +1,33 @@
-using FubuTestingSupport;
+using System;
+using FubuTestingSupport;
using NUnit.Framework;
+using Rhino.Mocks;
namespace FubuCore.Testing
{
[TestFixture]
public class BasicExtensionsTester
{
+
+ [Test]
+ public void if_not_null_against_a_nullable()
+ {
+ int? x = null;
+
+ var action = MockRepository.GenerateMock<Action<int>>();
+
+ x.IfNotNull(i =>
+ {
+ Assert.Fail("Should not have been called");
+ });
+
+ x = 3;
+
+ x.IfNotNull(action);
+
+ action.AssertWasCalled(i => i.Invoke(3));
+
+ }
[Test]
public void should_be_able_to_handle_null_targets()
@@ -36,5 +36,13 @@ public static T IfNotNull<T>(this object target, Func<T> valueFunc)
{
return target == null ? null : valueFunc();
}
+
+ public static void IfNotNull<T>(this T? target, Action<T> action) where T : struct
+ {
+ if (target.HasValue)
+ {
+ action(target.Value);
+ }
+ }
}
}

0 comments on commit ea941c1

Please sign in to comment.