added basic tests for jobject based inputs (#454)

* added basic tests for jobject based inputs

* fixed test case
pull/458/head
Abbas Cyclewala 2023-02-01 13:23:18 +05:30 committed by GitHub
parent 7b4e45d2ad
commit caf41e3cd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 0 deletions

View File

@ -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<object>("input.list[0].item3 == 1", new[] { new Models.RuleParameter("input", input) });
Assert.Equal(true,
value);
var value2 = ruleParser.Evaluate<object>("input.list[1].item2 == \"world\"", new[] { new Models.RuleParameter("input", input) });
Assert.Equal(true,
value2);
var value3= ruleParser.Evaluate<object>("string.Concat(input.list[0].item1,input.list[1].item2)", new[] { new Models.RuleParameter("input", input) });
Assert.Equal("helloworld", value3);
}
}
}

View File

@ -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 @@ namespace RulesEngine.UnitTest
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<ExpandoObject>(typedobj);
Assert.IsType<JObject>(typedobj);
Assert.NotNull(typedobj.Test);
}
[Fact]
public void CreateObject_dynamicObject()
{