Exemplo n.º 1
0
        private void ExploreTestMethod(IProject project, XunitTestClassElement classUnitTestElement, UnitTestElementConsumer consumer, IMethodInfo methodInfo)
        {
            var methodUnitTestElement = unitTestElementFactory.GetOrCreateTestMethod(project, classUnitTestElement, new ClrTypeName(methodInfo.TypeName), methodInfo.Name,
                                                                                     MethodUtility.GetSkipReason(methodInfo), methodInfo.SafelyGetTraits(), false);

            consumer(methodUnitTestElement);
        }
Exemplo n.º 2
0
            public void TestMethodHasSkipReasonParameter()
            {
                Type       testClassType = typeof(SkipTestClass);
                MethodInfo methodInfo    = testClassType.GetMethod("TestAttributeWithSkipReason");

                Assert.True(MethodUtility.IsSkip(Reflector.Wrap(methodInfo)));
            }
Exemplo n.º 3
0
            public void TestMethodsCanHaveNonVoidReturnType()
            {
                Type       testClassType = typeof(NonVoidReturnClass);
                MethodInfo methodInfo    = testClassType.GetMethod("NonVoidTest");

                Assert.True(MethodUtility.IsTest(Reflector.Wrap(methodInfo)));
            }
Exemplo n.º 4
0
            public void TestMethodIsStatic()
            {
                Type       testClassType = typeof(StaticTestMethodClass);
                MethodInfo methodInfo    = testClassType.GetMethod("StaticTestMethod");

                Assert.True(MethodUtility.IsTest(Reflector.Wrap(methodInfo)));
            }
Exemplo n.º 5
0
        private void ExploreTestMethod(IProject project, XunitTestClassElement classUnitTestElement, IUnitTestElementsObserver observer, IMethodInfo methodInfo)
        {
            var methodUnitTestElement = unitTestElementFactory.GetOrCreateTestMethod(project, classUnitTestElement, new ClrTypeName(methodInfo.TypeName), methodInfo.Name,
                                                                                     MethodUtility.GetSkipReason(methodInfo), methodInfo.GetTraits(), false);

            observer.OnUnitTestElement(methodUnitTestElement);
        }
        private IUnitTestElement ProcessTestMethod(IMethod method, IList<IUnitTestElement> subElements)
        {
            var type = method.GetContainingType();
            var @class = type as IClass;
            if (type == null || @class == null)
                return null;

            var typeInfo = @class.AsTypeInfo();
            if (@class.IsAbstract && TypeUtility.ContainsTestMethods(typeInfo))
                return ProcessTestMethodInAbstractClass(method, subElements);

            if (!IsValidTestClass(@class))
                return null;

            var command = TestClassCommandFactory.Make(typeInfo);
            if (command == null)
                return null;

            var testClassElement = classes[type];
            if (testClassElement == null)
                return null;

            var methodInfo = method.AsMethodInfo(typeInfo);
            if (command.IsTestMethod(methodInfo))
            {
                var clrTypeName = type.GetClrName();
                return unitTestElementFactory.GetOrCreateTestMethod(project, testClassElement, clrTypeName, method.ShortName, 
                    MethodUtility.GetSkipReason(methodInfo), methodInfo.GetTraits(), false);
            }

            return null;
        }
Exemplo n.º 7
0
            public IEnumerable <ITestCommand> AssertCommands(string name, IMethodInfo method)
            {
                foreach (var assertion in _asserts)
                {
                    // do not capture the iteration variable because
                    // all tests would point to the same assertion
                    var    capturableAssertion = assertion;
                    Action test =
                        () =>
                    {
                        using (SpecificationPrimitiveExecutor.Execute(_context))
                        {
                            if (_do != null)
                            {
                                SpecificationPrimitiveExecutor.Execute(_do);
                            }

                            SpecificationPrimitiveExecutor.Execute(capturableAssertion);
                        }
                    };

                    string testDescription = String.Format("{0}, {1}", name, assertion.Message);

                    yield return(new ActionTestCommand(method, testDescription, MethodUtility.GetTimeoutParameter(method), test));
                }
            }
