示例#1
0
        /// <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);
        }
示例#2
0
        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);
                }
            }
        }
示例#3
0
        public void XliffElement_XliffElementsDefaultCtor()
        {
            foreach (Type type in XliffElementPropertiesTests.GetXliffElementTypes())
            {
                const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
                ConstructorInfo    ctor;
                object             instance;

                if (type.IsAbstract)
                {
                    continue;
                }

                Console.WriteLine(type.FullName);

                ctor = type.GetConstructor(flags, null, XliffElementPropertiesTests._emptyTypes, null);
                Assert.IsNotNull(ctor);

                instance = ctor.Invoke(XliffElementPropertiesTests._emptyObjects);
                Assert.IsNotNull(instance);
            }
        }
示例#4
0
        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.");
            }
        }
示例#5
0
        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));
                    }
                }
            }
        }