-
Notifications
You must be signed in to change notification settings - Fork 367
/
ImportNotebookTests.cs
147 lines (126 loc) · 4.62 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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]);
}
}
[Theory]
[InlineData(".ipynb")]
[InlineData(".dib")]
public async Task It_produces_DisplayedValueProduced_events_for_markdown_cells(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+11",
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}");
events.Should().ContainSingle<DisplayedValueProduced>(v => v.FormattedValues.Any(f => f.MimeType == "text/markdown"));
}
}