示例#1
0
        private static SubmissionParser CreateSubmissionParser(
            string defaultLanguage = "csharp", IEnumerable <Kernel> additionalKernels = null)
        {
            using var compositeKernel = new CompositeKernel { DefaultKernelName = defaultLanguage };


            compositeKernel.Add(
                new CSharpKernel()
                .UseNugetDirective()
                .UseWho(),
                new[] { "c#", "C#" });

            compositeKernel.Add(
                new FSharpKernel()
                .UseNugetDirective(),
                new[] { "f#", "F#" });

            compositeKernel.Add(
                new PowerShellKernel(),
                new[] { "powershell" });

            if (additionalKernels is not null)
            {
                foreach (var additionalKernel in additionalKernels)
                {
                    compositeKernel.Add(additionalKernel);
                }
            }

            compositeKernel.UseDefaultMagicCommands();

            return(compositeKernel.SubmissionParser);
        }
        public async Task Handling_kernel_can_be_specified_using_kernel_alias_as_a_directive()
        {
            var cSharpKernel = new CSharpKernel();
            var fSharpKernel = new FSharpKernel();

            using var kernel = new CompositeKernel();
            kernel.Add(cSharpKernel, new[] { "C#" });
            kernel.Add(fSharpKernel, new[] { "F#" });
            kernel.DefaultKernelName = fSharpKernel.Name;

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

            var csharpCommand = new SubmitCode(@"
#!C#
new [] {1,2,3}");
            await kernel.SendAsync(csharpCommand);

            var fsharpCommand = new SubmitCode(@"
#!F#
[1;2;3]");

            await kernel.SendAsync(fsharpCommand);

            events.Should()
            .ContainSingle <CommandSucceeded>(e => e.Command == csharpCommand);
            events.Should()
            .ContainSingle <CommandSucceeded>(e => e.Command == fsharpCommand);
        }
示例#3
0
        private static IKernel CreateKernel(string defaultKernelName)
        {
            var compositeKernel = new CompositeKernel();

            compositeKernel.Add(
                new CSharpKernel()
                .UseDefaultFormatting()
                .UseNugetDirective()
                .UseKernelHelpers()
                .UseWho()
                .UseXplot()
                .UseMathAndLaTeX());

            compositeKernel.Add(
                new FSharpKernel()
                .UseDefaultFormatting()
                .UseKernelHelpers()
                .UseWho()
                .UseDefaultNamespaces()
                .UseXplot()
                .UseMathAndLaTeX());

            compositeKernel.Add(
                new PowerShellKernel(), new[] { "#!pwsh" });

            var kernel = compositeKernel
                         .UseDefaultMagicCommands()
                         .UseAbout();

            kernel.DefaultKernelName = defaultKernelName;
            kernel.Name = ".NET";

            return(kernel);
        }
示例#4
0
        private static SubmissionParser CreateSubmissionParser(
            string defaultLanguage = "csharp")
        {
            using var compositeKernel = new CompositeKernel();

            compositeKernel.DefaultKernelName = defaultLanguage;

            compositeKernel.Add(
                new CSharpKernel()
                .UseNugetDirective(),
                new[] { "c#", "C#" });

            compositeKernel.Add(
                new FSharpKernel()
                .UseNugetDirective(),
                new[] { "f#", "F#" });

            compositeKernel.Add(
                new PowerShellKernel(),
                new[] { "powershell" });

            compositeKernel.UseDefaultMagicCommands();

            return(compositeKernel.SubmissionParser);
        }
        private static IKernel CreateKernel(
            string defaultKernelName,
            FrontendEnvironment frontendEnvironment,
            StartupOptions startupOptions,
            HttpProbingSettings httpProbingSettings)
        {
            var compositeKernel = new CompositeKernel();

            compositeKernel.FrontendEnvironment = frontendEnvironment;

            compositeKernel.Add(
                new CSharpKernel()
                .UseDefaultFormatting()
                .UseNugetDirective()
                .UseKernelHelpers()
                .UseJupyterHelpers()
                .UseWho()
                .UseXplot()
                .UseMathAndLaTeX());

            compositeKernel.Add(
                new FSharpKernel()
                .UseDefaultFormatting()
                .UseNugetDirective()
                .UseKernelHelpers()
                .UseWho()
                .UseDefaultNamespaces()
                .UseXplot()
                .UseMathAndLaTeX());

            compositeKernel.Add(
                new PowerShellKernel()
                .UseJupyterHelpers()
                .UseXplot(),
                new[] { "#!pwsh" });

            compositeKernel.Add(
                new JavaScriptKernel(),
                new[] { "#!js" });

            compositeKernel.Add(
                new HtmlKernel());

            var kernel = compositeKernel
                         .UseDefaultMagicCommands()
                         .UseLog()
                         .UseAbout()
                         .UseHttpApi(startupOptions, httpProbingSettings);

            SetUpFormatters(frontendEnvironment);


            kernel.DefaultKernelName = defaultKernelName;
            var enableHttp = new SubmitCode("#!enable-http", compositeKernel.Name);

            enableHttp.PublishInternalEvents();
            compositeKernel.DeferCommand(enableHttp);
            return(kernel);
        }
示例#6
0
        public void FindKernel_finds_a_subkernel_of_a_composite_kernel_by_alias()
        {
            var one = new FakeKernel("one");
            var two = new FakeKernel("two");

            using var compositeKernel = new CompositeKernel();
            compositeKernel.Add(one, aliases: new[] { "one-alias" });
            compositeKernel.Add(two);

            var found = compositeKernel.FindKernel("one-alias");

            found.Should().BeSameAs(one);
        }
        public async Task VisitSubkernelsAsync_can_recurse_child_composite_kernels()
        {
            var visited = new List <string>();
            var child   = new CompositeKernel
            {
                Name = "child"
            };
            var grandchild = new CompositeKernel
            {
                Name = "grandchild"
            };
            var parent = new CompositeKernel
            {
                Name = "parent"
            };

            child.Add(grandchild);
            parent.Add(child);

            await parent.VisitSubkernelsAsync(kernel =>
            {
                visited.Add(kernel.Name);
                return(Task.CompletedTask);
            }, true);

            visited.Should().BeEquivalentTo("child", "grandchild");
        }
        public void VisitSubkernelsAndSelf_visits_subkernels_and_self()
        {
            var visited = new List <string>();
            var child   = new CompositeKernel
            {
                Name = "child"
            };
            var grandchild = new CompositeKernel
            {
                Name = "grandchild"
            };
            var parent = new CompositeKernel
            {
                Name = "parent"
            };

            child.Add(grandchild);
            parent.Add(child);

            parent.VisitSubkernelsAndSelf(kernel =>
            {
                visited.Add(kernel.Name);
            }, true);

            visited.Should().BeEquivalentTo("child", "parent", "grandchild");
        }
示例#9
0
        private CSharpKernel RegisterCSharpKernel()
        {
            var csharpKernel = new CSharpKernel()
                               .UseDefaultFormatting()
                               .UseNugetDirective()
                               .UseKernelHelpers()
                               .UseWho()
                               .UseDotNetVariableSharing()
                               //This is added locally
                               .UseWpf();

            _Kernel.Add(csharpKernel);

            csharpKernel.AddMiddleware(async(KernelCommand command, KernelInvocationContext context, KernelPipelineContinuation next) =>
            {
                if (RunOnDispatcher)
                {
                    await Dispatcher.InvokeAsync(async() => await next(command, context));
                }
                else
                {
                    await next(command, context);
                }
            });

            return(csharpKernel);
        }
        public async Task When_command_handler_registered_in_child_kernel_and_command_sent_to_parent_then_handler_is_executed()
        {
            using var compositeKernel = new CompositeKernel();
            var childKernel = new FakeKernel();

            compositeKernel.Add(childKernel);

            CustomCommandTypes.FirstSubmission.MyCommand commandPassedToHandler = null;
            KernelInvocationContext contextPassedToHandler = null;

            childKernel.RegisterCommandHandler <CustomCommandTypes.FirstSubmission.MyCommand>(
                (command, context) =>
            {
                commandPassedToHandler = command;
                contextPassedToHandler = context;
                return(Task.CompletedTask);
            });

            var commandSentToCompositeKernel = new CustomCommandTypes.FirstSubmission.MyCommand("xyzzy");
            await compositeKernel.SendAsync(commandSentToCompositeKernel);

            commandPassedToHandler
            .Should()
            .BeSameAs(commandSentToCompositeKernel);
            contextPassedToHandler
            .Should()
            .NotBeNull();
        }
        public async Task Handling_kernel_can_be_specified_as_a_default_via_an_alias()
        {
            var receivedOnFakeKernel = new List <KernelCommand>();

            var fakeKernel = new FakeKernel("fake")
            {
                Handle = (command, context) =>
                {
                    receivedOnFakeKernel.Add(command);
                    return(Task.CompletedTask);
                }
            };

            using var kernel = new CompositeKernel
                  {
                      new CSharpKernel()
                  };

            kernel.Add(fakeKernel, new[] { "totally-fake" });

            kernel.DefaultKernelName = "totally-fake";

            await kernel.SendAsync(
                new SubmitCode(
                    @"hello!"));

            receivedOnFakeKernel
            .Should()
            .ContainSingle(c => c is SubmitCode)
            .Which
            .As <SubmitCode>()
            .Code
            .Should()
            .Be("hello!");
        }
示例#12
0
        private static SubmissionParser CreateSubmissionParser(
            string defaultLanguage = "csharp",
            params Command[] directives)
        {
            using var kernel         = new CompositeKernel();
            kernel.DefaultKernelName = defaultLanguage;
            kernel.Add(new FakeKernel("csharp"));
            kernel.Add(new FakeKernel("fsharp"));
            kernel.Add(new FakeKernel("powershell"), new[] { "pwsh" });
            kernel.UseDefaultMagicCommands();

            foreach (var directive in directives)
            {
                kernel.AddDirective(directive);
            }

            return(kernel.SubmissionParser);
        }
        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);
        }
        public void When_child_kernel_is_added_then_its_frontend_environment_is_obtained_from_the_parent()
        {
            using var compositeKernel = new CompositeKernel();

            compositeKernel.FrontendEnvironment = new AutomationEnvironment();

            compositeKernel.Add(new CSharpKernel());

            compositeKernel
            .ChildKernels
            .OfType <Kernel>()
            .Single()
            .FrontendEnvironment
            .Should()
            .BeSameAs(compositeKernel.FrontendEnvironment);
        }
        public async Task when_target_kernel_is_specified_and_not_found_then_command_fails(int kernelCount)
        {
            using var kernel = new CompositeKernel();
            using var events = kernel.KernelEvents.ToSubscribedList();
            foreach (var kernelName in Enumerable.Range(0, kernelCount).Select(i => $"kernel{i}"))
            {
                kernel.Add(new FakeKernel(kernelName));
            }

            await kernel.SendAsync(
                new SubmitCode(
                    @"var x = 123;",
                    "unregistered kernel name"));

            events.Should()
            .ContainSingle <CommandFailed>(cf => cf.Exception is NoSuitableKernelException);
        }
        private CompositeKernel CreateCompositeKernel(IEnumerable <Kernel> subkernels, Language defaultKernelLanguage)
        {
            var kernel = new CompositeKernel().UseDefaultMagicCommands();

            foreach (var sub in subkernels)
            {
                kernel.Add(sub.LogEventsToPocketLogger());
            }

            kernel.DefaultKernelName = defaultKernelLanguage.LanguageName();

            KernelEvents = kernel.KernelEvents.ToSubscribedList();

            DisposeAfterTest(KernelEvents);
            DisposeAfterTest(kernel);

            return(kernel);
        }
        private static CompositeKernel CreateKernel(
            FrontendEnvironment frontendEnvironment,
            StartupOptions startupOptions)
        {
            using var _ = Log.OnEnterAndExit();

            var compositeKernel = new CompositeKernel
            {
                FrontendEnvironment = frontendEnvironment
            };

            var csharpKernel = new CSharpKernel()
                               .UseDefaultMagicCommands()
                               .UseDefaultFormatting()
                               .UseNugetDirective()
                               .UseKernelHelpers()
                               .UseWho()
                               .UseDotNetVariableSharing();

            compositeKernel.Add(
                csharpKernel,
                new[] { "c#", "C#" });

            var kernel = compositeKernel
                         .UseLog()
                         .UseKernelClientConnection(new ConnectNamedPipe())
                         .UseKernelClientConnection(new ConnectSignalR());

            if (startupOptions.Verbose)
            {
                kernel.LogEventsToPocketLogger();
            }

            SetUpFormatters();

            kernel.DefaultKernelName = csharpKernel.Name;
            RoverRobotSetup.SetupKernelEnvironment(csharpKernel).Wait();
            return(kernel);
        }
        public void VisitSubkernels_can_recurse_child_composite_kernels()
        {
            var visited = new List <string>();
            var child   = new CompositeKernel
            {
                Name = "child"
            };
            var grandchild = new CompositeKernel
            {
                Name = "grandchild"
            };
            var parent = new CompositeKernel
            {
                Name = "parent"
            };

            child.Add(grandchild);
            parent.Add(child);

            parent.VisitSubkernels(kernel => visited.Add(kernel.Name), true);

            visited.Should().BeEquivalentTo("child", "grandchild");
        }
        public void VisitSubkernels_does_not_recurse_by_default()
        {
            var visited = new List <string>();
            var child   = new CompositeKernel
            {
                Name = "child"
            };
            var grandchild = new CompositeKernel
            {
                Name = "grandchild"
            };
            var parent = new CompositeKernel
            {
                Name = "parent"
            };

            child.Add(grandchild);
            parent.Add(child);

            parent.VisitSubkernels(kernel => visited.Add(kernel.Name));

            visited.Should().BeEquivalentTo("child");
        }