Exemplo n.º 1
0
        public TestCaseDTO[] DiscoverTests()
        {
            var conv   = new DefaultConventions().Initialize();
            var finder = new SpecFinder(SandboxedAssembly.GetTypes(), "");
            var cb     = new ContextBuilder(finder, new Tags());
            var dia    = new DiaSession(SandboxedAssembly.Location);

            var examples = cb.Contexts()
                           .Build()
                           .AllContexts()
                           .SelectMany(context => context.Examples);

            var result = from example in examples
                         let method = GetAction(example)
                                      let location = dia.GetNavigationData(method.DeclaringType.FullName, method.Name)
                                                     ?? new DiaNavigationData(null, 0, 0)
                                                     select new TestCaseDTO
            {
                Name       = example.FullName(),
                FileName   = location.FileName,
                LineNumber = location.MinLineNumber,
                Traits     = example.Tags.ToArray()
            };

            return(result.ToArray());
        }
Exemplo n.º 2
0
        public void output_verification(Type output, Type [] testClasses, string tags)
        {
            var finder           = new SpecFinder(testClasses, "");
            var tagsFilter       = new Tags().Parse(tags);
            var builder          = new ContextBuilder(finder, tagsFilter, new DefaultConventions());
            var consoleFormatter = new ConsoleFormatter();

            var actual = new System.Collections.Generic.List <string>();

            consoleFormatter.WriteLineDelegate = actual.Add;

            var runner = new ContextRunner(tagsFilter, consoleFormatter, false);

            runner.Run(builder.Contexts().Build());

            var expectedString = GetExpectedOutput(output)
                                 .ScrubNewLines()
                                 .ScrubStackTrace()
                                 .ScrubTimes();

            var actualString = String.Join("\n", actual)
                               .ScrubStackTrace()
                               .Trim()
                               .ScrubTimes();

            actualString.Should().Be(expectedString);
        }
Exemplo n.º 3
0
        public static IEnumerable Examples()
        {
            var currentSpec = typeof(T);
            var finder      = new SpecFinder(new[] { currentSpec });
            var tagsFilter  = new Tags().Parse(currentSpec.Name);
            var builder     = new ContextBuilder(finder, tagsFilter, new DefaultConventions());

            try
            {
                // The test case source is run during the discovery phase of NUnit. This means that we cannot
                // run the examples now - setup fixtures haven't run yet, for example. But we use the result
                // of the discovery phase to produce a number of test cases.
                var testSuite = builder.Contexts().Build();
                var examples  = testSuite.SelectMany(ctx => ctx.AllExamples());
                return(examples.Select(example => new NUnitTestFromExample(tagsFilter, testSuite, example)));
            }
            catch (Exception ex)
            {
                // Generate a fake example that represents the setup failure.
                var example = new Example("Test setup failure");
                example.Context   = new Context("test failure"); // for Example.FullName() to work
                example.Exception = ex;
                example.HasRun    = true;
                return(new object[] { new NUnitTestFromExample(tagsFilter, null, example) });
            }
        }
Exemplo n.º 4
0
        public void debug()
        {
            //the specification class you want to test
            //this can be a regular expression
            var testClassYouWantToDebug = ".*";

            //initialize NSpec's specfinder
            var finder = new SpecFinder(
                Assembly.GetExecutingAssembly().Location,
                new Reflector(),
                testClassYouWantToDebug);

            //initialize NSpec's builder
            var builder = new ContextBuilder(
                finder,
                new DefaultConventions());

            //initialize the root context
            var contexts = builder.Contexts();

            //build the tests
            contexts.Build();

            //run the tests that were found
            contexts.Run();

            //print the output
            new ConsoleFormatter().Write(contexts);

            //assert that there aren't any failures or pendings
            // yeah i'm that much of a prick on myself
            contexts.Failures().Count().should_be(0);
            contexts.Pendings().Count().should_be(0);
        }
