Пример #1
0
        /// <summary>Run and verify the scenario expectations using the given logger for output.</summary>
        public static AppRunnerResult Verify(this AppRunner appRunner, Action <string?>?logLine, TestConfig?config, IScenario scenario)
        {
            if (scenario.When.Args != null && scenario.When.ArgsArray != null)
            {
                throw new InvalidOperationException($"Both {nameof(scenario.When)}.{nameof(scenario.When.Args)} and " +
                                                    $"{nameof(scenario.When)}.{nameof(scenario.When.ArgsArray)} were specified. " +
                                                    "Only one can be specified.");
            }

            logLine ??= Console.WriteLine;
            config ??= TestConfig.Default;

            var args = scenario.When.ArgsArray ?? scenario.When.Args !.SplitArgs();

            var origOnSuccess = config.OnSuccess;

            if (!config.OnError.CaptureAndReturnResult)
            {
                config = config.Where(c =>
                {
                    // silence success in RunInMem so results are not printed
                    // twice when asserts fail below.
                    // success will be replaced and printed again.
                    c.OnSuccess = TestConfig.Silent.OnSuccess;
                    c.OnError.CaptureAndReturnResult = true;
                });
            }
            var results = appRunner.RunInMem(
                args,
                logLine,
                scenario.When.OnReadLine,
                scenario.When.PipedInput,
                scenario.When.OnPrompt,
                config);

            config.OnSuccess = origOnSuccess;

            try
            {
                AssertExitCodeAndErrorMessage(scenario, results);

                scenario.Then.AssertOutput?.Invoke(results.Console.AllText());
                scenario.Then.AssertContext?.Invoke(results.CommandContext);

                if (scenario.Then.Output != null)
                {
                    results.Console.AllText().ShouldBe(scenario.Then.Output, "output");
                }

                results.LogResult(logLine);
            }
            catch (Exception)
            {
                results.LogResult(logLine, onError: true);
                throw;
            }

            return(results);
        }
 public static AppRunnerResult RunInMem(
     this AppRunner runner,
     string[] args,
     Func <TestConsole, string>?onReadLine = null,
     IEnumerable <string>?pipedInput       = null)
 {
     return(runner.RunInMem(args, Ambient.WriteLine, onReadLine, pipedInput));
 }
Пример #3
0
 public static AppRunnerResult RunInMem(
     this AppRunner runner,
     string[] args,
     ITestOutputHelper output,
     Func <TestConsole, string> onReadLine = null,
     IEnumerable <string> pipedInput       = null)
 {
     return(runner.RunInMem(args, output?.AsLogger(), onReadLine, pipedInput));
 }
Пример #4
0
 /// <summary>Run the console in memory and get the results that would be output to the shell</summary>
 public static AppRunnerResult RunInMem(this AppRunner runner,
                                        string args,
                                        Action <string?>?logLine = null,
                                        Func <ITestConsole, string>?onReadLine = null,
                                        IEnumerable <string>?pipedInput        = null,
                                        IPromptResponder?promptResponder       = null,
                                        TestConfig?config = null)
 {
     return(runner.RunInMem(args.SplitArgs(), logLine, onReadLine, pipedInput, promptResponder, config));
 }
Пример #5
0
        public void ShouldNotReachThisStep_ShouldFailTheTests()
        {
            // this test ensures the previous test is not passing with a false positive
            var appRunner = new AppRunner <App>()
                            .UseCancellationHandlers()
                            .Configure(c =>
            {
                c.UseMiddleware(ShouldNotReachThisStep, MiddlewareStages.PostTokenizePreParseInput);
            });

            Assert.Throws <Exception>(() => appRunner.RunInMem("Do"))
            .Message.Should().Be("This middleware should not have been called");
        }
Пример #6
0
        public void EnsureNoFalsePositives()
        {
            // this test ensures the previous test is not passing with a false positive
            var tokenSource = new CancellationTokenSource();
            var appRunner   = new AppRunner <App>()
                              .Configure(c =>
            {
                c.Services.AddOrUpdate(tokenSource);
                c.CancellationToken = tokenSource.Token;
                c.UseMiddleware(Throw, MiddlewareStages.PostTokenizePreParseInput);
            });

            Assert.Throws <Exception>(() => appRunner.RunInMem(new string[0]))
            .Message.Should().Be("This middleware should not have been called");
        }
        /// <summary>Run and verify the scenario expectations using the given logger for output.</summary>
        public static AppRunnerResult VerifyScenario(this AppRunner appRunner, ILogger logger, IScenario scenario)
        {
            if (scenario.WhenArgs != null && scenario.WhenArgsArray != null)
            {
                throw new InvalidOperationException($"Both {nameof(scenario.WhenArgs)} and {nameof(scenario.WhenArgsArray)} were specified.  Only one can be specified.");
            }

            AppRunnerResult results = null;
            var             args    = scenario.WhenArgsArray ?? scenario.WhenArgs.SplitArgs();

            try
            {
                results = appRunner.RunInMem(
                    args,
                    logger,
                    scenario.Given.OnReadLine,
                    scenario.Given.PipedInput,
                    scenario.Given.OnPrompt,
                    returnResultOnError: true);

                AssertExitCodeAndErrorMessage(scenario, results);

                if (scenario.Then.Result != null)
                {
                    results.OutputShouldBe(scenario.Then.Result);
                }

                if (scenario.Then.Outputs.Count > 0)
                {
                    AssertOutputItems(scenario, results);
                }

                return(results);
            }
            catch (Exception e)
            {
                logger.WriteLine(scenario.ToString());
                logger.WriteLine("");
                PrintContext(appRunner, logger);
                if (results != null)
                {
                    logger.WriteLine("");
                    logger.WriteLine("App Results:");
                    logger.WriteLine(results.ConsoleOutAndError);
                }
                throw;
            }
        }
        private AppRunnerResult RunInMem(int carNumber, string ownerName,
                                         ExecutionMiddleware postBindValues = null,
                                         ExecutionMiddleware preBindValues  = null)
        {
            var appRunner = new AppRunner <App>();

            if (postBindValues != null)
            {
                appRunner.Configure(c => c.UseMiddleware(postBindValues, MiddlewareStages.PostBindValuesPreInvoke, int.MaxValue));
            }
            if (preBindValues != null)
            {
                appRunner.Configure(c => c.UseMiddleware(preBindValues, MiddlewareStages.PostParseInputPreBindValues, int.MaxValue));
            }

            var args = $"NotifyOwner --Number {carNumber} --owner {ownerName}".SplitArgs();

            return(appRunner.RunInMem(args));
        }