public async Task GivenPipeOnSuperClass_WhenResolvingToInterface_ItShouldReturnSuperType() { // pre-arrange var instanceClassA = new TestClassA(); var instanceSuperClassB = new TestSuperClassB(); // arrange var builder = new SemanticBuilder(); builder.InstallPipe<TestClassA, IEnumerable<TestSuperClassB>>( (@as, broker) => { Assert.AreSame(instanceClassA, @as); return new[] { instanceSuperClassB }; }); var semanticBroker = builder.CreateBroker(); // act var returnedInstance = await semanticBroker.On(instanceClassA).Output<IEnumerable<ITestInterface>>(); // assert var result = returnedInstance.Single(); Assert.AreSame(instanceSuperClassB, result); }
public void Should_return_actual_objects_HashCode() { const string testString = "Hello World!"; var @class = new TestClassA { TestValue = testString }; var reader = new PropertyReader<TestClassA>("TestValue"); reader.GetItemHashCode(@class).ShouldEqual(testString.GetHashCode()); }
public void MapTest() { ConfigurationMapper<TestClassA, TestClassB> target = new ConfigurationMapper<TestClassA, TestClassB>(); TestClassA from = new TestClassA() { Name = "test" }; TestClassB to = new TestClassB(); target.Map(from, to); Assert.AreEqual(from.Name, to.InnerClassName); }
public void MapAll_ValidInput_CallsMapper() { var mapperMock = A.Fake<IMapper<TestClassA, TestClassB>>(); var target = new TestClassA[10]; var output = new TestClassB[10]; A.CallTo(() => mapperMock.MapMultiple(target)).Returns(output); IEnumerable<TestClassB> result = target.MapAll(mapperMock); Assert.AreEqual(output, result); }
public void ConvertTestComplex() { var target = new PropertyConverter(); var source = new TestClassA {InnerClass = new TestClassC()}; const string expected = "test"; source.InnerClass.Name = expected; var fromProperty = typeof (TestClassA).GetProperty("InnerClass"); var targetObject = new TestClassB {InnerClassName = "wrongstring"}; var toProperty = typeof (TestClassB).GetProperty("InnerClassName"); target.Convert(source, fromProperty, targetObject, toProperty); Assert.AreEqual(expected, targetObject.InnerClassName); Assert.AreEqual(expected, source.InnerClass.Name); }
public void ConvertTestSimple() { var target = new PropertyConverter(); var source = new TestClassA(); const string expected = "test"; source.Name = expected; var fromProperty = typeof (TestClassA).GetProperty("Name"); var targetObject = new TestClassB(); targetObject.Name = "wrongstring"; var toProperty = typeof (TestClassB).GetProperty("Name"); target.Convert(source, fromProperty, targetObject, toProperty); Assert.AreEqual(expected, targetObject.Name); Assert.AreEqual(expected, source.Name); }
public void ConvertTestComplex() { PropertyConverter target = new PropertyConverter(); TestClassA source = new TestClassA(); source.InnerClass = new TestClassC(); var expected = "test"; source.InnerClass.Name = expected; PropertyInfo fromProperty = typeof(TestClassA).GetProperty("InnerClass"); TestClassB targetObject = new TestClassB(); targetObject.InnerClassName = "wrongstring"; PropertyInfo toProperty = typeof(TestClassB).GetProperty("InnerClassName"); target.Convert(source, fromProperty, targetObject, toProperty); Assert.AreEqual(expected, targetObject.InnerClassName); Assert.AreEqual(expected, source.InnerClass.Name); }
public void TestCopyToExistingObject() { TestClassA a = new TestClassA() { TestClassC = new TestClassC() { MyProperty = "string" }, List = new List <TestClassC>() { new TestClassC() { MyProperty = "cstring" }, new TestClassC() { MyProperty = "cstring" }, }, MyProperty = "string", Array = new[] { new TestClassC() { MyProperty = "string", Obj = new TestClassD() { MyProperty = "sstring" } }, new TestClassC() { MyProperty = "string", Obj = new TestClassD() { MyProperty = "sstring" } }, } }; var b = new TestClassB(); a.MapTo(b); Assert.Equal(a.MyProperty, b.MyProperty); Assert.Equal(a.TestClassC.MyProperty, b.TestClassC.MyProperty); Assert.Equal(a.List.Count, b.List.Count); Assert.Equal(a.Array.Length, b.Array.Length); }
public void TestField() { ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a"); ParameterExpression b = Expression.Parameter(typeof(int), "b"); Expression <Func <TestClassA, int, int> > exp = Expression.Lambda <Func <TestClassA, int, int> >(Expression.MultiplyAssign(Expression.MakeMemberAccess(a, typeof(TestClassA).GetField("IntField")), b), a, b); var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences); var o = new TestClassA(); Assert.AreEqual(0, f(o, 0)); Assert.AreEqual(0, o.IntField); o.IntField = 1; Assert.AreEqual(2, f(o, 2)); Assert.AreEqual(2, o.IntField); o.IntField = -2; Assert.AreEqual(6, f(o, -3)); Assert.AreEqual(6, o.IntField); o.IntField = -2; Assert.AreEqual(-20, f(o, 10)); Assert.AreEqual(-20, o.IntField); o.IntField = 2000000000; unchecked { Assert.AreEqual(2000000000 * 2000000000, f(o, 2000000000)); Assert.AreEqual(2000000000 * 2000000000, o.IntField); } Assert.AreEqual(0, f(null, 1)); f = LambdaCompiler.Compile(exp, CompilerOptions.None); o = new TestClassA(); Assert.AreEqual(0, f(o, 0)); Assert.AreEqual(0, o.IntField); o.IntField = 1; Assert.AreEqual(2, f(o, 2)); Assert.AreEqual(2, o.IntField); o.IntField = -2; Assert.AreEqual(6, f(o, -3)); Assert.AreEqual(6, o.IntField); o.IntField = -2; Assert.AreEqual(-20, f(o, 10)); Assert.AreEqual(-20, o.IntField); o.IntField = 2000000000; unchecked { Assert.AreEqual(2000000000 * 2000000000, f(o, 2000000000)); Assert.AreEqual(2000000000 * 2000000000, o.IntField); } Assert.Throws <NullReferenceException>(() => f(null, 1)); }
public void TestCheckedNullableUnsigned() { ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a"); ParameterExpression b = Expression.Parameter(typeof(uint?), "b"); Expression <Func <TestClassA, uint?, uint?> > exp = Expression.Lambda <Func <TestClassA, uint?, uint?> >(Expression.SubtractAssignChecked(Expression.MakeMemberAccess(a, typeof(TestClassA).GetField("NullableUIntField")), b), a, b); var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences); var o = new TestClassA { NullableUIntField = 0 }; Assert.AreEqual(0, f(o, 0)); Assert.AreEqual(0, o.NullableUIntField); o.NullableUIntField = 4000000000; Assert.AreEqual(3000000000, f(o, 1000000000)); Assert.AreEqual(3000000000, o.NullableUIntField); o.NullableUIntField = 1; Assert.Throws <OverflowException>(() => f(o, 2)); Assert.IsNull(f(null, 1)); o.NullableUIntField = null; Assert.IsNull(f(o, 2)); Assert.IsNull(o.NullableUIntField); o.NullableUIntField = 1; Assert.IsNull(f(o, null)); Assert.IsNull(o.NullableUIntField); Assert.IsNull(f(o, null)); Assert.IsNull(o.NullableUIntField); f = LambdaCompiler.Compile(exp, CompilerOptions.None); o = new TestClassA { NullableUIntField = 0 }; Assert.AreEqual(0, f(o, 0)); Assert.AreEqual(0, o.NullableUIntField); o.NullableUIntField = 4000000000; Assert.AreEqual(3000000000, f(o, 1000000000)); Assert.AreEqual(3000000000, o.NullableUIntField); o.NullableUIntField = 1; Assert.Throws <OverflowException>(() => f(o, 2)); Assert.Throws <NullReferenceException>(() => f(null, 1)); o.NullableUIntField = null; Assert.IsNull(f(o, 2)); Assert.IsNull(o.NullableUIntField); o.NullableUIntField = 1; Assert.IsNull(f(o, null)); Assert.IsNull(o.NullableUIntField); Assert.IsNull(f(o, null)); Assert.IsNull(o.NullableUIntField); }
public void TestAssignArrayWithExtend3() { Expression <Func <TestClassA, int[]> > path1 = a => a.IntArray; Expression body = Expression.Assign(Expression.ArrayAccess(path1.Body, Expression.Constant(1)), Expression.Constant(-123)); Expression <Func <TestClassA, int> > exp = Expression.Lambda <Func <TestClassA, int> >(body, path1.Parameters); var f = LambdaCompiler.Compile(exp, CompilerOptions.All); var o = new TestClassA { IntArray = new[] { 12 } }; Assert.AreEqual(-123, f(o)); Assert.IsNotNull(o.IntArray); Assert.AreEqual(2, o.IntArray.Length); Assert.AreEqual(12, o.IntArray[0]); Assert.AreEqual(-123, o.IntArray[1]); }
public void Set_ProvideLocalizedContentArray_SetsProperty() { var testClass = new TestClassA(); var localizedContent = new[] { new LocalizedContent(DEFAULT_LANGUAGE, ANY_STRING_1), new LocalizedContent(OTHER_LANGUAGE, ANY_STRING_2) }; testClass.Set(i => i.PropertyA, localizedContent); var localized = testClass.Localize(DEFAULT_LANGUAGE); Assert.Equal(ANY_STRING_1, localized.PropertyA); }
public void Set_ProvideLocalizedContentDictionary_SetsProperty() { var testClass = new TestClassA(); var localizedContent = new Dictionary <string, string> { { DEFAULT_LANGUAGE, ANY_STRING_1 }, { OTHER_LANGUAGE, ANY_STRING_2 } }; testClass.Set(i => i.PropertyA, localizedContent); var localized = testClass.Localize(DEFAULT_LANGUAGE); Assert.Equal(ANY_STRING_1, localized.PropertyA); }
public void PropertyConverterNegativeTest() { PropertyConverter target = new PropertyConverter(); var source = new TestClassA(); var sourceProperty = source.GetType().GetProperty("InnerClass"); var result = new TestClassB(); var resultProperty = result.GetType().GetProperty("InnerClassCode"); source.InnerClass = new TestClassC(); target.Convert( source, sourceProperty, target, resultProperty, false); }
public void TestConditional2() { Expression <Func <TestClassA, string> > path = a => a.B.S; Expression <Func <TestClassA, bool> > condition = a => a.S == "zzz"; Expression assign = Expression.Assign(path.Body, Expression.Constant("qxx")); Expression test = new ParameterReplacer(condition.Parameters[0], path.Parameters[0]).Visit(condition.Body); Expression <Action <TestClassA> > exp = Expression.Lambda <Action <TestClassA> >(Expression.IfThenElse(test, assign, Expression.Default(typeof(void))), path.Parameters); var action = LambdaCompiler.Compile(exp, CompilerOptions.All); var o = new TestClassA { S = "zzz" }; action(o); Assert.IsNotNull(o.B); Assert.AreEqual(o.B.S, "qxx"); }
public async Task GivenRegistryWithMultiplePipes_WhenResolvingFromAToD_ItShouldChainAllThePipes() { for (int counter = 0; counter < 15; counter++) { // pre-arrange var instanceClassA = new TestClassA(); var instanceClassB = new TestClassB(); var instanceClassC = new TestClassC(); var instanceClassD = new TestClassD(); // arrange var semanticBuilder = new SemanticBuilder(); var enrollmentList = new List <Action> { () => semanticBuilder.InstallPipe <TestClassA, TestClassB>((a, innerBroker) => { Assert.AreSame(instanceClassA, a); return(instanceClassB); }), () => semanticBuilder.InstallPipe <TestClassB, TestClassC>((b, innerBroker) => { Assert.AreSame(instanceClassB, b); return(instanceClassC); }), () => semanticBuilder.InstallPipe <TestClassC, TestClassD>((c, innerBroker) => { Assert.AreSame(instanceClassC, c); return(instanceClassD); }), }; var enrollmentActions = enrollmentList.OrderBy(x => Guid.NewGuid()); foreach (var enrollmentAction in enrollmentActions) { enrollmentAction(); } ISemanticBroker broker = semanticBuilder.CreateBroker(); // act var solvedExecution = await broker.On(instanceClassA).Output <TestClassD>(); // assert Assert.AreEqual(instanceClassD, solvedExecution); } }
public void TestNullable() { ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a"); ParameterExpression b = Expression.Parameter(typeof(int?), "b"); Expression<Func<TestClassA, int?, int?>> exp = Expression.Lambda<Func<TestClassA, int?, int?>>(Expression.AndAssign(Expression.MakeIndex(Expression.MakeMemberAccess(a, typeof(TestClassA).GetField("NullableIntArray")), typeof(NullableIntArray).GetProperty("Item"), new[] {Expression.Constant("zzz"), Expression.Constant(1)}), b), a, b); var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences); var o = new TestClassA {NullableIntArray = new NullableIntArray()}; o.NullableIntArray["zzz", 1] = 0; Assert.AreEqual(0, f(o, 123)); Assert.AreEqual(0, o.NullableIntArray["zzz", 1]); o.NullableIntArray["zzz", 1] = 5; Assert.AreEqual(1, f(o, 3)); Assert.AreEqual(1, o.NullableIntArray["zzz", 1]); o.NullableIntArray["zzz", 1] = 17235476; Assert.AreEqual(17235476 & 73172563, f(o, 73172563)); Assert.AreEqual(17235476 & 73172563, o.NullableIntArray["zzz", 1]); Assert.IsNull(f(null, 1)); o.NullableIntArray["zzz", 1] = null; Assert.IsNull(f(o, 2)); Assert.IsNull(o.NullableIntArray["zzz", 1]); o.NullableIntArray["zzz", 1] = 1; Assert.IsNull(f(o, null)); Assert.IsNull(o.NullableIntArray["zzz", 1]); Assert.IsNull(f(o, null)); Assert.IsNull(o.NullableIntArray["zzz", 1]); f = LambdaCompiler.Compile(exp, CompilerOptions.None); o = new TestClassA {NullableIntArray = new NullableIntArray()}; o.NullableIntArray["zzz", 1] = 0; Assert.AreEqual(0, f(o, 123)); Assert.AreEqual(0, o.NullableIntArray["zzz", 1]); o.NullableIntArray["zzz", 1] = 5; Assert.AreEqual(1, f(o, 3)); Assert.AreEqual(1, o.NullableIntArray["zzz", 1]); o.NullableIntArray["zzz", 1] = 17235476; Assert.AreEqual(17235476 & 73172563, f(o, 73172563)); Assert.AreEqual(17235476 & 73172563, o.NullableIntArray["zzz", 1]); Assert.Throws<NullReferenceException>(() => f(null, 1)); o.NullableIntArray["zzz", 1] = null; Assert.IsNull(f(o, 2)); Assert.IsNull(o.NullableIntArray["zzz", 1]); o.NullableIntArray["zzz", 1] = 1; Assert.IsNull(f(o, null)); Assert.IsNull(o.NullableIntArray["zzz", 1]); Assert.IsNull(f(o, null)); Assert.IsNull(o.NullableIntArray["zzz", 1]); }
public void Test4() { ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a"); ParameterExpression b = Expression.Parameter(typeof(int), "b"); Expression<Func<TestClassA, int, uint>> exp = Expression.Lambda<Func<TestClassA, int, uint>>(Expression.RightShiftAssign(Expression.MakeMemberAccess(a, typeof(TestClassA).GetProperty("UIntProp")), b), a, b); var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences); var o = new TestClassA {UIntProp = 0}; Assert.AreEqual(0, f(o, 0)); Assert.AreEqual(0, o.UIntProp); o.UIntProp = 0; Assert.AreEqual(0, f(o, 10)); Assert.AreEqual(0, o.UIntProp); o.UIntProp = 1024; Assert.AreEqual(1, f(o, 10)); Assert.AreEqual(1, o.UIntProp); o.UIntProp = 1023; Assert.AreEqual(0, f(o, 10)); Assert.AreEqual(0, o.UIntProp); o.UIntProp = 3; Assert.AreEqual(1, f(o, 1)); Assert.AreEqual(1, o.UIntProp); o.UIntProp = 4000000000; Assert.AreEqual(2000000000, f(o, 1)); Assert.AreEqual(2000000000, o.UIntProp); Assert.AreEqual(0, f(null, 1)); f = LambdaCompiler.Compile(exp, CompilerOptions.None); o = new TestClassA {UIntProp = 0}; Assert.AreEqual(0, f(o, 0)); Assert.AreEqual(0, o.UIntProp); o.UIntProp = 0; Assert.AreEqual(0, f(o, 10)); Assert.AreEqual(0, o.UIntProp); o.UIntProp = 1024; Assert.AreEqual(1, f(o, 10)); Assert.AreEqual(1, o.UIntProp); o.UIntProp = 1023; Assert.AreEqual(0, f(o, 10)); Assert.AreEqual(0, o.UIntProp); o.UIntProp = 3; Assert.AreEqual(1, f(o, 1)); Assert.AreEqual(1, o.UIntProp); o.UIntProp = 4000000000; Assert.AreEqual(2000000000, f(o, 1)); Assert.AreEqual(2000000000, o.UIntProp); Assert.Throws<NullReferenceException>(() => f(null, 1)); }
public async Task GivenRegistryWithMultiplePipes_WhenResolvingFromAToD_ItShouldChainAllThePipes() { for (int counter = 0; counter < 15; counter++) { // pre-arrange var instanceClassA = new TestClassA(); var instanceClassB = new TestClassB(); var instanceClassC = new TestClassC(); var instanceClassD = new TestClassD(); // arrange var semanticBuilder = new SemanticBuilder(); var enrollmentList = new List<Action> { () => semanticBuilder.InstallPipe<TestClassA, TestClassB>((a, innerBroker) => { Assert.AreSame(instanceClassA, a); return instanceClassB; }), () => semanticBuilder.InstallPipe<TestClassB, TestClassC>((b, innerBroker) => { Assert.AreSame(instanceClassB, b); return instanceClassC; }), () => semanticBuilder.InstallPipe<TestClassC, TestClassD>((c, innerBroker) => { Assert.AreSame(instanceClassC, c); return instanceClassD; }), }; var enrollmentActions = enrollmentList.OrderBy(x=>Guid.NewGuid()); foreach (var enrollmentAction in enrollmentActions) { enrollmentAction(); } ISemanticBroker broker = semanticBuilder.CreateBroker(); // act var solvedExecution = await broker.On(instanceClassA).Output<TestClassD>(); // assert Assert.AreEqual(instanceClassD, solvedExecution); } }
public void TestEnumableProperties() { TestClassA a = new TestClassA() { TestClassC = new TestClassC() { MyProperty = "string" }, List = new List <TestClassC>() { new TestClassC() { MyProperty = "cstring" }, new TestClassC() { MyProperty = "cstring" }, }, MyProperty = "string", Array = new[] { new TestClassC() { MyProperty = "string", Obj = new TestClassD() { MyProperty = "sstring" } }, new TestClassC() { MyProperty = "string", Obj = new TestClassD() { MyProperty = "sstring" } }, } }; var b = a.Map <TestClassA, TestClassB>(); Assert.Equal(a.MyProperty, b.MyProperty); Assert.Equal(a.TestClassC.MyProperty, b.TestClassC.MyProperty); Assert.Equal(a.List.Count, b.List.Count); Assert.Equal(a.Array.Length, b.Array.Length); }
public void TestIntx() { ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a"); ParameterExpression b = Expression.Parameter(typeof(int), "b"); BinaryExpression addAssign = Expression.AddAssign(Expression.ArrayAccess(Expression.MakeMemberAccess(a, typeof(TestClassA).GetProperty("IntArray")), Expression.Constant(0)), b); Expression <Func <TestClassA, int, int> > exp = Expression.Lambda <Func <TestClassA, int, int> >(Expression.Block(addAssign, Expression.ArrayAccess(Expression.MakeMemberAccess(a, typeof(TestClassA).GetProperty("IntArray")), Expression.Constant(0))), a, b); var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences); var o = new TestClassA { IntArray = new[] { 0 } }; Assert.AreEqual(0, f(o, 0)); Assert.AreEqual(0, o.IntArray[0]); o.IntArray[0] = 1; Assert.AreEqual(3, f(o, 2)); Assert.AreEqual(3, o.IntArray[0]); o.IntArray[0] = -1; Assert.AreEqual(1, f(o, 2)); Assert.AreEqual(1, o.IntArray[0]); o.IntArray[0] = 2000000000; unchecked { Assert.AreEqual(2000000000 + 2000000000, f(o, 2000000000)); Assert.AreEqual(2000000000 + 2000000000, o.IntArray[0]); } Assert.AreEqual(0, f(null, 1)); f = LambdaCompiler.Compile(exp, CompilerOptions.None); o = new TestClassA { IntArray = new[] { 0 } }; Assert.AreEqual(0, f(o, 0)); Assert.AreEqual(0, o.IntArray[0]); o.IntArray[0] = 1; Assert.AreEqual(3, f(o, 2)); Assert.AreEqual(3, o.IntArray[0]); o.IntArray[0] = -1; Assert.AreEqual(1, f(o, 2)); Assert.AreEqual(1, o.IntArray[0]); o.IntArray[0] = 2000000000; unchecked { Assert.AreEqual(2000000000 + 2000000000, f(o, 2000000000)); Assert.AreEqual(2000000000 + 2000000000, o.IntArray[0]); } Assert.Throws <NullReferenceException>(() => f(null, 1)); }
public void TestAssignArrayWithExtend13() { Expression <Func <TestClassA, DateTime[]> > path1 = a => a.DateTimeArray; Expression <Func <DateTime> > path2 = () => MyBirthDate; Expression body = Expression.Assign(Expression.ArrayAccess(path1.Body, Expression.Constant(1)), path2.Body); Expression <Action <TestClassA> > exp = Expression.Lambda <Action <TestClassA> >(body, path1.Parameters); var f = LambdaCompiler.Compile(exp, CompilerOptions.All); var o = new TestClassA(); f(o); Assert.IsNotNull(o.DateTimeArray); Assert.AreEqual(2, o.DateTimeArray.Length); Assert.AreEqual(MyBirthDate, o.DateTimeArray[1]); f = LambdaCompiler.Compile(exp, CompilerOptions.None); f(o); Assert.AreEqual(MyBirthDate, o.DateTimeArray[1]); }
public void TestInvoke1() { Expression <Func <TestClassA, int> > exp = o => func(o.Y, o.Z); var a = new TestClassA { Y = 1, Z = 2 }; Console.WriteLine("Compile"); var ethalon = MeasureSpeed(exp.Compile(), a, 10000000, null); Console.WriteLine("Sharp"); MeasureSpeed(Func4, a, 10000000, ethalon); Console.WriteLine("GroboCompile without checking"); MeasureSpeed(LambdaCompiler.Compile(exp, CompilerOptions.None), a, 10000000, ethalon); Console.WriteLine("GroboCompile with checking"); MeasureSpeed(LambdaCompiler.Compile(exp, CompilerOptions.All), a, 10000000, ethalon); }
public void TestCheckedSigned() { ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a"); ParameterExpression b = Expression.Parameter(typeof(int), "b"); Expression <Func <TestClassA, int, int> > exp = Expression.Lambda <Func <TestClassA, int, int> >(Expression.MultiplyAssignChecked(Expression.MakeIndex(Expression.MakeMemberAccess(a, typeof(TestClassA).GetProperty("IntArray")), typeof(IntArray).GetProperty("Item"), new[] { Expression.Constant("zzz"), Expression.Constant(1) }), b), a, b); var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences); var o = new TestClassA { IntArray = new IntArray() }; o.IntArray["zzz", 1] = 0; Assert.AreEqual(0, f(o, 0)); Assert.AreEqual(0, o.IntArray["zzz", 1]); o.IntArray["zzz", 1] = 1; Assert.AreEqual(2, f(o, 2)); Assert.AreEqual(2, o.IntArray["zzz", 1]); o.IntArray["zzz", 1] = -2; Assert.AreEqual(6, f(o, -3)); Assert.AreEqual(6, o.IntArray["zzz", 1]); o.IntArray["zzz", 1] = -2; Assert.AreEqual(-20, f(o, 10)); Assert.AreEqual(-20, o.IntArray["zzz", 1]); o.IntArray["zzz", 1] = 2000000000; Assert.Throws <OverflowException>(() => f(o, 2000000000)); Assert.AreEqual(0, f(null, 1)); f = LambdaCompiler.Compile(exp, CompilerOptions.None); o = new TestClassA { IntArray = new IntArray() }; o.IntArray["zzz", 1] = 0; Assert.AreEqual(0, f(o, 0)); Assert.AreEqual(0, o.IntArray["zzz", 1]); o.IntArray["zzz", 1] = 1; Assert.AreEqual(2, f(o, 2)); Assert.AreEqual(2, o.IntArray["zzz", 1]); o.IntArray["zzz", 1] = -2; Assert.AreEqual(6, f(o, -3)); Assert.AreEqual(6, o.IntArray["zzz", 1]); o.IntArray["zzz", 1] = -2; Assert.AreEqual(-20, f(o, 10)); Assert.AreEqual(-20, o.IntArray["zzz", 1]); o.IntArray["zzz", 1] = 2000000000; Assert.Throws <OverflowException>(() => f(o, 2000000000)); Assert.Throws <NullReferenceException>(() => f(null, 1)); }
public void Map_MapperTarget_MapsCorrectly() { string expectedName = "Test Name"; int expectedCode = 412; var a = new TestClassA {Code = expectedCode, Name = expectedName}; var d = new TestClassD(); var target = new AttributeMapper<TestClassA, TestClassD>(); target.Map(a, d); Assert.AreEqual(expectedName, d.SomeOtherName); Assert.AreEqual(expectedCode, d.AnotherCode); Assert.AreEqual(expectedName, a.Name); Assert.AreEqual(expectedCode, a.Code); }
public void ConvertTestSimple() { PropertyConverter target = new PropertyConverter(); TestClassA source = new TestClassA(); var expected = "test"; source.Name = expected; PropertyInfo fromProperty = typeof(TestClassA).GetProperty("Name"); TestClassB targetObject = new TestClassB(); targetObject.Name = "wrongstring"; PropertyInfo toProperty = typeof(TestClassB).GetProperty("Name"); target.Convert(source, fromProperty, targetObject, toProperty); Assert.AreEqual(expected, targetObject.Name); Assert.AreEqual(expected, source.Name); }
public void TestSubLambda1WithGarbageCollecting() { Expression <Func <TestClassA, bool> > exp = o => o.ArrayB.Any(b => b.S == o.S); var a = new TestClassA { S = "zzz", ArrayB = new[] { new TestClassB { S = "zzz" }, } }; Console.WriteLine("GroboCompile without checking"); stop = false; var thread = new Thread(Collect); thread.Start(); MeasureSpeed(LambdaCompiler.Compile(exp, CompilerOptions.None), a, 100000000, null); stop = true; }
public void TestAssignArrayWithExtend11() { Expression <Func <TestClassA, bool[]> > path = a => a.BoolArray; Expression <Func <TestClassA, bool> > condition = a => a.X > 0; Expression body = Expression.Assign(Expression.ArrayAccess(path.Body, Expression.Constant(1)), new ParameterReplacer(condition.Parameters[0], path.Parameters[0]).Visit(condition.Body)); Expression <Action <TestClassA> > exp = Expression.Lambda <Action <TestClassA> >(body, path.Parameters); var f = LambdaCompiler.Compile(exp, CompilerOptions.All); var o = new TestClassA(); f(o); Assert.IsNotNull(o.BoolArray); Assert.AreEqual(2, o.BoolArray.Length); Assert.AreEqual(false, o.BoolArray[1]); o.X = 1; f(o); Assert.AreEqual(true, o.BoolArray[1]); }
public void TestAssignArrayWithExtend1() { Expression <Func <TestClassA, string> > path1 = a => a.ArrayB[1].S; Expression body = Expression.Assign(path1.Body, Expression.Constant("zzz")); Expression <Func <TestClassA, string> > exp = Expression.Lambda <Func <TestClassA, string> >(body, path1.Parameters); var f = LambdaCompiler.Compile(exp, CompilerOptions.All); var o = new TestClassA { ArrayB = new[] { new TestClassB { S = "qxx" }, } }; Assert.AreEqual("zzz", f(o)); Assert.IsNotNull(o.ArrayB); Assert.AreEqual(2, o.ArrayB.Length); Assert.AreEqual("zzz", o.ArrayB[1].S); Assert.AreEqual("qxx", o.ArrayB[0].S); }
public void TestAssignArrayWithExtend2() { Expression <Func <TestClassA, string> > path1 = a => a.ArrayB[1].C.ArrayD[2].ArrayE[3].S; Expression body = Expression.Assign(path1.Body, Expression.Constant("zzz")); Expression <Func <TestClassA, string> > exp = Expression.Lambda <Func <TestClassA, string> >(body, path1.Parameters); var f = LambdaCompiler.Compile(exp, CompilerOptions.All); var o = new TestClassA(); Assert.AreEqual("zzz", f(o)); Assert.IsNotNull(o.ArrayB); Assert.AreEqual(2, o.ArrayB.Length); Assert.IsNotNull(o.ArrayB[1].C); Assert.IsNotNull(o.ArrayB[1].C.ArrayD); Assert.AreEqual(3, o.ArrayB[1].C.ArrayD.Length); Assert.IsNotNull(o.ArrayB[1].C.ArrayD[2].ArrayE); Assert.AreEqual(4, o.ArrayB[1].C.ArrayD[2].ArrayE.Length); Assert.AreEqual("zzz", o.ArrayB[1].C.ArrayD[2].ArrayE[3].S); }
private static WeakReference DoTestConstsAreFreedAfterGarbageCollecting1() { var a = new TestClassA { S = "qxx" }; var result = new WeakReference(a); Expression <Func <TestClassA, string> > path = o => o.S; var exp = Expression.Lambda <Func <TestClassA, bool> >(Expression.Equal(path.Body, Expression.MakeMemberAccess(Expression.Constant(a), typeof(TestClassA).GetProperty("S"))), path.Parameters); var f = LambdaCompiler.Compile(exp, CompilerOptions.All); Assert.IsTrue(f(new TestClassA { S = "qxx" })); Assert.IsFalse(f(new TestClassA { S = "qzz" })); return(result); }
public void LocalizeItem_LocalizationDepthIsShallow_DoesNotLocalizeChildred() { var localizedContent = GetLocalizedContent(); var testClass = new TestClassA { ChildA = new TestClassA { PropertyA = localizedContent, PropertyB = localizedContent } }; var localized = testClass.Localize(DEFAULT_LANGUAGE); Assert.NotEqual(ANY_STRING_1, localized.ChildA.PropertyA); Assert.NotEqual(ANY_STRING_1, localized.ChildA.PropertyB); }
public void ConvertTestComplex() { var target = new PropertyConverter(); var source = new TestClassA { InnerClass = new TestClassC() }; const string expected = "test"; source.InnerClass.Name = expected; var fromProperty = typeof(TestClassA).GetProperty("InnerClass"); var targetObject = new TestClassB { InnerClassName = "wrongstring" }; var toProperty = typeof(TestClassB).GetProperty("InnerClassName"); target.Convert(source, fromProperty, targetObject, toProperty); Assert.AreEqual(expected, targetObject.InnerClassName); Assert.AreEqual(expected, source.InnerClass.Name); }
public void TestAssignArrayWithExtend8() { Expression <Func <TestClassA, string[]> > path = a => a.ArrayB[0].C.ArrayD[0].StringArray; Expression body = Expression.Assign(Expression.ArrayAccess(path.Body, Expression.Constant(1)), Expression.Constant("zzz")); Expression <Func <TestClassA, string> > exp = Expression.Lambda <Func <TestClassA, string> >(body, path.Parameters); var f = LambdaCompiler.Compile(exp, CompilerOptions.All); var o = new TestClassA(); Assert.AreEqual("zzz", f(o)); Assert.IsNotNull(o.ArrayB); Assert.AreEqual(1, o.ArrayB.Length); Assert.IsNotNull(o.ArrayB[0].C); Assert.IsNotNull(o.ArrayB[0].C.ArrayD); Assert.AreEqual(1, o.ArrayB[0].C.ArrayD.Length); Assert.IsNotNull(o.ArrayB[0].C.ArrayD[0].StringArray); Assert.AreEqual(2, o.ArrayB[0].C.ArrayD[0].StringArray.Length); Assert.IsNull(o.ArrayB[0].C.ArrayD[0].StringArray[0]); Assert.AreEqual("zzz", o.ArrayB[0].C.ArrayD[0].StringArray[1]); }
private void Run(object param) { var f = (Func <TestClassA, List <string> >)param; for (int i = 0; i < 1000000; ++i) { string s = Guid.NewGuid().ToString(); var a = new TestClassA { ArrayB = new[] { new TestClassB { S = s } } }; var list = f(a); if (list.Count == 0 || list[0] != s) { wasBug = true; } } }
public void TestAssignToDictionary2() { var parameter = Expression.Parameter(typeof(TestClassA)); var exp = Expression.Lambda <Func <TestClassA, string> >(Expression.Assign(Expression.Property(Expression.Call(Expression.Property(parameter, "Dict"), typeof(Dictionary <string, TestClassB>).GetProperty("Item").GetGetMethod(), new[] { Expression.Constant("zzz") }), "S"), Expression.Constant("2")), parameter); var f = LambdaCompiler.Compile(exp, CompilerOptions.All); var a = new TestClassA { Dict = new Dictionary <string, TestClassB> { { "zzz", new TestClassB { S = "1" } } } }; Assert.AreEqual("2", f(a)); Assert.AreEqual("2", a.Dict["zzz"].S); a = new TestClassA(); Assert.AreEqual("2", f(a)); Assert.AreEqual("2", a.Dict["zzz"].S); }
public void TestSubLambda1() { Expression <Func <TestClassA, bool> > exp = o => o.ArrayB.Any(b => b.S == o.S); var a = new TestClassA { S = "zzz", ArrayB = new[] { new TestClassB { S = "zzz" }, } }; Console.WriteLine("Sharp"); var ethalon = MeasureSpeed(Func2, a, 10000000, null); Console.WriteLine("GroboCompile without checking"); MeasureSpeed(LambdaCompiler.Compile(exp, CompilerOptions.None), a, 10000000, ethalon); Console.WriteLine("GroboCompile with checking"); MeasureSpeed(LambdaCompiler.Compile(exp, CompilerOptions.All), a, 10000000, ethalon); Console.WriteLine("Compile"); MeasureSpeed(exp.Compile(), a, 1000000, ethalon); }
public void TestDouble() { ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a"); ParameterExpression b = Expression.Parameter(typeof(double), "b"); Expression <Func <TestClassA, double, double> > exp = Expression.Lambda <Func <TestClassA, double, double> >(Expression.PowerAssign(Expression.MakeIndex(Expression.MakeMemberAccess(a, typeof(TestClassA).GetField("DoubleArray")), typeof(DoubleArray).GetProperty("Item"), new[] { Expression.Constant("zzz"), Expression.Constant(1) }), b), a, b); var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences); var o = new TestClassA { DoubleArray = new DoubleArray() }; o.DoubleArray["zzz", 1] = 0; Assert.AreEqual(1, f(o, 0)); Assert.AreEqual(1, o.DoubleArray["zzz", 1]); o.DoubleArray["zzz", 1] = 1; Assert.AreEqual(1, f(o, 2)); Assert.AreEqual(1, o.DoubleArray["zzz", 1]); o.DoubleArray["zzz", 1] = 2; Assert.AreEqual(16, f(o, 4)); Assert.AreEqual(16, o.DoubleArray["zzz", 1]); o.DoubleArray["zzz", 1] = -1; Assert.AreEqual(1, f(o, 2)); Assert.AreEqual(1, o.DoubleArray["zzz", 1]); Assert.AreEqual(0, f(null, 1)); f = LambdaCompiler.Compile(exp, CompilerOptions.None); o = new TestClassA { DoubleArray = new DoubleArray() }; o.DoubleArray["zzz", 1] = 0; Assert.AreEqual(1, f(o, 0)); Assert.AreEqual(1, o.DoubleArray["zzz", 1]); o.DoubleArray["zzz", 1] = 1; Assert.AreEqual(1, f(o, 2)); Assert.AreEqual(1, o.DoubleArray["zzz", 1]); o.DoubleArray["zzz", 1] = 2; Assert.AreEqual(16, f(o, 4)); Assert.AreEqual(16, o.DoubleArray["zzz", 1]); o.DoubleArray["zzz", 1] = -1; Assert.AreEqual(1, f(o, 2)); Assert.AreEqual(1, o.DoubleArray["zzz", 1]); Assert.Throws <NullReferenceException>(() => f(null, 1)); }
public void TestProp() { ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a"); ParameterExpression b = Expression.Parameter(typeof(int), "b"); Expression <Func <TestClassA, int, int> > exp = Expression.Lambda <Func <TestClassA, int, int> >(Expression.AddAssign(Expression.MakeMemberAccess(a, typeof(TestClassA).GetProperty("IntProp")), b), a, b); var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences); var o = new TestClassA(); Assert.AreEqual(0, f(o, 0)); Assert.AreEqual(0, o.IntProp); o.IntProp = 1; Assert.AreEqual(3, f(o, 2)); Assert.AreEqual(3, o.IntProp); o.IntProp = -1; Assert.AreEqual(1, f(o, 2)); Assert.AreEqual(1, o.IntProp); o.IntProp = 2000000000; unchecked { Assert.AreEqual(2000000000 + 2000000000, f(o, 2000000000)); Assert.AreEqual(2000000000 + 2000000000, o.IntProp); } Assert.AreEqual(0, f(null, 1)); f = LambdaCompiler.Compile(exp, CompilerOptions.None); o = new TestClassA(); Assert.AreEqual(0, f(o, 0)); Assert.AreEqual(0, o.IntProp); o.IntProp = 1; Assert.AreEqual(3, f(o, 2)); Assert.AreEqual(3, o.IntProp); o.IntProp = -1; Assert.AreEqual(1, f(o, 2)); Assert.AreEqual(1, o.IntProp); o.IntProp = 2000000000; unchecked { Assert.AreEqual(2000000000 + 2000000000, f(o, 2000000000)); Assert.AreEqual(2000000000 + 2000000000, o.IntProp); } Assert.Throws <NullReferenceException>(() => f(null, 1)); }
public void Map_MappedSource_SkipsMapping() { string expectedName = "Test Name"; int expectedCode = 412; var a = new TestClassA(); var d = new TestClassD {AnotherCode = expectedCode, SomeOtherName = expectedName}; var target = new AttributeMapper<TestClassD, TestClassA>(); target.Map(d, a); Assert.AreEqual(expectedName, d.SomeOtherName); Assert.AreEqual(expectedCode, d.AnotherCode); Assert.AreEqual(expectedName, a.Name); // since the mapping direction for AnotherCode is "TargetMemberName" only, // a.Code should be left with its default sourceValue. Assert.AreEqual(0, a.Code); }
public async Task GivenRegistration_WhenTheProcessDelegateIsCalled_ItShouldExecuteTheSpecifiedGenericCallback() { // pre-arrangement var expectedTestClassA = new TestClassA(); var expectedTestClassB = new TestClassB(); // arrange var semanticBuilder = new SemanticBuilder(); semanticBuilder.InstallPipe<TestClassA, TestClassB>((a, innerBroker) => { Assert.AreSame(expectedTestClassA, a); return expectedTestClassB; }); ISemanticBroker semanticBroker = semanticBuilder.CreateBroker(); // act var processedOuput = await semanticBroker.On(expectedTestClassA).Output<TestClassB>(); // assert Assert.AreSame(expectedTestClassB, processedOuput); }
public async Task GivenRegistryWithMultipleSources_WhenResolvingToSpecificType_ItShouldResolve() { // pre-arrange var instanceClassA = new TestClassA(); var instanceClassB = new TestClassB(); var instanceClassC = new TestClassC(); // arrange var semanaticBuilder = new SemanticBuilder(); semanaticBuilder.InstallPipe<TestClassA, TestClassB>((a, semanticBroker) => instanceClassB); semanaticBuilder.InstallPipe<TestClassA, TestClassC>((a, semanticBroker) => instanceClassC); ISemanticBroker broker = semanaticBuilder.CreateBroker(); // act var solveToB = await broker.On(instanceClassA).Output<TestClassB>(); var solveToC = await broker.On(instanceClassA).Output<TestClassC>(); // assert Assert.AreEqual(instanceClassB, solveToB); Assert.AreEqual(instanceClassC, solveToC); }
public void MapTest() { var target = new ConventionMapper<TestClassA, TestClassB>(); var from = new TestClassA(); var to = new TestClassB(); from.Code = 123; string expectedName = "Test123"; from.Name = expectedName; from.InnerClass = new TestClassC(); string expectedInnerName = "InnerName234"; double expectedInnerFraction = Math.PI; from.InnerClass.Name = expectedInnerName; from.InnerClass.Fraction = expectedInnerFraction; target.Map(from, to); Assert.AreEqual(expectedName, to.Name); Assert.AreEqual(expectedInnerName, to.InnerClassName); Assert.AreEqual(expectedInnerFraction, to.InnerClassFraction); // making sure the mapping didn't mess with the "sourceMember" object. Assert.AreEqual(expectedName, from.Name); Assert.AreEqual(expectedInnerName, from.InnerClass.Name); Assert.AreEqual(expectedInnerFraction, from.InnerClass.Fraction); }
public void MapTest() { var a = new TestClassA(); var c = new TestClassC(); PropertyInfo fromProp = typeof (TestClassA).GetProperty("Name"); PropertyInfo toProp = typeof (TestClassA).GetProperty("Name"); var mock = new ConverterMock(); bool executed = false; mock.ConvertAction = (f, fp, t, tp, s) => { Assert.AreEqual(c, f); Assert.AreEqual(fromProp, fp); Assert.AreEqual(a, t); Assert.AreEqual(toProp, tp); Assert.IsTrue(s); executed = true; }; var target = new ReflectionPropertyMappingInfo<TestClassC, TestClassA>( fromProp, toProp, true, mock); target.Map(c, a); Assert.IsTrue(executed); }
private void CheckMapping(TestClassA source, TestClassB target) { Assert.AreEqual(source.Name, target.Name); Assert.AreEqual(source.InnerClass.Name, target.InnerClassName); Assert.AreEqual(source.InnerClass.Fraction, target.InnerClassFraction); }
public void Map_NullInner_PropertySkipped() { var target = new ConventionMapper<TestClassA, TestClassB>(); var mapSource = new TestClassA { Name = "TestName", Code = 123, InnerClass = null, // this needs to be null for this scenario. Making it explicit here. }; TestClassB result = target.Map(mapSource); Assert.IsNull(result.InnerClassName); Assert.AreEqual(mapSource.Name, result.Name); }
public void Should_return_zero_when_trying_to_get_a_null_objects_HashCode() { var @class = new TestClassA(); var reader = new PropertyReader<TestClassA>("TestValue"); reader.GetItemHashCode(@class).ShouldEqual(0); }
private TestClassA GenerateSource() { Random rnd = new Random(); TestClassA source = new TestClassA() { Name = "Name" + rnd.Next(1000).ToString(), InnerClass = new TestClassC() { Name = "InnerName" + rnd.Next(1000).ToString(), Fraction = rnd.NextDouble() } }; return source; }
public void GivenRegistry_WhenResolvingToUnresolveableDestination_ItShouldThrowCannotResolveSemanticException() { var instanceClassA = new TestClassA(); var instanceClassC = new TestClassC(); // arrange var semanaticBuilder = new SemanticBuilder(); semanaticBuilder.InstallPipe<TestClassA, TestClassB>((a, semanticBroker) => new TestClassB()); semanaticBuilder.InstallPipe<TestClassB, TestClassA>((b, semanticBroker) => new TestClassA()); ISemanticBroker broker = semanaticBuilder.CreateBroker(); // act var exception1 = Assert.Throws<CannotResolveSemanticException>(async () => await broker.On(instanceClassA).Output<TestClassC>()); var exception2 = Assert.Throws<CannotResolveSemanticException>(async () => await broker.On(instanceClassC).Output<TestClassA>()); // assert string expectedExceptionMessage1 = string.Format("The input type '{0}' could not be resolved to output a type of {1}", typeof (TestClassA), typeof (TestClassC)); string expectedExceptionMessage2 = string.Format("The input type '{0}' could not be resolved to output a type of {1}", typeof (TestClassC), typeof (TestClassA)); Assert.AreEqual(expectedExceptionMessage1, exception1.Message); Assert.AreEqual(expectedExceptionMessage2, exception2.Message); }
public void PropertyConverterNegativeTest() { var target = new PropertyConverter(); var source = new TestClassA(); var sourceProperty = source.GetType().GetProperty("InnerClass"); var result = new TestClassB(); var resultProperty = result.GetType().GetProperty("InnerClassCode"); source.InnerClass = new TestClassC(); target.Convert( source, sourceProperty, target, resultProperty, false); }
public async Task GivenRegistryWithOnePipes_WhenResolvingFromAToB_ItShouldCallIt() { // pre-arrange var instanceClassA = new TestClassA(); var instanceClassB = new TestClassB(); // arrange var semanaticBuilder = new SemanticBuilder(); semanaticBuilder.InstallPipe<TestClassA, TestClassB>((a, semanticBroker) => { Assert.AreSame(instanceClassA, a); return instanceClassB; }); ISemanticBroker broker = semanaticBuilder.CreateBroker(); // act var solvedExecution = await broker.On(instanceClassA).Output<TestClassB>(); // assert Assert.AreEqual(instanceClassB, solvedExecution); }
public TestClassC(TestClassA testClassA, TestClassB testClassB, TestClassD testClassD) { this.TestClassA = testClassA; this.TestClassB = testClassB; this.TestClassD = testClassD; }
public TestClassB(TestClassA testClassA, TestClassD testClassD) { this.TestClassA = testClassA; this.TestClassD = testClassD; }
public TestClassAutoC(TestClassA testClassA, TestClassB testClassB) { this.TestClassA = testClassA; this.TestClassB = testClassB; }
private TestClassA GenerateSource() { var rnd = new Random(); var source = new TestClassA { Name = "Name" + rnd.Next(1000), InnerClass = new TestClassC { Name = "InnerName" + rnd.Next(1000), Fraction = rnd.NextDouble() } }; return source; }
public void Init() { objectA1 = new TestClassA(); objectA2 = new TestClassA(); objectB = new TestClassB(); }
public void Should_be_able_to_read_the_value_of_a_type_using_the_Reflection_strategy() { var t = new TestClassA { TestValue = "Hi" }; var p = new PropertyReader<TestClassA>(PropertyReadStrategy.Reflection, "TestValue"); p.ReadValue(t).ShouldEqual("Hi"); }