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)); }
public void BooleanSupport() { var converter = new ObjectToObjectConverter(); converter.Map.Add(new MapItem { Source = 1, Target = "True" }); converter.Map.Add(new MapItem { Source = 2, Target = "False" }); Assert.AreEqual(true, converter.Convert(1, typeof(bool), null, null)); Assert.AreEqual(false, converter.Convert(2, typeof(bool), null, null)); }
public void DontConvertAlreadyCorrectTypes() { var converter = new ObjectToObjectConverter(); var target = new T491851_Convertible(); converter.Map.Add(new MapItem { Source = 1, Target = target }); Assert.AreEqual(target, converter.Convert(1, typeof(T491851_Convertible_Base), null, null)); Assert.AreEqual(0, target.toTypeCalled); }
public void ObjectToObjectColorBrushConvertion() { var converter = new ObjectToObjectConverter(); converter.Map.Add(new MapItem { Source = "123", Target = "#ff0000" }); Assert.AreEqual("#ff0000", converter.Convert("123", typeof(string), null, null)); var color = (Color)converter.Convert("123", typeof(Color), null, null); Assert.AreEqual(0xff, color.R); Assert.AreEqual(0x00, color.G); Assert.AreEqual(0x00, color.B); color = ((SolidColorBrush)converter.Convert("123", typeof(SolidColorBrush), null, null)).Color; Assert.AreEqual(0xff, color.R); Assert.AreEqual(0x00, color.G); Assert.AreEqual(0x00, color.B); color = ((SolidColorBrush)converter.Convert("123", typeof(Brush), null, null)).Color; Assert.AreEqual(0xff, color.R); Assert.AreEqual(0x00, color.G); Assert.AreEqual(0x00, color.B); converter.Map.Add(new MapItem { Source = "abc", Target = "ff0000" }); Assert.AreEqual("ff0000", (string)converter.Convert("abc", typeof(Brush), null, null)); converter.Map.Add(new MapItem { Source = "xyz", Target = "#ff0000ff" }); color = ((SolidColorBrush)converter.Convert("xyz", typeof(Brush), null, null)).Color; Assert.AreEqual(0xff, color.A); Assert.AreEqual(0x00, color.R); Assert.AreEqual(0x00, color.G); Assert.AreEqual(0xff, color.B); converter.Map.Add(new MapItem { Source = "1", Target = "Red" }); color = ((SolidColorBrush)converter.Convert("1", typeof(Brush), null, null)).Color; Assert.AreEqual(Colors.Red, color); converter.Map.Add(new MapItem { Source = "2", Target = "Green" }); color = ((SolidColorBrush)converter.Convert("2", typeof(Brush), null, null)).Color; Assert.AreEqual(Colors.Green, color); }
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 NullableSupport() { var converter = new ObjectToObjectConverter(); converter.Map.Add(new MapItem { Source = 1, Target = "True" }); converter.Map.Add(new MapItem { Source = 2, Target = "False" }); converter.Map.Add(new MapItem { Source = 3, Target = null }); converter.Map.Add(new MapItem { Source = 4, Target = 10 }); Assert.AreEqual((bool?)true, converter.Convert(1, typeof(bool?), null, null)); Assert.AreEqual((bool?)false, converter.Convert(2, typeof(bool?), null, null)); Assert.AreEqual(null, converter.Convert(3, typeof(bool?), null, null)); Assert.AreEqual((int?)10, converter.Convert(4, typeof(int?), null, null)); Assert.AreEqual(null, converter.Convert(3, typeof(int?), null, null)); }