Exemplo n.º 8
0
            public void TestMethodIsAbstract()
            {
                Type       testClassType = typeof(AbstractMethodClass);
                MethodInfo methodInfo    = testClassType.GetMethod("AbstractMethod");

                Assert.False(MethodUtility.IsTest(Reflector.Wrap(methodInfo)));
            }
Exemplo n.º 9
0
        public TheoryCommand(IMethodInfo testMethod, object[] parameters, Type[] genericTypes)
            : base(testMethod, MethodUtility.GetDisplayName(testMethod), MethodUtility.GetTimeoutParameter(testMethod))
        {
            int idx;

            Parameters = parameters ?? new object[0];

            if (genericTypes != null && genericTypes.Length > 0)
            {
                string[] typeNames = new string[genericTypes.Length];
                for (idx = 0; idx < genericTypes.Length; idx++)
                {
                    typeNames[idx] = ConvertToSimpleTypeName(genericTypes[idx]);
                }

                DisplayName = String.Format("{0}<{1}>", DisplayName, string.Join(", ", typeNames));
            }

            ParameterInfo[] parameterInfos = testMethod.MethodInfo.GetParameters();
            string[]        displayValues  = new string[Math.Max(Parameters.Length, parameterInfos.Length)];

            for (idx = 0; idx < Parameters.Length; idx++)
            {
                displayValues[idx] = ParameterToDisplayValue(GetParameterName(parameterInfos, idx), Parameters[idx]);
            }

            for (; idx < parameterInfos.Length; idx++)  // Fill-in any missing parameters with "???"
            {
                displayValues[idx] = parameterInfos[idx].Name + ": ???";
            }

            DisplayName = String.Format("{0}({1})", DisplayName, string.Join(", ", displayValues));
        }
Exemplo n.º 10
0
        private void AppendTests(XunitTestClassElement classElement, ITypeInfo typeInfo, IEnumerable <IDeclaredType> types)
        {
            foreach (var declaredType in types)
            {
                if (declaredType.GetClrName().Equals(PredefinedType.OBJECT_FQN))
                {
                    continue;
                }

                var typeElement = declaredType.GetTypeElement();
                if (typeElement != null)
                {
                    var methodInfo = new PsiMethodInfoAdapter();
                    foreach (var method in typeElement.GetMembers().OfType <IMethod>())
                    {
                        methodInfo.Reset(method, typeInfo);
                        if (MethodUtility.IsTest(methodInfo))
                        {
                            var element = unitTestElementFactory.GetOrCreateTestMethod(project, classElement,
                                                                                       typeElement.GetClrName(), method.ShortName,
                                                                                       MethodUtility.GetSkipReason(methodInfo), methodInfo.GetTraits(),
                                                                                       false);
                            observer.OnUnitTestElementDisposition(UnitTestElementDisposition.NotYetClear(element));
                        }
                    }
                }
            }
        }
Exemplo n.º 11
0
            public void MethodDoesNotHaveTestAttribute()
            {
                Type       testClassType = typeof(NoTestAttributeClass);
                MethodInfo methodInfo    = testClassType.GetMethod("NoTestAttribute");

                Assert.False(MethodUtility.IsTest(Reflector.Wrap(methodInfo)));
            }
Exemplo n.º 12
0
            public void TestMethodWithTimeoutParameter()
            {
                Type       testClassType = typeof(TimeoutTestClass);
                MethodInfo methodInfo    = testClassType.GetMethod("TestAttributeWithTimeoutParameter");

                Assert.True(MethodUtility.HasTimeout(Reflector.Wrap(methodInfo)));
            }
Exemplo n.º 13
0
            public void ClassHasTrait()
            {
                Type       testClassType = typeof(ClassWithClassTraits);
                MethodInfo methodInfo    = testClassType.GetMethod("TestMethod");

                Assert.True(MethodUtility.HasTraits(Reflector.Wrap(methodInfo)));
            }
Exemplo n.º 14
0
            public void MethodHasATrait()
            {
                Type       testClassType = typeof(ClassWithTraits);
                MethodInfo methodInfo    = testClassType.GetMethod("SingleTraitOnAMethod");

                Assert.True(MethodUtility.HasTraits(Reflector.Wrap(methodInfo)));
            }
