Exemplo n.º 1
0
        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"));
        }
Exemplo n.º 2
0
        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( ));
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
            }
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }