// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using Newtonsoft.Json.Linq; using RulesEngine.HelperFunctions; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Dynamic; using Xunit; namespace RulesEngine.UnitTest { [ExcludeFromCodeCoverage] public class TestClass { public string Test { get; set; } public List TestList { get; set; } } [Trait("Category", "Unit")] [ExcludeFromCodeCoverage] public class UtilsTests { [Fact] public void GetTypedObject_dynamicObject() { dynamic obj = new ExpandoObject(); obj.Test = "hello"; obj.TestList = new List { 1, 2, 3 }; object typedobj = Utils.GetTypedObject(obj); Assert.IsNotType(typedobj); Assert.NotNull(typedobj.GetType().GetProperty("Test")); } [Fact] public void GetTypedObject_dynamicObject_multipleObjects() { dynamic obj = new ExpandoObject(); obj.Test = "hello"; obj.TestList = new List { 1, 2, 3 }; dynamic obj2 = new ExpandoObject(); obj2.Test = "world"; obj2.TestList = new List { 1, 2, 3 }; object typedobj = Utils.GetTypedObject(obj); object typedobj2 = Utils.GetTypedObject(obj2); Assert.IsNotType(typedobj); Assert.NotNull(typedobj.GetType().GetProperty("Test")); Assert.Equal(typedobj.GetType(), typedobj2.GetType()); } [Fact] public void GetTypedObject_nonDynamicObject() { var obj = new { Test = "hello" }; var typedobj = Utils.GetTypedObject(obj); Assert.IsNotType(typedobj); 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() { dynamic obj = new ExpandoObject(); obj.Test = "test"; obj.TestList = new List { 1, 2, 3 }; object newObj = Utils.CreateObject(typeof(TestClass), obj); Assert.IsNotType(newObj); Assert.NotNull(newObj.GetType().GetProperty("Test")); } [Fact] public void CreateAbstractType_dynamicObject() { dynamic obj = new ExpandoObject(); obj.Test = "test"; obj.TestList = new List { 1, 2, 3 }; obj.testEmptyList = new List(); Type type = Utils.CreateAbstractClassType(obj); Assert.NotEqual(typeof(ExpandoObject), type); Assert.NotNull(type.GetProperty("Test")); } } }