Exemplo n.º 5
0
        protected when_running_specs Run(params Type[] types)
        {
            this.types = types;

            var tagsFilter = new Tags().Parse(tags);

            builder = new ContextBuilder(new SpecFinder(types), tagsFilter, new DefaultConventions());

            contextCollection = builder.Contexts();

            contextCollection.Build();

            classContext = contextCollection
                           .AllContexts()
                           .Where(c => c is ClassContext)
                           .Cast <ClassContext>()
                           .FirstOrDefault(c => types.Contains(c.SpecType));

            methodContext = contextCollection.AllContexts().FirstOrDefault(c => c is MethodContext);

            runner = new ContextRunner(tagsFilter, formatter, failFast);

            runner.Run(contextCollection);

            return(this);
        }
        public NSpecResultModel Run()
        {
            var reflector = new Reflector(this._dllFile);

            var finder = new SpecFinder(reflector);

            var builder = new ContextBuilder(finder, new Tags().Parse(_tags), new DefaultConventions());

            var runner = new ContextRunner(builder, _formatter, false);

            var contexts = builder.Contexts().Build();

            if (contexts.AnyTaggedWithFocus())
            {
                builder = new ContextBuilder(finder, new Tags().Parse(NSpec.Domain.Tags.Focus), new DefaultConventions());

                runner = new ContextRunner(builder, _formatter, true);
            }

            var contextCollection = runner.Run(contexts);

            var serializableContextCollection = new SerializableContextCollection();

            BuildResponse(serializableContextCollection, contextCollection);

            return(new NSpecResultModel {
                ContextCollection = serializableContextCollection, Output = _formatter.GetFormattedString
            });
        }
        public NSpecResultModel BuildContexts()
        {
            var reflector = new Reflector(this._dllFile);

            var finder = new SpecFinder(reflector);

            var builder = new ContextBuilder(finder, new Tags().Parse(_tags), new DefaultConventions());

            var contexts = builder.Contexts().Build();

            if (contexts.AnyTaggedWithFocus())
            {
                builder = new ContextBuilder(finder, new Tags().Parse(NSpec.Domain.Tags.Focus), new DefaultConventions());
            }

            var contextCollection             = builder.Contexts().Build();
            var serializableContextCollection = new SerializableContextCollection();

            BuildResponse(serializableContextCollection, contextCollection);
            var tags = _tagsCollection.Distinct().Select(t => new TagPair {
                Tag = t, Assembly = _dllFile
            }).ToList();

            return(new NSpecResultModel {
                ContextCollection = serializableContextCollection, Output = string.Empty, Tags = tags
            });
        }
Exemplo n.º 8
0
        Test GetAssemblyTest(IAssemblyInfo assembly, Test parentTest, Version frameworkVersion, bool populateRecursively)
        {
            NSpecAssemblyTest assemblyTest;

            if (!assemblyTests.TryGetValue(assembly, out assemblyTest))
            {
                assemblyTest      = new NSpecAssemblyTest(assembly.Name, assembly, frameworkVersion);
                assemblyTest.Kind = TestKinds.Assembly;

                ModelUtils.PopulateMetadataFromAssembly(assembly, assemblyTest.Metadata);

                string frameworkName = String.Format("NSpec v{0}", frameworkVersion);
                assemblyTest.Metadata.SetValue(MetadataKeys.Framework, frameworkName);
                assemblyTest.Metadata.SetValue(MetadataKeys.File, assembly.Path);
                assemblyTest.Kind = TestKinds.Assembly;

                parentTest.AddChild(assemblyTest);
                assemblyTests.Add(assembly, assemblyTest);
            }

            if (populateRecursively)
            {
                var reflector = new NSpec.Domain.Reflector(assembly.Path);
                var finder    = new SpecFinder(reflector);
                var builder   = new ContextBuilder(finder, new DefaultConventions());

                ContextCollection contexts = builder.Contexts();
                contexts.Build();
                contexts.Do(c => assemblyTest.AddChild(this.CreateGallioTestFrom(c)));
            }

            return(assemblyTest);
        }
Exemplo n.º 9
0
    private static void Debug(SpecFinder finder)
    {
        var builder = new ContextBuilder(finder, new DefaultConventions());
        var runner  = new ContextRunner(new Tags(), new ConsoleFormatter(), false);
        var results = runner.Run(builder.Contexts().Build());

        // assert that there aren't any failures
        results.Failures().Count().Should().Be(0, "all examples passed");
    }
