Skip to content

Commit

Permalink
Tests for collectable and saveable assemblies
Browse files Browse the repository at this point in the history
  • Loading branch information
jbtule committed Nov 6, 2023
1 parent 4bfb0ce commit 7962dd7
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ImpromptuInterface/src/EmitProxy/BuildProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,14 @@ private ModuleBuilder Builder
}
}
#if NET40
public void Save(string dirPath)
public void Save(string dirPath = null)
{
if (AssemblyAccess.HasFlag(AssemblyBuilderAccess.Save))
{
if (dirPath == null)
{
dirPath = "";
}
_ab.Save(Path.Combine(dirPath, $"{AssemblyName}.dll"));
}
}
Expand Down
134 changes: 134 additions & 0 deletions Tests/UnitTestImpromptuInterface/Collectable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using NUnit.Framework;
using System;
using System.Reflection.Emit;
using ImpromptuInterface.Build;
using Microsoft.CSharp.RuntimeBinder;
using System.Collections.Generic;
using System.Dynamic;
using Dynamitey.DynamicObjects;
using ImpromptuInterface;

namespace UnitTestImpromptuInterface

{


[TestFixture]
public class Collectable : Helper
{

public static BuildProxy.AssemblyMaker CollectableMaker;

[OneTimeSetUp]
public void Setup()
{
CollectableMaker = new BuildProxy.AssemblyMaker(AssemblyBuilderAccess.RunAndCollect);
}


[Test]
public void AnonPropertyTest()
{
var tAnon = new { Prop1 = "Test", Prop2 = 42L, Prop3 = Guid.NewGuid() };

var tActsLike = CollectableMaker.ActLike<ISimpeleClassProps>(tAnon);


Assert.AreEqual(tAnon.Prop1, tActsLike.Prop1);
Assert.AreEqual(tAnon.Prop2, tActsLike.Prop2);
Assert.AreEqual(tAnon.Prop3, tActsLike.Prop3);
}

[Test]
public void InformalPropTest()
{
dynamic tNew = new ExpandoObject();
tNew.Prop1 = "Test";
tNew.Prop2 = 42L;
var tActsLike = CollectableMaker.ActLikeProperties(tNew, new Dictionary<string, Type>() { { "Prop1", typeof(string) } });


Assert.AreEqual(tNew.Prop1, tActsLike.Prop1);
AssertException<RuntimeBinderException>(() => { var tTest = tActsLike.Prop2; });
}

[Test]
public void NonNestedInterfaceTest()
{
dynamic tNew = new ExpandoObject();
dynamic tNew2 = new ExpandoObject();
tNew.NameLevel1 = "one";
tNew.Nested = new ExpandoObject();
tNew.Nested2 = new Func<object>(() => tNew2);

INonNest tActsLike = CollectableMaker.ActLike(tNew);

Assert.AreEqual(tNew.NameLevel1, tActsLike.NameLevel1);
Assert.Throws<RuntimeBinderException>(() => { var tval = tActsLike.Nested; });
Assert.Throws<RuntimeBinderException>(() =>
{
var tval = tActsLike.Nested2();
;
});
}


[Test]
public void InterfaceDictionaryWrappedTest()
{

var tDictionary = new Dictionary<string, object>
{
{"Test1", 1},
{"Test2", 2L},
{"Test3",1},
{"Test4","Two"},
{"TestD", new Dictionary<string,object>()
{
{"TestA","A"},
{"TestB","B"}
}
}
};

dynamic tDynamic = CollectableMaker.Create<Dictionary, IDynamicDict>(tDictionary);
dynamic tNotDynamic = CollectableMaker.Create<Dictionary, INonDynamicDict>(tDictionary);

Assert.AreEqual(tDynamic, tNotDynamic);

Assert.AreEqual(1, tDynamic.Test1);
Assert.AreEqual(2L, tDynamic.Test2);
Assert.AreEqual(TestEnum.One, tDynamic.Test3);
Assert.AreEqual(TestEnum.Two, tDynamic.Test4);

Assert.AreEqual("A", tDynamic.TestD.TestA);
Assert.AreEqual("B", tDynamic.TestD.TestB);

Assert.AreEqual(1, tNotDynamic.Test1);
Assert.AreEqual(2L, tNotDynamic.Test2);
Assert.AreEqual(TestEnum.One, tNotDynamic.Test3);
Assert.AreEqual(TestEnum.Two, tNotDynamic.Test4);

Assert.AreEqual(typeof(Dictionary<string, object>), tNotDynamic.TestD.GetType());
Assert.AreEqual(typeof(Dictionary), tDynamic.TestD.GetType());
}



[Test]
public void NestedInterfacetest()
{
dynamic tNew = new ExpandoObject();
tNew.NameLevel1 = "one";
tNew.Nested = new ExpandoObject();
tNew.Nested.NameLevel2 = "two";

INest tActsLike = CollectableMaker.ActLike<INest>(tNew);

Assert.AreEqual(tNew.NameLevel1, tActsLike.NameLevel1);
Assert.AreEqual(tNew.Nested.NameLevel2, tActsLike.Nested.NameLevel2);

}
}

}
142 changes: 142 additions & 0 deletions Tests/UnitTestImpromptuInterface/Savable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using NUnit.Framework;
using System;
using System.Reflection.Emit;
using ImpromptuInterface.Build;
using Microsoft.CSharp.RuntimeBinder;
using System.Collections.Generic;
using System.Dynamic;
using Dynamitey.DynamicObjects;
using ImpromptuInterface;

