public void ShouldYieldEvaluationResultsInCircle()
        {
            //GIVEN
            Displayable d1          = Substitute.For <Displayable>();
            Displayable d2          = Substitute.For <Displayable>();
            Displayable d3          = Substitute.For <Displayable>();
            var         displayable = new CircularDisplayable(d1, d2, d3);
            var         input       = Any.Array <char>();

            var d1Result = Any.String();
            var d2Result = Any.String();
            var d3Result = Any.String();

            d1.Evaluate(input).Returns(d1Result);
            d2.Evaluate(input).Returns(d2Result);
            d3.Evaluate(input).Returns(d3Result);

            //WHEN
            var result1 = displayable.Evaluate(input);
            var result2 = displayable.Evaluate(input);
            var result3 = displayable.Evaluate(input);
            var result4 = displayable.Evaluate(input);

            //THEN
            XAssert.Equal(d1Result, result1);
            XAssert.Equal(d2Result, result2);
            XAssert.Equal(d3Result, result3);
            XAssert.Equal(d1Result, result4);
        }
Beispiel #2
0
        public void Build_one_action_and_predicate()
        {
            string updatedValue = null;

            //Handle a string with one action
            var                typeHandlers  = new List <TypeHandler>();
            var                stringHandler = new TypeHandler(typeof(string));
            Action <string>    action        = s => { updatedValue = s; };
            Predicate <string> predicate     = s => s.Length > 5;
            var                handler       = PredicateAndHandler.CreateAction(action, predicate: predicate);

            stringHandler.Handlers.Add(handler);
            typeHandlers.Add(stringHandler);
            var builder = new MatchExpressionBuilder <object>();

            //Build the expression
            var result = builder.BuildLambdaExpression(typeHandlers);

            //Verify returned arguments
            XAssert.Equal(2, result.Arguments.Length, "Should contain the action and predicate delegate");
            Assert.Same(action, result.Arguments[0]);
            Assert.Same(predicate, result.Arguments[1]);

            //Compile the expression and test it
            var lambda = (Func <object, Action <String>, Predicate <string>, bool>)result.LambdaExpression.Compile();

            lambda("short", action, predicate);
            Assert.Null(updatedValue);
            lambda("longer value", action, predicate);
            Assert.Equal("longer value", updatedValue);
            lambda(4711, action, predicate);
            Assert.Equal("longer value", updatedValue);
        }
        public void Build_one_action_that_expects_base_type()
        {
            string updatedValue = null;

            //Handle a string with one action
            var             typeHandlers  = new List <TypeHandler>();
            var             stringHandler = new TypeHandler(typeof(string));
            Action <object> action        = s => { updatedValue = s is string? "WasString:" + (string)s : "WasNotString" + s.ToString(); };
            var             handler       = PredicateAndHandler.CreateAction(action, handlerFirstArgumentShouldBeBaseType: true);

            stringHandler.Handlers.Add(handler);
            typeHandlers.Add(stringHandler);
            var builder = new MatchExpressionBuilder <object>();

            //Build the expression
            var result = builder.BuildLambdaExpression(typeHandlers);

            //Verify returned arguments
            XAssert.Equal(1, result.Arguments.Length, "Should only contain the action delegate");
            Assert.Same(action, result.Arguments[0]);

            //Compile the expression and test it
            var lambda = (Func <object, Action <object>, bool>)result.LambdaExpression.Compile();

            lambda(4711, action);
            Assert.Equal(null, updatedValue);
            lambda("the value", action);
            Assert.Equal("WasString:the value", updatedValue);
        }
        public void Build_one_action_for_same_type()
        {
            string updatedValue = null;

            //Handle a string with one action
            var             typeHandlers  = new List <TypeHandler>();
            var             stringHandler = new TypeHandler(typeof(string));
            Action <string> action        = s => { updatedValue = s; };
            var             handler       = PredicateAndHandler.CreateAction(action);

            stringHandler.Handlers.Add(handler);
            typeHandlers.Add(stringHandler);
            var builder = new MatchExpressionBuilder <string>();

            //Build the expression
            var result = builder.BuildLambdaExpression(typeHandlers);

            //Verify returned arguments
            XAssert.Equal(1, result.Arguments.Length, "Should only contain the action delegate");
            Assert.Same(action, result.Arguments[0]);

            //Compile the expression and test it
            var lambda = (Func <string, Action <String>, bool>)result.LambdaExpression.Compile();

            lambda("the value", action);
            Assert.Equal("the value", updatedValue);
        }
        public void ShouldAllowGeneratingIntegersFromSequence()
        {
            var value1 = Any.IntegerFromSequence(startingValue: 12, step: 112);
            var value2 = Any.IntegerFromSequence(startingValue: 12, step: 112);

            XAssert.Equal(value1, value2 - 112);
            Assert.Greater(value1, 12);
        }
