Ejemplo n.º 1
0
        public async Task Split_non_referencing_directives_are_executed_in_textual_order()
        {
            var receivedCommands = new List <string>();

            using var kernel = new FakeKernel
                  {
                      Handle = (command, context) =>
                      {
                          receivedCommands.Add(((SubmitCode)command).Code);
                          return(Task.CompletedTask);
                      }
                  };

            kernel.AddDirective(new Command("#!one")
            {
                Handler = CommandHandler.Create(() =>
                {
                    receivedCommands.Add("#!one");
                })
            });

            kernel.AddDirective(new Command("#!two")
            {
                Handler = CommandHandler.Create(() =>
                {
                    receivedCommands.Add("#!two");
                })
            });

            var code1      = "var a = 1";
            var code2      = "var b = 2";
            var code3      = "var c = 3";
            var directive1 = "#!one";
            var directive2 = "#!two";

            await kernel.SubmitCodeAsync($@"
{code1}
{directive1}
{code2}
{directive2}
{code3}
");

            receivedCommands
            .Select(c => c.Trim())
            .Should()
            .BeEquivalentSequenceTo(
                code1,
                directive1,
                code2,
                directive2,
                code3);
        }
Ejemplo n.º 2
0
        public async Task OnComplete_can_be_used_to_act_on_completion_of_commands()
        {
            using var kernel = new FakeKernel();

            using var events = kernel.KernelEvents.ToSubscribedList();

            kernel.AddDirective(new Command("#!wrap")
            {
                Handler = CommandHandler.Create(async(KernelInvocationContext c) =>
                {
                    await c.DisplayAsync("hello!");

                    c.OnComplete(async context =>
                    {
                        await context.DisplayAsync("goodbye!");
                    });
                })
            });

            await kernel.SubmitCodeAsync("#!wrap");

            events
            .OfType <DisplayedValueProduced>()
            .Select(e => e.Value)
            .Should()
            .BeEquivalentSequenceTo("hello!", "goodbye!");
        }
        public async Task Action_directives_are_routed_by_kernel_chooser_directives(
            string chooseKernelCommand,
            string expectedInvokedOnKernelName)
        {
            var received = new List <string>();

            using var compositeKernel = new CompositeKernel();

            var fakeKernel1 = new FakeKernel("fake1");

            fakeKernel1.AddDirective(new Command("#!hi")
            {
                Handler = CommandHandler.Create(() => { received.Add("fake1"); })
            });

            var fakeKernel2 = new FakeKernel("fake2");

            fakeKernel2.AddDirective(new Command("#!hi")
            {
                Handler = CommandHandler.Create(() => { received.Add("fake2"); })
            });

            compositeKernel.Add(fakeKernel1, new[] { "fake1-alias" });
            compositeKernel.Add(fakeKernel2, new[] { "fake2-alias" });

            var result = await compositeKernel.SubmitCodeAsync($"{chooseKernelCommand}\n#!hi");

            result.KernelEvents.ToSubscribedList().Should().NotContainErrors();

            received
            .Should()
            .ContainSingle()
            .Which
            .Should()
            .Be(expectedInvokedOnKernelName);
        }