namespace UnitTestImpromptuInterface

{

#if NET40_OR_GREATER
[TestFixture]
public class Saveable : Helper
{

public static BuildProxy.AssemblyMaker SaveableMaker;

[OneTimeSetUp]
public void Setup()
{
SaveableMaker = new BuildProxy.AssemblyMaker(AssemblyBuilderAccess.RunAndSave, "II_Generated");
}

[OneTimeTearDown]
public void TearDown()
{
SaveableMaker.Save();
}

[Test]
public void AnonPropertyTest()
{
var tAnon = new { Prop1 = "Test", Prop2 = 42L, Prop3 = Guid.NewGuid() };

var tActsLike = SaveableMaker.ActLike<ISimpeleClassProps>(tAnon);


Assert.AreEqual(tAnon.Prop1, tActsLike.Prop1);
Assert.AreEqual(tAnon.Prop2, tActsLike.Prop2);
Assert.AreEqual(tAnon.Prop3, tActsLike.Prop3);
}

[Test]
public void InformalPropTest()
{
dynamic tNew = new ExpandoObject();
tNew.Prop1 = "Test";
tNew.Prop2 = 42L;
var tActsLike = SaveableMaker.ActLikeProperties(tNew, new Dictionary<string, Type>() { { "Prop1", typeof(string) } });


Assert.AreEqual(tNew.Prop1, tActsLike.Prop1);
AssertException<RuntimeBinderException>(() => { var tTest = tActsLike.Prop2; });
}

[Test]
public void NonNestedInterfaceTest()
{
dynamic tNew = new ExpandoObject();
dynamic tNew2 = new ExpandoObject();
tNew.NameLevel1 = "one";
tNew.Nested = new ExpandoObject();
tNew.Nested2 = new Func<object>(() => tNew2);

INonNest tActsLike = SaveableMaker.ActLike(tNew);

Assert.AreEqual(tNew.NameLevel1, tActsLike.NameLevel1);
Assert.Throws<RuntimeBinderException>(() => { var tval = tActsLike.Nested; });
Assert.Throws<RuntimeBinderException>(() =>
{
var tval = tActsLike.Nested2();
;
});
}


[Test]
public void InterfaceDictionaryWrappedTest()
{

var tDictionary = new Dictionary<string, object>
{
{"Test1", 1},
{"Test2", 2L},
{"Test3",1},
{"Test4","Two"},
{"TestD", new Dictionary<string,object>()
{
{"TestA","A"},
{"TestB","B"}
}
}
};

dynamic tDynamic = SaveableMaker.Create<Dictionary, IDynamicDict>(tDictionary);
dynamic tNotDynamic = SaveableMaker.Create<Dictionary, INonDynamicDict>(tDictionary);

Assert.AreEqual(tDynamic, tNotDynamic);

Assert.AreEqual(1, tDynamic.Test1);
Assert.AreEqual(2L, tDynamic.Test2);
Assert.AreEqual(TestEnum.One, tDynamic.Test3);
Assert.AreEqual(TestEnum.Two, tDynamic.Test4);

Assert.AreEqual("A", tDynamic.TestD.TestA);
Assert.AreEqual("B", tDynamic.TestD.TestB);

Assert.AreEqual(1, tNotDynamic.Test1);
Assert.AreEqual(2L, tNotDynamic.Test2);
Assert.AreEqual(TestEnum.One, tNotDynamic.Test3);
Assert.AreEqual(TestEnum.Two, tNotDynamic.Test4);

Assert.AreEqual(typeof(Dictionary<string, object>), tNotDynamic.TestD.GetType());
Assert.AreEqual(typeof(Dictionary), tDynamic.TestD.GetType());
}


[Test]
public void NestedInterfacetest()
{
dynamic tNew = new ExpandoObject();
tNew.NameLevel1 = "one";
tNew.Nested = new ExpandoObject();
tNew.Nested.NameLevel2 = "two";

INest tActsLike = SaveableMaker.ActLike<INest>(tNew);

Assert.AreEqual(tNew.NameLevel1, tActsLike.NameLevel1);
Assert.AreEqual(tNew.Nested.NameLevel2, tActsLike.Nested.NameLevel2);

}
}



#endif

}

0 comments on commit 7962dd7

Please sign in to comment.