public void ObjectToObjectCoercions() { var converter = new ObjectToObjectConverter(); converter.Map.Add(new MapItem { Source = "Red", Target = "1" }); converter.Map.Add(new MapItem { Source = "Green", Target = "2" }); converter.Map.Add(new MapItem { Source = MyColorEnum.Blue, Target = "3" }); Assert.AreEqual("1", converter.Convert("Red", typeof(string), null, null)); Assert.AreEqual("2", converter.Convert(MyColorEnum.Green, typeof(string), null, null)); Assert.AreEqual("3", converter.Convert("Blue", typeof(string), null, null)); Assert.AreEqual(null, converter.Convert(null, typeof(string), null, null)); converter.DefaultTarget = "def"; Assert.AreEqual("def", converter.Convert(null, typeof(string), null, null)); converter.Map.Add(new MapItem { Source = null, Target = "nullvalue" }); Assert.AreEqual("nullvalue", converter.Convert(null, typeof(string), null, null)); }
public void ObjectToObjectConverter() { var converter = new ObjectToObjectConverter(); Assert.AreEqual(null, converter.Convert(10, typeof(object), null, null)); Assert.AreEqual(null, converter.ConvertBack(10, typeof(object), null, null)); var instance1 = new MyClass("same"); var instance2 = new MyClass("same"); string converted1 = "converted"; string converted2 = "converted"; converter.Map.Add(new MapItem { Source = instance1, Target = "converted" }); Assert.AreEqual(converted1, converter.Convert(instance1, typeof(object), null, null)); Assert.AreEqual(converted1, converter.Convert(instance2, typeof(object), null, null)); Assert.AreEqual(converted2, converter.Convert(instance1, typeof(object), null, null)); Assert.AreEqual(converted2, converter.Convert(instance2, typeof(object), null, null)); Assert.AreEqual(instance1, converter.ConvertBack(converted1, typeof(object), null, null)); Assert.AreEqual(instance1, converter.ConvertBack(converted2, typeof(object), null, null)); Assert.AreEqual(instance2, converter.ConvertBack(converted1, typeof(object), null, null)); Assert.AreEqual(instance2, converter.ConvertBack(converted2, typeof(object), null, null)); converter.Map.Add(new MapItem { Source = null, Target = "nullto" }); converter.Map.Add(new MapItem { Source = "nullfrom", Target = null }); Assert.AreEqual("nullto", converter.Convert(null, typeof(object), null, null)); Assert.AreEqual("nullfrom", converter.ConvertBack(null, typeof(object), null, null)); converter.DefaultSource = "defsource"; converter.DefaultTarget = "deftarget"; Assert.AreEqual("deftarget", converter.Convert("nonexistent", typeof(object), null, null)); Assert.AreEqual("defsource", converter.ConvertBack("nonexistent", typeof(object), null, null)); converter = new ObjectToObjectConverter(); converter.Map.Add(new MapItem { Source = "true", Target = 1 }); converter.Map.Add(new MapItem { Source = "FALSE", Target = 15 }); Assert.AreEqual(1, converter.Convert(true, typeof(int), null, null)); Assert.AreEqual(15, converter.Convert(false, typeof(int), null, null)); converter = new ObjectToObjectConverter(); converter.DefaultTarget = Visibility.Visible; converter.Map.Add(new MapItem { Source = 0, Target = Visibility.Collapsed }); Assert.AreEqual(Visibility.Visible, converter.Convert(null, typeof(Visibility), null, null)); Assert.AreEqual(Visibility.Visible, converter.Convert(10, typeof(Visibility), null, null)); Assert.AreEqual(Visibility.Collapsed, converter.Convert(0, typeof(Visibility), null, null)); }