Skip to content

Commit caf41e3

Browse files
authored
added basic tests for jobject based inputs (#454)
* added basic tests for jobject based inputs * fixed test case
1 parent 7b4e45d commit caf41e3

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Newtonsoft.Json.Linq;
5+
using RulesEngine.ExpressionBuilders;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Diagnostics.CodeAnalysis;
9+
using System.Linq;
10+
using System.Text;
11+
using System.Threading.Tasks;
12+
using Xunit;
13+
using System.Text.Json;
14+
15+
namespace RulesEngine.UnitTest.RuleExpressionParserTests
16+
{
17+
[ExcludeFromCodeCoverage]
18+
public class RuleExpressionParserTests
19+
{
20+
public RuleExpressionParserTests() {
21+
22+
23+
}
24+
25+
26+
[Fact]
27+
public void TestExpressionWithJObject()
28+
{
29+
var ruleParser = new RuleExpressionParser(new Models.ReSettings());
30+
31+
var inputStr = @"{
32+
""list"": [
33+
{ ""item1"": ""hello"",
34+
""item3"": 1
35+
},
36+
{
37+
""item2"": ""world""
38+
}
39+
]
40+
}";
41+
42+
43+
var input = JObject.Parse(inputStr);
44+
45+
46+
var value = ruleParser.Evaluate<object>("input.list[0].item3 == 1", new[] { new Models.RuleParameter("input", input) });
47+
48+
Assert.Equal(true,
49+
value);
50+
51+
52+
var value2 = ruleParser.Evaluate<object>("input.list[1].item2 == \"world\"", new[] { new Models.RuleParameter("input", input) });
53+
54+
Assert.Equal(true,
55+
value2);
56+
57+
58+
var value3= ruleParser.Evaluate<object>("string.Concat(input.list[0].item1,input.list[1].item2)", new[] { new Models.RuleParameter("input", input) });
59+
60+
Assert.Equal("helloworld", value3);
61+
}
62+
}
63+
}

test/RulesEngine.UnitTest/UtilsTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using Newtonsoft.Json.Linq;
45
using RulesEngine.HelperFunctions;
56
using System;
67
using System.Collections.Generic;
@@ -61,6 +62,20 @@ public void GetTypedObject_nonDynamicObject()
6162
Assert.NotNull(typedobj.GetType().GetProperty("Test"));
6263
}
6364

65+
66+
[Fact]
67+
public void GetJObject_nonDynamicObject()
68+
{
69+
dynamic obj = JObject.FromObject(new {
70+
Test = "hello"
71+
});
72+
dynamic typedobj = Utils.GetTypedObject(obj);
73+
Assert.IsNotType<ExpandoObject>(typedobj);
74+
Assert.IsType<JObject>(typedobj);
75+
Assert.NotNull(typedobj.Test);
76+
}
77+
78+
6479
[Fact]
6580
public void CreateObject_dynamicObject()
6681
{

0 commit comments

Comments
 (0)