Exemplo n.º 15
0
            public void MethodDoesNotHaveTraits()
            {
                Type       testClassType = typeof(ClassWithTraits);
                MethodInfo methodInfo    = testClassType.GetMethod("MethodDoesNotHaveTestTraits");

                Assert.False(MethodUtility.HasTraits(Reflector.Wrap(methodInfo)));
            }
Exemplo n.º 16
0
            public void CanDecorateTestMethodWithMultipleFactDerivedAttributes()
            {
                MethodInfo method = typeof(Spy).GetMethod("TestMethod");

                List <ITestCommand> commands = new List <ITestCommand>(MethodUtility.GetTestCommands(Reflector.Wrap(method)));

                Assert.Equal(2, commands.Count);
            }
Exemplo n.º 17
0
            public void SkipReasonParameter()
            {
                Type       testClassType = typeof(SkipTestClass);
                MethodInfo methodInfo    = testClassType.GetMethod("TestAttributeWithSkipReason");

                string skipReason = MethodUtility.GetSkipReason(Reflector.Wrap(methodInfo));

                Assert.Equal("reason", skipReason);
            }
Exemplo n.º 18
0
            public void TimeoutParameter()
            {
                Type       testClassType = typeof(TimeoutTestClass);
                MethodInfo methodInfo    = testClassType.GetMethod("TestAttributeWithTimeoutParameter");

                long timeout = MethodUtility.GetTimeoutParameter(Reflector.Wrap(methodInfo));

                Assert.Equal <long>(1000, timeout);
            }
Exemplo n.º 19
0
            public void NoTraits()
            {
                Type       testClassType = typeof(ClassWithTraits);
                MethodInfo methodInfo    = testClassType.GetMethod("MethodDoesNotHaveTestTraits");

                var traits = MethodUtility.GetTraits(Reflector.Wrap(methodInfo));

                Assert.Equal(0, traits.Count);
            }
Exemplo n.º 20
0
        private void HandleTraits()
        {
            var traits = MethodUtility.GetTraits(methodInfo);

            categories   = ExtractTraitValues(traits, "category");
            owners       = ExtractTraitValues(traits, "owner");
            descriptions = ExtractTraitValues(traits, "description");

            testProperties = ConvertToTestPropeties(traits);
        }
Exemplo n.º 21
0
            public void ReturnsNullWithNoDisplayName()
            {
                string     methodName    = "WithoutDisplayName";
                Type       testClassType = typeof(ClassWithDisplayName);
                MethodInfo methodInfo    = testClassType.GetMethod(methodName);

                var result = MethodUtility.GetDisplayName(Reflector.Wrap(methodInfo));

                Assert.Null(result);
            }
Exemplo n.º 22
0
            public void ReturnsDisplayNameFromFact()
            {
                string     methodName    = "WithDisplayName";
                Type       testClassType = typeof(ClassWithDisplayName);
                MethodInfo methodInfo    = testClassType.GetMethod(methodName);

                var result = MethodUtility.GetDisplayName(Reflector.Wrap(methodInfo));

                Assert.Equal("My Display Name", result);
            }
Exemplo n.º 23
0
 protected override IEnumerable <ITestCommand> EnumerateTestCommands(IMethodInfo testMethod)
 {
     if (Helper.Credentials == null)
     {
         yield return(new SkipCommand(testMethod, MethodUtility.GetDisplayName(testMethod), "Automation settings not configured. Please set the OCTOKIT_GITHUBUSERNAME and OCTOKIT_GITHUBPASSWORD environment variables to a GitHub test account (i.e, DO NOT USE A \"REAL\" ACCOUNT)."));
     }
     else
     {
         yield return(new FactCommand(testMethod));
     }
 }
Exemplo n.º 24
0
            public void SingleTraitOnATestMethod()
            {
                Type       testClassType = typeof(ClassWithTraits);
                MethodInfo methodInfo    = testClassType.GetMethod("SingleTraitOnAMethod");

                var traits = MethodUtility.GetTraits(Reflector.Wrap(methodInfo));

                Assert.Single(traits);
                string description = Assert.Single(traits["Description"]);

                Assert.Equal("more than just the test method name", description);
            }
