Skip to content
This repository
Browse code

added the IfNotNull method for nullable types

  • Loading branch information...
commit ea941c123509569266bf9069cb6b445dbc708b8d 1 parent 5a15a5d
Jeremy D. Miller jeremydmiller authored
24 src/FubuCore.Testing/BasicExtensionsTester.cs
... ... @@ -1,11 +1,33 @@
1   -using FubuTestingSupport;
  1 +using System;
  2 +using FubuTestingSupport;
2 3 using NUnit.Framework;
  4 +using Rhino.Mocks;
3 5
4 6 namespace FubuCore.Testing
5 7 {
6 8 [TestFixture]
7 9 public class BasicExtensionsTester
8 10 {
  11 +
  12 + [Test]
  13 + public void if_not_null_against_a_nullable()
  14 + {
  15 + int? x = null;
  16 +
  17 + var action = MockRepository.GenerateMock<Action<int>>();
  18 +
  19 + x.IfNotNull(i =>
  20 + {
  21 + Assert.Fail("Should not have been called");
  22 + });
  23 +
  24 + x = 3;
  25 +
  26 + x.IfNotNull(action);
  27 +
  28 + action.AssertWasCalled(i => i.Invoke(3));
  29 +
  30 + }
9 31
10 32 [Test]
11 33 public void should_be_able_to_handle_null_targets()
8 src/FubuCore/BasicExtensions.cs
@@ -36,5 +36,13 @@ public static T IfNotNull<T>(this object target, Func<T> valueFunc)
36 36 {
37 37 return target == null ? null : valueFunc();
38 38 }
  39 +
  40 + public static void IfNotNull<T>(this T? target, Action<T> action) where T : struct
  41 + {
  42 + if (target.HasValue)
  43 + {
  44 + action(target.Value);
  45 + }
  46 + }
39 47 }
40 48 }

0 comments on commit ea941c1

Please sign in to comment.
Something went wrong with that request. Please try again.