-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPipeScript.tests.ps1
More file actions
103 lines (89 loc) · 3.24 KB
/
PipeScript.tests.ps1
File metadata and controls
103 lines (89 loc) · 3.24 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
describe PipeScript {
context 'Attribute Based Composition' {
it 'Can Compose Scripts Using Psuedo-Attributes' {
. ({
function f {
param(
[VFP()]
$P
)
}
} | .>Pipescript)
$IsVfp = @($ExecutionContext.SessionState.InvokeCommand.GetCommand('f', 'Function').Parameters['P'].Attributes.ValueFromPipeline) -ne $null
$IsVfp | Should -not -be $null
}
}
context 'Inline PipeScript' {
it 'Can be embedded in another language (to transmute source code or documents)' {
.> {
$CSharpLiteral = @'
namespace TestProgram/*{Get-Random}*/ {
public static class Program {
public static string Hello() {
string helloMessage = /*{param($n)
if ($n) {
"`"hello $n`""
} else {
'"hello"', '"hello world"', '"hey there"', '"howdy"' | Get-Random
}
}*/ string.Empty;
return helloMessage;
}
}
}
'@
[OutputFile(".\HelloWorld.ps1.cs")]$CSharpLiteral
}
$AddedFile = .> .\HelloWorld.ps1.cs
$addedType = Add-Type -TypeDefinition (Get-Content $addedFile.FullName -Raw) -PassThru
$addedType::Hello() | Should -belike 'H*'
$addedFile2 = .> .\HelloWorld.ps1.cs 1
$addedType2 = Add-Type -TypeDefinition (Get-Content $addedFile.FullName -Raw) -PassThru
$addedType2::Hello() | Should -be 'Hello 1'
Remove-Item .\HelloWorld.ps1.cs
Remove-Item $AddedFile.FullName
}
}
it 'Can transpile a scriptblock that is preceeded by the name of a transpiler' {
Invoke-PipeScript -ScriptBlock {
[bash]{param($msg = 'hello world') $msg}
} -ErrorAction Stop |
Should -BeLike '*!/usr/bin/env bash*pwsh*-command*$@*'
}
it 'Can make output explicit' {
Invoke-PipeScript -ScriptBlock {
[explicit]{1;echo 2;3}
} | should -be 2
Invoke-PipeScript -ScriptBlock {
[explicit()]
param()
1;echo 2;3
} | should -be 2
Invoke-PipeScript -ScriptBlock {
[explicit]{
1;echo 2;3;
Write-Output (4..5);
1,2,3|
Measure-Object -Sum -average | Write-Output;
$a = 1..2 | Measure-Object
}
} | Select-Object -First 1 | Should -be 2
}
it 'Will leave normal attributes alone' {
{
[OutputType([string])]
param()
"hello"
} | .>Pipescript | Should -BeLike '*?OutputType??string???*'
}
it 'Can transpile a function' {
{
function [explicit]ExplicitOutput {
"whoops"
return 1
}
} | .>PipeScript |
Should -BeLike "*function ExplicitOutput*`$null?=?`"whoops`"*return 1*"
}
}