Exemplo n.º 1
0
        public void ConjunctExpression()
        {
            // Arrange
            string dataJson = "{ `temp` : 100, `pie` : { `filling` : `apple` } }".Replace('`', '"');
            string ruleJson =
                "{ `and` : [  {`<` : [ { `var` : `temp` }, 110 ]},  {`==` : [ { `var` : `pie.filling` }, `apple` ] }] }"
                .Replace('`', '"');
            var evaluator = new JsonLogicEvaluator(EvaluateOperators.Default);
            var rule      = JObject.Parse(ruleJson);
            var localData = JObject.Parse(dataJson);

            _output.WriteLine($"{MethodBase.GetCurrentMethod().Name}() Testing {rule} against {localData}");

            // Act
            var result = evaluator.Apply(rule, localData);

            // Assert
            Assert.True((bool)result);
        }
Exemplo n.º 2
0
        public void NestedFilterVariableAccess(string ruleJson, string dataJson, string expectedJson)
        {
            // Arrange
            dataJson     = dataJson.Replace('`', '"');
            ruleJson     = ruleJson.Replace('`', '"');
            expectedJson = expectedJson.Replace('`', '"');

            var evaluator      = new JsonLogicEvaluator(EvaluateOperators.Default);
            var rule           = JsonFrom(ruleJson);
            var localData      = GetDataObject(JsonFrom(dataJson));
            var expectedResult = GetDataObject(JsonFrom(expectedJson));

            _output.WriteLine($"{MethodBase.GetCurrentMethod().Name}() Testing {rule} against {localData}");

            // Act
            var result = evaluator.Apply(rule, localData);

            // Assert
            Assert.Equal(expectedResult, result);
        }
Exemplo n.º 3
0
        public void Apply(string argsJson, object expectedResult)
        {
            // Arrange
            var rules     = JsonFrom(argsJson);
            var jsonLogic = new JsonLogicEvaluator(EvaluateOperators.Default);

            _output.WriteLine($"{MethodBase.GetCurrentMethod().Name}() Testing {rules} against {Data}");

            // Act
            var result = jsonLogic.Apply(rules, Data);

            // Assert
            if (expectedResult is Array)
            {
                string[] expectedResultArr =
                    (expectedResult as Array).Cast <object>().Select(i => i.ToString()).ToArray();
                string[] resultArr;

                if (result is Array)
                {
                    resultArr = (result as Array).Cast <object>().Select(i => i.ToString()).ToArray();
                }
                else if (result is IEnumerable <object> )
                {
                    resultArr = (result as IEnumerable <object>).Select(i => i.ToString()).ToArray();
                }
                else
                {
                    throw new Exception("Cannot cast resultArr");
                }

                Assert.Equal(expectedResultArr, resultArr);
            }
            else
            {
                Assert.Equal(expectedResult, result);
            }
        }
Exemplo n.º 4
0
        public void ApplyThrowsException(string rulesJson, Type exceptionType)
        {
            // Arrange
            var    rules     = JsonFrom(rulesJson);
            var    jsonLogic = new JsonLogicEvaluator(EvaluateOperators.Default);
            object result    = null;

            _output.WriteLine($"{MethodBase.GetCurrentMethod().Name}() Testing {rules} against {Data}");

            // Act & Assert
            try
            {
                result = jsonLogic.Apply(rules, Data);
            }
            catch (Exception e)
            {
                Assert.Equal(exceptionType, e.GetType());
            }
            finally
            {
                Assert.Null(result);
            }
        }
Exemplo n.º 5
0
 public DecisioningRuleExecutor(TargetClientConfig clientConfig)
 {
     this.clientConfig = clientConfig;
     this.evaluator    = new JsonLogicEvaluator(EvaluateOperators.Default);
 }