-
Notifications
You must be signed in to change notification settings - Fork 70
/
test.ps1
177 lines (141 loc) · 4.27 KB
/
test.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
. .\variables.ps1
. .\constants.ps1
. .\log.ps1
LogInfo "Collecting tests"
$missing = [System.Reflection.Missing]::Value
$excel = New-Object -ComObject Excel.Application
$book = $excel.Workbooks.Open($FILENAME, $missing, $true)
$modules = $book.VBProject.VBComponents;
$suites = @{}
$suiteFlags = @{}
$SUITE_FLAG_BEFORE_ALL = 1
$SUITE_FLAG_AFTER_ALL = 2
$SUITE_FLAG_BEFORE_EACH = 4
$SUITE_FLAG_AFTER_EACH = 8
$testCount = 0
For ($moduleIndex = 0; $moduleIndex -lt $modules.Count; $moduleIndex++)
{
# vba interop seems to be written in VB6 => indices start at 1
$module = $modules.Item($moduleIndex + 1)
If (!$module.Name.StartsWith("Test"))
{
Continue
}
$code = $module.CodeModule.Lines(1, $module.CodeModule.CountOfLines)
$lines = $code.Split("`r`n")
$suiteFlags[$module.Name] = 0
ForEach ($line in $lines)
{
If ($line.StartsWith("Public Function Test"))
{
If (!$suites.ContainsKey($module.Name))
{
$suites[$module.Name] = [System.Collections.ArrayList]@()
}
$testName = $line.Split(" ")[2].Trim("()")
$_ = $suites[$module.Name].Add($testName)
$testCount += 1
}
If ($line.StartsWith("Public Sub BeforeAll()"))
{
$suiteFlags[$module.Name] = $suiteFlags[$module.Name] -bor $SUITE_FLAG_BEFORE_ALL
}
If ($line.StartsWith("Public Sub AfterAll()"))
{
$suiteFlags[$module.Name] = $suiteFlags[$module.Name] -bor $SUITE_FLAG_AFTER_ALL
}
If ($line.StartsWith("Public Sub BeforeEach()"))
{
$suiteFlags[$module.Name] = $suiteFlags[$module.Name] -bor $SUITE_FLAG_BEFORE_EACH
}
If ($line.StartsWith("Public Sub AfterEach()"))
{
$suiteFlags[$module.Name] = $suiteFlags[$module.Name] -bor $SUITE_FLAG_AFTER_EACH
}
}
}
LogInfo "Found $($suites.Count) suites with $testCount tests"
LogEmptyLine
$passedSuites = 0
$passedTests = 0
Function HasSuiteFlag($flags, $flag)
{
Return ($flags -band $flag) -eq $flag
}
ForEach ($suite in $suites.Keys)
{
$successful = $true
$tests = $suites[$suite]
$flags = $suiteFlags[$suite]
$title = $suite + " (" + $tests.Count + " tests)"
echo $title
$hasBeforeAll = HasSuiteFlag $flags $SUITE_FLAG_BEFORE_ALL
$hasAfterAll = HasSuiteFlag $flags $SUITE_FLAG_AFTER_ALL
$hasBeforeEach = HasSuiteFlag $flags $SUITE_FLAG_BEFORE_EACH
$hasAfterEach = HasSuiteFlag $flags $SUITE_FLAG_AFTER_EACH
If ($hasBeforeAll)
{
$excel.Run($suite + "." + "BeforeAll")
}
ForEach ($test in $tests)
{
If ($hasBeforeEach)
{
$excel.Run($suite + "." + "BeforeEach")
}
$result = $excel.Run($suite + "." + $test)
Write-Host " " -NoNewline
if ($result.AssertSuccessful)
{
$passedTests += 1
Write-Host " PASS " -BackgroundColor Green -ForegroundColor White -NoNewline
}
else
{
$successful = $false
Write-Host " FAIL " -BackgroundColor Red -ForegroundColor White -NoNewline
}
Write-Host " $test" -NoNewline
if ($result.AssertSuccessful)
{
Write-Host ": $($result.AssertMessage)" -ForegroundColor Gray
}
else
{
LogEmptyLine
LogEmptyLine
Write-Host $result.AssertMessage -ForegroundColor Red
LogEmptyLine
}
If ($hasAfterEach)
{
$excel.Run($suite + "." + "AfterEach")
}
}
If ($hasAfterAll)
{
$excel.Run($suite + "." + "AfterAll")
}
if ($successful)
{
$passedSuites += 1
}
echo ""
}
$excel.Quit()
Function LogSummary($title, $passed, $failed)
{
Write-Host $title -NoNewline
Write-Host "$passed passed" -ForegroundColor Green -NoNewline
if ($failed -gt 0)
{
Write-Host ", " -NoNewline
Write-Host "${failed} failed" -ForegroundColor Red -NoNewline
}
$total = $passed + $failed
Write-Host ", $total total"
}
LogSummary "Test suites: " $passedSuites ($suites.Count - $passedSuites)
LogSummary "Tests: " $passedTests ($testCount - $passedTests)
LogInfo "Ran all test suites."
LogEmptyLine