Responsible for checking the nature of a type.
        private static IEnumerable <MethodBase> SelectEmptyTestMethods(Type type)
        {
            var selectEmptyTestMethods =
                from testMethod in TypeInvestigationService.GetTestMethods(type, false)
                where !testMethod.IsAbstract &&
                !TypeInvestigationService.IsExpectedExceptionTestMethod(testMethod) &&
                GetIntermediateLanguageFromMethodInfoBase(testMethod).Count() <= 2
                select testMethod;

            return(selectEmptyTestMethods);
        }
Exemplo n.º 2
0
        /// <summary>
        ///   Get the description of the test out of the test method name.
        /// </summary>
        /// <param name="targetMethod"> The target test method. </param>
        /// <returns> The literal description of the test. </returns>
        private static string GetDescription(MethodBase targetMethod)
        {
            if (targetMethod == null || targetMethod.DeclaringType == null)
            {
                return(string.Empty);
            }

            var naturalLanguageConditionName = GetConditionName(targetMethod);
            var naturalLanguageAssertName    = targetMethod.Name.Humanize();

            return(string.Format("Test case for {0}:\n\t{1},\n\t\t{2}.\n\n", TypeInvestigationService.GetTestedClassTypeName(targetMethod.DeclaringType), naturalLanguageConditionName, naturalLanguageAssertName));
        }
Exemplo n.º 3
0
        /// <summary>
        ///   The provide aspects.
        /// </summary>
        /// <param name="targetElement"> The target element. </param>
        /// <returns> The System.Collections.Generic.IEnumerable`1[T -&gt; PostSharp.Aspects.AspectInstance]. </returns>
        /// <remarks>
        ///   This method is called at build time and should just provide other aspects.
        /// </remarks>
        public IEnumerable <AspectInstance> ProvideAspects(object targetElement)
        {
            var targetType = (Type)targetElement;

            if (targetType.IsGenericType)
            {
                // PostSharp doesn't manage to inject on generic types (generic type definitions?), so for now we ignore it.
                yield break;
            }

            if (!targetType.IsDefined(typeof(DescriptionAttribute), false))
            {
                var contextDescription = GetNaturalLanguageContextName(targetType);
                var descriptionAttributeConstructorInfo = typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) });
                var introduceDescriptionAspect          = new CustomAttributeIntroductionAspect(new ObjectConstruction(descriptionAttributeConstructorInfo, contextDescription));

                // Add the Description attribute to the type.
                yield return(new AspectInstance(targetType, introduceDescriptionAspect));
            }

            var localTestMethods = TypeInvestigationService.GetAllTestMethods(targetType)
                                   .Where(m => m.DeclaringType == targetType);

            foreach (var targetMethod in localTestMethods)
            {
                var categoryAttributes = targetMethod.GetCustomAttributes(typeof(CategoryAttribute), false).Cast <CategoryAttribute>().ToArray();
                var categoryName       = string.Format("Specifications for {0}", TypeInvestigationService.GetTestedClassTypeName(targetMethod.DeclaringType));

                if (!categoryAttributes.Any() || categoryAttributes.All(x => x.Name != categoryName))
                {
                    var categoryAttributeConstructorInfo = typeof(CategoryAttribute).GetConstructor(new[] { typeof(string) });
                    var introduceCategoryAspect          = new CustomAttributeIntroductionAspect(new ObjectConstruction(categoryAttributeConstructorInfo, categoryName));

                    // Add the Category attribute to the type.
                    yield return(new AspectInstance(targetMethod, introduceCategoryAspect));
                }

                if (!targetMethod.IsDefined(typeof(DescriptionAttribute), false))
                {
                    var description = GetDescription(targetMethod);
                    if (!string.IsNullOrEmpty(description))
                    {
                        var descriptionAttributeConstructorInfo = typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) });
                        var introduceDescriptionAspect          = new CustomAttributeIntroductionAspect(new ObjectConstruction(descriptionAttributeConstructorInfo, description));

                        // Add the Description attribute to the type.
                        yield return(new AspectInstance(targetMethod, introduceDescriptionAspect));
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Checks whether the type is a candidate for the aspect to apply to it.
        /// </summary>
        /// <param name="type">the type to check if it needs the attribute.</param>
        /// <returns><c>true</c> if any of the <see cref="instanceLevelRules"/> attribute needs to be applied to the type; <c>false</c> otherwise.</returns>
        public override bool CompileTimeValidate(Type type)
        {
            if (!TypeInvestigationService.IsContextSpecification(type))
            {
                return(false);
            }

            var validates = false;

// ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var rule in this.instanceLevelRules)
            {
                // do not break as some validators raise errors.
                validates = validates | rule.CompileTimeValidate(type);
            }

            return(validates);
        }
Exemplo n.º 5
0
        private static IEnumerable <MethodBase> SelectExceptionResilientTestMethods(Type type)
        {
            var testMethodsInContext = TypeInvestigationService.GetAllContextSpecificationTypes(type)
                                       .SelectMany(nestedType => TypeInvestigationService.GetTestMethods(nestedType, true));
            var expectedExceptionTestMethodsInContext = TypeInvestigationService.GetTestMethods(type, true)
                                                        .Concat(testMethodsInContext)
                                                        .Where(TypeInvestigationService.IsExpectedExceptionTestMethod)
                                                        .ToArray();

            // If there is any test method marked with ExpectedExceptionAttribute, then all other act as if marked with ExceptionResilientAttribute
            if (expectedExceptionTestMethodsInContext.Any())
            {
                var testMethods = TypeInvestigationService.GetTestMethods(type, false);
                return(testMethods.Except(expectedExceptionTestMethodsInContext).ToArray());
            }

            return(Enumerable.Empty <MethodBase>());
        }
Exemplo n.º 6
0
        private static IEnumerable <MethodBase> SelectTestMethods(Type type)
        {
            var testMethods = TypeInvestigationService.GetTestMethods(type, false);

            return(testMethods.Except(SelectExceptionResilientTestMethods(type)));
        }
 /// <summary>
 /// Checks if the passed class contains any nested test fixture classes.
 /// </summary>
 /// <param name="type">
 /// The type of the class to check.
 /// </param>
 /// <returns>
 /// <c>true</c> if the passed class contains any any nested test fixture classes, <c>false</c> otherwise.
 /// </returns>
 public override bool CompileTimeValidate(Type type)
 {
     // Use reflection to find <see cref="TestFixtureAttribute"/>, instead of binding to the NUnit type directly.
     // This prevents versioning issues with PostSharp when applying the aspect at compile-time.
     return(TypeInvestigationService.IsContextSpecification(type) && base.CompileTimeValidate(type));
 }