MethodUtility.cs :  » Testing » xUnit.net » Xunit » Sdk » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Testing » xUnit.net 
xUnit.net » Xunit » Sdk » MethodUtility.cs
using System.Collections.Generic;

namespace Xunit.Sdk{
    /// <summary>
    /// Utility class which inspects methods for test information
    /// </summary>
    public static class MethodUtility
    {
        /// <summary>
        /// Gets the display name.
        /// </summary>
        /// <param name="method">The method to be inspected</param>
        /// <returns>The display name</returns>
        public static string GetDisplayName(IMethodInfo method)
        {
            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(FactAttribute)))
                return attribute.GetPropertyValue<string>("Name");

            return null;
        }

        /// <summary>
        /// Gets the skip reason from a test method.
        /// </summary>
        /// <param name="method">The method to be inspected</param>
        /// <returns>The skip reason</returns>
        public static string GetSkipReason(IMethodInfo method)
        {
            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(FactAttribute)))
                return attribute.GetPropertyValue<string>("Skip");

            return null;
        }

        /// <summary>
        /// Gets the test commands for a test method.
        /// </summary>
        /// <param name="method">The method to be inspected</param>
        /// <returns>The <see cref="ITestCommand"/> objects for the test method</returns>
        public static IEnumerable<ITestCommand> GetTestCommands(IMethodInfo method)
        {
            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(FactAttribute)))
                foreach (ITestCommand command in attribute.GetInstance<FactAttribute>().CreateTestCommands(method))
                    yield return command;
        }

        /// <summary>
        /// Gets the timeout value for a test method.
        /// </summary>
        /// <param name="method">The method to be inspected</param>
        /// <returns>The timeout, in milliseconds</returns>
        public static int GetTimeoutParameter(IMethodInfo method)
        {
            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(FactAttribute)))
                return attribute.GetPropertyValue<int>("Timeout");

            return -1;
        }

        /// <summary>
        /// Gets the traits on a test method.
        /// </summary>
        /// <param name="method">The method to be inspected</param>
        /// <returns>A dictionary of the traits</returns>
        public static MultiValueDictionary<string, string> GetTraits(IMethodInfo method)
        {
            var traits = new MultiValueDictionary<string, string>();

            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(TraitAttribute)))
                traits.AddValue(attribute.GetPropertyValue<string>("Name"),
                                attribute.GetPropertyValue<string>("Value"));

            return traits;
        }

        /// <summary>
        /// Determines whether a test method has a timeout.
        /// </summary>
        /// <param name="method">The method to be inspected</param>
        /// <returns>True if the method has a timeout; false, otherwise</returns>
        public static bool HasTimeout(IMethodInfo method)
        {
            if (!IsTest(method))
                return false;

            return (GetTimeoutParameter(method) != 0);
        }

        /// <summary>
        /// Determines whether a test method has traits.
        /// </summary>
        /// <param name="method">The method to be inspected</param>
        /// <returns>True if the method has traits; false, otherwise</returns>
        public static bool HasTraits(IMethodInfo method)
        {
            return method.HasAttribute(typeof(TraitAttribute));
        }

        /// <summary>
        /// Determines whether a test method should be skipped.
        /// </summary>
        /// <param name="method">The method to be inspected</param>
        /// <returns>True if the method should be skipped; false, otherwise</returns>
        public static bool IsSkip(IMethodInfo method)
        {
            if (!IsTest(method))
                return false;

            return GetSkipReason(method) != null;
        }

        /// <summary>
        /// Determines whether a method is a test method. A test method must be decorated
        /// with the <see cref="FactAttribute"/> (or derived class) and must not be abstract.
        /// </summary>
        /// <param name="method">The method to be inspected</param>
        /// <returns>True if the method is a test method; false, otherwise</returns>
        public static bool IsTest(IMethodInfo method)
        {
            return !method.IsAbstract &&
                   method.HasAttribute(typeof(FactAttribute));
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.