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 Clear_ShouldMapToClear() { // arrange var sut = ClearableValue <int> .Clear(); // act var result = sut.Map(e => e.ToString()); // assert result.Should().BeClearVariant(); }
public void ClearAndSetOfDefaultValue_ShouldNotBeEqual() { // arrange var clear = ClearableValue <int> .Clear(); var set = ClearableValue <int> .Set(default(int)); // act // assert clear.Equals(set).Should().BeFalse(); set.Equals(clear).Should().BeFalse(); }
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(); }
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 Clear_ShouldNotCallSetOrNoActionFunc() { // arrange var sut = ClearableValue <int> .Clear(); // act Action match = () => sut.Match( set: _ => throw new InvalidOperationException(), clear: () => State.Clear, noAction: () => throw new InvalidOperationException()); // assert match.Should().NotThrow(because: "matching 'clear' should not call neither 'set' nor 'noAction' function"); }
public void Clear_ShouldMatch() { // arrange var sut = ClearableValue <int> .Clear(); // act var result = sut.Match( set: _ => State.Set, clear: () => State.Clear, noAction: () => State.NoAction); // assert result.Should().Be(State.Clear); }
public static ClearableValue <string> ClearIfNullOrWhiteSpace(this string value) => !string.IsNullOrWhiteSpace(value) ? ClearableValue <string> .Set(value) : ClearableValue <string> .Clear();
public static ClearableValue <string> ClearIfNullOrEmpty(this string value) => !string.IsNullOrEmpty(value) ? ClearableValue <string> .Set(value) : ClearableValue <string> .Clear();
public static ClearableValue <T> ClearIfNull <T>(this T?value) where T : struct => value.HasValue ? ClearableValue.Set(value.Value) : ClearableValue <T> .Clear();
public static ClearableValue <T> ClearIfNull <T>(this T value) where T : class => value is null ? ClearableValue <T> .Clear() : ClearableValue.Set(value);