public bool TryBuild(out ClearableValue <T> result) { T value = Value; string state = State; if (string.IsNullOrEmpty(state) || string.Equals(state, Constants.Clearable_NoActionState, StringComparison.OrdinalIgnoreCase)) { result = ClearableValue <T> .NoAction(); return(true); } if (string.Equals(state, Constants.Clearable_ClearState, StringComparison.OrdinalIgnoreCase)) { result = ClearableValue <T> .Clear(); return(true); } if (string.Equals(state, Constants.Clearable_SetState, StringComparison.OrdinalIgnoreCase)) { result = ClearableValue <T> .Set(value); return(true); } result = default(ClearableValue <T>); return(false); }
public void GenericNoAction_ShouldBeNoAction() { // arrange var sut = ClearableValue.NoAction(Fixture.Create <int>()); // act // assert sut.Should().BeNoActionVariant(because: "{0} is created using generic NoAction<T>() method and should be treated as 'noAction'", sut); }
public void DomesticNoAction_ShouldBeNoAction() { // arrange var sut = ClearableValue <int> .NoAction(); // act // assert sut.Should().BeNoActionVariant(because: "{0} is created using domestic static NoAction() method and should be treated as 'noAction'", sut); }
public void NoAction_ShouldNotCallMapper() { // arrange var sut = ClearableValue <int> .NoAction(); // act Action map = () => sut.Map <int>(_ => throw new InvalidOperationException()); // assert map.Should().NotThrow(because: "mapping 'noAction' should not call mapper function"); }
public void NoAction_ShouldMapToNoAction() { // arrange var sut = ClearableValue <int> .NoAction(); // act var result = sut.Map(e => e.ToString()); // assert result.Should().BeNoActionVariant(); }
public void NoActionAndClear_ShouldNotBeEqual() { // arrange var noAction = ClearableValue <int> .NoAction(); var clear = ClearableValue <int> .Clear(); // act // assert noAction.Equals(clear).Should().BeFalse(); clear.Equals(noAction).Should().BeFalse(); }
public void NoActionAndSetOfDefaultValue_ShouldNotBeEqual() { // arrange var noAction = ClearableValue <int> .NoAction(); var set = ClearableValue <int> .Set(default(int)); // act // assert noAction.Equals(set).Should().BeFalse(); set.Equals(noAction).Should().BeFalse(); }
public void NoActionAndDefaultClearableValue_ShouldBeEqual() { // arrange var noAction = ClearableValue <int> .NoAction(); var @default = default(ClearableValue <int>); // act // assert noAction.Equals(@default).Should().BeTrue(); @default.Equals(noAction).Should().BeTrue(); }
private static IEnumerable <ClearableValue <string> > GetSerializeTestData() { yield return(ClearableValue <string> .NoAction()); yield return(ClearableValue <string> .Clear()); yield return(ClearableValue <string> .Set(null)); yield return(ClearableValue <string> .Set(string.Empty)); yield return(ClearableValue <string> .Set(System.Guid.NewGuid().ToString())); }
public void NoAction_ShouldNotCallSetOrClearFunc() { // arrange var sut = ClearableValue <int> .NoAction(); // act Action match = () => sut.Match( set: _ => throw new InvalidOperationException(), clear: () => throw new InvalidOperationException(), noAction: () => State.NoAction); // assert match.Should().NotThrow(because: "matching 'noAction' should not call neither 'set' nor 'clear' function"); }
public void NoAction_ShouldMatch() { // arrange var sut = ClearableValue <int> .NoAction(); // act var result = sut.Match( set: _ => State.Set, clear: () => State.Clear, noAction: () => State.NoAction); // assert result.Should().Be(State.NoAction); }
public static ClearableValue <T> SetIfNotNull <T>(this T?value) where T : struct => value.HasValue ? ClearableValue.Set(value.Value) : ClearableValue <T> .NoAction();