示例#1
0
        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);
        }
示例#2
0
            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);
            }
示例#3
0
            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);
            }
示例#4
0
            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");
            }
示例#5
0
            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();
            }
示例#9
0
        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()));
        }
示例#10
0
            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");
            }
示例#11
0
            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);
            }
示例#12
0
 public static ClearableValue <T> SetIfNotNull <T>(this T?value) where T : struct =>
 value.HasValue
     ? ClearableValue.Set(value.Value)
 : ClearableValue <T> .NoAction();