Exemplo n.º 10
0
    public void debug()
    {
        var currentSpec = this.GetType();
        var finder      = new SpecFinder(new[] { currentSpec });
        var builder     = new ContextBuilder(finder, new Tags().Parse(currentSpec.Name), new DefaultConventions());
        var runner      = new ContextRunner(builder, new ConsoleFormatter(), false);
        var results     = runner.Run(builder.Contexts().Build());

        results.Failures().Count().should_be(0);
    }
Exemplo n.º 11
0
        public void setup()
        {
            finder = MockRepository.GenerateMock <ISpecFinder>();

            finder.Stub(f => f.SpecClasses()).IgnoreArguments().Return(new[] { typeof(child), typeof(parent), typeof(sibling) });

            builder = new ContextBuilder(finder);

            builder.Contexts();
        }
Exemplo n.º 12
0
        protected void Run(params Type[] types)
        {
            var tagsFilter = new Tags().Parse("");

            var builder = new ContextBuilder(new SpecFinder(types), tagsFilter, new DefaultConventions());

            contextCollection = builder.Contexts();

            contextCollection.Build();
        }
Exemplo n.º 13
0
        public void Execute(IReceiveTestResult observer)
        {
            this.observer = observer;

            var finder = new SpecFinder(SandboxedAssembly.GetTypes(), "");
            var cb     = new ContextBuilder(finder, new Tags());

            cb.Contexts()
            .Build()
            .ForEach(context => context.Run(this, false));
        }
Exemplo n.º 14
0
        public void DebugServiceSpecs()
        {
            const string tagOrClassName = "ServiceSpecs";

            var types   = GetType().Assembly.GetTypes();
            var finder  = new SpecFinder(types, "");
            var builder = new ContextBuilder(finder, new Tags().Parse(tagOrClassName), new DefaultConventions());
            var runner  = new ContextRunner(builder, new ConsoleFormatter(), false);
            var results = runner.Run(builder.Contexts().Build());

            results.Failures().Count().should_be(0);
        }
Exemplo n.º 15
0
            public override bool Execute(RunnerOptions options)
            {
                var types      = Assembly.GetEntryAssembly().GetTypes();
                var finder     = new SpecFinder(types, options.TestClass);
                var tagsFilter = new Tags().Parse(options.TagsFlag);
                var builder    = new ContextBuilder(finder, tagsFilter, new DefaultConventions());
                var runner     = new ContextRunner(tagsFilter, GetFormatter(options), false);

                var results = runner.Run(builder.Contexts().Build());

                return(!results.Failures().Any());
            }
Exemplo n.º 16
0
        public void debug()
        {
            var currentSpec = GetType();
            var finder = new SpecFinder(new[] { currentSpec });
            var filter = new Tags().Parse(currentSpec.Name);
            var builder = new ContextBuilder(finder, filter, new DefaultConventions());
            var runner = new ContextRunner(filter, new ConsoleFormatter(), false);
            var result = runner.Run(builder.Contexts().Build());

            //assert that there aren't any failures
            result.Failures().Count().should_be(0);
        }
Exemplo n.º 17
0
        public void setup()
        {
            reflector = MockRepository.GenerateMock <IReflector>();

            RhinoMocksExtensions.Stub(reflector, r => r.GetTypesFrom("")).IgnoreArguments().Return(new[] { typeof(SpecClass) });

            var contextBuilder = new ContextBuilder(new SpecFinder("", reflector), new DefaultConventions());

            classContext = contextBuilder.Contexts().First();

            classContext.Run();
        }
Exemplo n.º 18
0
        public static void Main(string[] args)
        {
            var tagOrClassName = string.Join(",", args);
            var types          = typeof(TestRunner).Assembly.GetTypes();
            var finder         = new SpecFinder(types, string.Empty);
            var tagsFilter     = new Tags().Parse(tagOrClassName);
            var builder        = new ContextBuilder(finder, tagsFilter, new DefaultConventions());
            var runner         = new ContextRunner(tagsFilter, new ConsoleFormatter(), false);

            var results = runner.Run(builder.Contexts().Build());

            Environment.Exit(results.Failures().Count());
        }
        protected void RunWithReflector(Type specClassType)
        {
            IReflector reflector = MockRepository.GenerateMock <IReflector>();

            reflector.Stub(r => r.GetTypesFrom()).Return(new[] { specClassType });

            var contextBuilder = new ContextBuilder(new SpecFinder(reflector), new DefaultConventions());

            classContext = contextBuilder.Contexts().First();

            classContext.Build();

            classContext.Run(new SilentLiveFormatter(), failFast: false);
        }