Exemplo n.º 25
0
        private IUnitTestElement ProcessTestMethodInAbstractClass(ITypeInfo typeInfo, IMethod method, IList <IUnitTestElement> subElements)
        {
            var containingType = method.GetContainingType();

            if (containingType == null)
            {
                return(null);
            }

            IList <XunitTestClassElement> derivedElements;

            if (!derivedTestClassElements.TryGetValue(containingType, out derivedElements))
            {
                return(null);
            }

            var methodInfo = method.AsMethodInfo(typeInfo);

            if (!MethodUtility.IsTest(methodInfo))
            {
                return(null);
            }

            var inheritedTestMethodContainerElement = unitTestElementFactory.GetOrCreateInheritedTestMethodContainer(project,
                                                                                                                     containingType.GetClrName(), method.ShortName);

            foreach (var derivedClassElement in derivedElements)
            {
                XunitTestMethodElement methodInDerivedClass = null;
                foreach (var testMethodElement in derivedClassElement.Children.OfType <XunitTestMethodElement>())
                {
                    if (testMethodElement.Id.Equals(inheritedTestMethodContainerElement.Id))
                    {
                        testMethodElement.State = UnitTestElementState.Valid;
                        methodInDerivedClass    = testMethodElement;
                        break;
                    }
                }

                if (methodInDerivedClass == null)
                {
                    // TODO: Add traits
                    methodInDerivedClass = unitTestElementFactory.GetOrCreateTestMethod(project, derivedClassElement,
                                                                                        containingType.GetClrName().GetPersistent(), method.ShortName, string.Empty,
                                                                                        new OneToSetMap <string, string>(), false);
                }

                subElements.Add(methodInDerivedClass);
            }

            return(inheritedTestMethodContainerElement);
        }
Exemplo n.º 26
0
        public IEnumerable <ITestCommand> EnumerateTestCommands(IMethodInfo testMethod)
        {
            string skipReason = MethodUtility.GetSkipReason(testMethod);

            if (skipReason != null)
            {
                yield return(new SkipCommand(testMethod, MethodUtility.GetDisplayName(testMethod), skipReason));
            }
            else
            {
                yield return(new FixtureCommand(new FactCommand(testMethod), _fixtures));
            }
        }
Exemplo n.º 27
0
        private static bool IsUnitTestMethod(IMethod testMethod)
        {
            if (testMethod == null)
            {
                return(false);
            }
            var containingType = testMethod.GetContainingType() as IClass;

            if (containingType == null)
            {
                return(false);
            }
            return(MethodUtility.IsTest(testMethod.AsMethodInfo(containingType.AsTypeInfo())));
        }
Exemplo n.º 28
0
 protected override IEnumerable <ITestCommand> EnumerateTestCommands(IMethodInfo testMethod)
 {
     if (Helper.Organization == null)
     {
         return new[]
                {
                    new SkipCommand(testMethod, MethodUtility.GetDisplayName(testMethod), "Automation settings not configured. Please set the OCTOKIT_GITHUBORGANIZATION environment variable to a GitHub organization owned by the test account specified in OCTOKIT_GITHUBUSERNAME.")
                }
     }
     ;
     else
     {
         return(base.EnumerateTestCommands(testMethod));
     }
 }
Exemplo n.º 29
0
            public void TraitsOnClass()
            {
                Type       testClassType = typeof(ClassWithClassTraits);
                MethodInfo methodInfo    = testClassType.GetMethod("TestMethod");

                var traits = MethodUtility.GetTraits(Reflector.Wrap(methodInfo));

                Assert.Equal(2, traits.Count);
                string description = Assert.Single(traits["Description"]);

                Assert.Equal("more than just the test method name", description);
                string author = Assert.Single(traits["Author"]);

                Assert.Equal("James Newkirk", author);
            }
Exemplo n.º 30
0
 public static MultiValueDictionary <string, string> SafelyGetTraits(this IMethodInfo methodInfo)
 {
     try
     {
         // GetTraits relies on our attribute parsing to get at a property
         // value. Unless that value is set in the attribute constructor
         // via a named parameter (property) or a parameter with a name
         // similar to the required property, we'll return null, and that
         // can cause exceptions. If we get an exception, fail as nicely
         // as we can, with an empty collection of traits
         return(MethodUtility.GetTraits(methodInfo));
     }
     catch (Exception)
     {
         return(new MultiValueDictionary <string, string>());
     }
 }