public void IfNotNull_WithTarget_CallFuncTR_ReturnOtherType() { var target = new SomeTestClass(); // Do not call Func<TR>(), return default(TR) SomeOtherTestClass otherRtn = target.IfNotNull(() => new SomeOtherTestClass()); Assert.IsNotNull(otherRtn); }
public void IfNotNull_WithTarget_CallAction_ReturnSameType() { var target = new SomeTestClass(); var rtn = target.IfNotNull(() => target.MethodReturnVoid()); Assert.IsNotNull(rtn); Assert.AreSame(rtn, target); Assert.IsTrue(target.SomeMethodCalled); }
public void IfNotNull_WithTarget_CallActionT_ReturnSameType() { var target = new SomeTestClass(); // Do not call Action<T>, always return source var rtn = target.IfNotNull(x => x.MethodReturnVoid()); Assert.IsNotNull(rtn); Assert.AreSame(rtn, target); Assert.IsTrue(target.SomeMethodCalled); }
public void WorkingSamples() { var notNullType = new SomeTestClass(); var nullType = default(SomeTestClass); var action1 = new Action(() => { }); var action2 = new Action(() => { }); // // I think the null comparison everywhere is a bit of noise. // Below find some samples for null comparison that compresses the code a few lines. // // --------------------------------------------- // Basic null comparision // --------------------------------------------- if (nullType == null) action1(); // Less noisy (I think) if (nullType.IsNull()) action1(); if (nullType.IsNotNull()) action2(); // --------------------------------------------- // Else samples // --------------------------------------------- if (nullType == null) action1(); else action2(); // Replace with nullType .IfNull(action1) .IfNotNull(action2); // --------------------------------------------- // Avoid null intances // --------------------------------------------- var someNullType = default(SomeTestClass); if (someNullType == null) someNullType = new SomeTestClass(); someNullType.MethodReturnVoid(); // Replace with notNullType .IfNull(() => new SomeTestClass()) .MethodReturnVoid(); // --------------------------------------------- // Retrning the same type // --------------------------------------------- someNullType = default(SomeTestClass); if (someNullType == null) someNullType = new SomeTestClass(); someNullType.MethodReturnVoid(); var otherType = new SomeOtherTestClass(); otherType.OtherMethodReturnVoid(); // Replace with notNullType .IfNull(() => new SomeTestClass()) .Do(x => x.MethodReturnVoid()) .Do(x => new SomeOtherTestClass()) .OtherMethodReturnVoid(); // --------------------------------------------- // IEnumerable Samples // --------------------------------------------- IEnumerable<SomeTestClass> sequenceFull = new SomeTestClass[] { new SomeTestClass(), new SomeTestClass(), }; IEnumerable<SomeOtherTestClass> otherSequenceFull = new SomeOtherTestClass[] { new SomeOtherTestClass(), new SomeOtherTestClass(), }; sequenceFull .ForEach(x => x.MethodReturnVoid()) .Select(element => new { AnonymousBool = element.SomeMethodCalled }) .Where(element => !element.AnonymousBool) .Do(enumerable => Console.WriteLine("Items in Collection: {0}", enumerable.Count())) .IfEnumEmpty(() => sequenceFull) .ForEach(element => element.MethodReturnVoid()) .Do(enumerable => Console.WriteLine("Items in Collection: {0}", enumerable.Count())) ; // --------------------------------------------- // String Samples // --------------------------------------------- var nullStr = default(string); var strAction1 = new Action<string>(x => { }); var strAction2 = new Action<string>(x => { }); if (nullStr == null) action1(); else action2(); // simple if (nullStr.IsEmpty()) action1(); else action2(); // Replace (no much diffrencr but easier on the eyes) nullStr .IfEmpty(action1) .IfNotEmpty(strAction2); // Replace with assurance there will be a string nullStr .IfEmpty(() => "new string") .Do(x => strAction1(x)); // --------------------------------------------- // String Samples - Compare strings // --------------------------------------------- var strOne = "strOne"; var strone = "strone"; //var strTwo = "strOne"; // Strings are different if (strone.Equals(strOne)) Assert.Fail(); // Replace with Case insenitive extension if (strone.IsNotEqualTo(strOne)) Assert.Fail(); // More Compressed //strone.IfNotEqual(strOne, (X500DistinguishedName, y) => { }); }
public void IfNotNull_WithTarget_CallFuncTTR_ReturnOtherType() { var target = new SomeTestClass(); SomeOtherTestClass otherRtn = target.IfNotNull(x => new SomeOtherTestClass()); Assert.IsNotNull(otherRtn); }
public void IfNull_WithTarget_DoNotCallAction_ReturnSameTypeNull() { var target = new SomeTestClass(); SomeTestClass rtn = target.IfNull(() => new SomeTestClass()); Assert.AreSame(rtn, target); }
public void IfNull_WithTarget_DoNotCallFuncTR_ReturnOtherTypeNull() { var target = new SomeTestClass(); SomeOtherTestClass rtn = target.IfNull(() => new SomeOtherTestClass()); Assert.IsNull(rtn); }
public void IfNull_WithTarget_CallAction_ReturnTarget() { var target = new SomeTestClass(); // If T is null, call Action, always return T var rtn = target.IfNull(() => Assert.Fail()); Assert.AreSame(rtn, target); }
public void IfNotEmpty_WithFull_DoNotCallAction_ReturnNull() { var target = new SomeTestClass(); var result = _sequenceFull.IfEnumNotEmpty(x => target.MethodReturnVoid()); Assert.IsTrue(target.SomeMethodCalled); Assert.AreSame(result, _sequenceFull); }
public void IfEmpty_WithNull_CallAction_ReturnEmpty() { var target = new SomeTestClass(); IEnumerable<string> result = _sequenceNull.IfEnumEmpty(() => target.MethodReturnVoid()); Assert.IsFalse(result.Any()); Assert.IsTrue(target.SomeMethodCalled); }
public void IfEmpty_WithFull_DoNotCallAction_ReturnEmpty() { var target = new SomeTestClass(); var result = _sequenceFull.IfEnumEmpty(() => { Assert.Fail(); target.MethodReturnVoid(); }); Assert.AreSame(result, _sequenceFull); }
public void IfNotEmpty_WithString_CallActionString() { const string target = "a"; var someType = new SomeTestClass(); var result = target.IfNotEmpty(x => someType.MethodReturnVoid()); Assert.AreEqual(result, target); Assert.IsTrue(someType.SomeMethodCalled); }