public void TestMapIfExistWith2Steps() { var source = new ComplexTestClass(); var target = new NetworkCredentials(); // source.myProp2 is null, so source.myProp2.Password cannot be evaluated and x should stay the same source.MapIfExist(y => y.myProp2, y => y.Password, ref target.Passwort); Assert.IsNull(target.Passwort); // source.myProp2 is null, so source.myProp2.Password cannot be evaluated and x should stay the same target.Passwort = "hallo"; source.MapIfExist(y => y.myProp2, y => y.Password, ref target.Passwort); Assert.IsTrue(target.Passwort == "hallo"); // source.myProp1 is "geheim1", so source.myProp1.Password can be evaluated and x should be updated target.Passwort = "hallo"; source.MapIfExist(y => y.myProp1, y => y.Password, ref target.Passwort); Assert.IsTrue(target.Passwort == "geheim1"); // source is null, so source.myProp1.Password cannot be evaluated and x should stay the same target.Passwort = "hallo"; source = null; source.MapIfExist(y => y.myProp1, y => y.Password, ref target.Passwort); Assert.IsTrue(target.Passwort == "hallo"); }
public void NewIfNullTest() { var x = new ComplexTestClass(); // simple reference type Assert.AreEqual("geheim1", x.myProp1.NewIfNull().Password); Assert.AreEqual(null, x.myProp2.NewIfNull().Domain); // array Assert.AreEqual("domain", x.myProp3.NewIfNull(0).Domain); Assert.AreEqual(null, x.myProp3.NewIfNull(20).Domain); // list Assert.AreEqual("domain", x.myProp4.NewIfNull(0).Domain); Assert.AreEqual(null, x.myProp4.NewIfNull(70).Domain); // dictionary Assert.AreEqual("domain", x.myProp5.NewIfNull("key2").Domain); Assert.AreEqual(null, x.myProp5.NewIfNull("nonexistingKey").Domain); }
public void GetPropertyValueTestCoBa2() { var x = new ComplexTestClass(); Assert.AreEqual("geheim1", Tools.GetPropertyValue(x, "myProp1.Password")); Assert.IsNull(Tools.GetPropertyValue(x, "myProp2.Password")); Assert.AreEqual("geheim3", Tools.GetPropertyValue(x, "myProp3[1].Password")); Assert.AreEqual("geheim4", Tools.GetPropertyValue(x, "myProp4[0].Password")); Assert.AreEqual("geheim7", Tools.GetPropertyValue(x, "myProp5[key2].Password")); Assert.AreEqual(new DateTime(2009, 7, 8), Tools.GetPropertyValue(x, "myProp6")); Assert.AreEqual("08.07.2009 00:00:00", Tools.GetPropertyValueString(x, "myProp6")); Assert.AreEqual("08.07.2009 00:00:00", Tools.GetPropertyValueString(x, "AsString()")); Assert.AreEqual("08.07.2009 00:00:00", Tools.GetPropertyValueString(x, "AsString(----)")); Assert.IsNull(Tools.GetPropertyValue(x, "myProp7")); Assert.AreEqual(string.Empty, Tools.GetPropertyValueString(x, "myProp7")); Assert.IsNull(Tools.GetPropertyValue(x, "myProp4[20?].Password")); Assert.IsNull(Tools.GetPropertyValue(x, "myProp5[nonexistingkey?].Password")); }
public void GetPropertyNulls() { var x = new ComplexTestClass(); Assert.AreEqual("default", x.NewIfNull().myProp2.NewIfNull().Domain.DefaultIfNullOrEmpty("default")); }
public void MapIfDiffersTest() { var source1 = new ComplexTestClass(); var source2 = new ComplexTestClass(); var target = new NetworkCredentials(); var dirty = false; // both NULL : no mapping MappingHelper.MapIfDiffers(ref dirty, source1, source2, y => y.myProp2.Password, x => target.Passwort = x); Assert.IsFalse(dirty); // both "geheim1" : no mapping MappingHelper.MapIfDiffers(ref dirty, source1, source2, y => y.myProp1.Password, x => target.Passwort = x); Assert.IsFalse(dirty); // source1 = "geheim1", source2 = "something completely different" : !!mapping!! source2.myProp1.Password = "******"; MappingHelper.MapIfDiffers(ref dirty, source1, source2, y => y.myProp1.Password, x => target.Passwort = x); Assert.IsTrue(dirty); // source1 = NULL, source2 = "something completely different" : !!mapping!! source1.myProp1 = null; MappingHelper.MapIfDiffers(ref dirty, source1, source2, y => y.myProp1.Password, x => target.Passwort = x); Assert.IsTrue(dirty); Assert.IsNull(target.Passwort); // source1 = NULL, source2 = "something completely different" : !!mapping!! source1.myProp1 = null; MappingHelper.MapIfDiffers( ref dirty, source1, source2, y => y.myProp1.Password.ToString(), x => target.Passwort = x); Assert.IsTrue(dirty); Assert.IsNull(target.Passwort); source1.myProp6 = new DateTime(2010, 7, 1); MappingHelper.MapIfDiffers(ref dirty, source1, source2, y => y.myProp6, x => source2.myProp6 = x); Assert.IsTrue(dirty); Assert.AreEqual(source2.myProp6, source1.myProp6); source1.myProp9 = new DateTime(2010, 7, 1); MappingHelper.MapIfDiffers(ref dirty, source1, source2, y => y.myProp9, x => source2.myProp9 = x); Assert.IsTrue(dirty); Assert.AreEqual(source2.myProp9, source1.myProp9); source1.myProp9 = new DateTime(); source1.myProp10 = new ComplexTestClass { myProp9 = new DateTime(2010, 7, 6) }; MappingHelper.MapIfDiffers(ref dirty, source1, source2, y => y.myProp10.myProp9, x => source2.myProp9 = x); Assert.IsTrue(dirty); Assert.AreEqual(new DateTime(2010, 7, 6), source2.myProp9); Assert.AreEqual(new DateTime(2010, 7, 6), source1.myProp10.myProp9); // access property of nullable type that is NULL // in this case we will get the default value of the value type property or NULL in case of a nullable type source1.myProp10.myProp6 = null; source2.myProp8 = 0; MappingHelper.MapIfDiffers( ref dirty, source1, source2, y => y.myProp10.myProp6.Value.Year, x => source2.myProp8 = x); Assert.AreEqual(0, source2.myProp8); }