コード例 #1
0
        protected override IEnumerable <string> SyntaxCore()
        {
            var builder = new SpockCollectionString();

            builder.AppendLine("partial void {0}();", this.id);
            return(builder);
        }
コード例 #2
0
        public override ISpockElements <string> Declaration(IMethodSignature signature)
        {
            var result = new SpockCollectionString();

            result.AppendLine(
                "{0} {1} {2}{3:d}",
                this.Scope,
                this.ReturnType,
                this.Name,
                signature);

            result.AppendLine("{");
            foreach (var syntax in this.callSyntax)
            {
                result.AppendLine("   {0}", syntax);
            }

            result.AppendLine("}");
            return(result);
        }
コード例 #3
0
        public override ISpockElements <string> Declaration(IMethodSignature signature)
        {
            var result = new SpockCollectionString();

            result.AppendLine(
                "{0} {1} {2}{3:d};",
                this.Scope,
                this.ReturnType,
                this.Name,
                signature);
            return(result);
        }
コード例 #4
0
        public ISpockElements <string> XmlComments()
        {
            var builder = new SpockCollectionString();

            if (this.Comments.Any())
            {
                foreach (var comment in this.Comments)
                {
                    builder.AppendLine(comment);
                }
            }

            return(builder);
        }
コード例 #5
0
        protected override IEnumerable <string> SyntaxCore()
        {
            var builder = new SpockCollectionString();

            builder.AppendLine();
            builder.AppendLine("private {0} {1}", this.testCaseType, this.testCaseName);
            builder.AppendLine("{");
            foreach (var element in this.step.DocString)
            {
                builder.AppendLine("   yield return \"{0}\";", element);
            }

            builder.AppendLine("}");
            builder.AppendLine();
            builder.AppendLine(this.Signature);

            return(builder);
        }
        protected override IEnumerable <string> Syntax()
        {
            var builder = new SpockCollectionString();

            builder.AppendLine("   var exceptions = new List<System.Exception>();");
            builder.AppendLine();
            foreach (var args in this.signatures)
            {
                builder.AppendLine("   try");
                builder.AppendLine("   {0}", "{");
                builder.AppendLine("      {0}{1:v};", this.methods.Specification.Name, args);
                builder.AppendLine("   {0}", "}");
                builder.AppendLine("   catch({0} ex)", this.framework.TestAssertException().Value);
                builder.AppendLine("   {0}", "{");
                builder.AppendLine("      var msg = string.Format(\"{0:R} \\r\\n{{0}}\\r\\n\\r\\n{{1}}\", ex.Message, ex);", args);
                builder.AppendLine("      var decorator = new AssertFailedException(msg, ex);");
                builder.AppendLine("      exceptions.Add(decorator);");
                builder.AppendLine("   {0}", "}");
                builder.AppendLine();
            }

            builder.AppendLine("   if (exceptions.Any())");
            builder.AppendLine("{0}", "   {");
            builder.AppendLine("       var count = exceptions.Count;");
            builder.AppendLine("       Console.WriteLine(\"{{0}} test failed\\r\\n\", count);");
            builder.AppendLine("       foreach (var exception in exceptions.GetRange(0, count - 1))");
            builder.AppendLine("{0}", "       {");
            builder.AppendLine("           Console.WriteLine(exception.Message);");
            builder.AppendLine("{0}", "       }");
            builder.AppendLine();
            builder.AppendLine("       throw exceptions.Last();");
            builder.AppendLine("{0}", "   }");

            return(builder);
        }
コード例 #7
0
        public ISpockElements <string> Header()
        {
            var builder = new SpockCollectionString();

            builder.AppendLine(@"// <copyright file=""{0}.cs"" company=""Erratic Motion Ltd"">", this.fixtureInvariants.FixtureName.Split('.')[0]);
            builder.AppendLine(@"// Copyright (c) Erratic Motion Ltd. All rights reserved.");
            builder.AppendLine(@"// Licensed under the MIT license. See LICENSE file in the project root for full license information.");
            builder.AppendLine(@"// </copyright>");
            builder.AppendLine();
            builder.AppendLine(@"//----------------------------------------------------------------------------");
            builder.AppendLine(@"// <auto-generated>");
            builder.AppendLine(@"//     This code was generated by a tool.");
            builder.AppendLine(@"//");
            builder.AppendLine(@"//     Changes to this file may cause incorrect behaviour and will be lost if");
            builder.AppendLine(@"//     the code is regenerated.");
            builder.AppendLine(@"// </auto-generated>");
            builder.AppendLine(@"//----------------------------------------------------------------------------");
            builder.AppendLine();
            builder.AppendLine(
                @"// spock -t:{0} -n:{1} -f:{2}.feature",
                this.Options.TestRunner,
                this.fixtureInvariants.Namespace,
                this.fixtureInvariants.FixtureName);

            builder.AppendLine();

            return(builder);
        }