Exemplo n.º 20
0
        protected void RunWithReflector(Type specClassType)
        {
            Mock <IReflector> reflector = new Mock <IReflector>();

            reflector.Setup(r => r.GetTypesFrom()).Returns(new[] { specClassType });

            var contextBuilder = new ContextBuilder(new SpecFinder(reflector.Object), new DefaultConventions());

            contexts = contextBuilder.Contexts();

            contexts.Build();

            contexts.Run(new SilentLiveFormatter(), failFast: false);
        }
Exemplo n.º 21
0
        public static void Main(string[] args)
        {
            var types      = typeof(Program).Assembly.GetTypes();
            var finder     = new SpecFinder(types, "");
            var tagsFilter = new Tags().Parse("");
            var builder    = new ContextBuilder(finder, tagsFilter, new DefaultConventions());
            var runner     = new ContextRunner(tagsFilter, new ConsoleFormatter(), false);
            var results    = runner.Run(builder.Contexts().Build());

            if (results.Failures().Count() > 0)
            {
                Environment.Exit(1);
            }
        }
Exemplo n.º 22
0
        public void Select(string binaryPath, string tagsText)
        {
            var reflector = new Reflector(binaryPath);

            var finder = new SpecFinder(reflector);

            var conventions = new DefaultConventions();

            TagsFilter = new Tags().Parse(tagsText);

            var builder = new ContextBuilder(finder, TagsFilter, conventions);

            Contexts = builder.Contexts().Build();
        }
Exemplo n.º 23
0
        public static void Run <T>(this ITestOutputHelper helper) where T : NSpec.nspec
        {
            var spec      = typeof(T);
            var finder    = new SpecFinder(new[] { spec });
            var tags      = new Tags().Parse(spec.Name);
            var builder   = new ContextBuilder(finder, tags, new DefaultConventions());
            var formatter = new MyFormatter(helper);
            var runner    = new ContextRunner(tags, formatter, false);
            var contexts  = builder.Contexts().Build();
            var results   = runner.Run(contexts);

            //assert that there aren't any failures
            results.Failures().Count().Should().Be(0);
        }
Exemplo n.º 24
0
        public void setup()
        {
            reflector = MockRepository.GenerateMock <IReflector>();

            reflector.Stub(r => r.GetTypesFrom()).Return(new[] { typeof(SpecClass) });

            var contextBuilder = new ContextBuilder(new SpecFinder(reflector), new DefaultConventions());

            classContext = contextBuilder.Contexts().First();

            classContext.Build();

            classContext.Run(new SilentLiveFormatter(), failFast: false);
        }
Exemplo n.º 25
0
        public void debug()
        {
            //the specification class you want to test
            //this can be a regular expression
            var testClassYouWantToDebug = "NSpec.Tests.ClassContextBug.Child";

            //initialize NSpec's specfinder
            var finder = new SpecFinder(
                new Reflector(typeof(NestContextsTests).GetTypeInfo().Assembly.Location),
                testClassYouWantToDebug);

            //initialize NSpec's builder
            var builder = new ContextBuilder(finder, new DefaultConventions());

            //this line runs the tests you specified in the filter
            var           noTagsFilter = new Tags();
            TestFormatter formatter    = new TestFormatter();

            new ContextRunner(noTagsFilter, formatter, false).Run(builder.Contexts().Build());

            Context grandParent = formatter.Contexts[0];

            Assert.That(grandParent.Name, Is.EqualTo("Grand Parent"));
            Assert.That(grandParent.Contexts.Count, Is.EqualTo(2));
            Assert.That(grandParent.Contexts[0].Name, Is.EqualTo("Grand Parent Context"));
            Assert.That(grandParent.Contexts[1].Name, Is.EqualTo("Parent"));
            Assert.That(grandParent.Contexts[0].Examples[0].Spec, Is.EqualTo("TestValue should be \"Grand Parent!!!\""));
            Assert.That(grandParent.Contexts[0].Examples[0].Exception, Is.Null);
            Assert.That(grandParent.Contexts[0].Examples[0].Pending, Is.False);

            Context parent = formatter.Contexts[0].Contexts[1];

            Assert.That(parent.Name, Is.EqualTo("Parent"));
            Assert.That(parent.Contexts.Count, Is.EqualTo(2));
            Assert.That(parent.Contexts[0].Name, Is.EqualTo("Parent Context"));
            Assert.That(parent.Contexts[1].Name, Is.EqualTo("Child"));
            Assert.That(parent.Contexts[0].Examples[0].Spec, Is.EqualTo("TestValue should be \"Grand Parent.Parent!!!@@@\""));
            Assert.That(parent.Contexts[0].Examples[0].Exception, Is.Null);
            Assert.That(parent.Contexts[0].Examples[0].Pending, Is.False);

            Context child = formatter.Contexts[0].Contexts[1].Contexts[1];

            Assert.That(child.Name, Is.EqualTo("Child"));
            Assert.That(child.Contexts.Count, Is.EqualTo(1));
            Assert.That(child.Contexts[0].Name, Is.EqualTo("Child Context"));
            Assert.That(child.Contexts[0].Examples[0].Spec, Is.EqualTo("TestValue should be \"Grand Parent.Parent.Child!!!@@@###\""));
            Assert.That(child.Contexts[0].Examples[0].Exception, Is.Null);
            Assert.That(child.Contexts[0].Examples[0].Pending, Is.False);
        }
