-
Notifications
You must be signed in to change notification settings - Fork 368
/
ImportNotebookTests.cs
88 lines (77 loc) · 2.97 KB
/
ImportNotebookTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.Documents;
using Microsoft.DotNet.Interactive.Documents.Jupyter;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.FSharp;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Xunit;
namespace Microsoft.DotNet.Interactive.Tests
{
public class ImportNotebookTests
{
[Theory]
[InlineData(".ipynb")]
[InlineData(".dib")]
public async Task It_imports_and_runs_well_known_polyglot_file_formats(string notebookExt)
{
using var kernel = new CompositeKernel {
new CSharpKernel(),
new FSharpKernel(),
new HtmlKernel()
}
.UseImportMagicCommand();
var document = new InteractiveDocument
{
new InteractiveDocumentElement
{
Contents = "6+5",
KernelName = "csharp"
},
new InteractiveDocumentElement
{
Contents = "5+3",
KernelName = "markdown" //should not evaluate to 8
},
new InteractiveDocumentElement
{
Contents = "5+3",
KernelName = "html" //should not evaluate to 8
},
new InteractiveDocumentElement
{
Contents = "11*2",
KernelName = "fsharp"
},
new InteractiveDocumentElement
{
Contents = "11*3",
KernelName = "csharp"
}
};
var notebookContents = notebookExt switch
{
".ipynb" => document.ToJupyterJson(),
".dib" => document.ToCodeSubmissionContent(),
_ => throw new InvalidOperationException($"Unrecognized extension for a notebook: {notebookExt}")
};
var filePath = $@".\testnotebook{notebookExt}";
await File.WriteAllTextAsync(filePath, notebookContents);
using var events = kernel.KernelEvents.ToSubscribedList();
await kernel.SubmitCodeAsync($"#!import {filePath}");
var returnedValues = events.Where(x => x.GetType() == typeof(ReturnValueProduced)).ToArray();
int[] results = new int[] { 11, 22, 33 };
returnedValues.Length.Should().Be(results.Length);
for (int i=0 ; i < results.Length; i++)
{
((ReturnValueProduced)returnedValues[i]).Value.Should().Be(results[i]);
}
}
}
}