Пример #1
0
        public void TestShortcut()
        {
            var rule = new Rule("TestShortcut")
            {
                Expression = "1 AND 2 OR 3",
                Clauses    = new List <Clause>()
                {
                    new Clause(Operation.IsTrue, "BoolField")
                    {
                        Label  = "1",
                        Invert = true
                    },
                    new Clause(Operation.Custom, "StringField")
                    {
                        Label = "2"
                    },
                    new Clause(Operation.IsTrue, "BoolField")
                    {
                        Label = "3"
                    }
                }
            };

            var rule2 = new Rule("TestShortcut")
            {
                Expression = "1 OR 2",
                Clauses    = new List <Clause>()
                {
                    new Clause(Operation.IsTrue, "BoolField")
                    {
                        Label = "1",
                    },
                    new Clause(Operation.Custom, "StringField")
                    {
                        CustomOperation = "BAR",
                        Label           = "2",
                    }
                }
            };

            var analyzer = new Analyzer();

            analyzer.SetOperation(new OatOperation(Operation.Custom, analyzer)
            {
                CustomOperation   = "BAR",
                OperationDelegate = (Clause _, object?__, object?___, IEnumerable <ClauseCapture>?c) =>
                {
                    // We should shortcut calling the custom operation entirely, because it is not being captured
                    // Given the test data this line should never be hit
                    Assert.Fail();
                    return(new OperationResult(false, null));
                }
            });
            var target = new TestObject()
            {
                StringField = "Magic",
                BoolField   = true
            };
            var cap = analyzer.GetCapture(rule, target);

            cap = analyzer.GetCapture(rule2, target);
        }
Пример #2
0
        public void VerifyEqOperator()
        {
            var assertTrueObject = new TestObject()
            {
                StringField = "Magic",
                BoolField   = true,
                IntField    = 700
            };

            var assertFalseObject = new TestObject()
            {
                StringField = "NotMagic",
                BoolField   = false,
                IntField    = 701
            };

            var stringEquals = new Rule("String Equals Rule")
            {
                Target  = "TestObject",
                Clauses = new List <Clause>()
                {
                    new Clause(Operation.Equals, "StringField")
                    {
                        Data = new List <string>()
                        {
                            "Magic"
                        }
                    }
                }
            };

            var boolEquals = new Rule("Bool Equals Rule")
            {
                Target  = "TestObject",
                Clauses = new List <Clause>()
                {
                    new Clause(Operation.Equals, "BoolField")
                    {
                        Data = new List <string>()
                        {
                            "True"
                        }
                    }
                }
            };

            var intEquals = new Rule("Int Equals Rule")
            {
                Target  = "TestObject",
                Clauses = new List <Clause>()
                {
                    new Clause(Operation.Equals, "IntField")
                    {
                        Data = new List <string>()
                        {
                            "700"
                        }
                    }
                }
            };

            var analyzer = new Analyzer();
            var ruleList = new List <Rule>()
            {
                boolEquals, intEquals, stringEquals
            };

            var trueObjectResults  = analyzer.Analyze(ruleList, assertTrueObject);
            var falseObjectResults = analyzer.Analyze(ruleList, assertFalseObject);

            Assert.IsTrue(trueObjectResults.Any(x => x.Name == "Bool Equals Rule"));
            Assert.IsTrue(trueObjectResults.Any(x => x.Name == "Int Equals Rule"));
            Assert.IsTrue(trueObjectResults.Any(x => x.Name == "String Equals Rule"));

            Assert.IsFalse(falseObjectResults.Any(x => x.Name == "Bool Equals Rule"));
            Assert.IsFalse(falseObjectResults.Any(x => x.Name == "Int Equals Rule"));
            Assert.IsFalse(falseObjectResults.Any(x => x.Name == "String Equals Rule"));

            trueObjectResults  = analyzer.Analyze(ruleList, null, assertTrueObject);
            falseObjectResults = analyzer.Analyze(ruleList, null, assertFalseObject);

            Assert.IsTrue(trueObjectResults.Any(x => x.Name == "Bool Equals Rule"));
            Assert.IsTrue(trueObjectResults.Any(x => x.Name == "Int Equals Rule"));
            Assert.IsTrue(trueObjectResults.Any(x => x.Name == "String Equals Rule"));

            Assert.IsFalse(falseObjectResults.Any(x => x.Name == "Bool Equals Rule"));
            Assert.IsFalse(falseObjectResults.Any(x => x.Name == "Int Equals Rule"));
            Assert.IsFalse(falseObjectResults.Any(x => x.Name == "String Equals Rule"));
        }
