-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathTests.fs
More file actions
314 lines (274 loc) · 13.7 KB
/
Tests.fs
File metadata and controls
314 lines (274 loc) · 13.7 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
namespace Fuchu
open System
module Strings=
let contains (substr:string) (t: string) = t.Contains substr
module Dummy =
open Fuchu
[<Tests>]
let testA = TestLabel ("test A", TestList [])
[<Tests>]
let testB() = TestLabel ("test B", TestList [])
let thisModuleType = lazy Type.GetType "Fuchu.Dummy, Fuchu.Tests"
module EmptyModule =
let thisModuleType = lazy Type.GetType "Fuchu.EmptyModule, Fuchu.Tests"
type CustomNUnitException(msg) =
inherit NUnit.Framework.AssertionException(msg)
module Tests =
open Fuchu
open Fuchu.Impl
open System.Threading
open System.IO
open System.Globalization
let (==?) actual expected = Assert.Equal("", expected, actual)
[<Tests>]
let tests =
TestList [
testCase "basic" <| fun _ -> Assert.Equal("2+2", 4, 2+2)
test "using computation expression" {
Assert.Equal("2+2", 4, 2+2)
}
testList "sumTestResults" [
let sumTestResultsTests =
[
{ TestRunResult.Name = ""; Result = Passed; Time = TimeSpan.FromMinutes 2. }
{ TestRunResult.Name = ""; Result = TestResult.Error (ArgumentException()); Time = TimeSpan.FromMinutes 3. }
{ TestRunResult.Name = ""; Result = Failed ""; Time = TimeSpan.FromMinutes 4. }
{ TestRunResult.Name = ""; Result = Passed; Time = TimeSpan.FromMinutes 5. }
{ TestRunResult.Name = ""; Result = Failed ""; Time = TimeSpan.FromMinutes 6. }
{ TestRunResult.Name = ""; Result = Passed; Time = TimeSpan.FromMinutes 7. }
]
let r = lazy sumTestResults sumTestResultsTests
yield testCase "Passed" <| fun _ ->
r.Value.Passed ==? 3
yield testCase "Failed" <| fun _ ->
r.Value.Failed ==? 2
yield testCase "Exception" <| fun _ ->
r.Value.Errored ==? 1
yield testCase "Time" <| fun _ ->
r.Value.Time ==? TimeSpan.FromMinutes 27.
]
testList "TestResultCounts" [
testList "plus" [
let testResultCountsSum name f =
testProperty name
(FsCheck.Prop.forAll twoTestResultCounts.Value <|
fun (a,b) ->
let r = a + b
f a b r)
yield testResultCountsSum "Passed" <|
fun a b r -> r.Passed = a.Passed + b.Passed
yield testResultCountsSum "Ignored" <|
fun a b r -> r.Ignored = a.Ignored + b.Ignored
yield testResultCountsSum "Failed" <|
fun a b r -> r.Failed = a.Failed + b.Failed
yield testResultCountsSum "Errored" <|
fun a b r -> r.Errored = a.Errored + b.Errored
yield testResultCountsSum "Time" <|
fun a b r -> r.Time = a.Time + b.Time
]
testCase "ToString" <| fun _ ->
let c1 = { Passed = 1; Ignored = 5; Failed = 2; Errored = 3; Time = TimeSpan.FromSeconds 20. }
c1.ToString() ==? "6 tests run: 1 passed, 5 ignored, 2 failed, 3 errored (00:00:20)\n"
]
testList "Exception handling" [
testCase "NUnit ignore" <| fun _ ->
let test() = NUnit.Framework.Assert.Ignore "a"
let test = TestCase test
match evalSilent test with
| [{ Result = Ignored "a" }] -> ()
| x -> failtestf "Expected result = Ignored, got\n %A" x
testCase "Inherit AssertionException counts as failure" <| fun _ ->
let test () = raise (new CustomNUnitException("hello"))
let test = TestCase test
match evalSilent test with
| [{ Result = Failed (String.StartsWith "\nhello") }] -> ()
| x -> failtestf "Expected result = Failed, got\n %A" x
testCase "Fuchu ignore" <| fun _ ->
let test () = skiptest "b"
let test = TestCase test
match evalSilent test with
| [{ Result = Ignored "b" }] -> ()
| x -> failtestf "Expected result = Ignored, got\n %A" x
]
testList "Setup & teardown" [
// just demoing how you can use a higher-order function as setup/teardown
let withMemoryStream f () =
use s = new MemoryStream()
let r = f s
s.Capacity ==? 5
r
yield testCase "1" (withMemoryStream (fun ms -> ms.Capacity <- 5))
yield testCase "2" (withMemoryStream (fun ms -> ms.Capacity <- 5))
]
testList "Setup & teardown 2" [
// just demoing how you can use a higher-order function as setup/teardown
let tests = [
"1", fun (ms: MemoryStream) -> ms.Capacity <- 5
"2", fun ms -> ms.Capacity <- 5
]
let withMemoryStream f () =
use s = new MemoryStream()
let r = f s
s.Capacity ==? 5
r
for name,test in tests ->
testCase name (withMemoryStream test)
]
testList "Setup & teardown 3" [
let withMemoryStream f () =
use ms = new MemoryStream()
f ms
yield! testFixture withMemoryStream [
"can read",
fun ms -> ms.CanRead ==? true
"can write",
fun ms -> ms.CanWrite ==? true
]
]
testList "Test filter" [
let tests =
TestList [
testCase "a" ignore
testCase "b" ignore
testList "c" [
testCase "d" ignore
testCase "e" ignore
]
]
yield testCase "with one testcase" <| fun _ ->
let t = Test.filter ((=) "a") tests |> Test.toTestCodeList |> Seq.toList
t.Length ==? 1 // same as assertEqual "" 1 t.Length
yield testCase "with nested testcase" <| fun _ ->
let t = Test.filter (Strings.contains "d") tests |> Test.toTestCodeList |> Seq.toList
t.Length ==? 1
yield testCase "with one testlist" <| fun _ ->
let t = Test.filter (Strings.contains "c") tests |> Test.toTestCodeList |> Seq.toList
t.Length ==? 2
yield testCase "with no results" <| fun _ ->
let t = Test.filter ((=) "z") tests |> Test.toTestCodeList |> Seq.toList
t.Length ==? 0
]
testList "Timeout" [
testCase "fail" <| fun _ ->
let test = TestCase(Test.timeout 10 (fun _ -> Thread.Sleep 100))
let result = evalSilent test |> sumTestResults
result.Failed ==? 1
testCase "pass" <| fun _ ->
let test = TestCase(Test.timeout 1000 ignore)
let result = evalSilent test |> sumTestResults
result.Passed ==? 1
]
testList "Reflection" [
let getMember name =
Dummy.thisModuleType.Value.GetMember name
|> Array.tryFind (fun _ -> true)
let getTest =
getMember
>> Option.bind testFromMember
>> Option.bind (function TestLabel(name, _) -> Some name | _ -> None)
yield testCase "from member" <| fun _ ->
getTest "testA" ==? Some "test A"
yield testCase"from function" <| fun _ ->
getTest "testB" ==? Some "test B"
yield testCase"from type" <| fun _ ->
match testFromType Dummy.thisModuleType.Value with
| Some (TestList (
Seq.Two (
TestLabel("test B", TestList _),
TestLabel("test A", TestList _)))) -> ()
| x -> failtestf "TestList expected, found %A" x
yield testCase "from empty type" <| fun _ ->
let test = testFromType EmptyModule.thisModuleType.Value
Assert.None("", test)
]
testList "parse args" [
testCase "default" <|
fun _ ->
let opts = parseArgs [||]
opts.Parallel ==? false
testCase "parallel" <|
fun _ ->
let opts = parseArgs [|"/m"|]
opts.Parallel ==? true
]
testList "transformations" [
testCase "multiple cultures" <| fun _ ->
let withCulture culture f x =
let c = Thread.CurrentThread.CurrentCulture
try
Thread.CurrentThread.CurrentCulture <- culture
f x
finally
Thread.CurrentThread.CurrentCulture <- c
let testWithCultures (cultures: #seq<CultureInfo>) =
Test.replaceTestCode <| fun name test ->
testList name [
for c in cultures ->
testCase c.Name (withCulture c test)
]
let atest = test "parse" {
Single.Parse("123,33") ==? 123.33f
}
let cultures =
["en-US"; "es-AR"; "fr-FR"]
|> List.map CultureInfo.GetCultureInfo
let culturizedTests = testWithCultures cultures atest
let results =
evalSilent culturizedTests
|> Seq.map (fun r -> r.Name, r.Result)
|> Map.ofSeq
Assert.Equal("results count", 3, results.Count)
let inline assertTrue msg x = Assert.Equal(msg, true, x)
assertTrue "parse en-US fails" (TestResult.isFailed results.["parse/en-US"])
assertTrue "parse es-AR passes" (TestResult.isPassed results.["parse/es-AR"])
assertTrue "parse fr-FR passes" (TestResult.isPassed results.["parse/fr-FR"])
]
testList "assertions" [
testList "NotEqual" [
testCase "pass" <| fun _ ->
Assert.NotEqual("should be different", "", "monkey")
testCase "fail" <| fun _ ->
let test () = Assert.NotEqual("should fail", "", "")
assertTestFails test
]
testList "raise" [
testCase "pass" <| fun _ ->
Assert.Raise("", typeof<ArgumentNullException>, fun _ -> nullArg "")
testCase "fail with incorrect exception" <| fun _ ->
let test () = Assert.Raise("", typeof<ArgumentException>, fun _ -> nullArg "")
assertTestFails test
testCase "fail with no exception" <| fun _ ->
let test () = Assert.Raise("", typeof<ArgumentNullException>, ignore)
assertTestFails test
]
testList "string contain" [
testCase "pass" <| fun _ ->
Assert.StringContains("", "hello", "hello world")
testCase "fail" <| fun _ ->
let test () = Assert.StringContains("", "a", "hello world")
assertTestFails test
]
]
testList "computation expression" [
let testNormal a =
testCase "" <| fun _ ->
if a < 0
then failtest "negative"
if a > 5
then failwith "over 5"
let testCompExp a =
test "" {
if a < 0
then failtest "negative"
if a > 5
then failwith "over 5"
}
for c in [-5; 1; 6] ->
testCase (sprintf "compare comp.exp. and normal with value %d" c) <| fun _ ->
let normal = evalSilent <| testNormal c
let compexp = evalSilent <| testCompExp c
let normalTag = TestResult.tag normal.[0].Result
let compexpTag = TestResult.tag compexp.[0].Result
Assert.Equal("result", normalTag, compexpTag)
]
]