forked from dfinke/PowerShellNotebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSNBDSL.tests.ps1
More file actions
145 lines (118 loc) · 4.52 KB
/
Copy pathPSNBDSL.tests.ps1
File metadata and controls
145 lines (118 loc) · 4.52 KB
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
Import-Module $PSScriptRoot\..\PowerShellNotebook.psd1 -Force
Describe "Test Invoke PS Notebook" -Tag 'InvokePSNotebook' {
It "Should have New-PSNotebook" {
$actual = Get-Command New-PSNotebook -ErrorAction SilentlyContinue
$actual | Should -Not -Be $Null
}
It "Should have Add-NotebookCode" {
$actual = Get-Command Add-NotebookCode -ErrorAction SilentlyContinue
$actual | Should -Not -Be $Null
}
It "Should have Add-NotebookMarkdown" {
$actual = Get-Command Add-NotebookMarkdown -ErrorAction SilentlyContinue
$actual | Should -Not -Be $Null
}
It "Should generate correct PowerShell notebook format" {
$actualJson = New-PSNotebook -AsText {
Add-NotebookCode "8+12"
Add-NotebookCode "8+3"
Add-NotebookMarkdown @'
## Math
- show addition
- show other
'@
}
$actual = $actualJson | ConvertFrom-Json
$actual.cells.count | Should -Be 3
$actual.cells[0].source | Should -BeExactly "8+12"
$actual.cells[1].source | Should -BeExactly "8+3"
$actual.cells[2].source | Should -BeExactly "## Math
- show addition
- show other
"
}
It "Save file" {
$fullName = "TestDrive:\test.ipnyb"
New-PSNotebook -NoteBookName $fullName {
Add-NotebookCode "8+12"
}
$r = Test-Path $fullName
$r | Should -Be $true
}
It "Should have correct top level metadata" {
$actual = ConvertFrom-Json (New-PSNotebook -AsText { })
<#
{
"metadata": {
"kernelspec": {
"name": "powershell",
"display_name": "PowerShell"
},
"language_info": {
"name": "powershell",
"codemirror_mode": "shell",
"mimetype": "text/x-sh",
"file_extension": ".ps1"
}
},
"nbformat_minor": 2,
"nbformat": 4,
"cells": [
]
}
#>
$actual.metadata | Should -Not -Be $null
$actual.metadata.kernelspec.name | Should -BeExactly 'powershell'
$actual.metadata.kernelspec.display_name | Should -BeExactly 'PowerShell'
$actual.metadata.language_info.name | Should -BeExactly 'powershell'
$actual.metadata.language_info.codemirror_mode | Should -BeExactly 'shell'
$actual.metadata.language_info.mimetype | Should -BeExactly 'text/x-sh'
$actual.metadata.language_info.file_extension | Should -BeExactly '.ps1'
$actual.nbformat_minor | Should -Be 2
$actual.nbformat | Should -Be 4
$actual.cells.Count | Should -Be 0
}
It "Should have correct markdown metadata" {
<#
{
"metadata": {
"kernelspec": {
"name": "powershell",
"display_name": "PowerShell"
},
"language_info": {
"name": "powershell",
"codemirror_mode": "shell",
"mimetype": "text/x-sh",
"file_extension": ".ps1"
}
},
"nbformat_minor": 2,
"nbformat": 4,
"cells": [
{"cell_type":"markdown","metadata":{},"source":["# Hello World"]}
]
}
#>
$nb = New-PSNotebook -AsText {
Add-NotebookMarkdown -markdown "# Hello World"
}
$actual = ConvertFrom-Json $nb
$actual.cells.Count | Should -Be 1
$actual.cells[0].cell_type | Should -BeExactly 'markdown'
$actual.cells[0].metadata.GetType().name | Should -BeExactly "PSCustomObject"
$actual.cells[0].source | Should -BeExactly '# Hello World'
}
It "Tests Invoke returning a string" {
$s = "'Hello World'"
$PSNotebookRunspace = New-PSNotebookRunspace
$actual = $PSNotebookRunspace.Invoke($s)
$actual.Trim() | Should -BeExactly "Hello World"
}
It "Tests Invoke returning an object" {
$s = "[PSCustomObject]@{msg='Hello World'}"
$PSNotebookRunspace = New-PSNotebookRunspace -ReturnAsObjects
$actual = $PSNotebookRunspace.Invoke($s)
$actual.msg | Should -BeExactly "Hello World"
}
}