Пример #3
0
        public void VerifyLtOperator()
        {
            var trueGtObject = new TestObject()
            {
                IntField = 8999
            };
            var falseGtObject = new TestObject()
            {
                IntField = 9000
            };

            var gtRule = new Rule("Lt Rule")
            {
                Target  = "TestObject",
                Clauses = new List <Clause>()
                {
                    new Clause(Operation.LessThan, "IntField")
                    {
                        Data = new List <string>()
                        {
                            "9000"
                        }
                    }
                }
            };

            var badGtRule = new Rule("Bad Lt Rule")
            {
                Target  = "TestObject",
                Clauses = new List <Clause>()
                {
                    new Clause(Operation.LessThan, "IntField")
                    {
                        Data = new List <string>()
                        {
                            "CONTOSO"
                        }
                    }
                }
            };

            var ltAnalyzer = new Analyzer();
            var ruleList   = new List <Rule>()
            {
                gtRule
            };

            Assert.IsTrue(ltAnalyzer.Analyze(ruleList, trueGtObject).Any());
            Assert.IsFalse(ltAnalyzer.Analyze(ruleList, falseGtObject).Any());

            Assert.IsTrue(ltAnalyzer.Analyze(ruleList, null, trueGtObject).Any());
            Assert.IsFalse(ltAnalyzer.Analyze(ruleList, null, falseGtObject).Any());

            ruleList = new List <Rule>()
            {
                badGtRule
            };

            Assert.IsFalse(ltAnalyzer.Analyze(ruleList, trueGtObject).Any());
            Assert.IsFalse(ltAnalyzer.Analyze(ruleList, falseGtObject).Any());

            Assert.IsFalse(ltAnalyzer.Analyze(ruleList, null, trueGtObject).Any());
            Assert.IsFalse(ltAnalyzer.Analyze(ruleList, null, falseGtObject).Any());
        }
