public void Test_ToString() { var mib = new MenuItemBinding(); Assert.AreEqual("(Empty)", mib.ToString(), "#A1"); foreach (var entry in toStringValues) { ToStringTestProperty(entry.Key, entry.Value); } }
void ToStringTestProperty(string propertyName, string expectedValue) { PropertyInfo pi = typeof(MenuItemBinding).GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public); if (pi == null) { Assert.Fail("Property '{0}' not found.", propertyName); } object defaultValue = null; object[] attrs = pi.GetCustomAttributes(typeof(DefaultValueAttribute), false); Type t = pi.PropertyType; if (attrs != null && attrs.Length > 0) { var dva = attrs [0] as DefaultValueAttribute; defaultValue = dva.Value; } else { if (t == typeof(string)) { defaultValue = String.Empty; } else if (t == typeof(bool)) { defaultValue = false; } else if (t == typeof(int)) { defaultValue = Int32.MaxValue; } else { Assert.Fail("Unsupported return type '{0}' for property '{1}'", t.FullName, propertyName); } } object setToValue = null; if (t == typeof(string)) { string v = defaultValue as String; if (v == String.Empty || v != "value") { setToValue = "value"; } else { setToValue = "value123"; } } else if (t == typeof(bool)) { bool v = (bool)defaultValue; if (v) { setToValue = false; } else { setToValue = true; } } else if (t == typeof(int)) { int v = (int)defaultValue; if (v == Int32.MaxValue) { v = Int32.MinValue; } else { v = Int32.MaxValue; } } else { Assert.Fail("Unsupported return type '{0}' for property '{1}'", t.FullName, propertyName); } var mib = new MenuItemBinding(); pi.SetValue(mib, setToValue, null); Assert.AreEqual(expectedValue, mib.ToString(), propertyName); }