-
Notifications
You must be signed in to change notification settings - Fork 11
/
test-poke.ps1
151 lines (122 loc) · 4.14 KB
/
test-poke.ps1
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
148
149
150
151
Clear-Host
Set-Location $psscriptroot
Import-Module .\poke.psd1 -force
#$VerbosePreference = "Continue"
$error.Clear()
function Assert-True {
param(
[parameter(position=0, mandatory=$true)]
[validatenotnull()]
[scriptblock]$Script,
[parameter(position=1)]
[validatenotnullorempty()]
[string]$Name = 'Assert-True'
)
$eap = $ErrorActionPreference
$detail = "Assert-True [ $Name ] "
Write-Host -NoNewline $detail
try {
$erroractionpreference = 'stop'
if ((& $script) -eq $true) {
write-host -ForegroundColor Green "$(' ' * (100 - $detail.Length))[PASS]"
return
}
$reason = 'Assert failed.'
}
catch {
$reason = "Error: $_"
}
finally {
$ErrorActionPreference = $eap
}
write-host -ForegroundColor Red "$(' ' * (100 - $detail.Length))[FAIL] " -NoNewline
write-host "Reason: '$reason'"
}
#
# begin tests
#
assert-true {
$proxy = peek -name System.Text.StringBuilder
$proxy.psobject.typenames.contains("Pokeable.Object")
} -name 'type proxy'
assert-true {
$sb = new-object System.Text.StringBuilder
$proxy = peek $sb
$proxy.psobject.typenames.contains("Pokeable.Object")
} -name 'instance proxy'
assert-true {
$sb = new-object System.Text.StringBuilder
$proxy = peek $sb
$proxy.length -eq 0
} -name 'instance property'
assert-true {
$sb = new-object System.Text.StringBuilder
$proxy = peek $sb
$proxy.append(42) > $null
$proxy.length -eq 2
} -name 'instance method with overloads'
assert-true {
$proxy = peek -name system.string
$s = $proxy.format('hello, {0}', [object[]]@('world'))
$s -eq 'hello, world'
} -name 'static method with overloads'
#
# static methods
#
assert-true {
$delegate = [string]::format | Get-Delegate -Delegate 'func[string,object,string]'
$delegate.invoke('hello, {0}', 'world') -eq 'hello, world'
} -name "[string]::format | get-delegate -delegate 'func[string,object,string]'"
assert-true {
$delegate = [console]::writeline | Get-Delegate -Delegate 'action[int]'
$delegate -is [action[int]]
} -name "[console]::writeline | get-delegate -delegate 'action[int]'"
assert-true {
$delegate = [string]::format | Get-Delegate string,string
$delegate.invoke('hello, {0}', 'world') -eq 'hello, world'
} -name '[string]::format | get-delegate string,string'
assert-true {
$delegate = [console]::beep | Get-Delegate @()
$delegate -is [action]
} -name '[console]::beep | get-delegate @()'
assert-true {
$delegate = [console]::beep | Get-Delegate -DelegateType action
$delegate -is [action]
} -name '[console]::beep | Get-Delegate -DelegateType action'
assert-true {
$delegate = [string]::IsNullOrEmpty | get-delegate
$delegate -is [func[string,bool]]
} -name '[string]::IsNullOrEmpty | get-delegate # single overload'
assert-true {
$delegate = [string]::IsNullOrEmpty | get-delegate string
$delegate -is [func[string,bool]]
} -name '[string]::IsNullOrEmpty | get-delegate string # single overload'
assert-true {
$int = [int] | peek
$n = 0;
$int.TryParse("1", [ref]$n)
} -name "[int]::tryparse ([string], [ref][int])"
#
# instance methods
#
assert-true {
$sb = new-object text.stringbuilder
$delegate = $sb.Append | get-delegate string
$delegate -is [System.Func[string,System.Text.StringBuilder]]
} -name "`$sb.Append | get-delegate string"
assert-true {
$sb = new-object text.stringbuilder
$delegate = $sb.AppendFormat | get-delegate string, int, int
$delegate -is [System.Func[string,object,object,System.Text.StringBuilder]]
} -name "`$sb.AppendFormat | get-delegate string, int, int"
#
# readonly field
#
assert-true {
$re = peek ([regex])
$re.DefaultMatchTimeout = New-TimeSpan -Seconds 42
$re.DefaultMatchTimeout.Seconds -eq 42
} -name 'readonly static field (setter)'
$VerbosePreference = 'SilentlyContinue'
$s = peek system.string
$s | Get-Member | Sort-Object modifier | Format-Table -group modifier