protected override XElement CreateTestTypeXml(MethodInfo testMethod)
        {
            var methodAttrs = testMethod.GetCustomAttributes(true);

            XElement xtest = new XElement(XNames.ChessUnitTest);

            // Elements: instrumentAssembly
            // Not included here, already included at the testMethod level.

            // Elements: chessContext
            var contexts = methodAttrs
                           .OfType <ITestContextsProviderAttribute>()
                           .SelectMany(attr => attr.GetContexts(testMethod))
                           .OfType <ChessTestContext>()
                           .OrderBy(ctx => ctx.Name);

            if (contexts.GroupBy(ctx => ctx.Name).Any(g => g.Count() > 1))
            {
                throw new Exception(String.Format("The method {0} on class {1} has contexts with duplicate names.", testMethod.Name, testMethod.ReflectedType.FullName));
            }
            xtest.Add(
                contexts.Select(TestAssemblyReader.TestContextToXElement)
                );

            // Elements: expectedChessResult
            xtest.Add(
                from attr in methodAttrs.OfType <ExpectedChessResultAttribute>()
                select new XElement(XNames.ExpectedChessResult
                                    , XmlUtil.CreateAttributeIfNotNull(XNames.AKey, attr.Key)
                                    , attr._exitCode.HasValue ? new XAttribute("ExitCode", attr.ExitCode) : null
                                    , attr.SchedulesRan > 0 ? new XAttribute("SchedulesRan", attr.SchedulesRan) : null
                                    , attr.LastThreadCount > 0 ? new XAttribute("LastThreadCount", attr.LastThreadCount) : null
                                    , attr.LastExecSteps > 0 ? new XAttribute("LastExecSteps", attr.LastExecSteps) : null
                                    , attr.LastHBExecSteps > 0 ? new XAttribute("LastHBExecSteps", attr.LastHBExecSteps) : null
                                    ));

            return(xtest);
        }
示例#2
0
        internal static XElement TestContextToXElement(ChessTestContext chessCtx)
        {
            XElement xctx = new XElement(XNames.ChessContext
                                         , XmlUtil.CreateAttributeIfNotNull(XNames.AName, chessCtx.Name)
                                         , XmlUtil.CreateAttributeIfNotNull(XNames.AExpectedResultKey, chessCtx.ExpectedResultKey)
                                         , XmlUtil.CreateAttributeIfNotNull(XNames.AExpectedChessResultKey, chessCtx.ExpectedChessResultKey)
                                         );

            if (!String.IsNullOrWhiteSpace(chessCtx.PreRunScript))
            {
                xctx.Add(new XElement(XNames.MChessPreRunScript, chessCtx.PreRunScript));
            }

            // Add mchess options
            MChessOptions opts  = chessCtx.Options;
            XElement      xopts = opts.ToXElement();

            if (xopts.HasAttributes)
            {
                // Add the serialized options, keeping the same attribute names
                foreach (var attr in xopts.Attributes())
                {
                    xctx.Add(new XAttribute(attr));
                }
            }

            if (xopts.HasElements)
            {
                // Add the serialized options, keeping the same element names and namespaces
                foreach (var el in xopts.Elements())
                {
                    xctx.Add(new XElement(el));
                }
            }

            return(xctx);
        }
示例#3
0
        private XElement ReadTestMethod(MethodInfo testMethod)
        {
            XElement xtestMethod = new XElement(XNames.TestMethod
                                                , new XAttribute(XNames.AName, testMethod.Name)
                                                );

            // Element: parameters
            var testParams     = testMethod.GetParameters();
            int parameterCount = testParams.Length;

            if (parameterCount != 0)
            {
                xtestMethod.Add(new XElement(XNames.Parameters,
                                             from p in testParams
                                             select new XElement(XNames.Param
                                                                 , new XAttribute(XNames.AName, p.Name)
                                                                 , new XAttribute(XNames.AType, p.ParameterType.FullName)
                                                                 )
                                             ));
            }

            var methodAttrs = testMethod.GetCustomAttributes(true);

            // Elements: testArgs
            // We only allow them for methods that actually take parameters
            if (parameterCount != 0)
            {
                xtestMethod.Add(
                    from argsProviderAttr in methodAttrs.OfType <ITestMethodArgsProvider>()
                    from args in argsProviderAttr.GetArgs(testMethod)
                    where args.Length == parameterCount  // Only take valid args sets
                    select new XElement(XNames.TestArgs,
                                        args.Select(arg => new XElement(XNames.Arg, arg.ToString()))
                                        )
                    );
            }

            // Element: expectedTestResults
            var expResultAttrs = methodAttrs
                                 .OfType <ExpectedResultAttribute>()
                                 .OrderBy(attr => attr.Key);

            if (expResultAttrs.GroupBy(ctx => ctx.Key).Any(g => g.Count() > 1))
            {
                throw new Exception(String.Format("The method {0} on class {1} has {2} attributes with duplicate keys.", testMethod.Name, testMethod.ReflectedType.FullName, typeof(ExpectedResultAttribute).Name));
            }
            foreach (var expResultAttr in expResultAttrs)
            {
                xtestMethod.Add(new XElement(XNames.ExpectedTestResult
                                             , XmlUtil.CreateAttributeIfNotNull(XNames.AKey, expResultAttr.Key)
                                             , new XAttribute(XNames.AResultType, expResultAttr.Result)
                                             , XmlUtil.CreateElementIfNotNull(XTestResultNames.ResultMessage, expResultAttr.Message)
                                             ));
            }

            // Element: expectedRegressionTestResult
            var expRegressionResultAttr = methodAttrs.OfType <RegressionTestExpectedResultAttribute>().SingleOrDefault();

            if (expRegressionResultAttr != null)
            {
                xtestMethod.Add(new XElement(XNames.ExpectedRegressionTestResult
                                             , new XAttribute(XNames.AResultType, String.Join(" ", expRegressionResultAttr.ExpectedResultTypes))
                                             ));
            }

            // Element: instrumentAssembly
            // Included at this level to make them easily available to any test type that uses chess
            // Elements: instrumentAssembly
            xtestMethod.Add(
                methodAttrs.OfType <ChessInstrumentAssemblyAttribute>()
                .Select(attr => attr.ToTestlistXml())
                );
            xtestMethod.Add(
                methodAttrs.OfType <ChessDefaultPreemptabilityAttribute>()
                .Select(attr => attr.ToTestlistXml())
                );
            xtestMethod.Add(
                methodAttrs.OfType <ChessTogglePreemptabilityAttribute>()
                .Select(attr => attr.ToTestlistXml())
                );


            // Test Types elements
            xtestMethod.Add(
                methodAttrs
                .OfType <ITestTypeAttribute>()
                .Select(testTypeAttr => testTypeAttr.GetTestTypeXml(testMethod))
                .OrderBy(x => x.Name.LocalName) // To keep things consistent since attributes may be in any order
                );

            return(xtestMethod);
        }