예제 #1
0
 private static void AddTypeProperties(Type type)
 {
     if (!type_properties.ContainsKey(type))
     {
         ITypeInfo typeInfo            = TypeFactory.GetTypeInfo(type);
         IList <PropertyMetadata> list = new List <PropertyMetadata>();
         foreach (PropertyInfo property in typeInfo.GetProperties())
         {
             if (!(property.Name == "Item"))
             {
                 PropertyMetadata item = default(PropertyMetadata);
                 item.Info    = property;
                 item.IsField = false;
                 list.Add(item);
             }
         }
         foreach (FieldInfo field in typeInfo.GetFields())
         {
             PropertyMetadata item2 = default(PropertyMetadata);
             item2.Info    = field;
             item2.IsField = true;
             list.Add(item2);
         }
         lock (type_properties_lock)
         {
             try
             {
                 type_properties.Add(type, list);
             }
             catch (ArgumentException)
             {
             }
         }
     }
 }
예제 #2
0
        private void ExploreContext(IProject project, IMetadataAssembly assembly, ITypeInfo type)
        {
            var contextElement = _factory.GetOrCreateContext(
                project,
                new ClrTypeName(type.FullyQualifiedName),
                assembly.Location,
                type.GetSubject(),
                type.GetTags().ToArray(),
                type.IsIgnored());

            _observer.OnUnitTestElement(contextElement);

            var fields = type.GetFields().ToArray();

            var specifications = fields.Where(x => x.IsSpecification());
            var behaviors      = fields.Where(x => x.IsBehavior());

            foreach (var specification in specifications)
            {
                ExploreSpecification(project, contextElement, type, specification);
            }

            foreach (var behavior in behaviors)
            {
                ExploreBehavior(project, contextElement, type, behavior);
            }

            _observer.OnUnitTestElementChanged(contextElement);
        }
 public static bool IsContext(this ITypeInfo type)
 {
     return(!type.IsAbstract &&
            !type.GetCustomAttributes(FullNames.BehaviorsAttribute, false).Any() &&
            !type.GetGenericArguments().Any() &&
            type.GetFields().Any(x => x.IsSpecification() || x.IsBehavior()));
 }
예제 #4
0
        public void FormatDescribesTheObject()
        {
            ITypeInfo type = Reflector.Wrap(typeof(GenericClass <>));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add((IGenericParameterInfo)type.GenericArguments[0], typeof(int));
            slotValues.Add(type.GetConstructors(PublicInstance)[1].Parameters[0], 1);
            slotValues.Add(type.GetFields(PublicInstance)[0], 2);
            slotValues.Add(type.GetProperties(PublicInstance)[0], 3);

            ObjectCreationSpec spec = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);

            Assert.AreEqual("Foo<int>(1): fieldValue=2, Property=3", spec.Format("Foo", RuntimeAccessor.ServiceLocator.Resolve <IFormatter>()));
        }
예제 #5
0
 private static void AddObjectMetadata(Type type)
 {
     if (!object_metadata.ContainsKey(type))
     {
         ObjectMetadata value    = default(ObjectMetadata);
         ITypeInfo      typeInfo = TypeFactory.GetTypeInfo(type);
         if (typeInfo.GetInterface("System.Collections.IDictionary") != null)
         {
             value.IsDictionary = true;
         }
         value.Properties = new Dictionary <string, PropertyMetadata>();
         foreach (PropertyInfo property in typeInfo.GetProperties())
         {
             if (property.Name == "Item")
             {
                 ParameterInfo[] indexParameters = property.GetIndexParameters();
                 if (indexParameters.Length == 1 && indexParameters[0].ParameterType == typeof(string))
                 {
                     value.ElementType = property.PropertyType;
                 }
             }
             else if (!value.IsDictionary || !dictionary_properties_to_ignore.Contains(property.Name))
             {
                 PropertyMetadata value2 = default(PropertyMetadata);
                 value2.Info = property;
                 value2.Type = property.PropertyType;
                 value.Properties.Add(property.Name, value2);
             }
         }
         foreach (FieldInfo field in typeInfo.GetFields())
         {
             PropertyMetadata value3 = default(PropertyMetadata);
             value3.Info    = field;
             value3.IsField = true;
             value3.Type    = field.FieldType;
             value.Properties.Add(field.Name, value3);
         }
         lock (object_metadata_lock)
         {
             try
             {
                 object_metadata.Add(type, value);
             }
             catch (ArgumentException)
             {
             }
         }
     }
 }
예제 #6
0
        public void CreateInstanceWithGenericStructInstantiationDefaultConstructor()
        {
            ITypeInfo type = Reflector.Wrap(typeof(GenericStruct <int>));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add(type.GetFields(PublicInstance)[0], 2);
            slotValues.Add(type.GetProperties(PublicInstance)[0], 3);

            ObjectCreationSpec  spec     = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);
            GenericStruct <int> instance = (GenericStruct <int>)spec.CreateInstance();

            Assert.AreEqual(0, instance.constructorParamValue);
            Assert.AreEqual(2, instance.fieldValue);
            Assert.AreEqual(3, instance.propertyValue);
        }
