Exemplo n.º 1
0
        private void Check <TArg, TResult>([NotNull] Expression <Func <TArg, TResult> > expression, [NotNull] Expression <Func <TArg, TResult> > expectedSimplified)
        {
            var simplifiedExpression = simplifier.Simplify(expression);

            Assert.True(ExpressionEquivalenceChecker.Equivalent(simplifiedExpression, expectedSimplified, strictly: false, distinguishEachAndCurrent: true),
                        "Failed to simplify expression:\nExpected to get '{0}',\n        but got '{1}'", expectedSimplified, simplifiedExpression);
        }
Exemplo n.º 2
0
        protected internal override KeyValuePair <Expression, List <KeyValuePair <int, MutatorConfiguration> > > BuildRawMutators <TValue>(Expression <Func <TData, TValue> > path)
        {
            List <KeyValuePair <int, MutatorConfiguration> > mutators = null;
            Expression abstractPath = null;

            foreach (var tree in trees)
            {
                var current = tree.GetRawMutators(path);
                if (current.Key == null)
                {
                    continue;
                }
                if (abstractPath != null && !ExpressionEquivalenceChecker.Equivalent(abstractPath, current.Key, false, true))
                {
                    throw new InvalidOperationException();
                }
                abstractPath = current.Key;
                if (mutators == null)
                {
                    mutators = current.Value;
                }
                else
                {
                    mutators.AddRange(current.Value);
                }
            }

            return(new KeyValuePair <Expression, List <KeyValuePair <int, MutatorConfiguration> > >(abstractPath, mutators));
        }
Exemplo n.º 3
0
        public static void AssertEquivalentExpressions(this Expression actual, Expression expected)
        {
            var equivalent        = ExpressionEquivalenceChecker.Equivalent(actual, expected, strictly: false, distinguishEachAndCurrent: true);
            var expectedDebugView = ExpressionCompiler.DebugViewGetter(expected.Simplify());
            var actualDebugView   = ExpressionCompiler.DebugViewGetter(actual.Simplify());

            Assert.IsTrue(equivalent, $"Expressions are not equivalent.\nExpected:\n{expectedDebugView}\nActual:\n{actualDebugView}");
        }
        private void Check <TArg1, TArg2, TResult>(
            [NotNull] Expression <Func <TArg1, TArg2, TResult> > rawExpression,
            [NotNull] Expression <Func <TArg1, TArg2, TResult> > expectedExpression)
        {
            var actualExpression = new IfNotNullProcessor().Visit(rawExpression);

            Assert.True(ExpressionEquivalenceChecker.Equivalent(actualExpression, expectedExpression, strictly: false, distinguishEachAndCurrent: true),
                        "Failed to eliminate IfNotNull:\nExpected to get '{0}',\n        but got '{1}'", expectedExpression, actualExpression);
        }
        public static Expression GetAlienArray(this ModelConfigurationNode node)
        {
            var arrays = node.GetArrays();

            if (!arrays.TryGetValue(node.RootType, out var result))
            {
                return(null);
            }
            return(ExpressionEquivalenceChecker.Equivalent(Expression.Lambda(result, result.ExtractParameters()).ExtractPrimaryDependencies()[0].Body, node.Path, false, true) ? null : result);
        }
        private static void AssertEquivalent(LambdaExpression[] actualExpressions, LambdaExpression[] expectedExpressions)
        {
            void AssertEquivalentExpressions(IAssertionContext <Expression> context)
            {
                ExpressionEquivalenceChecker.Equivalent(context.Subject, context.Expectation, true, true)
                .Should().BeTrue($"because\nActual:\n{context.Subject}\n\nExpected:\n{context.Expectation}\n");
            }

            actualExpressions.Should().BeEquivalentTo(expectedExpressions.Select(PrepareLambda), config => config.Using <Expression>(AssertEquivalentExpressions).WhenTypeIs <Expression>());
        }
