Пример #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 GenericSet_ShouldBeSet()
            {
                // arrange
                var sut = ClearableValue.Set(Fixture.Create <int>());

                // act
                // assert
                sut.Should().BeSetVariant(because: "{0} is created using generic Set<T>() method and should be treated as 'set'", sut);
            }
            public void SetOfDifferentValues_ShouldNotBeEqual()
            {
                // arrange
                var set1 = ClearableValue <int> .Set(1);

                var set2 = ClearableValue <int> .Set(2);

                // act
                // assert
                set1.Equals(set2).Should().BeFalse();
                set2.Equals(set1).Should().BeFalse();
            }
            public void SetOfSameValues_ShouldBeEqual()
            {
                // arrange
                var set1 = ClearableValue <int> .Set(1);

                var set2 = ClearableValue <int> .Set(1);

                // act
                // assert
                set1.Equals(set2).Should().BeTrue();
                set2.Equals(set1).Should().BeTrue();
            }
            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 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();
            }
Пример #7
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()));
        }
Пример #8
0
            public void Set_ShouldNotCallClearOrNoActionFunc()
            {
                // arrange
                var sut = ClearableValue <int> .Set(Fixture.Create <int>());

                // act
                Action match = () => sut.Match(
                    set: _ => State.Set,
                    clear: () => throw new InvalidOperationException(),
                    noAction: () => throw new InvalidOperationException());

                // assert
                match.Should().NotThrow(because: "matching 'set' should not call neither 'clear' nor 'noAction' function");
            }
Пример #9
0
            public void Set_ShouldMatch()
            {
                // arrange
                var sut = ClearableValue <int> .Set(Fixture.Create <int>());

                // act
                var result = sut.Match(
                    set: _ => State.Set,
                    clear: () => State.Clear,
                    noAction: () => State.NoAction);

                // assert
                result.Should().Be(State.Set);
            }
Пример #10
0
            public void Set_ShouldMapToSet()
            {
                // arrange
                int originalValue = Fixture.Create <int>();
                var sut           = ClearableValue <int> .Set(originalValue);

                Func <int, string> map           = e => e.ToString();
                string             expectedValue = map(originalValue);

                // act
                var result = sut.Map(map);

                // assert
                result.Should().BeSetOf(expectedValue);
            }
Пример #11
0
 public static ClearableValue <string> ClearIfNullOrWhiteSpace(this string value) =>
 !string.IsNullOrWhiteSpace(value)
     ? ClearableValue <string> .Set(value)
     : ClearableValue <string> .Clear();
Пример #12
0
 public static ClearableValue <string> ClearIfNullOrEmpty(this string value) =>
 !string.IsNullOrEmpty(value)
     ? ClearableValue <string> .Set(value)
     : ClearableValue <string> .Clear();
Пример #13
0
 public static ClearableValue <T> SetIfNotNull <T>(this T?value) where T : struct =>
 value.HasValue
     ? ClearableValue.Set(value.Value)
 : ClearableValue <T> .NoAction();
Пример #14
0
 public static ClearableValue <T> ClearIfNull <T>(this T?value) where T : struct =>
 value.HasValue
     ? ClearableValue.Set(value.Value)
 : ClearableValue <T> .Clear();
Пример #15
0
 public static ClearableValue <T> ClearIfNull <T>(this T value) where T : class =>
 value is null
     ? ClearableValue <T> .Clear()
 : ClearableValue.Set(value);