From caf41e3cd3b6312e8e0b6794744def0ebe92abfe Mon Sep 17 00:00:00 2001 From: Abbas Cyclewala Date: Wed, 1 Feb 2023 13:23:18 +0530 Subject: [PATCH] added basic tests for jobject based inputs (#454) * added basic tests for jobject based inputs * fixed test case --- .../RuleExpressionParserTests.cs | 63 +++++++++++++++++++ test/RulesEngine.UnitTest/UtilsTests.cs | 15 +++++ 2 files changed, 78 insertions(+) create mode 100644 test/RulesEngine.UnitTest/RuleExpressionParserTests/RuleExpressionParserTests.cs diff --git a/test/RulesEngine.UnitTest/RuleExpressionParserTests/RuleExpressionParserTests.cs b/test/RulesEngine.UnitTest/RuleExpressionParserTests/RuleExpressionParserTests.cs new file mode 100644 index 00000000..bda3ad2b --- /dev/null +++ b/test/RulesEngine.UnitTest/RuleExpressionParserTests/RuleExpressionParserTests.cs @@ -0,0 +1,63 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using Newtonsoft.Json.Linq; +using RulesEngine.ExpressionBuilders; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; +using System.Text.Json; + +namespace RulesEngine.UnitTest.RuleExpressionParserTests +{ + [ExcludeFromCodeCoverage] + public class RuleExpressionParserTests + { + public RuleExpressionParserTests() { + + + } + + + [Fact] + public void TestExpressionWithJObject() + { + var ruleParser = new RuleExpressionParser(new Models.ReSettings()); + + var inputStr = @"{ + ""list"": [ + { ""item1"": ""hello"", + ""item3"": 1 + }, + { + ""item2"": ""world"" + } + ] + }"; + + + var input = JObject.Parse(inputStr); + + + var value = ruleParser.Evaluate("input.list[0].item3 == 1", new[] { new Models.RuleParameter("input", input) }); + + Assert.Equal(true, + value); + + + var value2 = ruleParser.Evaluate("input.list[1].item2 == \"world\"", new[] { new Models.RuleParameter("input", input) }); + + Assert.Equal(true, + value2); + + + var value3= ruleParser.Evaluate("string.Concat(input.list[0].item1,input.list[1].item2)", new[] { new Models.RuleParameter("input", input) }); + + Assert.Equal("helloworld", value3); + } + } +} diff --git a/test/RulesEngine.UnitTest/UtilsTests.cs b/test/RulesEngine.UnitTest/UtilsTests.cs index 5432f993..ef88ec4d 100644 --- a/test/RulesEngine.UnitTest/UtilsTests.cs +++ b/test/RulesEngine.UnitTest/UtilsTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +using Newtonsoft.Json.Linq; using RulesEngine.HelperFunctions; using System; using System.Collections.Generic; @@ -61,6 +62,20 @@ public void GetTypedObject_nonDynamicObject() Assert.NotNull(typedobj.GetType().GetProperty("Test")); } + + [Fact] + public void GetJObject_nonDynamicObject() + { + dynamic obj = JObject.FromObject(new { + Test = "hello" + }); + dynamic typedobj = Utils.GetTypedObject(obj); + Assert.IsNotType(typedobj); + Assert.IsType(typedobj); + Assert.NotNull(typedobj.Test); + } + + [Fact] public void CreateObject_dynamicObject() {