// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) 2014 OxyPlot contributors
//
// --------------------------------------------------------------------------------------------------------------------
namespace ExampleLibrary
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
///
/// Enumerates all examples in the assembly.
///
public static class Examples
{
///
/// Gets all examples in the specified category.
///
/// The type of the category.
public static IEnumerable GetCategory(Type categoryType)
{
var typeInfo = categoryType.GetTypeInfo();
var examplesAttribute = typeInfo.GetCustomAttributes().FirstOrDefault();
var examplesTags = typeInfo.GetCustomAttributes().FirstOrDefault() ?? new TagsAttribute();
var types = new List();
var baseType = typeInfo;
while (baseType != null)
{
types.Add(baseType.AsType());
baseType = baseType.BaseType?.GetTypeInfo();
}
foreach (var t in types)
{
foreach (var method in t.GetRuntimeMethods())
{
var exampleAttribute = method.GetCustomAttributes().FirstOrDefault();
if (exampleAttribute != null)
{
var exampleTags = method.GetCustomAttributes().FirstOrDefault() ?? new TagsAttribute();
var tags = new List(examplesTags.Tags);
tags.AddRange(exampleTags.Tags);
yield return
new ExampleInfo(
examplesAttribute.Category,
exampleAttribute.Title,
tags.ToArray(),
method,
exampleAttribute.ExcludeFromAutomatedTests);
}
}
}
}
///
/// Gets all examples.
///
public static IEnumerable GetList()
{
foreach (var type in typeof(Examples).GetTypeInfo().Assembly.DefinedTypes)
{
if (!type.GetCustomAttributes().Any())
{
continue;
}
foreach (var example in GetCategory(type.AsType()))
{
yield return example;
}
}
}
///
/// Gets all examples suitable for automated test.
///
public static IEnumerable GetListForAutomatedTest()
{
return GetList().Where(ex => !ex.ExcludeFromAutomatedTests);
}
///
/// Gets the first example of each category suitable for automated test.
///
public static IEnumerable GetFirstExampleOfEachCategoryForAutomatedTest()
{
return GetListForAutomatedTest()
.GroupBy(example => example.Category)
.Select(group => group.First());
}
///
/// Gets the 'rendering capabilities' examples suitable for automated test.
///
public static IEnumerable GetRenderingCapabilitiesForAutomatedTest()
{
return GetCategory(typeof(RenderingCapabilities)).Where(ex => !ex.ExcludeFromAutomatedTests);
}
}
}