Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added the IfNotNull method for nullable types

  • Loading branch information...
commit ea941c123509569266bf9069cb6b445dbc708b8d 1 parent 5a15a5d
@jeremydmiller jeremydmiller authored
View
24 src/FubuCore.Testing/BasicExtensionsTester.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()
View
8 src/FubuCore/BasicExtensions.cs
@@ -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);
+ }
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.