/// <summary> /// Gets a list of properties across all <see cref="XliffElement"/> types that are attributed with /// <see cref="SchemaEntityAttribute"/>, optionally excluding abstract types. /// </summary> /// <param name="skipAbstractTypes">True to ignore abstract types, false to include them.</param> /// <returns>A list of types and their associated properties. All types are classes that derive from /// <see cref="XliffElement"/>.</returns> private static List <Tuple <Type, PropertyInfo> > GetSchemaEntityProperties(bool skipAbstractTypes) { List <Tuple <Type, PropertyInfo> > result; result = new List <Tuple <Type, PropertyInfo> >(); foreach (Type type in XliffElementPropertiesTests.GetXliffElementTypes()) { IEnumerable <PropertyInfo> propertyList; if (skipAbstractTypes && type.IsAbstract) { continue; } propertyList = XliffElementPropertiesTests.GetSchemaEntityProperties(type); foreach (PropertyInfo property in propertyList) { result.Add(new Tuple <Type, PropertyInfo>(type, property)); } } // // Make sure this method worked correctly. // Assert.IsTrue(result.Count > 0, "GetSchemaEntityProperties failed to return items."); return(result); }
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_XliffElementsProperties() { List <Tuple <Type, PropertyInfo> > propertyList; propertyList = XliffElementPropertiesTests.GetSchemaEntityProperties(true); foreach (Tuple <Type, PropertyInfo> tuple in propertyList) { MethodInfo[] methods; bool result; Console.WriteLine(tuple.Item1.Name + "." + tuple.Item2.Name); // // Verify the property has both get and set accessors. // methods = tuple.Item2.GetAccessors(false); Assert.IsNotNull(methods, "No accessors found."); if (tuple.Item2.PropertyType == typeof(IDictionary <string, string>)) { Assert.AreEqual(1, methods.Length, "Accessor count mismatch."); result = methods[0].Name.StartsWith("get_"); } else { Assert.AreEqual(2, methods.Length, "Accessor count mismatch."); result = (methods[0].Name.StartsWith("get_") && methods[1].Name.StartsWith("set_")) || (methods[0].Name.StartsWith("set_") && methods[1].Name.StartsWith("get_")); } Assert.IsTrue(result, "Property does not contain get and set accessors."); } }