public void WithoutTypeConversion() { Hashtable source = new Hashtable(); Inventor target = new Inventor(); source["name"] = "Nikola Tesla"; source["dob"] = new DateTime(1856, 7, 9); // I know this is pupin's graduation year but I need a date here source["dateofgraduation"] = new DateTime(1883, 1, 1); source["inventions"] = new string[] { "One", "Two" }; mgr.BindSourceToTarget(source, target, null); Assert.IsTrue(mgr.HasBindings); Assert.AreEqual("Nikola Tesla", target.Name); Assert.AreEqual(new DateTime(1856, 7, 9), target.DOB); Assert.AreEqual(new string[] { "One", "Two" }, target.Inventions); Assert.IsNull(target.PlaceOfBirth.City); Assert.IsNull(target.PlaceOfBirth.Country); target.Name = "Tesla, Nikola"; target.DOB = DateTime.Today; target.PlaceOfBirth.City = "Smiljan"; target.PlaceOfBirth.Country = "Lika"; mgr.BindTargetToSource(source, target, null); Assert.AreEqual("Tesla, Nikola", source["name"]); Assert.AreEqual(DateTime.Today, source["dob"]); Assert.AreEqual("One", ((string[])source["inventions"])[0]); Assert.AreEqual("Smiljan", source["cityOfBirth"]); Assert.AreEqual("Lika", source["countryOfBirth"]); }
public void UnhandledTypeConversionExceptionTargetToSource() { BaseBindingManager dbm = new BaseBindingManager(); Inventor st = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian"); st.Inventions = new string[] { "Invention One", "Invention Two" }; dbm.AddBinding("pob", "DOB"); try { dbm.BindTargetToSource(st, st, null); Assert.Fail("Binding date to custom Place type should throw an exception."); } catch (TypeMismatchException) {} // binding state is not remembered with ValidationErrors=null! try { dbm.BindSourceToTarget(st, st, null); Assert.Fail("Binding custom Place to date type should throw an exception."); } catch (TypeMismatchException) {} }
public void DirectionTargetToSource() { BaseBindingManager dbm = new BaseBindingManager(); Inventor source = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian"); Hashtable target = new Hashtable(); dbm.AddBinding("Name", "['name']", BindingDirection.TargetToSource); dbm.BindSourceToTarget(source, target, null); Assert.IsNull(target["name"]); target["name"] = "Mihajlo Pupin"; dbm.BindTargetToSource(source, target, null); Assert.AreEqual("Mihajlo Pupin", source.Name); }
public void HandledTypeConversionExceptionTargetToSource() { BaseBindingManager dbm = new BaseBindingManager(); IValidationErrors errors = new ValidationErrors(); Inventor st = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian"); st.Inventions = new string[] { "Invention One", "Invention Two" }; SimpleExpressionBinding binding = new SimpleExpressionBinding("pob", "DOB"); binding.SetErrorMessage("error", "errors"); dbm.AddBinding(binding); dbm.BindTargetToSource(st, st, errors); Assert.IsFalse(binding.IsValid(errors)); Assert.IsFalse(errors.IsEmpty); Assert.AreEqual(1, errors.GetErrors("errors").Count); // make sure that the old value doesn't override current invalid value dbm.BindSourceToTarget(st, st, errors); Assert.AreEqual(new DateTime(1856, 7, 9), st.DOB); }
public void UnhandledTypeConversionExceptionSourceToTarget() { BaseBindingManager dbm = new BaseBindingManager(); Hashtable source = new Hashtable(); source["boolValue"] = false; Inventor target = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian"); dbm.AddBinding("['boolValue']", "DOB"); try { dbm.BindSourceToTarget(source, target, null); Assert.Fail("Binding boolean to date should throw an exception."); } catch (TypeMismatchException) {} // binding state is not remembered with ValidationErrors=null! dbm.BindTargetToSource(source, target, null); Assert.AreEqual(target.DOB, source["boolValue"]); }
public void BindNullValues() { Hashtable source; Inventor target; target = new Inventor(); source = new Hashtable(); // this is legal (dog is nullable) BaseBindingManager mgr = new BaseBindingManager(); mgr.AddBinding("['dateofgraduation']", "DateOfGraduation"); source["dateofgraduation"] = null; target.DateOfGraduation = DateTime.Now; mgr.BindSourceToTarget(source, target, null); Assert.IsNull(target.DateOfGraduation); source["dateofgraduation"] = DateTime.Now; mgr.BindTargetToSource(source, target, null); Assert.IsNull(source["dateofgraduation"]); }
public void HandledTypeConversionExceptionSourceToTarget() { BaseBindingManager dbm = new BaseBindingManager(); IValidationErrors errors = new ValidationErrors(); Hashtable source = new Hashtable(); source["boolValue"] = false; Inventor target = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian"); SimpleExpressionBinding binding = new SimpleExpressionBinding("['boolValue']", "DOB"); binding.SetErrorMessage("error", "errors"); dbm.AddBinding(binding); dbm.BindSourceToTarget(source, target, errors); Assert.IsFalse(binding.IsValid(errors)); Assert.IsFalse(errors.IsEmpty); Assert.AreEqual(1, errors.GetErrors("errors").Count); // make sure that the old value doesn't override current invalid value dbm.BindTargetToSource(source, target, errors); Assert.AreEqual(false, source["boolValue"]); }