public void XliffElement_SchemaPropertyAttributes() { List <Tuple <Type, PropertyInfo> > propertyList; const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; propertyList = XliffElementPropertiesTests.GetSchemaEntityProperties(true); foreach (Tuple <Type, PropertyInfo> tuple in propertyList) { ConstructorInfo ctor; Dictionary <string, AttributeData> attributes; object instance; object value; Console.WriteLine(tuple.Item1.Name + "." + tuple.Item2.Name); ctor = tuple.Item1.GetConstructor(flags, null, XliffElementPropertiesTests._emptyTypes, null); instance = ctor.Invoke(XliffElementPropertiesTests._emptyObjects); try { value = tuple.Item2.GetValue(instance); } catch (TargetInvocationException e) { if (e.InnerException is NotSupportedException) { Console.WriteLine("Skipping test because the property is not supported by this object."); continue; } else { throw; } } // // This logic is not ideal because it uses reflection to get at a private member, but we really want // to verify that the public property and the internal property are mapped correctly. We could make // the internal attributes public or internal, but then other methods would be able to access the // field and that is not ideal because nothing else should know about this logic. // attributes = (Dictionary <string, AttributeData>) typeof(XliffElement) .GetField("attributes", flags) .GetValue(instance); Assert.AreEqual(value, attributes[tuple.Item2.Name].Value); value = XliffElementPropertiesTests.GetNewValue(tuple.Item2.PropertyType, value); // SubFormatStyle doesn't have a setter. if ((!(instance is Data) || (tuple.Item2.Name != "Space")) && (tuple.Item2.Name != "SubFormatStyle")) { tuple.Item2.SetValue(instance, value); Assert.AreEqual(value, attributes[tuple.Item2.Name].Value); } } }
public void XliffElement_SchemaProperties() { List <Tuple <Type, PropertyInfo> > propertyList; const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; propertyList = XliffElementPropertiesTests.GetProperties(true); foreach (Tuple <Type, PropertyInfo> tuple in propertyList) { ConstructorInfo ctor; object instance; object oldValue; object newValue; bool notSupported; if (!XliffElementPropertiesTests.HasSetter(tuple.Item2)) { continue; } Console.WriteLine(tuple.Item1.Name + "." + tuple.Item2.Name); ctor = tuple.Item1.GetConstructor(flags, null, XliffElementPropertiesTests._emptyTypes, null); instance = ctor.Invoke(XliffElementPropertiesTests._emptyObjects); newValue = null; try { oldValue = tuple.Item2.GetValue(instance); newValue = XliffElementPropertiesTests.GetNewValue(tuple.Item2.PropertyType, oldValue); Assert.AreNotEqual(newValue, oldValue, "The new value and old value are the same."); notSupported = false; } catch (TargetInvocationException e) { // If the property is not supported a NotSupportedException is thrown and is wrapped by a // TargetInvocationException by .NET. Verify that is the case here. If it's not a // NotSupportedException then something else happened and the test should fail. Assert.IsInstanceOfType(e.InnerException, typeof(NotSupportedException)); notSupported = true; } try { tuple.Item2.SetValue(instance, newValue); oldValue = tuple.Item2.GetValue(instance); Assert.AreEqual(newValue, oldValue, "The setter didn't update the property."); Assert.IsFalse(notSupported, "The getter was not supported but the setter is."); } catch (TargetInvocationException e) { // If the property is not supported a NotSupportedException is thrown and is wrapped by a // TargetInvocationException by .NET. Verify that is the case here. If it's not a // NotSupportedException then something else happened and the test should fail. if ((instance is Data) && (tuple.Item2.Name == "Space")) { Assert.IsInstanceOfType(e.InnerException, typeof(ArgumentException)); } else { Assert.IsInstanceOfType(e.InnerException, typeof(NotSupportedException)); } } } }