Skip to content

Commit

Permalink
added basic tests for jobject based inputs (#454)
Browse files Browse the repository at this point in the history
* added basic tests for jobject based inputs

* fixed test case
  • Loading branch information
abbasc52 authored Feb 1, 2023
1 parent 7b4e45d commit caf41e3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
15 changes: 15 additions & 0 deletions test/RulesEngine.UnitTest/UtilsTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<ExpandoObject>(typedobj);
Assert.IsType<JObject>(typedobj);
Assert.NotNull(typedobj.Test);
}


[Fact]
public void CreateObject_dynamicObject()
{
Expand Down

0 comments on commit caf41e3

Please sign in to comment.