private SampleGroup LoadCategory(SampleSuite suite, string category, string prefix, string fileSourceCode) { SampleGroup categoryGroup = new SampleGroup(suite, category, null); Type suiteType = suite.GetType(); MethodInfo[] methodsInCategoryArray = suiteType.GetMethods(); List <MethodInfo> methodsInCategory = new List <MethodInfo>(); foreach (MethodInfo c in methodsInCategoryArray) { if (c.Name.StartsWith(prefix) == true) { CategoryAttribute[] attributes = (CategoryAttribute[])c.GetCustomAttributes(typeof(CategoryAttribute), false); if (attributes.Length > 0 && attributes[0].Category == category) { methodsInCategory.Add(c); } } } foreach (MethodInfo mi in methodsInCategory) { categoryGroup.Children.Add(LoadSample(suite, mi, fileSourceCode)); } return(categoryGroup); }
public SampleGroup Load(SampleSuite suite) { Type suiteType = suite.GetType(); // get attributes attached to suite var descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(suiteType, typeof(DescriptionAttribute)); var titleAttribute = (TitleAttribute)Attribute.GetCustomAttribute(suiteType, typeof(TitleAttribute)); var prefixAttribute = (PrefixAttribute)Attribute.GetCustomAttribute(suiteType, typeof(PrefixAttribute)); if (prefixAttribute == null) { throw new InvalidOperationException("[Prefix] attribute not specified on " + suiteType.Name); } string prefix = prefixAttribute.Prefix; SampleGroup suiteGroup = new SampleGroup(suite, (titleAttribute != null) ? titleAttribute.Title : "", (descriptionAttribute != null) ? descriptionAttribute.Description : ""); string sourceFile = FindSourceFile(suiteType.Name + ".cs"); string sourceCode = (sourceFile != null) ? File.ReadAllText(sourceFile) : ""; var categories = suiteType.GetMethods() .Where(c => c.Name.ToLower().StartsWith(prefix.ToLower())) .Select(c => new { Attributes = (CategoryAttribute[])c.GetCustomAttributes(typeof(CategoryAttribute), false) }) .Where(c => c.Attributes.Length > 0) .Select(c => c.Attributes.First().Category) .Distinct(); foreach (var cat in categories) { suiteGroup.Children.Add(LoadCategory(suite, cat, prefix, sourceCode)); } return(suiteGroup); }
public SampleGroup Load(SampleSuite suite) { Type suiteType = suite.GetType(); // get attributes attached to suite DescriptionAttribute descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(suiteType, typeof(DescriptionAttribute)); TitleAttribute titleAttribute = (TitleAttribute)Attribute.GetCustomAttribute(suiteType, typeof(TitleAttribute)); PrefixAttribute prefixAttribute = (PrefixAttribute)Attribute.GetCustomAttribute(suiteType, typeof(PrefixAttribute)); if (prefixAttribute == null) { throw new InvalidOperationException("[Prefix] attribute not specified on " + suiteType.Name); } string prefix = prefixAttribute.Prefix; SampleGroup suiteGroup = new SampleGroup(suite, (titleAttribute != null) ? titleAttribute.Title : "", (descriptionAttribute != null) ? descriptionAttribute.Description : ""); string sourceFile = FindSourceFile(suiteType.Name + ".cs"); string sourceCode = (sourceFile != null) ? File.ReadAllText(sourceFile) : ""; MethodInfo[] categoriesObj = suiteType.GetMethods(); List <string> categories = new List <string>(); foreach (MethodInfo c in categoriesObj) { if (c.Name.StartsWith(prefix) == true) { CategoryAttribute[] attributes = (CategoryAttribute[])c.GetCustomAttributes(typeof(CategoryAttribute), false); if (attributes.Length > 0) { if (categories.Contains(attributes[0].Category) == false) { categories.Add(attributes[0].Category); } } } } foreach (string cat in categories) { suiteGroup.Children.Add(LoadCategory(suite, cat, prefix, sourceCode)); } return(suiteGroup); }
private SampleGroup LoadCategory(SampleSuite suite, string category, string prefix, string fileSourceCode) { SampleGroup categoryGroup = new SampleGroup(suite, category, null); Type suiteType = suite.GetType(); IEnumerable <MethodInfo> methodsInCategory = suiteType.GetMethods() .Where(c => c.Name.ToLower().StartsWith(prefix.ToLower())) .OrderBy(c => c.Name) .Select(c => new { Method = c, Attributes = (CategoryAttribute[])c.GetCustomAttributes(typeof(CategoryAttribute), false) }) .Where(c => c.Attributes.Length > 0 && c.Attributes[0].Category == category) .Select(c => c.Method); foreach (MethodInfo mi in methodsInCategory) { categoryGroup.Children.Add(LoadSample(suite, mi, fileSourceCode)); } return(categoryGroup); }