Beispiel #6
0
        public void AllowAssertingWhetherConstClassHasUniqueValues()
        {
            XAssert.HasUniqueConstants <ConstsWithUniqueValues>();

            var exception = Assert.Throws <DuplicateConstantException>(XAssert.HasUniqueConstants <ConstsWithRepeatingValues>);

            XAssert.Equal("Val1 <0> is a duplicate of Val3 <0>", exception.Message);
        }
        public void ShouldGenerateStringOfGivenLength(int stringLength)
        {
            //WHEN
            var str = Any.StringOfLength(stringLength);

            //THEN
            XAssert.Equal(stringLength, str.Length);
        }
Beispiel #8
0
        public void Build_two_funcs_for_different_types()
        {
            string updatedValue = null;

            //Handle a string with one action
            var typeHandlers          = new List <TypeHandler>();
            var stringHandler         = new TypeHandler(typeof(string));
            Func <string, bool> func1 = s =>
            {
                if (s.Length > 5)
                {
                    updatedValue = s;
                    return(true);
                }
                return(false);
            };
            var handler1 = PredicateAndHandler.CreateFunc(func1);

            stringHandler.Handlers.Add(handler1);
            typeHandlers.Add(stringHandler);

            var intHandler = new TypeHandler(typeof(int));
            //Handle an int with one action
            Func <int, bool> func2 = i =>
            {
                if (i < 10)
                {
                    updatedValue = i.ToString();
                    return(true);
                }
                return(false);
            };
            var handler2 = PredicateAndHandler.CreateFunc(func2);

            intHandler.Handlers.Add(handler2);
            typeHandlers.Add(intHandler);

            //Build the expression
            var builder = new MatchExpressionBuilder <object>();
            var result  = builder.BuildLambdaExpression(typeHandlers);

            //Verify returned arguments
            XAssert.Equal(2, result.Arguments.Length, "Should contain the funcs");
            Assert.Same(func1, result.Arguments[0]);
            Assert.Same(func2, result.Arguments[1]);

            //Compile the expression and test it
            var lambda = (Func <object, Func <String, bool>, Func <int, bool>, bool>)result.LambdaExpression.Compile();

            lambda("short", func1, func2);
            Assert.Equal(null, updatedValue);
            lambda("longer value", func1, func2);
            Assert.Equal("longer value", updatedValue);
            lambda(2, func1, func2);
            Assert.Equal("2", updatedValue);
            lambda(4711, func1, func2);
            Assert.Equal("2", updatedValue);
        }
        public void ShouldAllowGeneratingDivisibleIntegers()
        {
            var value1 = Any.IntegerDivisibleBy(5);
            var value2 = Any.IntegerDivisibleBy(5);

            XAssert.NotEqual(value1, value2);
            XAssert.Equal(0, value1 % 5);
            XAssert.Equal(0, value2 % 5);
        }
 private static void AssertStringIsNumeric(string theString, int expectedLength)
 {
     XAssert.Equal(expectedLength, theString.Length);
     foreach (var character in theString)
     {
         Assert.True(char.IsDigit(character), $"Expected digit, got {character}");
     }
     Assert.AreNotEqual('0', theString[0]);
 }
Beispiel #11
0
        public object CreateInstanceAsValueObjectWithPreviousParameters()
        {
            var instance = DefaultValue.Of(_type);

            this.Invoking(_ => { instance = _.CreateInstanceWithCurrentConstructorArguments(); })
            .Should().NotThrow(_type + " cannot even be created as a value object");
            XAssert.Equal(_type, instance.GetType());
            return(instance);
        }
        public void ShouldBeAbleToWrapSubstitutesAndSkipOverridingResultsStubbedWithNonDefaultValues()
        {
            var instance        = Any.SubstituteOf <RecursiveInterface>();
            var anotherInstance = Substitute.For <RecursiveInterface>();

            instance.Nested.Returns(anotherInstance);

            XAssert.Equal(anotherInstance, instance.Nested);
        }
