From c3aeba67c894edfa42dcb97335befa57362526ba Mon Sep 17 00:00:00 2001 From: Tom Atwood Date: Tue, 6 Aug 2024 20:10:14 -0400 Subject: [PATCH] feature/#42: Fixed build messages. --- Tests/Invoke.cs | 2445 ++++++++++++++++++++++++------------------- Tests/TuplerTest.cs | 88 +- 2 files changed, 1402 insertions(+), 1131 deletions(-) diff --git a/Tests/Invoke.cs b/Tests/Invoke.cs index 82b31ec..7a0f232 100644 --- a/Tests/Invoke.cs +++ b/Tests/Invoke.cs @@ -1,1090 +1,1355 @@ -using Dynamitey.SupportLibrary; -using Microsoft.CSharp.RuntimeBinder; -using Moq; -using System.Drawing; -using System.Dynamic; -using System.Globalization; -using System.Linq.Expressions; -using System.Xml.Linq; - -namespace Dynamitey.Tests -{ - public class Invoke - { - [OneTimeTearDown] - public void DestroyCaches() - { - Dynamic.ClearCaches(); - } - - [Test] - public void TestDynamicSet() - { - dynamic tExpando = new ExpandoObject(); - var tSetValue = "1"; - Dynamic.InvokeSet(tExpando, "Test", tSetValue); - Assert.That(tExpando.Test, Is.EqualTo(tSetValue)); - } - - [Test] - public void TestPocoSet() - { - var tPoco = new PropPoco(); - var tSetValue = "1"; - Dynamic.InvokeSet(tPoco, "Prop1", tSetValue); - Assert.That(tPoco.Prop1, Is.EqualTo(tSetValue)); - } - - [Test] - public void TestStructSet() - { - object tPoco = new PropStruct(); - var tSetValue = "1"; - Dynamic.InvokeSet(tPoco, "Prop1", tSetValue); - Assert.That(((PropStruct)tPoco).Prop1, Is.EqualTo(tSetValue)); - } - - [Test] - public void TestCacheableDyanmicSetAndPocoSetAndSetNull() - { - dynamic tExpando = new ExpandoObject(); - var tSetValueD = "4"; - - var tCachedInvoke = new CacheableInvocation(InvocationKind.Set, "Prop1"); - tCachedInvoke.Invoke((object)tExpando, tSetValueD); - - Assert.That(tExpando.Prop1, Is.EqualTo(tSetValueD)); - - var tPoco = new PropPoco(); - var tSetValue = "1"; - - tCachedInvoke.Invoke(tPoco, tSetValue); - Assert.That(tPoco.Prop1, Is.EqualTo(tSetValue)); - - string tSetValue2 = null; - tCachedInvoke.Invoke(tPoco, tSetValue2); - Assert.That(tPoco.Prop1, Is.EqualTo(tSetValue2)); - } - - [Test] - public void TestConvert() - { - var tEl = new XElement("Test", "45"); - var tCast = Dynamic.InvokeConvert(tEl, typeof(int), @explicit: true); - Assert.That(tCast.GetType(), Is.EqualTo(typeof(int))); - Assert.That(tCast, Is.EqualTo(45)); - } - - [Test] - public void TestConvertCacheable() - { - var tEl = new XElement("Test", "45"); - var tCacheInvoke = new CacheableInvocation(InvocationKind.Convert, convertType: typeof(int), convertExplicit: true); - var tCast = tCacheInvoke.Invoke(tEl); - Assert.That(tCast.GetType(), Is.EqualTo(typeof(int))); - Assert.That(tCast, Is.EqualTo(45)); - } - - [Test] - public void TestConstruct() - { - var tCast = Dynamic.InvokeConstructor(typeof(List), new object[] - { - new string[] { "one", "two", "three" } - }); - Assert.That(tCast[1], Is.EqualTo("two")); - } - - [Test] - public void TestCacheableConstruct() - { - var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 1); - dynamic tCast = tCachedInvoke.Invoke(typeof(List), new object[] - { - new string[] { "one", "two", "three" } - }); - Assert.That(tCast[1], Is.EqualTo("two")); - } - - [Test] - public void TestConstructOptional() - { - var argname = InvokeArg.Create; - PocoOptConstructor tCast = Dynamic.InvokeConstructor(typeof(PocoOptConstructor), argname("three", "3")); - Assert.That(tCast.One, Is.EqualTo("-1")); - Assert.That(tCast.Two, Is.EqualTo("-2")); - Assert.That(tCast.Three, Is.EqualTo("3")); - } - - [Test] - public void TestCacheableConstructOptional() - { - var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 1, argNames: new[] { "three" }); - var tCast = (PocoOptConstructor)tCachedInvoke.Invoke(typeof(PocoOptConstructor), "3"); - Assert.That(tCast.One, Is.EqualTo("-1")); - Assert.That(tCast.Two, Is.EqualTo("-2")); - Assert.That(tCast.Three, Is.EqualTo("3")); - } - - [Test] - public void TestOptionalArgumentActivationNoneAndCacheable() - { - Assert.Throws(() => Activator.CreateInstance()); - - var tList = Dynamic.InvokeConstructor(typeof(DynamicObjects.List)); - Assert.That(tList.GetType(), Is.EqualTo(typeof(DynamicObjects.List))); - - var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor); - var tList1 = tCachedInvoke.Invoke(typeof(DynamicObjects.List)); - Assert.That(tList1.GetType(), Is.EqualTo(typeof(DynamicObjects.List))); - } - - [Test] - public void TestConstructValueType() - { - var tCast = Dynamic.InvokeConstructor(typeof(DateTime), 2009, 1, 20); - Assert.That(tCast.Day, Is.EqualTo(20)); - } - - [Test] - public void TestCacheableConstructValueType() - { - var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 3); - dynamic tCast = tCachedInvoke.Invoke(typeof(DateTime), 2009, 1, 20); - Assert.That(tCast.Day, Is.EqualTo(20)); - } - - [Test] - public void TestConstructValueTypeJustDynamic() - { - dynamic day = 20; - dynamic year = 2009; - dynamic month = 1; - var tCast = new DateTime(year, month, day); - DateTime tDate = tCast; - Assert.That(tDate.Day, Is.EqualTo(20)); - } - - [Test] - public void TestConstructprimativetype() - { - var tCast = Dynamic.InvokeConstructor(typeof(Int32)); - Assert.That(tCast, Is.EqualTo(default(Int32))); - } - - [Test] - public void TestConstructDateTimeNoParams() - { - var tCast = Dynamic.InvokeConstructor(typeof(DateTime)); - Assert.That(tCast, Is.EqualTo(default(DateTime))); - } - - [Test] - public void TestConstructOBjectNoParams() - { - var tCast = Dynamic.InvokeConstructor(typeof(object)); - Assert.That(tCast.GetType(), Is.EqualTo(typeof(object))); - } - - [Test] - public void TestConstructNullableprimativetype() - { - var tCast = Dynamic.InvokeConstructor(typeof(Nullable)); - Assert.That(tCast, Is.EqualTo(null)); - } - - [Test] - public void TestConstructGuid() - { - var tCast = Dynamic.InvokeConstructor(typeof(Guid)); - Assert.That(tCast, Is.EqualTo(default(Guid))); - } - - [Test] - public void TestCacheablePrimativeDateTimeObjectNullableAndGuidNoParams() - { - var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor); - - dynamic tCast = tCachedInvoke.Invoke(typeof(Int32)); - Assert.That(tCast, Is.EqualTo(default(Int32))); - - tCast = tCachedInvoke.Invoke(typeof(DateTime)); - Assert.That(tCast, Is.EqualTo(default(DateTime))); - - tCast = tCachedInvoke.Invoke(typeof(List)); - Assert.That(tCast.GetType(), Is.EqualTo(typeof(List))); - - tCast = tCachedInvoke.Invoke(typeof(object)); - Assert.That(tCast.GetType(), Is.EqualTo(typeof(object))); - - tCast = tCachedInvoke.Invoke(typeof(Nullable)); - Assert.That(tCast, Is.EqualTo(null)); - - tCast = tCachedInvoke.Invoke(typeof(Guid)); - Assert.That(tCast, Is.EqualTo(default(Guid))); - } - - [Test] - public void TestStaticCall() - { - var @static = InvokeContext.CreateStatic; - var generic = InvokeMemberName.Create; - var tOut = Dynamic.InvokeMember(@static(typeof(StaticType)), generic("Create", new[] { typeof(bool) }), 1); - Assert.That(tOut, Is.EqualTo(false)); - } - - [Test] - public void TestCacheableStaticCall() - { - var @static = InvokeContext.CreateStatic; - var generic = InvokeMemberName.Create; - var tCached = new CacheableInvocation(InvocationKind.InvokeMember, generic("Create", new[] { typeof(bool) }), argCount: 1, context: @static(typeof(StaticType))); - var tOut = tCached.Invoke(typeof(StaticType), 1); - Assert.That(tOut, Is.EqualTo(false)); - } - - private class TestClass - { - public static int StaticProperty { get; set; } - } - - [Test] - public void TestStaticPropertySetFollowedByGetTest() - { - var staticContext = InvokeContext.CreateStatic; - Dynamic.InvokeSet(staticContext(typeof(TestClass)), "StaticProperty", 42); - var tOut = Dynamic.InvokeGet(staticContext(typeof(TestClass)), "StaticProperty"); - Assert.That(tOut, Is.EqualTo(42)); - } - - [Test] - public void TestImplicitConvert() - { - var tEl = 45; - var tCast = Dynamic.InvokeConvert(tEl, typeof(long)); - Assert.That(tCast.GetType(), Is.EqualTo(typeof(long))); - } - - [Test] - public void TestCoerceConverterColor() - { - var colorString = "PaleVioletRed"; - var color = Dynamic.CoerceConvert(colorString, typeof(Color)); - Assert.That(color, Is.TypeOf()); - Assert.That(color, Is.EqualTo(Color.PaleVioletRed)); - } - - [Test] - public void TestCoerceConverterDBNULL() - { - var tEl = DBNull.Value; - var tCast = Dynamic.CoerceConvert(tEl, typeof(long)); - Assert.That(tCast.GetType(), Is.EqualTo(typeof(long))); - - var tCast2 = Dynamic.CoerceConvert(tEl, typeof(string)); - Assert.That(tCast2, Is.EqualTo(null)); - Assert.AreNotEqual(null, tEl); - } - - [Test] - public void TestCacheableImplicitConvert() - { - var tEl = 45; - var tCachedInvoke = CacheableInvocation.CreateConvert(typeof(long)); - var tCast = tCachedInvoke.Invoke(tEl); - Assert.That(tCast.GetType(), Is.EqualTo(typeof(long))); - } - - [Test] - public void TestCacheableGet() - { - var tCached = new CacheableInvocation(InvocationKind.Get, "Prop1"); - var tSetValue = "1"; - var tAnon = new PropPoco { Prop1 = tSetValue }; - var tOut = tCached.Invoke(tAnon); - Assert.That(tOut, Is.EqualTo(tSetValue)); - - var tSetValue2 = "2"; - tAnon = new PropPoco { Prop1 = tSetValue2 }; - var tOut2 = tCached.Invoke(tAnon); - Assert.That(tOut2, Is.EqualTo(tSetValue2)); - } - - [Test] - public void TestGetIndexer() - { - dynamic tSetValue = "1"; - var tAnon = new[] { tSetValue, "2" }; - string tOut = Dynamic.InvokeGetIndex(tAnon, 0); - Assert.That(tOut, Is.EqualTo(tSetValue)); - } - - [Test] - public void TestGetIndexerValue() - { - var tAnon = new int[] { 1, 2 }; - int tOut = Dynamic.InvokeGetIndex(tAnon, 1); - Assert.That(tOut, Is.EqualTo(tAnon[1])); - } - - [Test] - public void TestGetLengthArray() - { - var tAnon = new[] { "1", "2" }; - int tOut = Dynamic.InvokeGet(tAnon, "Length"); - Assert.That(tOut, Is.EqualTo(2)); - } - - [Test] - public void TestGetIndexerArray() - { - dynamic tSetValue = "1"; - var tAnon = new List { tSetValue, "2" }; - string tOut = Dynamic.InvokeGetIndex(tAnon, 0); - Assert.That(tOut, Is.EqualTo(tSetValue)); - } - - [Test] - public void TestCacheableIndexer() - { - var tStrings = new[] { "1", "2" }; - var tCachedInvoke = new CacheableInvocation(InvocationKind.GetIndex, argCount: 1); - var tOut = (string)tCachedInvoke.Invoke(tStrings, 0); - Assert.That(tOut, Is.EqualTo(tStrings[0])); - - var tOut2 = (string)tCachedInvoke.Invoke(tStrings, 1); - Assert.That(tOut2, Is.EqualTo(tStrings[1])); - - var tInts = new int[] { 3, 4 }; - var tOut3 = (int)tCachedInvoke.Invoke(tInts, 0); - Assert.That(tOut3, Is.EqualTo(tInts[0])); - - var tOut4 = (int)tCachedInvoke.Invoke(tInts, 1); - Assert.That(tOut4, Is.EqualTo(tInts[1])); - - var tList = new List { "5", "6" }; - var tOut5 = (string)tCachedInvoke.Invoke(tList, 0); - Assert.That(tOut5, Is.EqualTo(tList[0])); - - var tOut6 = (string)tCachedInvoke.Invoke(tList, 0); - Assert.That(tOut6, Is.EqualTo(tList[0])); - } - - [Test] - public void TestSetIndexer() - { - dynamic tSetValue = "3"; - var tAnon = new List { "1", "2" }; - Dynamic.InvokeSetIndex(tAnon, 0, tSetValue); - Assert.That(tAnon[0], Is.EqualTo(tSetValue)); - } - - [Test] - public void TestCacheableSetIndexer() - { - dynamic tSetValue = "3"; - var tList = new List { "1", "2" }; - var tCachedInvoke = new CacheableInvocation(InvocationKind.SetIndex, argCount: 2); - tCachedInvoke.Invoke(tList, 0, tSetValue); - Assert.That(tList[0], Is.EqualTo(tSetValue)); - } - - [Test] - public void TestMethodDynamicPassAndGetValue() - { - dynamic tExpando = new ExpandoObject(); - tExpando.Func = new Func(it => it.ToString()); - var tValue = 1; - var tOut = Dynamic.InvokeMember(tExpando, "Func", tValue); - Assert.That(tOut, Is.EqualTo(tValue.ToString())); - } - - [Test] - public void TestCacheableMethodDynamicPassAndGetValue() - { - dynamic tExpando = new ExpandoObject(); - tExpando.Func = new Func(it => it.ToString()); - var tValue = 1; - var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Func", 1); - var tOut = tCachedInvoke.Invoke((object)tExpando, tValue); - Assert.That(tOut, Is.EqualTo(tValue.ToString())); - } - - [Test] - public void TestMethodStaticOverloadingPassAndGetValue() - { - var tPoco = new OverloadingMethPoco(); - var tValue = 1; - var tOut = Dynamic.InvokeMember(tPoco, "Func", tValue); - Assert.That(tOut, Is.EqualTo("int")); - Assert.That((object)tOut, Is.EqualTo("int")); //should still be int because this uses runtime type - - var tOut2 = Dynamic.InvokeMember(tPoco, "Func", 1m); - Assert.That(tOut2, Is.EqualTo("object")); - - var tOut3 = Dynamic.InvokeMember(tPoco, "Func", new { Anon = 1 }); - Assert.That(tOut3, Is.EqualTo("object")); - } - - [Test] - public void TestCachedMethodStaticOverloadingPassAndGetValue() - { - var tPoco = new OverloadingMethPoco(); - var tValue = 1; - var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 1); - var tOut = tCachedInvoke.Invoke(tPoco, tValue); - Assert.That(tOut, Is.EqualTo("int")); - Assert.That((object)tOut, Is.EqualTo("int")); //should still be int because this uses runtime type - - var tOut2 = tCachedInvoke.Invoke(tPoco, 1m); - Assert.That(tOut2, Is.EqualTo("object")); - - var tOut3 = tCachedInvoke.Invoke(tPoco, new { Anon = 1 }); - Assert.That(tOut3, Is.EqualTo("object")); - } - - [Test] - public void TestMethodPocoOverloadingPassAndGetValueArg() - { - var tPoco = new OverloadingMethPoco(); - var tValue = 1; - var tOut = Dynamic.InvokeMember(tPoco, "Func", new InvokeArg("arg", tValue)); - Assert.That(tOut, Is.EqualTo("int")); - Assert.That((object)tOut, Is.EqualTo("int")); //should still be int because this uses runtime type - - var tOut2 = Dynamic.InvokeMember(tPoco, "Func", 1m); - Assert.That(tOut2, Is.EqualTo("object")); - - var tOut3 = Dynamic.InvokeMember(tPoco, "Func", new { Anon = 1 }); - Assert.That(tOut3, Is.EqualTo("object")); - } - - [Test] - public void TestMethodPocoOverloadingPassAndGetValueArgOptional() - { - var tPoco = new OverloadingMethPoco(); - var tValue = 1; - var arg = InvokeArg.Create; - var tOut = Dynamic.InvokeMember(tPoco, "Func", arg("two", tValue)); - Assert.That(tOut, Is.EqualTo("object named")); - Assert.That((object)tOut, Is.EqualTo("object named")); - } - - [Test] - public void TestCacheableMethodPocoOverloadingPassAndGetValueArgOptional() - { - var tPoco = new OverloadingMethPoco(); - var tValue = 1; - var tCachedIvnocation = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 1, argNames: new[] { "two" }); - var tOut = tCachedIvnocation.Invoke(tPoco, tValue); - Assert.That(tOut, Is.EqualTo("object named")); - Assert.That((object)tOut, Is.EqualTo("object named")); - } - - [Test] - public void TestCacheableMethodPocoOverloadingPassAndGetValueArgPostiionalOptional() - { - var tPoco = new OverloadingMethPoco(); - var tValue1 = 1; - var tValue2 = 2; - var tCachedIvnocation = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 2, argNames: new[] { "two" }); - var tOut = tCachedIvnocation.Invoke(tPoco, tValue1, tValue2); - Assert.That(tOut, Is.EqualTo("object named")); - Assert.That((object)tOut, Is.EqualTo("object named")); - } - - [Test] - public void TestMethodPocoOverloadingPass2AndGetValueArgOptional() - { - var tPoco = new OverloadingMethPoco(); - var tValue = 1; - var arg = InvokeArg.Create; - var tOut = Dynamic.InvokeMember(tPoco, "Func", arg("two", tValue), arg("one", tValue)); - Assert.That(tOut, Is.EqualTo("object named")); - Assert.That((object)tOut, Is.EqualTo("object named")); - } - - [Test] - public void TestMethodPocoOverloadingPassAndGetValueNull() - { - var tPoco = new OverloadingMethPoco(); - var tValue = 1; - var tOut = Dynamic.InvokeMember(tPoco, "Func", tValue); - Assert.That(tOut, Is.EqualTo("int")); - Assert.That((object)tOut, Is.EqualTo("int")); //should still be int because this uses runtime type - - var tOut2 = Dynamic.InvokeMember(tPoco, "Func", 1m); - Assert.That(tOut2, Is.EqualTo("object")); - - var tOut3 = Dynamic.InvokeMember(tPoco, "Func", null); - Assert.That(tOut3, Is.EqualTo("object")); - - var tOut4 = Dynamic.InvokeMember(tPoco, "Func", null, null, "test", null, null, null); - Assert.That(tOut4, Is.EqualTo("object 6")); - - var tOut5 = Dynamic.InvokeMember(tPoco, "Func", null, null, null, null, null, null); - Assert.That(tOut5, Is.EqualTo("object 6")); - } - - /// - /// To dynamically invoke a method with out or ref parameters you need to know the signature - /// - [Test] - public void TestOutMethod() - { - string tResult = String.Empty; - var tPoco = new MethOutPoco(); - var tName = "Func"; - var tContext = GetType(); - var tBinder = Binder.InvokeMember(CSharpBinderFlags.None, tName, null, tContext, - new[] - { - CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), - CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsOut | CSharpArgumentInfoFlags.UseCompileTimeType, null) - }); - - var tSite = Dynamic.CreateCallSite(tBinder, tName, tContext); - tSite.Target.Invoke(tSite, tPoco, out tResult); - Assert.That(tResult, Is.EqualTo("success")); - } - - [Test] - public void TestMethodDynamicPassVoid() - { - var tTest = "Wrong"; - var tValue = "Correct"; - dynamic tExpando = new ExpandoObject(); - tExpando.Action = new Action(it => tTest = it); - Dynamic.InvokeMemberAction(tExpando, "Action", tValue); - Assert.That(tTest, Is.EqualTo(tValue)); - } - - [Test] - public void TestCacheableMethodDynamicPassVoid() - { - var tTest = "Wrong"; - var tValue = "Correct"; - dynamic tExpando = new ExpandoObject(); - tExpando.Action = new Action(it => tTest = it); - var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMemberAction, "Action", argCount: 1); - tCachedInvoke.Invoke((object)tExpando, tValue); - Assert.That(tTest, Is.EqualTo(tValue)); - } - - [Test] - public void TestCacheableMethodDynamicUnknowns() - { - var tTest = "Wrong"; - var tValue = "Correct"; - dynamic tExpando = new ExpandoObject(); - tExpando.Action = new Action(it => tTest = it); - tExpando.Func = new Func(it => it); - var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMemberUnknown, "Action", argCount: 1); - tCachedInvoke.Invoke((object)tExpando, tValue); - Assert.That(tTest, Is.EqualTo(tValue)); - - var tCachedInvoke2 = new CacheableInvocation(InvocationKind.InvokeMemberUnknown, "Func", argCount: 1); - var Test2 = tCachedInvoke2.Invoke((object)tExpando, tValue); - Assert.That(Test2, Is.EqualTo(tValue)); - } - - [Test] - public void TestMethodPocoGetValue() - { - var tValue = 1; - var tOut = Dynamic.InvokeMember(tValue, "ToString"); - Assert.That(tOut, Is.EqualTo(tValue.ToString())); - } - - [Test] - public void TestMethodPocoPassAndGetValue() - { - HelpTestPocoPassAndGetValue("Test", "Te"); - HelpTestPocoPassAndGetValue("Test", "st"); - } - - private void HelpTestPocoPassAndGetValue(string tValue, string tParam) - { - var tExpected = tValue.StartsWith(tParam); - var tOut = Dynamic.InvokeMember(tValue, "StartsWith", tParam); - Assert.That(tOut, Is.EqualTo(tExpected)); - } - - [Test] - public void TestGetDynamic() - { - var tSetValue = "1"; - dynamic tExpando = new ExpandoObject(); - tExpando.Test = tSetValue; - var tOut = Dynamic.InvokeGet(tExpando, "Test"); - Assert.That(tOut, Is.EqualTo(tSetValue)); - } - - [Test] - public void TestGetDynamicChained() - { - var tSetValue = "1"; - dynamic tExpando = new ExpandoObject(); - tExpando.Test = new ExpandoObject(); - tExpando.Test.Test2 = new ExpandoObject(); - tExpando.Test.Test2.Test3 = tSetValue; - var tOut = Dynamic.InvokeGetChain(tExpando, "Test.Test2.Test3"); - Assert.That(tOut, Is.EqualTo(tSetValue)); - } - - [Test] - public void TestGetDynamicChainedWithIndexes() - { - var tSetValue = "1"; - dynamic tExpando = Build.NewObject( - Test: Build.NewObject( - Test2: Build.NewList( - Build.NewObject(Test3: Build.NewObject(Test4: tSetValue)) - ) - ) - ); - var tOut = Dynamic.InvokeGetChain(tExpando, "Test.Test2[0].Test3['Test4']"); - Assert.That(tOut, Is.EqualTo(tSetValue)); - } - - [Test] - public void TestSetDynamicChained() - { - var tSetValue = "1"; - dynamic tExpando = new ExpandoObject(); - tExpando.Test = new ExpandoObject(); - tExpando.Test.Test2 = new ExpandoObject(); - Dynamic.InvokeSetChain(tExpando, "Test.Test2.Test3", tSetValue); - Assert.That(tExpando.Test.Test2.Test3, Is.EqualTo(tSetValue)); - } - - [Test] - public void TestSetDynamicChainedWithInexes() - { - var tSetValue = "1"; - dynamic tExpando = Build.NewObject( - Test: Build.NewObject( - Test2: Build.NewList( - Build.NewObject(Test3: Build.NewObject()) - ) - ) - ); - var tOut = Dynamic.InvokeSetChain(tExpando, "Test.Test2[0].Test3['Test4']", tSetValue); - Assert.That(tExpando.Test.Test2[0].Test3["Test4"], Is.EqualTo(tSetValue)); - Assert.That(tOut, Is.EqualTo(tSetValue)); - } - - [Test] - public void TestSetDynamicAllDict() - { - var tSetValue = "1"; - dynamic tExpando = new ExpandoObject(); - tExpando.Test = new ExpandoObject(); - tExpando.Test.Test2 = new ExpandoObject(); - Dynamic.InvokeSetAll(tExpando, new Dictionary { { "Test.Test2.Test3", tSetValue }, { "One", 1 }, { "Two", 2 } }); - Dynamic.InvokeSetChain(tExpando, "Test.Test2.Test3", tSetValue); - Assert.That(tExpando.Test.Test2.Test3, Is.EqualTo(tSetValue)); - Assert.That(tExpando.One, Is.EqualTo(1)); - Assert.That(tExpando.Two, Is.EqualTo(2)); - } - - [Test] - public void TestSetDynamicAllAnonymous() - { - dynamic tExpando = new ExpandoObject(); - Dynamic.InvokeSetAll(tExpando, new { One = 1, Two = 2, Three = 3 }); - Assert.That(tExpando.One, Is.EqualTo(1)); - Assert.That(tExpando.Two, Is.EqualTo(2)); - Assert.That(tExpando.Three, Is.EqualTo(3)); - } - - [Test] - public void TestSetDynamicAllNamed() - { - dynamic tExpando = new ExpandoObject(); - Dynamic.InvokeSetAll(tExpando, One: 1, Two: 2, Three: 3); - Assert.That(tExpando.One, Is.EqualTo(1)); - Assert.That(tExpando.Two, Is.EqualTo(2)); - Assert.That(tExpando.Three, Is.EqualTo(3)); - } - - [Test] - public void TestSetDynamicChainedOne() - { - var tSetValue = "1"; - dynamic tExpando = new ExpandoObject(); - Dynamic.InvokeSetChain(tExpando, "Test", tSetValue); - Assert.That(tExpando.Test, Is.EqualTo(tSetValue)); - } - - [Test] - public void TestGetDynamicChainedOne() - { - var tSetValue = "1"; - dynamic tExpando = new ExpandoObject(); - tExpando.Test = tSetValue; - var tOut = Dynamic.InvokeGetChain(tExpando, "Test"); - Assert.That(tOut, Is.EqualTo(tSetValue)); - } - - [Test] - public void TestCacheableGetDynamic() - { - var tSetValue = "1"; - dynamic tExpando = new ExpandoObject(); - tExpando.Test = tSetValue; - var tCached = new CacheableInvocation(InvocationKind.Get, "Test"); - var tOut = tCached.Invoke((object)tExpando); - Assert.That(tOut, Is.EqualTo(tSetValue)); - } - - [Test] - public void TestStaticGet() - { - var @static = InvokeContext.CreateStatic; - var tDate = Dynamic.InvokeGet(@static(typeof(DateTime)), "Today"); - Assert.That(tDate, Is.EqualTo(DateTime.Today)); - } - - [Test] - public void TestCacheableStaticGet() - { - var @static = InvokeContext.CreateStatic; - var tCached = new CacheableInvocation(InvocationKind.Get, "Today", context: @static(typeof(DateTime))); - var tDate = tCached.Invoke(typeof(DateTime)); - Assert.That(tDate, Is.EqualTo(DateTime.Today)); - } - - [Test] - public void TestStaticGet2() - { - var @static = InvokeContext.CreateStatic; - var tVal = Dynamic.InvokeGet(@static(typeof(StaticType)), "Test"); - Assert.That(tVal, Is.EqualTo(true)); - } - - [Test] - public void TestStaticGet3() - { - var tVal = Dynamic.InvokeGet((StaticContext)typeof(StaticType), "Test"); - Assert.That(tVal, Is.EqualTo(true)); - } - - [Test] - public void TestStaticSet() - { - var @static = InvokeContext.CreateStatic; - int tValue = 12; - Dynamic.InvokeSet(@static(typeof(StaticType)), "TestSet", tValue); - Assert.That(StaticType.TestSet, Is.EqualTo(tValue)); - } - - [Test] - public void TestCacheableStaticSet() - { - int tValue = 12; - var @static = InvokeContext.CreateStatic; - var tCachedInvoke = new CacheableInvocation(InvocationKind.Set, "TestSet", context: @static(typeof(StaticType))); - tCachedInvoke.Invoke(typeof(StaticType), tValue); - Assert.That(StaticType.TestSet, Is.EqualTo(tValue)); - } - - [Test] - public void TestStaticDateTimeMethod() - { - var @static = InvokeContext.CreateStatic; - object tDateDyn = "01/20/2009"; - var tDate = Dynamic.InvokeMember(@static(typeof(DateTime)), "Parse", tDateDyn, CultureInfo.GetCultureInfo("en-US")); - Assert.That(tDate, Is.EqualTo(new DateTime(2009, 1, 20))); - } - - [Test] - public void TestCacheableStaticDateTimeMethod() - { - var @static = InvokeContext.CreateStatic; - object tDateDyn = "01/20/2009"; - var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Parse", 2, context: @static(typeof(DateTime))); - var tDate = tCachedInvoke.Invoke(typeof(DateTime), tDateDyn, CultureInfo.GetCultureInfo("en-US")); - Assert.That(tDate, Is.EqualTo(new DateTime(2009, 1, 20))); - } - - [Test] - public void TestIsEvent() - { - dynamic tPoco = new PocoEvent(); - var tResult = Dynamic.InvokeIsEvent(tPoco, "Event"); - Assert.That(tResult, Is.EqualTo(true)); - } - - [Test] - public void TestCacheableIsEventAndIsNotEvent() - { - object tPoco = new PocoEvent(); - var tCachedInvoke = new CacheableInvocation(InvocationKind.IsEvent, "Event"); - var tResult = tCachedInvoke.Invoke(tPoco); - Assert.That(tResult, Is.EqualTo(true)); - - dynamic tDynamic = new DynamicObjects.Dictionary(); - tDynamic.Event = null; - var tResult2 = tCachedInvoke.Invoke((object)tDynamic); - Assert.That(tResult2, Is.EqualTo(false)); - } - - [Test] - public void TestIsNotEvent() - { - dynamic tDynamic = new DynamicObjects.Dictionary(); - tDynamic.Event = null; - var tResult = Dynamic.InvokeIsEvent(tDynamic, "Event"); - Assert.That(tResult, Is.EqualTo(false)); - - bool tTest = false; - bool tTest2 = false; - tDynamic.Event += new EventHandler((@object, args) => { tTest = true; }); - tDynamic.Event += new EventHandler((@object, args) => { tTest2 = true; }); - Assert.That(tTest, Is.EqualTo(false)); - Assert.That(tTest2, Is.EqualTo(false)); - - tDynamic.Event(null, null); - Assert.That(tTest, Is.EqualTo(true)); - Assert.That(tTest2, Is.EqualTo(true)); - } - - [Test] - public void TestPocoAddAssign() - { - var tPoco = new PocoEvent(); - bool tTest = false; - Dynamic.InvokeAddAssignMember(tPoco, "Event", new EventHandler((@object, args) => { tTest = true; })); - tPoco.OnEvent(null, null); - Assert.That(tTest, Is.EqualTo(true)); - - var tPoco2 = new PropPoco() { Prop2 = 3 }; - Dynamic.InvokeAddAssignMember(tPoco2, "Prop2", 4); - Assert.That(tPoco2.Prop2, Is.EqualTo(7L)); - } - - [Test] - public void TestCacheablePocoAddAssign() - { - var tPoco = new PocoEvent(); - bool tTest = false; - var tCachedInvoke = new CacheableInvocation(InvocationKind.AddAssign, "Event"); - tCachedInvoke.Invoke(tPoco, new EventHandler((@object, args) => { tTest = true; })); - tPoco.OnEvent(null, null); - Assert.That(tTest, Is.EqualTo(true)); - - var tPoco2 = new PropPoco() { Event = 3 }; - tCachedInvoke.Invoke(tPoco2, 4); - Assert.That(tPoco2.Event, Is.EqualTo(7L)); - } - - [Test] - public void TestPocoSubtractAssign() - { - var tPoco = new PocoEvent(); - bool tTest = false; - var tEvent = new EventHandler((@object, args) => { tTest = true; }); - tPoco.Event += tEvent; - Dynamic.InvokeSubtractAssignMember(tPoco, "Event", tEvent); - tPoco.OnEvent(null, null); - Assert.That(tTest, Is.EqualTo(false)); - - Dynamic.InvokeSubtractAssignMember(tPoco, "Event", tEvent); // Test Second Time - - var tPoco2 = new PropPoco() { Prop2 = 3 }; - Dynamic.InvokeSubtractAssignMember(tPoco2, "Prop2", 4); - Assert.That(tPoco2.Prop2, Is.EqualTo(-1L)); - } - - [Test] - public void TestCacheablePocoSubtractAssign() - { - var tPoco = new PocoEvent(); - bool tTest = false; - var tEvent = new EventHandler((@object, args) => { tTest = true; }); - var tCachedInvoke = new CacheableInvocation(InvocationKind.SubtractAssign, "Event"); - tPoco.Event += tEvent; - tCachedInvoke.Invoke(tPoco, tEvent); - tPoco.OnEvent(null, null); - Assert.That(tTest, Is.EqualTo(false)); - tCachedInvoke.Invoke(tPoco, tEvent); // Test Second Time - - var tPoco2 = new PropPoco() { Event = 3 }; - tCachedInvoke.Invoke(tPoco2, 4); - Assert.That(tPoco2.Event, Is.EqualTo(-1)); - } - - [Test] - public void TestDynamicAddAssign() - { - var tDyanmic = Build.NewObject(Prop2: 3, Event: null, OnEvent: new ThisAction((@this, obj, args) => @this.Event(obj, args))); - bool tTest = false; - Dynamic.InvokeAddAssignMember(tDyanmic, "Event", new EventHandler((@object, args) => { tTest = true; })); - tDyanmic.OnEvent(null, null); - Assert.That(tTest, Is.EqualTo(true)); - Dynamic.InvokeAddAssignMember(tDyanmic, "Prop2", 4); - Assert.That(tDyanmic.Prop2, Is.EqualTo(7L)); - } - - [Test] - public void TestCacheableDynamicAddAssign() - { - var tDyanmic = Build.NewObject(Prop2: 3, Event: null, OnEvent: new ThisAction((@this, obj, args) => @this.Event(obj, args))); - var tDynamic2 = Build.NewObject(Event: 3); - bool tTest = false; - var tCachedInvoke = new CacheableInvocation(InvocationKind.AddAssign, "Event"); - tCachedInvoke.Invoke((object)tDyanmic, new EventHandler((@object, args) => { tTest = true; })); - tDyanmic.OnEvent(null, null); - Assert.That(tTest, Is.EqualTo(true)); - tCachedInvoke.Invoke((object)tDynamic2, 4); - Assert.That(tDynamic2.Event, Is.EqualTo(7)); - } - - [Test] - public void TestDynamicSubtractAssign() - { - var tDyanmic = Build.NewObject(Prop2: 3, Event: null, OnEvent: new ThisAction((@this, obj, args) => @this.Event(obj, args))); - bool tTest = false; - var tEvent = new EventHandler((@object, args) => { tTest = true; }); - tDyanmic.Event += tEvent; - Dynamic.InvokeSubtractAssignMember(tDyanmic, "Event", tEvent); - tDyanmic.OnEvent(null, null); - Assert.That(tTest, Is.EqualTo(false)); - Dynamic.InvokeSubtractAssignMember(tDyanmic, "Prop2", 4); - Assert.That(tDyanmic.Prop2, Is.EqualTo(-1L)); - } - - [Test] - public void TestCacheableDynamicSubtractAssign() - { - var tDyanmic = Build.NewObject(Prop2: 3, Event: null, OnEvent: new ThisAction((@this, obj, args) => @this.Event(obj, args))); - var tDynamic2 = Build.NewObject(Event: 3); - bool tTest = false; - var tEvent = new EventHandler((@object, args) => { tTest = true; }); - var tCachedInvoke = new CacheableInvocation(InvocationKind.SubtractAssign, "Event"); - tDyanmic.Event += tEvent; - tCachedInvoke.Invoke((object)tDyanmic, tEvent); - tDyanmic.OnEvent(null, null); - Assert.That(tTest, Is.EqualTo(false)); - tCachedInvoke.Invoke((object)tDynamic2, 4); - Assert.That(tDynamic2.Event, Is.EqualTo(-1)); - } - - [Test] - public void TestDynamicMemberNamesExpando() - { - ExpandoObject tExpando = Build.NewObject(One: 1); - Assert.That(Dynamic.GetMemberNames(tExpando, dynamicOnly: true).Single(), Is.EqualTo("One")); - } - - [Test] - public void TestDynamicMemberNamesImpromput() - { - DynamicObjects.Dictionary tDict = Build.NewObject(Two: 2); - Assert.That(Dynamic.GetMemberNames(tDict, dynamicOnly: true).Single(), Is.EqualTo("Two")); - } - - [Test] - public void TestCachedInvocationEquality() - { - var tCachedIvnocation1 = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 2, argNames: new[] { "two" }); - var tCachedIvnocation2 = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 2, argNames: new[] { "two" }); - Assert.That(tCachedIvnocation1.GetHashCode(), Is.EqualTo(tCachedIvnocation2.GetHashCode())); - Assert.That(tCachedIvnocation1, Is.EqualTo(tCachedIvnocation2)); - } - - private DynamicObject CreateMock(ExpressionType op) - { - var tMock = new Mock() { CallBase = true }; - object result = It.IsAny(); - tMock.Setup( - s => s.TryBinaryOperation(It.Is(b => b.Operation == op), It.IsAny(), out result) - ).Returns(true); - return tMock.Object; - } - - public class OperatorTestDynObject : DynamicObject - { - private ExpressionType _type; - - public OperatorTestDynObject(ExpressionType type) - { - _type = type; - } - - public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result) - { - Assert.That(binder.Operation, Is.EqualTo(_type)); - result = _type; - return true; - } - - public override bool TryUnaryOperation(UnaryOperationBinder binder, out object result) - { - Assert.That(binder.Operation, Is.EqualTo(_type)); - result = _type; - return true; - } - } - - private void RunBinaryMockTests(ExpressionType type) - { - var mock = new OperatorTestDynObject(type); - var dummy = new Object(); - Dynamic.InvokeBinaryOperator(mock, type, dummy); - } - - private void RunUnaryMockTests(ExpressionType type) - { - var mock = new OperatorTestDynObject(type); - Dynamic.InvokeUnaryOpartor(type, mock); - } - - [Test] - public void TestInvokeAdd() - { - Assert.That(Dynamic.InvokeBinaryOperator(1, ExpressionType.Add, 2), Is.EqualTo(3)); - } - - [Test] - public void TestInvokeBasicUnaryOperatorsDynamic() - { - RunUnaryMockTests(ExpressionType.Not); - RunUnaryMockTests(ExpressionType.Negate); - RunUnaryMockTests(ExpressionType.Increment); - RunUnaryMockTests(ExpressionType.Decrement); - } - - [Test] - public void TestInvokeBasicBinaryOperatorsDynamic() - { - RunBinaryMockTests(ExpressionType.Add); - RunBinaryMockTests(ExpressionType.Subtract); - RunBinaryMockTests(ExpressionType.Divide); - RunBinaryMockTests(ExpressionType.Multiply); - RunBinaryMockTests(ExpressionType.Modulo); - RunBinaryMockTests(ExpressionType.And); - RunBinaryMockTests(ExpressionType.Or); - RunBinaryMockTests(ExpressionType.ExclusiveOr); - RunBinaryMockTests(ExpressionType.LeftShift); - RunBinaryMockTests(ExpressionType.RightShift); - RunBinaryMockTests(ExpressionType.AddAssign); - RunBinaryMockTests(ExpressionType.SubtractAssign); - RunBinaryMockTests(ExpressionType.DivideAssign); - RunBinaryMockTests(ExpressionType.MultiplyAssign); - RunBinaryMockTests(ExpressionType.ModuloAssign); - RunBinaryMockTests(ExpressionType.AndAssign); - RunBinaryMockTests(ExpressionType.OrAssign); - RunBinaryMockTests(ExpressionType.ExclusiveOrAssign); - RunBinaryMockTests(ExpressionType.LeftShiftAssign); - RunBinaryMockTests(ExpressionType.RightShiftAssign); - } - - [Test] - public void TestInvokeSubtract() - { - Assert.That(Dynamic.InvokeBinaryOperator(1, ExpressionType.Subtract, 2), Is.EqualTo(-1)); - } - } -} +using Dynamitey.SupportLibrary; +using Microsoft.CSharp.RuntimeBinder; +using Moq; +using System.Drawing; +using System.Dynamic; +using System.Globalization; +using System.Linq.Expressions; +using System.Xml.Linq; + +namespace Dynamitey.Tests +{ + public class Invoke + { + [OneTimeTearDown] + public void DestroyCaches() + { + Dynamic.ClearCaches(); + } + + [Test] + public void TestDynamicSet() + { + dynamic tExpando = new ExpandoObject(); + var tSetValue = "1"; + Dynamic.InvokeSet(tExpando, "Test", tSetValue); + Assert.Multiple(() => + { + Assert.That(tExpando.Test, Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestPocoSet() + { + var tPoco = new PropPoco(); + var tSetValue = "1"; + Dynamic.InvokeSet(tPoco, "Prop1", tSetValue); + Assert.Multiple(() => + { + Assert.That(tPoco.Prop1, Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestStructSet() + { + object tPoco = new PropStruct(); + var tSetValue = "1"; + Dynamic.InvokeSet(tPoco, "Prop1", tSetValue); + Assert.Multiple(() => + { + Assert.That(((PropStruct)tPoco).Prop1, Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestCacheableDyanmicSetAndPocoSetAndSetNull() + { + dynamic tExpando = new ExpandoObject(); + var tSetValueD = "4"; + + var tCachedInvoke = new CacheableInvocation(InvocationKind.Set, "Prop1"); + tCachedInvoke.Invoke((object)tExpando, tSetValueD); + + Assert.Multiple(() => + { + Assert.That(tExpando.Prop1, Is.EqualTo(tSetValueD)); + + var tPoco = new PropPoco(); + var tSetValue = "1"; + + tCachedInvoke.Invoke(tPoco, tSetValue); + Assert.That(tPoco.Prop1, Is.EqualTo(tSetValue)); + + string tSetValue2 = null; + tCachedInvoke.Invoke(tPoco, tSetValue2); + Assert.That(tPoco.Prop1, Is.EqualTo(tSetValue2)); + }); + } + + [Test] + public void TestConvert() + { + var tEl = new XElement("Test", "45"); + var tCast = Dynamic.InvokeConvert(tEl, typeof(int), @explicit: true); + Assert.Multiple(() => + { + Assert.That(tCast.GetType(), Is.EqualTo(typeof(int))); + Assert.That(tCast, Is.EqualTo(45)); + }); + } + + [Test] + public void TestConvertCacheable() + { + var tEl = new XElement("Test", "45"); + var tCacheInvoke = new CacheableInvocation(InvocationKind.Convert, convertType: typeof(int), convertExplicit: true); + var tCast = tCacheInvoke.Invoke(tEl); + Assert.Multiple(() => + { + Assert.That(tCast.GetType(), Is.EqualTo(typeof(int))); + Assert.That(tCast, Is.EqualTo(45)); + }); + } + + [Test] + public void TestConstruct() + { + var tCast = Dynamic.InvokeConstructor(typeof(List), new object[] + { + new string[] { "one", "two", "three" } + }); + Assert.Multiple(() => + { + Assert.That(tCast[1], Is.EqualTo("two")); + }); + } + + [Test] + public void TestCacheableConstruct() + { + var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 1); + dynamic tCast = tCachedInvoke.Invoke(typeof(List), new object[] + { + new string[] { "one", "two", "three" } + }); + Assert.Multiple(() => + { + Assert.That(tCast[1], Is.EqualTo("two")); + }); + } + + [Test] + public void TestConstructOptional() + { + var argname = InvokeArg.Create; + PocoOptConstructor tCast = Dynamic.InvokeConstructor(typeof(PocoOptConstructor), argname("three", "3")); + Assert.Multiple(() => + { + Assert.That(tCast.One, Is.EqualTo("-1")); + Assert.That(tCast.Two, Is.EqualTo("-2")); + Assert.That(tCast.Three, Is.EqualTo("3")); + }); + } + + [Test] + public void TestCacheableConstructOptional() + { + var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 1, argNames: new[] { "three" }); + var tCast = (PocoOptConstructor)tCachedInvoke.Invoke(typeof(PocoOptConstructor), "3"); + Assert.Multiple(() => + { + Assert.That(tCast.One, Is.EqualTo("-1")); + Assert.That(tCast.Two, Is.EqualTo("-2")); + Assert.That(tCast.Three, Is.EqualTo("3")); + }); + } + + [Test] + public void TestOptionalArgumentActivationNoneAndCacheable() + { + Assert.Throws(() => Activator.CreateInstance()); + + var tList = Dynamic.InvokeConstructor(typeof(DynamicObjects.List)); + Assert.Multiple(() => + { + Assert.That(tList.GetType(), Is.EqualTo(typeof(DynamicObjects.List))); + + var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor); + var tList1 = tCachedInvoke.Invoke(typeof(DynamicObjects.List)); + Assert.That(tList1.GetType(), Is.EqualTo(typeof(DynamicObjects.List))); + }); + } + + [Test] + public void TestConstructValueType() + { + var tCast = Dynamic.InvokeConstructor(typeof(DateTime), 2009, 1, 20); + Assert.Multiple(() => + { + Assert.That(tCast.Day, Is.EqualTo(20)); + }); + } + + [Test] + public void TestCacheableConstructValueType() + { + var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 3); + dynamic tCast = tCachedInvoke.Invoke(typeof(DateTime), 2009, 1, 20); + Assert.Multiple(() => + { + Assert.That(tCast.Day, Is.EqualTo(20)); + }); + } + + [Test] + public void TestConstructValueTypeJustDynamic() + { + dynamic day = 20; + dynamic year = 2009; + dynamic month = 1; + var tCast = new DateTime(year, month, day); + DateTime tDate = tCast; + Assert.Multiple(() => + { + Assert.That(tDate.Day, Is.EqualTo(20)); + }); + } + + [Test] + public void TestConstructprimativetype() + { + var tCast = Dynamic.InvokeConstructor(typeof(Int32)); + Assert.Multiple(() => + { + Assert.That(tCast, Is.EqualTo(default(Int32))); + }); + } + + [Test] + public void TestConstructDateTimeNoParams() + { + var tCast = Dynamic.InvokeConstructor(typeof(DateTime)); + Assert.Multiple(() => + { + Assert.That(tCast, Is.EqualTo(default(DateTime))); + }); + } + + [Test] + public void TestConstructOBjectNoParams() + { + var tCast = Dynamic.InvokeConstructor(typeof(object)); + Assert.Multiple(() => + { + Assert.That(tCast.GetType(), Is.EqualTo(typeof(object))); + }); + } + + [Test] + public void TestConstructNullableprimativetype() + { + var tCast = Dynamic.InvokeConstructor(typeof(Nullable)); + Assert.Multiple(() => + { + Assert.That(tCast, Is.EqualTo(null)); + }); + } + + [Test] + public void TestConstructGuid() + { + var tCast = Dynamic.InvokeConstructor(typeof(Guid)); + Assert.Multiple(() => + { + Assert.That(tCast, Is.EqualTo(default(Guid))); + }); + } + + [Test] + public void TestCacheablePrimativeDateTimeObjectNullableAndGuidNoParams() + { + var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor); + + Assert.Multiple(() => + { + dynamic tCast = tCachedInvoke.Invoke(typeof(Int32)); + Assert.That(tCast, Is.EqualTo(default(Int32))); + + tCast = tCachedInvoke.Invoke(typeof(DateTime)); + Assert.That(tCast, Is.EqualTo(default(DateTime))); + + tCast = tCachedInvoke.Invoke(typeof(List)); + Assert.That(tCast.GetType(), Is.EqualTo(typeof(List))); + + tCast = tCachedInvoke.Invoke(typeof(object)); + Assert.That(tCast.GetType(), Is.EqualTo(typeof(object))); + + tCast = tCachedInvoke.Invoke(typeof(Nullable)); + Assert.That(tCast, Is.EqualTo(null)); + + tCast = tCachedInvoke.Invoke(typeof(Guid)); + Assert.That(tCast, Is.EqualTo(default(Guid))); + }); + } + + [Test] + public void TestStaticCall() + { + var @static = InvokeContext.CreateStatic; + var generic = InvokeMemberName.Create; + var tOut = Dynamic.InvokeMember(@static(typeof(StaticType)), generic("Create", new[] { typeof(bool) }), 1); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(false)); + }); + } + + [Test] + public void TestCacheableStaticCall() + { + var @static = InvokeContext.CreateStatic; + var generic = InvokeMemberName.Create; + var tCached = new CacheableInvocation(InvocationKind.InvokeMember, generic("Create", new[] { typeof(bool) }), argCount: 1, context: @static(typeof(StaticType))); + var tOut = tCached.Invoke(typeof(StaticType), 1); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(false)); + }); + } + + private class TestClass + { + public static int StaticProperty { get; set; } + } + + [Test] + public void TestStaticPropertySetFollowedByGetTest() + { + var staticContext = InvokeContext.CreateStatic; + Dynamic.InvokeSet(staticContext(typeof(TestClass)), "StaticProperty", 42); + var tOut = Dynamic.InvokeGet(staticContext(typeof(TestClass)), "StaticProperty"); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(42)); + }); + } + + [Test] + public void TestImplicitConvert() + { + var tEl = 45; + var tCast = Dynamic.InvokeConvert(tEl, typeof(long)); + Assert.Multiple(() => + { + Assert.That(tCast.GetType(), Is.EqualTo(typeof(long))); + }); + } + + [Test] + public void TestCoerceConverterColor() + { + var colorString = "PaleVioletRed"; + var color = Dynamic.CoerceConvert(colorString, typeof(Color)); + Assert.Multiple(() => + { + Assert.That(color, Is.TypeOf()); + Assert.That(color, Is.EqualTo(Color.PaleVioletRed)); + }); + } + + [Test] + public void TestCoerceConverterDBNULL() + { + var tEl = DBNull.Value; + var tCast = Dynamic.CoerceConvert(tEl, typeof(long)); + Assert.Multiple(() => + { + Assert.That(tCast.GetType(), Is.EqualTo(typeof(long))); + + var tCast2 = Dynamic.CoerceConvert(tEl, typeof(string)); + Assert.That(tCast2, Is.EqualTo(null)); + Assert.AreNotEqual(null, tEl); + }); + } + + [Test] + public void TestCacheableImplicitConvert() + { + var tEl = 45; + var tCachedInvoke = CacheableInvocation.CreateConvert(typeof(long)); + var tCast = tCachedInvoke.Invoke(tEl); + Assert.Multiple(() => + { + Assert.That(tCast.GetType(), Is.EqualTo(typeof(long))); + }); + } + + [Test] + public void TestCacheableGet() + { + var tCached = new CacheableInvocation(InvocationKind.Get, "Prop1"); + var tSetValue = "1"; + var tAnon = new PropPoco { Prop1 = tSetValue }; + var tOut = tCached.Invoke(tAnon); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(tSetValue)); + + var tSetValue2 = "2"; + tAnon = new PropPoco { Prop1 = tSetValue2 }; + var tOut2 = tCached.Invoke(tAnon); + Assert.That(tOut2, Is.EqualTo(tSetValue2)); + }); + } + + [Test] + public void TestGetIndexer() + { + dynamic tSetValue = "1"; + var tAnon = new[] { tSetValue, "2" }; + string tOut = Dynamic.InvokeGetIndex(tAnon, 0); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestGetIndexerValue() + { + var tAnon = new int[] { 1, 2 }; + int tOut = Dynamic.InvokeGetIndex(tAnon, 1); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(tAnon[1])); + }); + } + + [Test] + public void TestGetLengthArray() + { + var tAnon = new[] { "1", "2" }; + int tOut = Dynamic.InvokeGet(tAnon, "Length"); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(2)); + }); + } + + [Test] + public void TestGetIndexerArray() + { + dynamic tSetValue = "1"; + var tAnon = new List { tSetValue, "2" }; + string tOut = Dynamic.InvokeGetIndex(tAnon, 0); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestCacheableIndexer() + { + var tStrings = new[] { "1", "2" }; + var tCachedInvoke = new CacheableInvocation(InvocationKind.GetIndex, argCount: 1); + Assert.Multiple(() => + { + var tOut = (string)tCachedInvoke.Invoke(tStrings, 0); + Assert.That(tOut, Is.EqualTo(tStrings[0])); + + var tOut2 = (string)tCachedInvoke.Invoke(tStrings, 1); + Assert.That(tOut2, Is.EqualTo(tStrings[1])); + + var tInts = new int[] { 3, 4 }; + var tOut3 = (int)tCachedInvoke.Invoke(tInts, 0); + Assert.That(tOut3, Is.EqualTo(tInts[0])); + + var tOut4 = (int)tCachedInvoke.Invoke(tInts, 1); + Assert.That(tOut4, Is.EqualTo(tInts[1])); + + var tList = new List { "5", "6" }; + var tOut5 = (string)tCachedInvoke.Invoke(tList, 0); + Assert.That(tOut5, Is.EqualTo(tList[0])); + + var tOut6 = (string)tCachedInvoke.Invoke(tList, 0); + Assert.That(tOut6, Is.EqualTo(tList[0])); + }); + } + + [Test] + public void TestSetIndexer() + { + dynamic tSetValue = "3"; + var tAnon = new List { "1", "2" }; + Dynamic.InvokeSetIndex(tAnon, 0, tSetValue); + Assert.Multiple(() => + { + Assert.That(tAnon[0], Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestCacheableSetIndexer() + { + dynamic tSetValue = "3"; + var tList = new List { "1", "2" }; + var tCachedInvoke = new CacheableInvocation(InvocationKind.SetIndex, argCount: 2); + tCachedInvoke.Invoke(tList, 0, tSetValue); + Assert.Multiple(() => + { + Assert.That(tList[0], Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestMethodDynamicPassAndGetValue() + { + dynamic tExpando = new ExpandoObject(); + tExpando.Func = new Func(it => it.ToString()); + var tValue = 1; + var tOut = Dynamic.InvokeMember(tExpando, "Func", tValue); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(tValue.ToString())); + }); + } + + [Test] + public void TestCacheableMethodDynamicPassAndGetValue() + { + dynamic tExpando = new ExpandoObject(); + tExpando.Func = new Func(it => it.ToString()); + var tValue = 1; + var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Func", 1); + var tOut = tCachedInvoke.Invoke((object)tExpando, tValue); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(tValue.ToString())); + }); + } + + [Test] + public void TestMethodStaticOverloadingPassAndGetValue() + { + var tPoco = new OverloadingMethPoco(); + var tValue = 1; + var tOut = Dynamic.InvokeMember(tPoco, "Func", tValue); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo("int")); + Assert.That((object)tOut, Is.EqualTo("int")); //should still be int because this uses runtime type + + var tOut2 = Dynamic.InvokeMember(tPoco, "Func", 1m); + Assert.That(tOut2, Is.EqualTo("object")); + + var tOut3 = Dynamic.InvokeMember(tPoco, "Func", new { Anon = 1 }); + Assert.That(tOut3, Is.EqualTo("object")); + }); + } + + [Test] + public void TestCachedMethodStaticOverloadingPassAndGetValue() + { + var tPoco = new OverloadingMethPoco(); + var tValue = 1; + var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 1); + var tOut = tCachedInvoke.Invoke(tPoco, tValue); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo("int")); + Assert.That((object)tOut, Is.EqualTo("int")); //should still be int because this uses runtime type + + var tOut2 = tCachedInvoke.Invoke(tPoco, 1m); + Assert.That(tOut2, Is.EqualTo("object")); + + var tOut3 = tCachedInvoke.Invoke(tPoco, new { Anon = 1 }); + Assert.That(tOut3, Is.EqualTo("object")); + }); + } + + [Test] + public void TestMethodPocoOverloadingPassAndGetValueArg() + { + var tPoco = new OverloadingMethPoco(); + var tValue = 1; + var tOut = Dynamic.InvokeMember(tPoco, "Func", new InvokeArg("arg", tValue)); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo("int")); + Assert.That((object)tOut, Is.EqualTo("int")); //should still be int because this uses runtime type + + var tOut2 = Dynamic.InvokeMember(tPoco, "Func", 1m); + Assert.That(tOut2, Is.EqualTo("object")); + + var tOut3 = Dynamic.InvokeMember(tPoco, "Func", new { Anon = 1 }); + Assert.That(tOut3, Is.EqualTo("object")); + }); + } + + [Test] + public void TestMethodPocoOverloadingPassAndGetValueArgOptional() + { + var tPoco = new OverloadingMethPoco(); + var tValue = 1; + var arg = InvokeArg.Create; + var tOut = Dynamic.InvokeMember(tPoco, "Func", arg("two", tValue)); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo("object named")); + Assert.That((object)tOut, Is.EqualTo("object named")); + }); + } + + [Test] + public void TestCacheableMethodPocoOverloadingPassAndGetValueArgOptional() + { + var tPoco = new OverloadingMethPoco(); + var tValue = 1; + var tCachedIvnocation = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 1, argNames: new[] { "two" }); + var tOut = tCachedIvnocation.Invoke(tPoco, tValue); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo("object named")); + Assert.That((object)tOut, Is.EqualTo("object named")); + }); + } + + [Test] + public void TestCacheableMethodPocoOverloadingPassAndGetValueArgPostiionalOptional() + { + var tPoco = new OverloadingMethPoco(); + var tValue1 = 1; + var tValue2 = 2; + var tCachedIvnocation = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 2, argNames: new[] { "two" }); + var tOut = tCachedIvnocation.Invoke(tPoco, tValue1, tValue2); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo("object named")); + Assert.That((object)tOut, Is.EqualTo("object named")); + }); + } + + [Test] + public void TestMethodPocoOverloadingPass2AndGetValueArgOptional() + { + var tPoco = new OverloadingMethPoco(); + var tValue = 1; + var arg = InvokeArg.Create; + var tOut = Dynamic.InvokeMember(tPoco, "Func", arg("two", tValue), arg("one", tValue)); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo("object named")); + Assert.That((object)tOut, Is.EqualTo("object named")); + }); + } + + [Test] + public void TestMethodPocoOverloadingPassAndGetValueNull() + { + var tPoco = new OverloadingMethPoco(); + var tValue = 1; + var tOut = Dynamic.InvokeMember(tPoco, "Func", tValue); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo("int")); + Assert.That((object)tOut, Is.EqualTo("int")); //should still be int because this uses runtime type + + var tOut2 = Dynamic.InvokeMember(tPoco, "Func", 1m); + Assert.That(tOut2, Is.EqualTo("object")); + + var tOut3 = Dynamic.InvokeMember(tPoco, "Func", null); + Assert.That(tOut3, Is.EqualTo("object")); + + var tOut4 = Dynamic.InvokeMember(tPoco, "Func", null, null, "test", null, null, null); + Assert.That(tOut4, Is.EqualTo("object 6")); + + var tOut5 = Dynamic.InvokeMember(tPoco, "Func", null, null, null, null, null, null); + Assert.That(tOut5, Is.EqualTo("object 6")); + }); + } + + /// + /// To dynamically invoke a method with out or ref parameters you need to know the signature + /// + [Test] + public void TestOutMethod() + { + string tResult = String.Empty; + var tPoco = new MethOutPoco(); + var tName = "Func"; + var tContext = GetType(); + var tBinder = Binder.InvokeMember(CSharpBinderFlags.None, tName, null, tContext, + new[] + { + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsOut | CSharpArgumentInfoFlags.UseCompileTimeType, null) + }); + + var tSite = Dynamic.CreateCallSite(tBinder, tName, tContext); + tSite.Target.Invoke(tSite, tPoco, out tResult); + Assert.Multiple(() => + { + Assert.That(tResult, Is.EqualTo("success")); + }); + } + + [Test] + public void TestMethodDynamicPassVoid() + { + var tTest = "Wrong"; + var tValue = "Correct"; + dynamic tExpando = new ExpandoObject(); + tExpando.Action = new Action(it => tTest = it); + Dynamic.InvokeMemberAction(tExpando, "Action", tValue); + Assert.Multiple(() => + { + Assert.That(tTest, Is.EqualTo(tValue)); + }); + } + + [Test] + public void TestCacheableMethodDynamicPassVoid() + { + var tTest = "Wrong"; + var tValue = "Correct"; + dynamic tExpando = new ExpandoObject(); + tExpando.Action = new Action(it => tTest = it); + var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMemberAction, "Action", argCount: 1); + tCachedInvoke.Invoke((object)tExpando, tValue); + Assert.Multiple(() => + { + Assert.That(tTest, Is.EqualTo(tValue)); + }); + } + + [Test] + public void TestCacheableMethodDynamicUnknowns() + { + var tTest = "Wrong"; + var tValue = "Correct"; + dynamic tExpando = new ExpandoObject(); + tExpando.Action = new Action(it => tTest = it); + tExpando.Func = new Func(it => it); + var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMemberUnknown, "Action", argCount: 1); + tCachedInvoke.Invoke((object)tExpando, tValue); + Assert.Multiple(() => + { + Assert.That(tTest, Is.EqualTo(tValue)); + + var tCachedInvoke2 = new CacheableInvocation(InvocationKind.InvokeMemberUnknown, "Func", argCount: 1); + var Test2 = tCachedInvoke2.Invoke((object)tExpando, tValue); + Assert.That(Test2, Is.EqualTo(tValue)); + }); + } + + [Test] + public void TestMethodPocoGetValue() + { + var tValue = 1; + var tOut = Dynamic.InvokeMember(tValue, "ToString"); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(tValue.ToString())); + }); + } + + [Test] + public void TestMethodPocoPassAndGetValue() + { + HelpTestPocoPassAndGetValue("Test", "Te"); + HelpTestPocoPassAndGetValue("Test", "st"); + } + + private static void HelpTestPocoPassAndGetValue(string tValue, string tParam) + { + var tExpected = tValue.StartsWith(tParam); + var tOut = Dynamic.InvokeMember(tValue, "StartsWith", tParam); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(tExpected)); + }); + } + + [Test] + public void TestGetDynamic() + { + var tSetValue = "1"; + dynamic tExpando = new ExpandoObject(); + tExpando.Test = tSetValue; + var tOut = Dynamic.InvokeGet(tExpando, "Test"); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestGetDynamicChained() + { + var tSetValue = "1"; + dynamic tExpando = new ExpandoObject(); + tExpando.Test = new ExpandoObject(); + tExpando.Test.Test2 = new ExpandoObject(); + tExpando.Test.Test2.Test3 = tSetValue; + var tOut = Dynamic.InvokeGetChain(tExpando, "Test.Test2.Test3"); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestGetDynamicChainedWithIndexes() + { + var tSetValue = "1"; + dynamic tExpando = Build.NewObject( + Test: Build.NewObject( + Test2: Build.NewList( + Build.NewObject(Test3: Build.NewObject(Test4: tSetValue)) + ) + ) + ); + var tOut = Dynamic.InvokeGetChain(tExpando, "Test.Test2[0].Test3['Test4']"); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestSetDynamicChained() + { + var tSetValue = "1"; + dynamic tExpando = new ExpandoObject(); + tExpando.Test = new ExpandoObject(); + tExpando.Test.Test2 = new ExpandoObject(); + Dynamic.InvokeSetChain(tExpando, "Test.Test2.Test3", tSetValue); + Assert.Multiple(() => + { + Assert.That(tExpando.Test.Test2.Test3, Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestSetDynamicChainedWithInexes() + { + var tSetValue = "1"; + dynamic tExpando = Build.NewObject( + Test: Build.NewObject( + Test2: Build.NewList( + Build.NewObject(Test3: Build.NewObject()) + ) + ) + ); + var tOut = Dynamic.InvokeSetChain(tExpando, "Test.Test2[0].Test3['Test4']", tSetValue); + Assert.Multiple(() => + { + Assert.That(tExpando.Test.Test2[0].Test3["Test4"], Is.EqualTo(tSetValue)); + Assert.That(tOut, Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestSetDynamicAllDict() + { + var tSetValue = "1"; + dynamic tExpando = new ExpandoObject(); + tExpando.Test = new ExpandoObject(); + tExpando.Test.Test2 = new ExpandoObject(); + Dynamic.InvokeSetAll(tExpando, new Dictionary { { "Test.Test2.Test3", tSetValue }, { "One", 1 }, { "Two", 2 } }); + Dynamic.InvokeSetChain(tExpando, "Test.Test2.Test3", tSetValue); + Assert.Multiple(() => + { + Assert.That(tExpando.Test.Test2.Test3, Is.EqualTo(tSetValue)); + Assert.That(tExpando.One, Is.EqualTo(1)); + Assert.That(tExpando.Two, Is.EqualTo(2)); + }); + } + + [Test] + public void TestSetDynamicAllAnonymous() + { + dynamic tExpando = new ExpandoObject(); + Dynamic.InvokeSetAll(tExpando, new { One = 1, Two = 2, Three = 3 }); + Assert.Multiple(() => + { + Assert.That(tExpando.One, Is.EqualTo(1)); + Assert.That(tExpando.Two, Is.EqualTo(2)); + Assert.That(tExpando.Three, Is.EqualTo(3)); + }); + } + + [Test] + public void TestSetDynamicAllNamed() + { + dynamic tExpando = new ExpandoObject(); + Dynamic.InvokeSetAll(tExpando, One: 1, Two: 2, Three: 3); + Assert.Multiple(() => + { + Assert.That(tExpando.One, Is.EqualTo(1)); + Assert.That(tExpando.Two, Is.EqualTo(2)); + Assert.That(tExpando.Three, Is.EqualTo(3)); + }); + } + + [Test] + public void TestSetDynamicChainedOne() + { + var tSetValue = "1"; + dynamic tExpando = new ExpandoObject(); + Dynamic.InvokeSetChain(tExpando, "Test", tSetValue); + Assert.Multiple(() => + { + Assert.That(tExpando.Test, Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestGetDynamicChainedOne() + { + var tSetValue = "1"; + dynamic tExpando = new ExpandoObject(); + tExpando.Test = tSetValue; + var tOut = Dynamic.InvokeGetChain(tExpando, "Test"); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestCacheableGetDynamic() + { + var tSetValue = "1"; + dynamic tExpando = new ExpandoObject(); + tExpando.Test = tSetValue; + var tCached = new CacheableInvocation(InvocationKind.Get, "Test"); + var tOut = tCached.Invoke((object)tExpando); + Assert.Multiple(() => + { + Assert.That(tOut, Is.EqualTo(tSetValue)); + }); + } + + [Test] + public void TestStaticGet() + { + var @static = InvokeContext.CreateStatic; + var tDate = Dynamic.InvokeGet(@static(typeof(DateTime)), "Today"); + Assert.Multiple(() => + { + Assert.That(tDate, Is.EqualTo(DateTime.Today)); + }); + } + + [Test] + public void TestCacheableStaticGet() + { + var @static = InvokeContext.CreateStatic; + var tCached = new CacheableInvocation(InvocationKind.Get, "Today", context: @static(typeof(DateTime))); + var tDate = tCached.Invoke(typeof(DateTime)); + Assert.Multiple(() => + { + Assert.That(tDate, Is.EqualTo(DateTime.Today)); + }); + } + + [Test] + public void TestStaticGet2() + { + var @static = InvokeContext.CreateStatic; + var tVal = Dynamic.InvokeGet(@static(typeof(StaticType)), "Test"); + Assert.Multiple(() => + { + Assert.That(tVal, Is.EqualTo(true)); + }); + } + + [Test] + public void TestStaticGet3() + { + var tVal = Dynamic.InvokeGet((StaticContext)typeof(StaticType), "Test"); + Assert.Multiple(() => + { + Assert.That(tVal, Is.EqualTo(true)); + }); + } + + [Test] + public void TestStaticSet() + { + var @static = InvokeContext.CreateStatic; + int tValue = 12; + Dynamic.InvokeSet(@static(typeof(StaticType)), "TestSet", tValue); + Assert.Multiple(() => + { + Assert.That(StaticType.TestSet, Is.EqualTo(tValue)); + }); + } + + [Test] + public void TestCacheableStaticSet() + { + int tValue = 12; + var @static = InvokeContext.CreateStatic; + var tCachedInvoke = new CacheableInvocation(InvocationKind.Set, "TestSet", context: @static(typeof(StaticType))); + tCachedInvoke.Invoke(typeof(StaticType), tValue); + Assert.Multiple(() => + { + Assert.That(StaticType.TestSet, Is.EqualTo(tValue)); + }); + } + + [Test] + public void TestStaticDateTimeMethod() + { + var @static = InvokeContext.CreateStatic; + object tDateDyn = "01/20/2009"; + var tDate = Dynamic.InvokeMember(@static(typeof(DateTime)), "Parse", tDateDyn, CultureInfo.GetCultureInfo("en-US")); + Assert.Multiple(() => + { + Assert.That(tDate, Is.EqualTo(new DateTime(2009, 1, 20))); + }); + } + + [Test] + public void TestCacheableStaticDateTimeMethod() + { + var @static = InvokeContext.CreateStatic; + object tDateDyn = "01/20/2009"; + var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Parse", 2, context: @static(typeof(DateTime))); + var tDate = tCachedInvoke.Invoke(typeof(DateTime), tDateDyn, CultureInfo.GetCultureInfo("en-US")); + Assert.Multiple(() => + { + Assert.That(tDate, Is.EqualTo(new DateTime(2009, 1, 20))); + }); + } + + [Test] + public void TestIsEvent() + { + dynamic tPoco = new PocoEvent(); + var tResult = Dynamic.InvokeIsEvent(tPoco, "Event"); + Assert.Multiple(() => + { + Assert.That(tResult, Is.EqualTo(true)); + }); + } + + [Test] + public void TestCacheableIsEventAndIsNotEvent() + { + object tPoco = new PocoEvent(); + var tCachedInvoke = new CacheableInvocation(InvocationKind.IsEvent, "Event"); + var tResult = tCachedInvoke.Invoke(tPoco); + Assert.Multiple(() => + { + Assert.That(tResult, Is.EqualTo(true)); + + dynamic tDynamic = new DynamicObjects.Dictionary(); + tDynamic.Event = null; + var tResult2 = tCachedInvoke.Invoke((object)tDynamic); + Assert.That(tResult2, Is.EqualTo(false)); + }); + } + + [Test] + public void TestIsNotEvent() + { + dynamic tDynamic = new DynamicObjects.Dictionary(); + tDynamic.Event = null; + var tResult = Dynamic.InvokeIsEvent(tDynamic, "Event"); + Assert.Multiple(() => + { + Assert.That(tResult, Is.EqualTo(false)); + + bool tTest = false; + bool tTest2 = false; + tDynamic.Event += new EventHandler((@object, args) => { tTest = true; }); + tDynamic.Event += new EventHandler((@object, args) => { tTest2 = true; }); + Assert.That(tTest, Is.EqualTo(false)); + Assert.That(tTest2, Is.EqualTo(false)); + + tDynamic.Event(null, null); + Assert.That(tTest, Is.EqualTo(true)); + Assert.That(tTest2, Is.EqualTo(true)); + }); + } + + [Test] + public void TestPocoAddAssign() + { + var tPoco = new PocoEvent(); + bool tTest = false; + Dynamic.InvokeAddAssignMember(tPoco, "Event", new EventHandler((@object, args) => { tTest = true; })); + tPoco.OnEvent(null, null); + Assert.Multiple(() => + { + Assert.That(tTest, Is.EqualTo(true)); + + var tPoco2 = new PropPoco() { Prop2 = 3 }; + Dynamic.InvokeAddAssignMember(tPoco2, "Prop2", 4); + Assert.That(tPoco2.Prop2, Is.EqualTo(7L)); + }); + } + + [Test] + public void TestCacheablePocoAddAssign() + { + var tPoco = new PocoEvent(); + bool tTest = false; + var tCachedInvoke = new CacheableInvocation(InvocationKind.AddAssign, "Event"); + tCachedInvoke.Invoke(tPoco, new EventHandler((@object, args) => { tTest = true; })); + tPoco.OnEvent(null, null); + Assert.Multiple(() => + { + Assert.That(tTest, Is.EqualTo(true)); + + var tPoco2 = new PropPoco() { Event = 3 }; + tCachedInvoke.Invoke(tPoco2, 4); + Assert.That(tPoco2.Event, Is.EqualTo(7L)); + }); + } + + [Test] + public void TestPocoSubtractAssign() + { + var tPoco = new PocoEvent(); + bool tTest = false; + var tEvent = new EventHandler((@object, args) => { tTest = true; }); + tPoco.Event += tEvent; + Dynamic.InvokeSubtractAssignMember(tPoco, "Event", tEvent); + tPoco.OnEvent(null, null); + Assert.Multiple(() => + { + Assert.That(tTest, Is.EqualTo(false)); + + Dynamic.InvokeSubtractAssignMember(tPoco, "Event", tEvent); // Test Second Time + + var tPoco2 = new PropPoco() { Prop2 = 3 }; + Dynamic.InvokeSubtractAssignMember(tPoco2, "Prop2", 4); + Assert.That(tPoco2.Prop2, Is.EqualTo(-1L)); + }); + } + + [Test] + public void TestCacheablePocoSubtractAssign() + { + var tPoco = new PocoEvent(); + bool tTest = false; + var tEvent = new EventHandler((@object, args) => { tTest = true; }); + var tCachedInvoke = new CacheableInvocation(InvocationKind.SubtractAssign, "Event"); + tPoco.Event += tEvent; + tCachedInvoke.Invoke(tPoco, tEvent); + tPoco.OnEvent(null, null); + Assert.Multiple(() => + { + Assert.That(tTest, Is.EqualTo(false)); + tCachedInvoke.Invoke(tPoco, tEvent); // Test Second Time + + var tPoco2 = new PropPoco() { Event = 3 }; + tCachedInvoke.Invoke(tPoco2, 4); + Assert.That(tPoco2.Event, Is.EqualTo(-1)); + }); + } + + [Test] + public void TestDynamicAddAssign() + { + var tDyanmic = Build.NewObject(Prop2: 3, Event: null, OnEvent: new ThisAction((@this, obj, args) => @this.Event(obj, args))); + bool tTest = false; + Dynamic.InvokeAddAssignMember(tDyanmic, "Event", new EventHandler((@object, args) => { tTest = true; })); + tDyanmic.OnEvent(null, null); + Assert.Multiple(() => + { + Assert.That(tTest, Is.EqualTo(true)); + Dynamic.InvokeAddAssignMember(tDyanmic, "Prop2", 4); + Assert.That(tDyanmic.Prop2, Is.EqualTo(7L)); + }); + } + + [Test] + public void TestCacheableDynamicAddAssign() + { + var tDyanmic = Build.NewObject(Prop2: 3, Event: null, OnEvent: new ThisAction((@this, obj, args) => @this.Event(obj, args))); + var tDynamic2 = Build.NewObject(Event: 3); + bool tTest = false; + var tCachedInvoke = new CacheableInvocation(InvocationKind.AddAssign, "Event"); + tCachedInvoke.Invoke((object)tDyanmic, new EventHandler((@object, args) => { tTest = true; })); + tDyanmic.OnEvent(null, null); + Assert.Multiple(() => + { + Assert.That(tTest, Is.EqualTo(true)); + tCachedInvoke.Invoke((object)tDynamic2, 4); + Assert.That(tDynamic2.Event, Is.EqualTo(7)); + }); + } + + [Test] + public void TestDynamicSubtractAssign() + { + var tDyanmic = Build.NewObject(Prop2: 3, Event: null, OnEvent: new ThisAction((@this, obj, args) => @this.Event(obj, args))); + bool tTest = false; + var tEvent = new EventHandler((@object, args) => { tTest = true; }); + tDyanmic.Event += tEvent; + Dynamic.InvokeSubtractAssignMember(tDyanmic, "Event", tEvent); + tDyanmic.OnEvent(null, null); + Assert.Multiple(() => + { + Assert.That(tTest, Is.EqualTo(false)); + Dynamic.InvokeSubtractAssignMember(tDyanmic, "Prop2", 4); + Assert.That(tDyanmic.Prop2, Is.EqualTo(-1L)); + }); + } + + [Test] + public void TestCacheableDynamicSubtractAssign() + { + var tDyanmic = Build.NewObject(Prop2: 3, Event: null, OnEvent: new ThisAction((@this, obj, args) => @this.Event(obj, args))); + var tDynamic2 = Build.NewObject(Event: 3); + bool tTest = false; + var tEvent = new EventHandler((@object, args) => { tTest = true; }); + var tCachedInvoke = new CacheableInvocation(InvocationKind.SubtractAssign, "Event"); + tDyanmic.Event += tEvent; + tCachedInvoke.Invoke((object)tDyanmic, tEvent); + tDyanmic.OnEvent(null, null); + Assert.Multiple(() => + { + Assert.That(tTest, Is.EqualTo(false)); + tCachedInvoke.Invoke((object)tDynamic2, 4); + Assert.That(tDynamic2.Event, Is.EqualTo(-1)); + }); + } + + [Test] + public void TestDynamicMemberNamesExpando() + { + ExpandoObject tExpando = Build.NewObject(One: 1); + Assert.Multiple(() => + { + Assert.That(Dynamic.GetMemberNames(tExpando, dynamicOnly: true).Single(), Is.EqualTo("One")); + }); + } + + [Test] + public void TestDynamicMemberNamesImpromput() + { + DynamicObjects.Dictionary tDict = Build.NewObject(Two: 2); + Assert.Multiple(() => + { + Assert.That(Dynamic.GetMemberNames(tDict, dynamicOnly: true).Single(), Is.EqualTo("Two")); + }); + } + + [Test] + public void TestCachedInvocationEquality() + { + var tCachedIvnocation1 = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 2, argNames: new[] { "two" }); + var tCachedIvnocation2 = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 2, argNames: new[] { "two" }); + Assert.Multiple(() => + { + Assert.That(tCachedIvnocation1.GetHashCode(), Is.EqualTo(tCachedIvnocation2.GetHashCode())); + Assert.That(tCachedIvnocation1, Is.EqualTo(tCachedIvnocation2)); + }); + } + + private static DynamicObject CreateMock(ExpressionType op) + { + var tMock = new Mock() { CallBase = true }; + object result = It.IsAny(); + tMock.Setup( + s => s.TryBinaryOperation(It.Is(b => b.Operation == op), It.IsAny(), out result) + ).Returns(true); + result = op; // Ensure result is assigned + return tMock.Object; + } + + public class OperatorTestDynObject : DynamicObject + { + private readonly ExpressionType _type; + + public OperatorTestDynObject(ExpressionType type) + { + _type = type; + } + + public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result) + { + Assert.Multiple(() => + { + Assert.That(binder.Operation, Is.EqualTo(_type)); + }); + result = _type; + return true; + } + + public override bool TryUnaryOperation(UnaryOperationBinder binder, out object result) + { + Assert.Multiple(() => + { + Assert.That(binder.Operation, Is.EqualTo(_type)); + }); + result = _type; + return true; + } + } + + private static void RunBinaryMockTests(ExpressionType type) + { + var mock = new OperatorTestDynObject(type); + var dummy = new Object(); + Dynamic.InvokeBinaryOperator(mock, type, dummy); + } + + private static void RunUnaryMockTests(ExpressionType type) + { + var mock = new OperatorTestDynObject(type); + Dynamic.InvokeUnaryOpartor(type, mock); + } + + [Test] + public void TestInvokeAdd() + { + Assert.Multiple(() => + { + Assert.That(Dynamic.InvokeBinaryOperator(1, ExpressionType.Add, 2), Is.EqualTo(3)); + }); + } + + [Test] + public void TestInvokeBasicUnaryOperatorsDynamic() + { + RunUnaryMockTests(ExpressionType.Not); + RunUnaryMockTests(ExpressionType.Negate); + RunUnaryMockTests(ExpressionType.Increment); + RunUnaryMockTests(ExpressionType.Decrement); + } + + [Test] + public void TestInvokeBasicBinaryOperatorsDynamic() + { + RunBinaryMockTests(ExpressionType.Add); + RunBinaryMockTests(ExpressionType.Subtract); + RunBinaryMockTests(ExpressionType.Divide); + RunBinaryMockTests(ExpressionType.Multiply); + RunBinaryMockTests(ExpressionType.Modulo); + RunBinaryMockTests(ExpressionType.And); + RunBinaryMockTests(ExpressionType.Or); + RunBinaryMockTests(ExpressionType.ExclusiveOr); + RunBinaryMockTests(ExpressionType.LeftShift); + RunBinaryMockTests(ExpressionType.RightShift); + RunBinaryMockTests(ExpressionType.AddAssign); + RunBinaryMockTests(ExpressionType.SubtractAssign); + RunBinaryMockTests(ExpressionType.DivideAssign); + RunBinaryMockTests(ExpressionType.MultiplyAssign); + RunBinaryMockTests(ExpressionType.ModuloAssign); + RunBinaryMockTests(ExpressionType.AndAssign); + RunBinaryMockTests(ExpressionType.OrAssign); + RunBinaryMockTests(ExpressionType.ExclusiveOrAssign); + RunBinaryMockTests(ExpressionType.LeftShiftAssign); + RunBinaryMockTests(ExpressionType.RightShiftAssign); + } + + [Test] + public void TestInvokeSubtract() + { + Assert.Multiple(() => + { + Assert.That(Dynamic.InvokeBinaryOperator(1, ExpressionType.Subtract, 2), Is.EqualTo(-1)); + }); + } + } +} diff --git a/Tests/TuplerTest.cs b/Tests/TuplerTest.cs index dcbbe54..804359b 100644 --- a/Tests/TuplerTest.cs +++ b/Tests/TuplerTest.cs @@ -1,70 +1,67 @@ -namespace Dynamitey.Tests +namespace Dynamitey.Tests { [TestFixture] public class TuplerTest - { + { [Test] public void DynamicCreateTypedTuple() { object tup = Tupler.Create(1, "2", "3", 4); - var tup2 = Tuple.Create(1, "2", "3", 4); - Assert.That(tup, Is.TypeOf(tup2.GetType())); - - Assert.That(tup, Is.EqualTo(tup2)); + Assert.Multiple(() => + { + Assert.That(tup, Is.TypeOf(tup2.GetType())); + Assert.That(tup, Is.EqualTo(tup2)); + }); } [Test] public void DynamicCreateTypedTuple8() { - object tup = Tupler.Create(1, "2", "3", 4, - 5, 6, 7, "8"); - + object tup = Tupler.Create(1, "2", "3", 4, 5, 6, 7, "8"); var tup2 = Tuple.Create(1, "2", "3", 4, 5, 6, 7, "8"); - Assert.That(tup, Is.TypeOf(tup2.GetType())); - - Assert.That(tup, Is.EqualTo(tup2)); + Assert.Multiple(() => + { + Assert.That(tup, Is.TypeOf(tup2.GetType())); + Assert.That(tup, Is.EqualTo(tup2)); + }); } [Test] public void DynamicCreateLongTypedTuple() { - object tup = Tupler.Create(1, "2", "3", 4, - 5, 6, 7, "8", "9", 10, "11", 12); - + object tup = Tupler.Create(1, "2", "3", 4, 5, 6, 7, "8", "9", 10, "11", 12); var tup2 = new Tuple>( - 1, "2", "3", 4, - 5, 6, 7, Tuple.Create("8", "9", 10, "11", 12) - ); - - Assert.That(tup, Is.TypeOf(tup2.GetType())); - - Assert.That(tup, Is.EqualTo(tup2)); + 1, "2", "3", 4, 5, 6, 7, Tuple.Create("8", "9", 10, "11", 12) + ); + + Assert.Multiple(() => + { + Assert.That(tup, Is.TypeOf(tup2.GetType())); + Assert.That(tup, Is.EqualTo(tup2)); + }); } [Test] public void DynamicTupleSize() { var tup = Tuple.Create(1, 2, 3, 4, 5); - Assert.That((object)Tupler.Size(tup), Is.EqualTo(5)); - } + } [Test] public void DynamicTupleSize8() { var tup = Tuple.Create(1, 2, 3, 4, 5, 6, 7, 8); - Assert.That((object)Tupler.Size(tup), Is.EqualTo(8)); - } + } [Test] public void DynamicTupleSize20() { var tup = Tupler.Create(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20); - Assert.That((object)Tupler.Size(tup), Is.EqualTo(20)); } @@ -73,7 +70,7 @@ public void DynamicTupleToList() { var tup = Tuple.Create(1, 2, 3, 4, 5); var exp = Enumerable.Range(1, 5).ToList(); - Assert.That((object)Tupler.ToList(tup), Is.EqualTo(exp)); + Assert.That((object)Tupler.ToList(tup), Is.EqualTo(exp)); } [Test] @@ -90,15 +87,18 @@ public void DynamicTupleToList20() var tup = Tupler.Create(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20); var exp = Enumerable.Range(1, 20).ToList(); Assert.That((object)Tupler.ToList(tup), Is.EqualTo(exp)); - } - + } + [Test] public void DynamicListToTuple() { var exp = Enumerable.Range(1, 5).ToList(); var tup = exp.ToTuple(); - Assert.That((object)Tupler.IsTuple(tup), Is.True); - Assert.That((object)Tupler.ToList(tup), Is.EqualTo(exp)); + Assert.Multiple(() => + { + Assert.That((object)Tupler.IsTuple(tup), Is.True); + Assert.That((object)Tupler.ToList(tup), Is.EqualTo(exp)); + }); } [Test] @@ -106,19 +106,25 @@ public void DynamicListToTuplet8() { var exp = Enumerable.Range(1, 8).ToList(); var tup = exp.ToTuple(); - Assert.That((object)Tupler.IsTuple(tup), Is.True); - Assert.That((object)Tupler.ToList(tup), Is.EqualTo(exp)); + Assert.Multiple(() => + { + Assert.That((object)Tupler.IsTuple(tup), Is.True); + Assert.That((object)Tupler.ToList(tup), Is.EqualTo(exp)); + }); } [Test] public void DynamicListToTuple20() - { + { var exp = Enumerable.Range(1, 20).ToList(); var tup = exp.ToTuple(); - Assert.That((object)Tupler.IsTuple(tup), Is.True); - Assert.That((object)Tupler.ToList(tup), Is.EqualTo(exp)); - } - + Assert.Multiple(() => + { + Assert.That((object)Tupler.IsTuple(tup), Is.True); + Assert.That((object)Tupler.ToList(tup), Is.EqualTo(exp)); + }); + } + [Test] public void DynamicTupleIndex() { @@ -139,5 +145,5 @@ public void DynamicTupleIndex19() var tup = Tupler.Create(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20); Assert.That((object)Tupler.Index(tup, 19), Is.EqualTo(20)); } - } -} + } +}