private static void CheckDependencies(ModelConfigurationNode root, MutatorConfiguration mutator) { if (root == null || root.Root == root || mutator == null || mutator.Dependencies == null) { return; } foreach (var dependency in mutator.Dependencies) { if (!root.Root.Traverse(dependency.Body, root, out _, false)) { throw new FoundExternalDependencyException("Unable to build validator for the subtree '" + root.Parent + "' due to the external dependency '" + dependency + "'"); } } }
public static void AddMutator(this ModelConfigurationNode node, MutatorConfiguration mutator) { if (mutator.IsUncoditionalSetter()) { for (var i = 0; i < node.Mutators.Count; ++i) { if (node.mutators[i].Value.IsUncoditionalSetter() && ExpressionEquivalenceChecker.Equivalent(node.Path, node.mutators[i].Key, false, false)) { node.mutators[i] = new KeyValuePair <Expression, MutatorConfiguration>(node.Path, mutator); return; } } } node.mutators.Add(new KeyValuePair <Expression, MutatorConfiguration>(node.Path, mutator)); }
public static bool IsUncoditionalSetter(this MutatorConfiguration mutator) { return(mutator is EqualsToConfiguration && !(mutator is EqualsToIfConfiguration)); }
private static KeyValuePair <Expression, MutatorConfiguration> PurgeFilters(Expression path, MutatorConfiguration mutator) { var filters = new List <LambdaExpression>(); var cleanedPath = path.CleanFilters(filters); if (filters.Any(filter => filter != null)) { var shards = path.SmashToSmithereens(); var cleanedShards = cleanedPath.SmashToSmithereens(); var aliases = new List <KeyValuePair <Expression, Expression> >(); var i = 0; var j = 0; LambdaExpression condition = null; foreach (var filter in filters) { while (!(shards[i].NodeType == ExpressionType.Call && (((MethodCallExpression)shards[i]).Method.IsCurrentMethod() || ((MethodCallExpression)shards[i]).Method.IsEachMethod()))) { ++i; } while (!(cleanedShards[j].NodeType == ExpressionType.Call && (((MethodCallExpression)cleanedShards[j]).Method.IsCurrentMethod() || ((MethodCallExpression)cleanedShards[j]).Method.IsEachMethod()))) { ++j; } if (filter == null) { continue; } aliases.Add(new KeyValuePair <Expression, Expression>(cleanedShards[j], shards[i])); condition = condition == null ? filter : condition.AndAlso(filter, false); ++i; ++j; } mutator = mutator.ResolveAliases(new LambdaAliasesResolver(aliases)).If(condition); } return(new KeyValuePair <Expression, MutatorConfiguration>(cleanedPath, mutator)); }
private static void Qxx(ModelConfigurationNode destTree, List <KeyValuePair <Expression, Expression> > conditionalSetters, MutatorConfiguration mutatedMutator, CompositionPerformer performer, Expression resolvedKey) { var unconditionalSetter = conditionalSetters.SingleOrDefault(pair => pair.Value == null); Expression invertedCondition = null; foreach (var setter in conditionalSetters) { var mutatedPath = setter.Key; var condition = setter.Value; if (condition == null) { continue; } Expression currentInvertedCondition = Expression.Not(condition); invertedCondition = invertedCondition == null ? currentInvertedCondition : Expression.AndAlso(invertedCondition, currentInvertedCondition); var primaryDependencies = Expression.Lambda(mutatedPath, mutatedPath.ExtractParameters()).ExtractPrimaryDependencies().Select(lambda => lambda.Body); var commonPath = primaryDependencies.FindLCP(); var destNode = commonPath == null ? destTree : destTree.Traverse(commonPath, true); destNode.mutators.Add(PurgeFilters(mutatedPath, mutatedMutator.If(Expression.Lambda(condition, condition.ExtractParameters())))); } { var mutatedPath = unconditionalSetter.Key ?? performer.Perform(resolvedKey); var primaryDependencies = Expression.Lambda(mutatedPath, mutatedPath.ExtractParameters()).ExtractPrimaryDependencies().Select(lambda => lambda.Body); var commonPath = primaryDependencies.FindLCP(); var destNode = commonPath == null ? destTree : destTree.Traverse(commonPath, true); destNode.mutators.Add(PurgeFilters(mutatedPath, invertedCondition == null ? mutatedMutator : mutatedMutator.If(Expression.Lambda(invertedCondition, invertedCondition.ExtractParameters())))); } }
public static void AddMutatorSmart(this ModelConfigurationNode node, LambdaExpression path, MutatorConfiguration mutator) { path = (LambdaExpression)path.Simplify(); LambdaExpression filter; var simplifiedPath = PathSimplifier.SimplifyPath(path, out filter); mutator = mutator.ResolveAliases(ExpressionAliaser.CreateAliasesResolver(simplifiedPath.Body, path.Body)); node.Traverse(simplifiedPath.Body, true).AddMutator(path.Body, filter == null ? mutator : mutator.If(filter)); }