public void XorSolutions( ) { IRuleChecker <IList <string> > rc = new RuleCheckerBase <IList <string> >( ); rc.Add( new Xor <IList <string> >( new Contains <string>("a"), new Contains <string>("b"))); IList <string> list = new CList <string>( ); IViolation <IList <string> > violation = rc.Check(list); Assert.IsNotInstanceOf <NonViolation <IList <string> > >(violation); ISolution <IList <string> >[] solutions = violation.Solutions.ToArray( ); Assert.AreEqual(2, solutions.Length); IAction <IList <string> >[] actions1 = solutions[0].Actions.ToArray( ); IAction <IList <string> >[] actions2 = solutions[1].Actions.ToArray( ); Assert.AreEqual(1, actions1.Length); Assert.AreEqual(1, actions2.Length); Assert.AreEqual(1, actions1.Count(a => a is Add <string>)); Assert.AreEqual(0, actions1.Count(a => a is Remove <string>)); Assert.AreEqual(1, actions2.Count(a => a is Add <string>)); Assert.AreEqual(0, actions2.Count(a => a is Remove <string>)); IAction <IList <string> >[] allActions = actions1.Union(actions2).ToArray( ); Assert.AreEqual(1, allActions.Count(a => a is Add <string> add && add.Target == "a")); Assert.AreEqual(1, allActions.Count(a => a is Add <string> add && add.Target == "b")); Assert.AreEqual(0, allActions.Count(a => a is Remove <string> rem && rem.Target == "a")); Assert.AreEqual(0, allActions.Count(a => a is Remove <string> rem && rem.Target == "b")); }
public void ImpliesViolation( ) { IRuleChecker <IList <string> > rc = new RuleCheckerBase <IList <string> >( ); rc.Add( new Implies <IList <string> >( new Contains <string>("hello"), new Contains <string>("world"))); IList <string> list = new List <string> { "hello" }; IViolation <IList <string> > violation = rc.Check(list); Assert.IsInstanceOf <Violation <IList <string> > >(violation); Assert.AreEqual(2, violation.Solutions.Count( )); ISolution <IList <string> > solRemA = violation.Solutions.FirstOrDefault(s => s.Actions.FirstOrDefault( ) is Remove <string>); Assert.NotNull(solRemA); Assert.AreEqual(1, solRemA.Actions.Count( )); Assert.AreEqual("hello", (solRemA.Actions.FirstOrDefault( ) as Remove <string>)?.Target); ISolution <IList <string> > solAddB = violation.Solutions.FirstOrDefault(s => s.Actions.FirstOrDefault( ) is Add <string>); Assert.NotNull(solAddB); Assert.AreEqual(1, solAddB.Actions.Count( )); Assert.AreEqual("world", (solAddB.Actions.FirstOrDefault( ) as Add <string>)?.Target); Assert.AreEqual(solAddB, violation.Solutions.FirstOrDefault( )); }
public bool Resolve(IParserResult prIn, out IParserResult prOut, IBehaviors context) { IEnumerable <IRule <IParserResult> > rules = _rules.Union( RuleDeriver.Instance.DeriveRules(prIn.CliSpecification)) .ToList( ); HelpOption hlpOpt = (HelpOption)prIn.CliSpecification.Options.FirstOrDefault(opt => opt is HelpOption); if (hlpOpt != null) { rules = rules.Select( r => new HelpIsSetOr(hlpOpt, r)); } RuleCheckerBase <IParserResult> rc = new RuleCheckerBase <IParserResult>(rules); if (rc.Check(prIn, context) == NonViolation <IParserResult> .Instance) { prOut = prIn; return(true); } IViolation <IParserResult> violation; prOut = (IParserResult)prIn.Clone( ); while ((violation = rc.Check(prOut, context)) != NonViolation <IParserResult> .Instance) { _config.Out.WriteLine( ); _config.Out.WriteLine(prOut); _config.Out.WriteLine($"> {violation.Description}"); if (!violation.HasSolution) { _config.Out.WriteLine("> conflict has no solution"); prOut = null; return(false); } switch (Prompt(violation.Solutions, out ISolution <IParserResult> chosenSolution)) { case UserAction.Cancel: prOut = null; return(false); case UserAction.AutoSolve: AutoSolve(prOut, out prOut, rules, context); break; case UserAction.ApplySolution: ApplySolution(prOut, out prOut, chosenSolution, context); break; } } return(true); }
public void ContainsNoViolation( ) { IRuleChecker <IList <string> > rc = new RuleCheckerBase <IList <string> >( ); rc.Add(new Contains <string>("hello")); IList <string> list = new List <string> { "hello" }; Assert.AreEqual(rc.Check(list), NonViolation <IList <string> > .Instance); }
public void ContainsViolation( ) { IRuleChecker <IList <string> > rc = new RuleCheckerBase <IList <string> >( ); rc.Add(new Contains <string>("hello")); IList <string> list = new List <string>( ); IViolation <IList <string> > violation = rc.Check(list); Assert.IsInstanceOf <Violation <IList <string> > >(violation); Assert.AreEqual(1, violation.Solutions.Count( )); Assert.AreEqual(1, violation.Solutions.First( ).Actions.Count( )); Assert.IsInstanceOf <Add <string> >(violation.Solutions.First( ).Actions.First( )); Assert.AreEqual("hello", (violation.Solutions.First( ).Actions.First( ) as Add <string>)?.Target); }
private void XorN(Xor <IList <string> > rule, params string[] args) { IRuleChecker <IList <string> > rc = new RuleCheckerBase <IList <string> >( ); rc.Add(rule); IList <string> list = args.Where(arg => arg != null).ToList( ); IViolation <IList <string> > violation = rc.Check(list); if (args.Count(arg => arg != null) == 1) { Assert.IsInstanceOf <NonViolation <IList <string> > >(violation); } else { Assert.IsNotInstanceOf <NonViolation <IList <string> > >(violation); } }
public void ImpliesNoViolation( ) { IRuleChecker <IList <string> > rc = new RuleCheckerBase <IList <string> >( ); rc.Add( new Implies <IList <string> >( new Contains <string>("hello"), new Contains <string>("world"))); IList <string> list = new List <string>( ); IViolation <IList <string> > violation = rc.Check(list); Assert.IsInstanceOf <NonViolation <IList <string> > >(violation); list.Add("world"); violation = rc.Check(list); Assert.IsInstanceOf <NonViolation <IList <string> > >(violation); list.Add("hello"); violation = rc.Check(list); Assert.IsInstanceOf <NonViolation <IList <string> > >(violation); }
public void Or( ) { IRuleChecker <IList <string> > rc = new RuleCheckerBase <IList <string> >( ); rc.Add(new Or <IList <string> >(new Contains <string>("hello"), new Contains <string>("world"))); IList <string> list = new List <string>( ); IViolation <IList <string> > violation = rc.Check(list); Assert.IsInstanceOf <Violation <IList <string> > >(violation); Assert.AreEqual(2, violation.Solutions.Count( )); ISolution <IList <string> > solAddA = violation.Solutions .FirstOrDefault( s => s.Actions.FirstOrDefault( ) is Add <string> && (s.Actions.FirstOrDefault( ) as Add <string>)?.Target == "hello"); Assert.IsNotNull(solAddA); Assert.AreEqual(1, solAddA.Actions.Count( )); ISolution <IList <string> > solAddB = violation.Solutions .FirstOrDefault( s => s.Actions.FirstOrDefault( ) is Add <string> && (s.Actions.FirstOrDefault( ) as Add <string>)?.Target == "world"); Assert.IsNotNull(solAddB); Assert.AreEqual(1, solAddB.Actions.Count( )); list.Add("hello"); violation = rc.Check(list); Assert.IsInstanceOf <NonViolation <IList <string> > >(violation); list.Remove("hello"); list.Add("world"); violation = rc.Check(list); Assert.IsInstanceOf <NonViolation <IList <string> > >(violation); list.Add("hello"); violation = rc.Check(list); Assert.IsInstanceOf <NonViolation <IList <string> > >(violation); }