Exemplo n.º 7
0
        public static void AddMutator(this ModelConfigurationNode node, Expression path, MutatorConfiguration mutator)
        {
            if (mutator.IsUncoditionalSetter())
            {
                for (var i = 0; i < node.mutators.Count; ++i)
                {
                    if (node.mutators[i].Value.IsUncoditionalSetter() && ExpressionEquivalenceChecker.Equivalent(path, node.mutators[i].Key, false, false))
                    {
                        node.mutators[i] = new KeyValuePair <Expression, MutatorConfiguration>(path, mutator);
                        return;
                    }
                }
            }

            node.mutators.Add(new KeyValuePair <Expression, MutatorConfiguration>(path, mutator));
        }
Exemplo n.º 8
0
        private void DoTest(Expression expression, params Expression[] expectedExpressions)
        {
            var smithereens = expression.SmashToSmithereens();

            Assert.That(smithereens.Length, Is.EqualTo(expectedExpressions.Length),
                        $"Expected {expectedExpressions.Length} smithereens, but got {smithereens.Length}.\n" +
                        $"Result: {FormatExpressions(smithereens)}\n" +
                        $"Expected result: {FormatExpressions(expectedExpressions)}");
            for (var i = 0; i < expectedExpressions.Length; ++i)
            {
                var x = smithereens[i];
                var y = expectedExpressions[i];
                Assert.That(ExpressionEquivalenceChecker.Equivalent(x, y, strictly: false, distinguishEachAndCurrent: false),
                            $"Smithereens differ:\n{FormatExpression(x)}\n{FormatExpression(y)}\n\n" +
                            $"Result: {FormatExpressions(smithereens)}\n" +
                            $"Expected result: {FormatExpressions(expectedExpressions)}");
            }
        }
Exemplo n.º 9
0
        public static Expression LCP(this Expression exp1, Expression exp2)
        {
            if (exp1 == null || exp2 == null)
            {
                return(null);
            }
            var shards1 = exp1.SmashToSmithereens();
            var shards2 = exp2.SmashToSmithereens();
            int i;

            for (i = 0; i < shards1.Length && i < shards2.Length; ++i)
            {
                if (!ExpressionEquivalenceChecker.Equivalent(shards1[i], shards2[i], false, true))
                {
                    break;
                }
            }

            return(i == 0 ? null : shards1[i - 1]);
        }
Exemplo n.º 10
0
 private void DoTest(Expression source, Expression expected)
 {
     Assert.That(ExpressionEquivalenceChecker.Equivalent(source.CanonizeParameters(), expected, strictly: true, distinguishEachAndCurrent: true));
 }
 private static void DoTest(Expression expression, Expression expected)
 {
     Assert.That(ExpressionEquivalenceChecker.Equivalent(expression, expected, strictly: false, distinguishEachAndCurrent: true),
                 () => $"Expected:\n{expected}\nBut was:\n{expression}");
 }