Beispiel #13
0
        public void Build_with_more_than_15_arguments()
        {
            //15 is the maximum number of arguments without having to put actions/funcs/predicates in an object array.
            //This comes from BuildLambdaExpression builds a Func, and Funcs may only have 16 input args: Func<T1,...,T16,TResult>
            //T1-argument is reserved for the obligatory value argument, which leaves us with 15 arguments to be used for the
            //actions/funcs/predicates. If we use more than that T16-argument will be an object[] containing the rest

            string updatedValue = null;

            //Handle a string with one action
            var                typeHandlers    = new List <TypeHandler>();
            var                stringHandler   = new TypeHandler(typeof(string));
            Action <string>    actionString    = s => { };
            Predicate <string> predicateString = s => false;

            for (var i = 0; i < 7; i++)
            {
                stringHandler.Handlers.Add(PredicateAndHandler.CreateAction(actionString, predicate: predicateString));
            }
            typeHandlers.Add(stringHandler);

            var intHandler = new TypeHandler(typeof(int));
            //Handle an int with one action
            Action <int>    actionInt    = i => { updatedValue = i.ToString(); };
            Predicate <int> predicateInt = i => i < 100;
            var             handler2     = PredicateAndHandler.CreateAction(actionInt, predicate: predicateInt);

            intHandler.Handlers.Add(handler2);
            typeHandlers.Add(intHandler);

            //Build the expression
            var builder = new MatchExpressionBuilder <object>();
            var result  = builder.BuildLambdaExpression(typeHandlers);

            //Verify returned arguments
            XAssert.Equal(15, result.Arguments.Length, "Should contain the action and predicate delegates");
            var lastArgument = result.Arguments.Last();

            Assert.IsType <object[]>(lastArgument);
            var extraParamsArray = (object[])lastArgument;

            Assert.Equal(2, extraParamsArray.Length);
            Assert.Same(actionInt, extraParamsArray[0]);
            Assert.Same(predicateInt, extraParamsArray[1]);

            //Compile the expression and test it
            var lambda = (Func <object, Action <String>, Predicate <string>, Action <String>, Predicate <string>, Action <String>, Predicate <string>, Action <String>, Predicate <string>, Action <String>, Predicate <string>, Action <String>, Predicate <string>, Action <String>, Predicate <string>, object[], bool>)
                         result.LambdaExpression.Compile();

            lambda("some value", actionString, predicateString, actionString, predicateString, actionString, predicateString, actionString, predicateString, actionString, predicateString, actionString, predicateString, actionString, predicateString, extraParamsArray);
            Assert.Equal(null, updatedValue);
            lambda(4711, actionString, predicateString, actionString, predicateString, actionString, predicateString, actionString, predicateString, actionString, predicateString, actionString, predicateString, actionString, predicateString, extraParamsArray);
            Assert.Equal(null, updatedValue);
            lambda(42, actionString, predicateString, actionString, predicateString, actionString, predicateString, actionString, predicateString, actionString, predicateString, actionString, predicateString, actionString, predicateString, extraParamsArray);
            Assert.Equal("42", updatedValue);
        }
        public void ShouldCreateSortedSetWithThreeDistinctValues()
        {
            //WHEN
            var set = Any.SortedSet <int>();

            //THEN
            CollectionAssert.IsOrdered(set);
            CollectionAssert.AllItemsAreUnique(set);
            XAssert.Equal(3, set.Count);
        }
        public void ShouldAllowCreatingCustomCollectionInstances()
        {
            var customCollection = Any.Instance <MyOwnCollection <RecursiveInterface> >();

            XAssert.Equal(3, customCollection.Count);
            foreach (var recursiveInterface in customCollection)
            {
                XAssert.NotNull(recursiveInterface);
            }
        }
        public void ShouldAllowGeneratingDistinctIntegersWithExactNumberOfDigits()
        {
            var length = MaxLengthOfInt() - 1;
            var value1 = Any.IntegerWithExactDigitsCount(length);
            var value2 = Any.IntegerWithExactDigitsCount(length);

            XAssert.Equal(length, value1.ToString().Length, value1.ToString());
            XAssert.Equal(length, value2.ToString().Length, value2.ToString());
            XAssert.NotEqual(value1, value2);
        }
Beispiel #17
0
        public void ShouldDepascalizeString(string input, string expected, string comment)
        {
            //GIVEN
            var depascalization = new Depascalization.Transformation();

            //WHEN
            var depascalizedString = depascalization.Of(input);

            //THEN
            XAssert.Equal(expected, depascalizedString);
        }
        public void ShouldAllowGeneratingFuncs()
        {
            //GIVEN
            var func = Any.Func <int, int, string>();

            //WHEN
            var result1 = func(1, 2);
            var result2 = func(1, 3);

            //THEN
            XAssert.Equal(result2, result1);
        }