コード例 #8
0
        public ISpockElements <string> Spock()
        {
            var framework = TestFramework.For(this.Options);

            var builder = new SpockCollectionString();

            builder.Append(this.Header());
            builder.AppendLine("namespace {0} {{", this.FixtureInvariants.Namespace);
            if (!framework.SupportsParameterDriven)
            {
                builder.AppendLine("using System;");
                builder.AppendLine("using System.Collections.Generic;");
                builder.AppendLine("using System.Linq;");
            }

            builder.AppendLine("using global::ErraticMotion.Test.Tools.Gherkin.Annotations;");
            builder.AppendLine("using {0};", framework.Namespace);
            builder.Append(this.XmlDocComments);
            builder.AppendLine("[FeatureId(\"{0}\")]", this.FixtureInvariants.FeatureId);
#if GHERKIN_ATTRIBUTES
            builder.Append(this.GherkinAttributes);
#endif
            builder.AppendLine("[GeneratedFromFeature]");

            var testClass = framework.TestClass();
            if (testClass.Supported)
            {
                builder.AppendLine(testClass.Value);
            }

            var testCategory = framework.TestCategory(Target.Class);
            if (testCategory.Supported)
            {
                builder.AppendLine(testCategory.Value);
            }

            builder.AppendLine("public partial class {0}", this.FixtureInvariants.FixtureName);
            builder.AppendLine("{0}", "{");
            if (this.gherkin.Background != null)
            {
                foreach (var s in SyntaxScenarioStep.CreateBuilder.For(this.gherkin.Background.Steps))
                {
                    builder.AddRange(s.Syntax);
                }
            }

            builder.Append(this.FixtureMethods);
            builder.AppendLine("{0}", "}");
            builder.AppendLine("{0}", "}");
            return(builder);
        }
コード例 #9
0
        protected override IEnumerable <string> SyntaxCore()
        {
            if (!this.initialized)
            {
                throw new InvalidOperationException("Sub-classes need to call Initialize in their constructor");
            }

            var builder = new SpockCollectionString();

            builder.AppendLine();

            var testCaseName = $"{this.id}TestCases()";

            builder.AppendLine("private {0} {1}", this.testCaseType, testCaseName);
            builder.AppendLine("{");
            foreach (var element in this.methodTestCases)
            {
                if (this.moreThanOneArg)
                {
                    builder.AppendLine(
                        "   yield return new {0}{1:v};",
                        this.typeSyntax,
                        element);
                }
                else
                {
                    foreach (var argument in element.Arguments)
                    {
                        builder.AppendLine(
                            "   yield return {0:v};",
                            argument);
                    }
                }
            }

            builder.AppendLine("}");

            if (this.GenerateType && this.moreThanOneArg)
            {
                var sig       = new TestCaseSignature(this.step.TestCase.Parameters, MethodArgType.Parameter);
                var signature = sig.Join(this.methodTestCases[0]);
                builder.AppendLine();
                builder.AppendLine("private partial class {0}", this.typeSyntax);
                builder.AppendLine("{");
                builder.AppendLine("   public {0}{1}", this.typeSyntax, signature);
                builder.AppendLine("   {");
                signature.Arguments.ForAll(x => builder.AppendLine("      {0:p} = {0:v};", x));
                builder.AppendLine("   }");
                signature.Arguments.ForAll(x =>
                {
                    builder.AppendLine();
                    builder.AppendLine("   public {0:t} {0:p} {{ get; private set; }}", x);
                });

                builder.AppendLine("}");
            }

            builder.AppendLine();
            builder.AppendLine(this.Signature);
            return(builder);
        }
コード例 #10
0
        public ISpockElements <string> Syntax()
        {
            var framework     = TestFramework.For(this.Options);
            var builder       = new SpockCollectionString();
            var referenceTags = new List <string>();

            var stepBuilder = SyntaxScenarioSteps.For(this.background, this.scenario.Steps, this.Methods, this.Signature, this.TestCases.Any());

            builder.AddRange(stepBuilder.Syntax);
            builder.AppendLine();
            builder.Append(stepBuilder.Method.Declaration(this.Signature));
            builder.AppendLine();
            builder.Append(this.XmlComments());
            builder.AppendLine("[ScenarioId(\"{0}\")]", this.Identity.Id);
#if GHERKIN_ATTRIBUTES
            builder.Append(GherkinAttribtes());
            foreach (var args in testCases)
            {
                builder.AppendLine("[Where{0:v}]", args);
            }
#endif
            foreach (var tag in referenceTags)
            {
                builder.AppendLine(tag);
            }

            var testIgnore = framework.TestIgnore();
            if (testIgnore.Supported && this.Options.SetIgnore)
            {
                builder.AppendLine(testIgnore.Value);
            }

            var testCategory = framework.TestCategory(Target.Method);
            if (testCategory.Supported)
            {
                builder.AppendLine(testCategory.Value);
            }

            if (framework.SupportsParameterDriven)
            {
                if (this.TestCases.Any())
                {
                    var facts = framework.TestFacts(this.TestCases).ToArray();
                    if (facts[0].Supported)
                    {
                        builder.AppendLine("[System.CLSCompliant(false)]");
                        foreach (var args in facts)
                        {
                            builder.AppendLine(args.Value);
                        }
                    }
                }
                else
                {
                    var testMethod = framework.TestMethod();
                    if (testMethod.Supported)
                    {
                        builder.AppendLine(testMethod.Value);
                    }
                }

                builder.Append(this.Methods.Conceptual.Declaration(this.Signature));
                builder.AppendLine("{0}", "{");
                builder.AppendLine("   {0}", this.Methods.Specification.CallSyntax(this.Signature));
                builder.AppendLine("{0}", "}");
            }
            else
            {
                var testMethod = framework.TestMethod();
                if (testMethod.Supported)
                {
                    builder.AppendLine(testMethod.Value);
                }

                builder.Append(this.Methods.Conceptual.Declaration(new EmptyMethodSignature()));
                builder.AppendLine("{0}", "{");
                var testCaseBuilder = SyntaxTestCasesBuilder.For(this.TestCases, this.Methods, framework);
                builder.AddRange(testCaseBuilder.Build());
                builder.AppendLine("{0}", "}");
            }

            return(builder);
        }