Exemplo n.º 12
0
        protected List <MutatorConfiguration> Canonize(IEnumerable <KeyValuePair <int, MutatorConfiguration> > mutators)
        {
            // todo ich: kill, as soon as scripts are fixed
            // todo ich: handle validation priorities
            if (mutators == null)
            {
                return(null);
            }
            var hideIfConfigurations           = new List <HideIfConfiguration>();
            var disableIfConfigurations        = new List <DisableIfConfiguration>();
            var staticAggregatorConfigurations = new Dictionary <string, List <ConditionalAggregatorConfiguration> >();
            //var validatorConfigurations = new List<KeyValuePair<int, ValidatorConfiguration>>();
            SetSourceArrayConfiguration setSourceArrayConfiguration = null;
            var otherConfigurations = new List <MutatorConfiguration>();

            foreach (var mutator in mutators)
            {
                if (mutator.Value is HideIfConfiguration)
                {
                    hideIfConfigurations.Add((HideIfConfiguration)mutator.Value);
                }
                else if (mutator.Value is DisableIfConfiguration)
                {
                    disableIfConfigurations.Add((DisableIfConfiguration)mutator.Value);
                }
//                else if(mutator.Value is ValidatorConfiguration)
//                    validatorConfigurations.Add(new KeyValuePair<int, ValidatorConfiguration>(mutator.Key, (ValidatorConfiguration)mutator.Value));
                else if (mutator.Value is SetSourceArrayConfiguration)
                {
                    var currentSetSourceArrayConfiguration = (SetSourceArrayConfiguration)mutator.Value;
                    if (setSourceArrayConfiguration == null)
                    {
                        setSourceArrayConfiguration = currentSetSourceArrayConfiguration;
                    }
                    else if (!ExpressionEquivalenceChecker.Equivalent(setSourceArrayConfiguration.SourceArray, currentSetSourceArrayConfiguration.SourceArray, false, true))
                    {
                        throw new InvalidOperationException("An attempt to set array from different sources: '" + setSourceArrayConfiguration.SourceArray + "' and '" + currentSetSourceArrayConfiguration.SourceArray + "'");
                    }
                }
                else if (mutator.Value is ConditionalAggregatorConfiguration)
                {
                    var staticAggregator = (ConditionalAggregatorConfiguration)mutator.Value;
                    List <ConditionalAggregatorConfiguration> staticAggregators;
                    if (!staticAggregatorConfigurations.TryGetValue(staticAggregator.Name, out staticAggregators))
                    {
                        staticAggregatorConfigurations.Add(staticAggregator.Name, staticAggregators = new List <ConditionalAggregatorConfiguration>());
                    }
                    staticAggregators.Add(staticAggregator);
                }
                else
                {
                    otherConfigurations.Add(mutator.Value);
                }
            }

            if (hideIfConfigurations.Count == 1)
            {
                otherConfigurations.Add(hideIfConfigurations.Single());
            }
            else if (hideIfConfigurations.Count > 1)
            {
                var condition = hideIfConfigurations[0].Condition;
                var type      = hideIfConfigurations[0].Type;
                for (var i = 1; i < hideIfConfigurations.Count; ++i)
                {
                    condition = condition.OrElse(hideIfConfigurations[i].Condition);
                }
                otherConfigurations.Add(new HideIfConfiguration(type, condition));
            }

            if (disableIfConfigurations.Count == 1)
            {
                otherConfigurations.Add(disableIfConfigurations.Single());
            }
            else if (disableIfConfigurations.Count > 1)
            {
                var condition = disableIfConfigurations[0].Condition;
                var type      = disableIfConfigurations[0].Type;
                for (var i = 1; i < disableIfConfigurations.Count; ++i)
                {
                    condition = condition.OrElse(disableIfConfigurations[i].Condition);
                }
                otherConfigurations.Add(new DisableIfConfiguration(type, condition));
            }

            foreach (var item in staticAggregatorConfigurations)
            {
                var staticAggregators = item.Value;
                if (staticAggregators.Count == 1)
                {
                    otherConfigurations.Add(staticAggregators.Single());
                }
                else
                {
                    var condition = staticAggregators[0].Condition;
                    var type      = staticAggregators[0].Type;
                    for (var i = 1; i < staticAggregators.Count; ++i)
                    {
                        condition = condition.OrElse(staticAggregators[i].Condition);
                    }
                    otherConfigurations.Add(new ConditionalAggregatorConfiguration(type, condition, item.Key));
                }
            }

            if (setSourceArrayConfiguration != null)
            {
                otherConfigurations.Add(setSourceArrayConfiguration);
            }
            return(otherConfigurations);
        }
 private static void AssertEquivalentExpressions(Expression expected, Expression actual)
 {
     ExpressionEquivalenceChecker.Equivalent(expected, actual, false, true)
     .Should().BeTrue($"because\nExpected:\n{expected}\n\nActual:\n{actual}");
 }
 private void DoTest([NotNull] Expression actual, [NotNull] Expression expected)
 {
     Assert.That(ExpressionEquivalenceChecker.Equivalent(actual, expected, strictly: false, distinguishEachAndCurrent: true),
                 () => $"Expected:\n{expected}\nBut was:\n{actual}");
 }