Beispiel #19
0
        public void ShouldCreateResolutionCapableOfGeneratingArrays()
        {
            //GIVEN
            var resolution = new SpecialCasesOfResolutions <RecursiveInterface[]>(new CollectionGenerator(new GenericMethodProxyCalls())).CreateResolutionOfArray();

            //WHEN

            //THEN
            Assert.True(resolution.Applies());
            XAssert.NotNull(resolution.Apply(Any.Instance <IInstanceGenerator>()));
            XAssert.Equal(3, resolution.Apply(Any.Instance <IInstanceGenerator>()).Length);
        }
        public void ShouldAlwaysReturnTheSameValueFromProxiedMethodOnTheSameObject()
        {
            //GIVEN
            var obj = Any.Instance <ISimple>();

            //WHEN
            var valueFirstTime  = obj.GetString();
            var valueSecondTime = obj.GetString();

            //THEN
            XAssert.Equal(valueFirstTime, valueSecondTime);
        }
        public void ShouldAllowGeneratingDistinctUnsignedLongWithMaxNumberOfDigits()
        {
            var maxLength = MaxLengthOfULong();
            var value1    = Any.UnsignedLongIntegerWithExactDigitsCount(maxLength);
            var value2    = Any.UnsignedLongIntegerWithExactDigitsCount(maxLength);

            XAssert.Equal(maxLength,
                          value1.ToString().Length,
                          value1.ToString());
            XAssert.Equal(maxLength,
                          value2.ToString().Length, value2.ToString());
            XAssert.NotEqual(value1, value2);
        }
        public void ShouldAllowAccessToValuesSetOnPropertiesOnInterfaceInstancesWhenBothGetAndSetArePublic()
        {
            //GIVEN
            var someValue = Any.Integer();
            var obj       = Any.Instance <IGetSettable <int> >();

            //WHEN
            obj.Value = 123;
            obj.Value = someValue;

            //THEN
            XAssert.Equal(someValue, obj.Value);
        }
        public void ShouldBeAbleToWrapSubstitutesAndNotOverrideStubbedValues()
        {
            //GIVEN
            var instance = Any.SubstituteOf <RecursiveInterface>();

            instance.Number.Returns(44543);

            //WHEN
            var result = instance.Number;

            //THEN
            XAssert.Equal(44543, result);
        }
Beispiel #24
0
        public void Build_two_actions_and_predicate_for_different_types()
        {
            string updatedValue = null;

            //Handle a string with one action
            var                typeHandlers  = new List <TypeHandler>();
            var                stringHandler = new TypeHandler(typeof(string));
            Action <string>    action1       = s => { updatedValue = s; };
            Predicate <string> predicate1    = s => s.Length > 5 && s.Length < 15;
            var                handler1      = PredicateAndHandler.CreateAction(action1, predicate: predicate1);

            stringHandler.Handlers.Add(handler1);
            typeHandlers.Add(stringHandler);

            var intHandler = new TypeHandler(typeof(int));
            //Handle an int with one action
            Action <int>    action2    = i => { updatedValue = i.ToString(); };
            Predicate <int> predicate2 = i => i < 10;
            var             handler2   = PredicateAndHandler.CreateAction(action2, predicate: predicate2);

            intHandler.Handlers.Add(handler2);
            typeHandlers.Add(intHandler);

            //Build the expression
            var builder = new MatchExpressionBuilder <object>();
            var result  = builder.BuildLambdaExpression(typeHandlers);

            //Verify returned arguments
            XAssert.Equal(4, result.Arguments.Length, "Should contain the action and predicate delegates");
            Assert.Same(action1, result.Arguments[0]);
            Assert.Same(predicate1, result.Arguments[1]);
            Assert.Same(action2, result.Arguments[2]);
            Assert.Same(predicate2, result.Arguments[3]);

            //Compile the expression and test it
            var lambda = (Func <object, Action <String>, Predicate <string>, Action <int>, Predicate <int>, bool>)result.LambdaExpression.Compile();

            lambda("short", action1, predicate1, action2, predicate2);
            Assert.Equal(null, updatedValue);
            lambda("longer value", action1, predicate1, action2, predicate2);
            Assert.Equal("longer value", updatedValue);
            lambda("12345678901234567890", action1, predicate1, action2, predicate2);
            Assert.Equal("longer value", updatedValue);
            lambda(2, action1, predicate1, action2, predicate2);
            Assert.Equal("2", updatedValue);
            lambda(4711, action1, predicate1, action2, predicate2);
            Assert.Equal("2", updatedValue);
        }
        public void ShouldOutputBlankCharWhenEvaluatedForArrayThatDoesNotContainTrigger()
        {
            //GIVEN
            var onTrigger     = Any.Char();
            var fallbackValue = Any.String();
            var fallback      = Substitute.For <Displayable>();
            var inputTriggers = Any.ArrayWithout(onTrigger);
            var segment       = new SwitchableSegment(onTrigger, Any.String(), fallback);

            fallback.Evaluate(inputTriggers).Returns(fallbackValue);

            //WHEN
            var result = segment.Evaluate(inputTriggers);

            //THEN
            XAssert.Equal(fallbackValue, result);
        }
        public void ShouldOutputOnValueWhenEvaluatedForArrayThatContainsTrigger()
        {
            //GIVEN
            var onTrigger     = Any.Char();
            var onValue       = Any.String();
            var inputTriggers = Any.ArrayWith(onTrigger);
            var fallback      = Substitute.For <Displayable>();
            var segment       = new SwitchableSegment(onTrigger, onValue, new BlankSpace());

            fallback.Evaluate(inputTriggers).Returns(Any.StringOtherThan(onValue));

            //WHEN
            var result = segment.Evaluate(inputTriggers);

            //THEN
            XAssert.Equal(onValue, result);
        }