Exemplo n.º 26
0
    //[Test]
    public void debug()
    {
        var tagOrClassName = "class_or_tag_you_want_to_debug";

        var types = GetType().Assembly.GetTypes();
        // OR
        // var types = new Type[]{typeof(Some_Type_Containg_some_Specs)};
        var finder  = new SpecFinder(types, "");
        var builder = new ContextBuilder(finder, new Tags().Parse(tagOrClassName), new DefaultConventions());
        var runner  = new ContextRunner(builder, new ConsoleFormatter(), false);
        var results = runner.Run(builder.Contexts().Build());

        //assert that there aren't any failures
        results.Failures().Count().should_be(0);
    }
Exemplo n.º 27
0
        public void Execute(IReceiveTestResult observer, string[] testNames)
        {
            this.observer = observer;

            var examples = new HashSet <string>(testNames);

            var finder = new SpecFinder(SandboxedAssembly.GetTypes(), "");
            var cb     = new ContextBuilder(finder, new Tags());

            cb.Contexts()
            .Build()
            .SelectMany(c => c.AllExamples())
            .Where(example => examples.Contains(example.FullName()))
            .ForEach(example => example.Context.Run(this, false, example.Context.GetInstance()));
        }
Exemplo n.º 28
0
        public static void Main()
        {
            var tagOrClassName = "";

            var types = typeof(Program).Assembly.GetTypes();
            // OR
            // var types = new Type[]{typeof(Some_Type_Containg_some_Specs)};
            var finder  = new SpecFinder(types, "");
            var builder = new ContextBuilder(finder, new Tags().Parse(tagOrClassName), new DefaultConventions());
            var runner  = new ContextRunner(builder, new ConsoleFormatter(), failFast: true);
            var results = runner.Run(builder.Contexts().Build());

            //assert that there aren't any failures
            results.Failures().Count().should_be(0);
        }
Exemplo n.º 29
0
        public ContextCollection BuildContextCollection(string binaryPath)
        {
            var reflector = new Reflector(binaryPath);

            var finder = new SpecFinder(reflector);

            var conventions = new DefaultConventions();

            var contextBuilder = new ContextBuilder(finder, conventions);

            var contextCollection = contextBuilder.Contexts();

            contextCollection.Build();

            return(contextCollection);
        }
Exemplo n.º 30
0
    public void debug()
    {
        var tagOrClassName = "class_or_tag_you_want_to_debug";

        var types = GetType().Assembly.GetTypes();

        // OR
        types = new System.Type[] { typeof(StatisticsCacheServiceTest.NSpecTests.garbage_collecting_test) };
        var finder  = new SpecFinder(types, "");
        var builder = new ContextBuilder(finder, new Tags().Parse(tagOrClassName), new DefaultConventions());
        var runner  = new ContextRunner(builder, new ConsoleFormatter(), false);
        var results = runner.Run(builder.Contexts().Build());

        //assert that there aren't any failures
        results.Failures().Count().should_be(0);
    }