예제 #7
0
        public void CreateInstanceWithGenericClass()
        {
            ITypeInfo type = Reflector.Wrap(typeof(GenericClass <>));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add((IGenericParameterInfo)type.GenericArguments[0], typeof(int));
            slotValues.Add(type.GetConstructors(PublicInstance)[1].Parameters[0], 1);
            slotValues.Add(type.GetFields(PublicInstance)[0], 2);
            slotValues.Add(type.GetProperties(PublicInstance)[0], 3);

            ObjectCreationSpec spec     = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);
            GenericClass <int> instance = (GenericClass <int>)spec.CreateInstance();

            Assert.AreEqual(1, instance.constructorParamValue);
            Assert.AreEqual(2, instance.fieldValue);
            Assert.AreEqual(3, instance.propertyValue);
        }
        /// <summary>
        /// Infers whether the type is a test type based on its structure.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Returns true if the type any associated patterns, if it has
        /// non-nested type members (subject to <see cref="GetMemberBindingFlags" />)
        /// with patterns, if it has generic parameters with patterns, or if any
        /// of its nested types satisfy the preceding rules.
        /// </para>
        /// </remarks>
        /// <param name="evaluator">The pattern evaluator.</param>
        /// <param name="type">The type.</param>
        /// <returns>True if the type is likely a test type.</returns>
        protected virtual bool InferTestType(IPatternEvaluator evaluator, ITypeInfo type)
        {
            if (evaluator.HasPatterns(type))
            {
                return(true);
            }

            BindingFlags bindingFlags = GetMemberBindingFlags(type);

            if (HasCodeElementWithPattern(evaluator, type.GetMethods(bindingFlags)) ||
                HasCodeElementWithPattern(evaluator, type.GetProperties(bindingFlags)) ||
                HasCodeElementWithPattern(evaluator, type.GetFields(bindingFlags)) ||
                HasCodeElementWithPattern(evaluator, type.GetEvents(bindingFlags)))
            {
                return(true);
            }

            if (type.IsGenericTypeDefinition && HasCodeElementWithPattern(evaluator, type.GenericArguments))
            {
                return(true);
            }

            if (ShouldConsumeConstructors(type) &&
                HasCodeElementWithPattern(evaluator, type.GetConstructors(ConstructorBindingFlags)))
            {
                return(true);
            }

            foreach (ITypeInfo nestedType in type.GetNestedTypes(NestedTypeBindingFlags))
            {
                if (InferTestType(evaluator, nestedType))
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #9
0
        public void SpecPropertiesDescribeTheObject()
        {
            ITypeInfo        type                 = Reflector.Wrap(typeof(GenericClass <>));
            IConstructorInfo constructor          = type.GetConstructors(PublicInstance)[1];
            IParameterInfo   constructorParameter = constructor.Parameters[0];
            IFieldInfo       field                = type.GetFields(PublicInstance)[0];
            IPropertyInfo    property             = type.GetProperties(PublicInstance)[0];

            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add((IGenericParameterInfo)type.GenericArguments[0], typeof(int));
            slotValues.Add(constructorParameter, 1);
            slotValues.Add(field, 2);
            slotValues.Add(property, 3);

            ObjectCreationSpec spec = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);

            Assert.AreSame(type, spec.Type);
            Assert.AreSame(slotValues, spec.SlotValues);
            Assert.AreSame(NullConverter.Instance, spec.Converter);
            Assert.AreEqual(typeof(GenericClass <int>), spec.ResolvedType);

            Assert.AreEqual(constructor, Reflector.Wrap(spec.ResolvedConstructor));
            Assert.AreElementsEqual(new object[] { 1 }, spec.ResolvedConstructorArguments);

            List <KeyValuePair <FieldInfo, object> > fieldValues = new List <KeyValuePair <FieldInfo, object> >(spec.ResolvedFieldValues);

            Assert.Count(1, fieldValues);
            Assert.AreEqual(field, Reflector.Wrap(fieldValues[0].Key));
            Assert.AreEqual(2, fieldValues[0].Value);

            List <KeyValuePair <PropertyInfo, object> > propertyValues = new List <KeyValuePair <PropertyInfo, object> >(spec.ResolvedPropertyValues);

            Assert.Count(1, propertyValues);
            Assert.AreEqual(property, Reflector.Wrap(propertyValues[0].Key));
            Assert.AreEqual(3, propertyValues[0].Value);
        }
예제 #10
0
        /// <summary>
        /// Consumes type members including fields, properties, methods and events.
        /// </summary>
        /// <param name="typeScope">The scope to be used as the containing scope.</param>
        /// <param name="type">The type whose members are to be consumed.</param>
        protected void ConsumeMembers(IPatternScope typeScope, ITypeInfo type)
        {
            BindingFlags bindingFlags = GetMemberBindingFlags(type);

            // TODO: We should probably process groups of members in sorted order working outwards
            //       from the base type, like an onion.
            foreach (IFieldInfo field in CodeElementSorter.SortMembersByDeclaringType(type.GetFields(bindingFlags)))
            {
                typeScope.Consume(field, false, DefaultFieldPattern);
            }

            foreach (IPropertyInfo property in CodeElementSorter.SortMembersByDeclaringType(type.GetProperties(bindingFlags)))
            {
                typeScope.Consume(property, false, DefaultPropertyPattern);
            }

            foreach (IMethodInfo method in CodeElementSorter.SortMembersByDeclaringType(type.GetMethods(bindingFlags)))
            {
                typeScope.Consume(method, false, DefaultMethodPattern);
            }

            foreach (IEventInfo @event in CodeElementSorter.SortMembersByDeclaringType(type.GetEvents(bindingFlags)))
            {
                typeScope.Consume(@event, false, DefaultEventPattern);
            }
        }
 public static bool IsBehaviorContainer(this ITypeInfo type)
 {
     return(!type.IsAbstract &&
            type.GetCustomAttributes(FullNames.BehaviorsAttribute, false).Any() &&
            type.GetFields().Any(x => x.IsSpecification()));
 }
예제 #12
0
 public override FieldInfo[] GetFields(BindingFlags bindingAttr)
 {
     return(GenericCollectionUtils.ConvertAllToArray <IFieldInfo, FieldInfo>(adapter.GetFields(bindingAttr),
                                                                             delegate(IFieldInfo field) { return field.Resolve(false); }));
 }