Beispiel #27
0
        public void ShouldReturnAllElementsInOrderTheyWereAdded()
        {
            //GIVEN
            var element1 = Any.Integer();
            var element2 = Any.Integer();
            var element3 = Any.Integer();
            var list     = CircularList.CreateStartingFrom0(element1, element2, element3);

            //WHEN
            var returnedElement1 = list.Next();
            var returnedElement2 = list.Next();
            var returnedElement3 = list.Next();

            //THEN
            XAssert.Equal(returnedElement1, element1);
            XAssert.Equal(returnedElement2, element2);
            XAssert.Equal(returnedElement3, element3);
        }
Beispiel #28
0
        public void ShouldStartOverReturningElementsWhenItRunsOutOfElements()
        {
            //GIVEN
            var element1 = Any.Integer();
            var element2 = Any.Integer();

            var list = CircularList.CreateStartingFrom0(element1, element2);

            //WHEN
            var returnedElement1 = list.Next();
            var returnedElement2 = list.Next();
            var returnedElement3 = list.Next();
            var returnedElement4 = list.Next();

            //THEN
            XAssert.Equal(returnedElement1, element1);
            XAssert.Equal(returnedElement2, element2);
            XAssert.Equal(returnedElement3, element1);
            XAssert.Equal(returnedElement4, element2);
        }
Beispiel #29
0
        public void Build_one_func_that_expects_base_type()
        {
            string updatedValue = null;

            //Handle a string with one action
            var typeHandlers         = new List <TypeHandler>();
            var stringHandler        = new TypeHandler(typeof(string));
            Func <object, bool> func = o =>
            {
                var s = (string)o;
                if (s.Length > 5)
                {
                    updatedValue = s;
                    return(true);
                }
                return(false);
            };
            var handler = PredicateAndHandler.CreateAction(func, handlerFirstArgumentShouldBeBaseType: true);

            stringHandler.Handlers.Add(handler);
            typeHandlers.Add(stringHandler);
            var builder = new MatchExpressionBuilder <object>();

            //Build the expression
            var result = builder.BuildLambdaExpression(typeHandlers);

            //Verify returned arguments
            XAssert.Equal(1, result.Arguments.Length, "Should only contain the action delegate");
            Assert.Same(func, result.Arguments[0]);

            //Compile the expression and test it
            var lambda = (Func <object, Func <object, bool>, bool>)result.LambdaExpression.Compile();

            lambda(4711, func);
            Assert.Equal(null, updatedValue);
            lambda("short", func);
            Assert.Equal(null, updatedValue);
            lambda("the value", func);
            Assert.Equal("the value", updatedValue);
        }
Beispiel #30
0
 private void AssertOneCall <T>(IReadOnlyList <T> to, string description, Func <T, bool> check)
 {
     XAssert.Equal(1, to.Count, description + ": Expected one call but in fact " + to.Count + " calls were made");
     Assert.True(check(to[0]), description + ": did not match");
 }