Пример #4
0
        public void VerifyContainsOperator()
        {
            var trueStringObject = new TestObject()
            {
                StringField = "ThisStringContainsMagic"
            };

            var falseStringObject = new TestObject()
            {
                StringField = "ThisOneDoesNot"
            };

            var stringContains = new Rule("String Contains Rule")
            {
                Target  = "TestObject",
                Clauses = new List <Clause>()
                {
                    new Clause(Operation.Contains, "StringField")
                    {
                        Data = new List <string>()
                        {
                            "Magic",
                            "String"
                        }
                    }
                }
            };

            var stringAnalyzer = new Analyzer();
            var ruleList       = new List <Rule>()
            {
                stringContains
            };

            Assert.IsTrue(stringAnalyzer.Analyze(ruleList, trueStringObject).Any());
            Assert.IsFalse(stringAnalyzer.Analyze(ruleList, falseStringObject).Any());

            Assert.IsTrue(stringAnalyzer.Analyze(ruleList, null, trueStringObject).Any());
            Assert.IsFalse(stringAnalyzer.Analyze(ruleList, null, falseStringObject).Any());

            var trueListObject = new TestObject()
            {
                StringListField = new List <string>()
                {
                    "One",
                    "Magic",
                    "Abra Kadabra"
                }
            };

            var falseListObject = new TestObject()
            {
                StringListField = new List <string>()
                {
                    "One",
                    "Two",
                    "Three"
                }
            };

            var listContains = new Rule("List Contains Rule")
            {
                Target  = "TestObject",
                Clauses = new List <Clause>()
                {
                    new Clause(Operation.Contains, "StringListField")
                    {
                        Data = new List <string>()
                        {
                            "Magic",
                            "Abra Kadabra"
                        }
                    }
                }
            };

            var listAnalyzer = new Analyzer();

            ruleList = new List <Rule>()
            {
                listContains
            };

            Assert.IsTrue(listAnalyzer.Analyze(ruleList, trueListObject).Any());
            Assert.IsFalse(listAnalyzer.Analyze(ruleList, falseListObject).Any());

            Assert.IsTrue(listAnalyzer.Analyze(ruleList, null, trueListObject).Any());
            Assert.IsFalse(listAnalyzer.Analyze(ruleList, null, falseListObject).Any());

            var trueEnumListObject = new TestObject()
            {
                EnumListField = new List <Enum>()
                {
                    ContainsTestEnum.Magic
                }
            };

            var falseEnumListObject = new TestObject()
            {
                EnumListField = new List <Enum>()
                {
                    ContainsTestEnum.Nothing
                }
            };

            var enumListContains = new Rule("Enum List Contains Rule")
            {
                Target  = "TestObject",
                Clauses = new List <Clause>()
                {
                    new Clause(Operation.Contains, "EnumListField")
                    {
                        Data = new List <string>()
                        {
                            "Magic"
                        }
                    }
                }
            };

            var enumListAnalyzer = new Analyzer();

            ruleList = new List <Rule>()
            {
                enumListContains
            };

            Assert.IsTrue(enumListAnalyzer.Analyze(ruleList, trueEnumListObject).Any());
            Assert.IsFalse(enumListAnalyzer.Analyze(ruleList, falseEnumListObject).Any());

            Assert.IsTrue(enumListAnalyzer.Analyze(ruleList, null, trueEnumListObject).Any());
            Assert.IsFalse(enumListAnalyzer.Analyze(ruleList, null, falseEnumListObject).Any());

            var trueStringDictObject = new TestObject()
            {
                StringDictField = new Dictionary <string, string>()
                {
                    { "One", "Money" },
                    { "Two", "Show" },
                    { "Magic Word", "Please" }
                }
            };

            var falseStringDictObject = new TestObject()
            {
                StringDictField = new Dictionary <string, string>()
                {
                    { "One", "Magic" },
                    { "Two", "Something" },
                    { "Three", "Please" }
                }
            };

            var stringDictContains = new Rule("String Dict Contains Rule")
            {
                Target  = "TestObject",
                Clauses = new List <Clause>()
                {
                    new Clause(Operation.Contains, "StringDictField")
                    {
                        DictData = new List <KeyValuePair <string, string> >()
                        {
                            new KeyValuePair <string, string>("Magic Word", "Please"),
                            new KeyValuePair <string, string>("Two", "Show"),
                            new KeyValuePair <string, string>("One", "Money")
                        }
                    }
                }
            };

            var stringDictAnalyzer = new Analyzer();

            ruleList = new List <Rule>()
            {
                stringDictContains
            };

            Assert.IsTrue(stringDictAnalyzer.Analyze(ruleList, trueStringDictObject).Any());
            Assert.IsFalse(stringDictAnalyzer.Analyze(ruleList, falseStringDictObject).Any());

            Assert.IsTrue(stringDictAnalyzer.Analyze(ruleList, null, trueStringDictObject).Any());
            Assert.IsFalse(stringDictAnalyzer.Analyze(ruleList, null, falseStringDictObject).Any());

            var trueListDictObject = new TestObject()
            {
                StringListDictField = new Dictionary <string, List <string> >()
                {
                    {
                        "Magic Words", new List <string>()
                        {
                            "Please",
                            "Thank You"
                        }
                    }
                }
            };

            var falseListDictObject = new TestObject()
            {
                StringListDictField = new Dictionary <string, List <string> >()
                {
                    {
                        "Magic Words", new List <string>()
                        {
                            "Scallywag",
                            "Magic"
                        }
                    }
                }
            };

            var listDictContains = new Rule("List Dict Contains Rule")
            {
                Target  = "TestObject",
                Clauses = new List <Clause>()
                {
                    new Clause(Operation.Contains, "StringListDictField")
                    {
                        DictData = new List <KeyValuePair <string, string> >()
                        {
                            new KeyValuePair <string, string>("Magic Words", "Please"),
                            new KeyValuePair <string, string>("Magic Words", "Thank You"),
                        }
                    }
                }
            };

            var listDictAnalyzer = new Analyzer();

            ruleList = new List <Rule>()
            {
                listDictContains
            };

            Assert.IsTrue(listDictAnalyzer.Analyze(ruleList, trueListDictObject).Any());
            Assert.IsFalse(listDictAnalyzer.Analyze(ruleList, falseListDictObject).Any());

            Assert.IsTrue(listDictAnalyzer.Analyze(ruleList, null, trueListDictObject).Any());
            Assert.IsFalse(listDictAnalyzer.Analyze(ruleList, null, falseListDictObject).Any());

            var enumFlagsContains = new Rule("Enum Flags Contains Rule")
            {
                Clauses = new List <Clause>()
                {
                    new Clause(Operation.Contains)
                    {
                        Data = new List <string>()
                        {
                            "Magic", "Normal"
                        }
                    }
                }
            };

            var enumAnalyzer = new Analyzer();

            ruleList = new List <Rule>()
            {
                enumFlagsContains
            };

            Assert.IsTrue(enumAnalyzer.Analyze(ruleList, Words.Magic | Words.Normal).Any());
            Assert.IsFalse(enumAnalyzer.Analyze(ruleList, Words.Shazam).Any());
            Assert.IsFalse(enumAnalyzer.Analyze(ruleList, Words.Normal).Any());

            Assert.IsTrue(enumAnalyzer.Analyze(ruleList, null, Words.Magic | Words.Normal).Any());
            Assert.IsFalse(enumAnalyzer.Analyze(ruleList, null, Words.Shazam).Any());
            Assert.IsFalse(enumAnalyzer.Analyze(ruleList, null, Words.Normal).Any());
        }