public static GetTestedClassTypeName ( |
||
targetType | The test class from which the tested class must be found. | |
return | string |
/// <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)); }
/// <summary> /// The provide aspects. /// </summary> /// <param name="targetElement"> The target element. </param> /// <returns> The System.Collections.Generic.IEnumerable`1[T -> 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)); } } } }