-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expecto.fs
165 lines (125 loc) · 4.64 KB
/
Expecto.fs
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
module Tests
open Expecto
open System
let testCase' = testCase;
let test' = test;
let testProperty' name f = testPropertyWithConfig { FsCheckConfig.defaultConfig with arbitrary = []} name f
let theory (name:string) (cases: #seq<'T>) (fTest: 'T -> 'U) =
let dataToTest caseData =
testCase (string caseData) <| fun () ->
fTest caseData |> ignore
testList name (cases |> Seq.map dataToTest |> List.ofSeq)
let theoryWithResult (name:string) (cases: #seq<('T*'U)>) (fTest: 'T -> 'U) =
let dataToTest (caseData,expected) =
testCase (string (caseData, expected)) <| fun () ->
let actual = fTest caseData
Expect.equal actual expected $"Input: {caseData} \nExpected {expected} \nActual: {actual}"
testList name (cases |> Seq.map dataToTest |> List.ofSeq)
type ICrudAPI<'a> = {
Get: Guid -> 'a option
Save: Guid -> 'a -> unit
ListAll: unit -> 'a list
Generate: unit -> 'a
}
let reusableTestSuite (variantExplanation: string) (sutFactory: unit -> ICrudAPI<'a>) =
testList $"Can CRUD for: {variantExplanation}" [
testCase "Geting an unsaved item returns none" <| fun () ->
let sut = sutFactory ()
Expect.equal (sut.Get (Guid.NewGuid())) None ""
testCase "Get and save can roundtrip" <| fun () ->
let sut = sutFactory ()
let expected = sut.Generate ()
let id = Guid.NewGuid()
sut.Save id expected
Expect.equal (sut.Get id) (Some expected) ""
let setEqual actual expected message =
let likeness (list: 'a list) =
{| Length = list.Length; UniqueValues = set list|}
Expect.equal (likeness actual) (likeness expected) message
testCase "List should include all saved items" <| fun () ->
let sut = sutFactory ()
let expected = List.init 5 (fun _ -> sut.Generate ())
expected |> List.iter (fun x -> sut.Save (Guid.NewGuid()) x)
setEqual (sut.ListAll ()) expected ""
]
let InMemoryCrudAPIFactory () =
let store = Collections.Generic.Dictionary<Guid, int> ()
{
Generate = (fun () -> Random.Shared.Next())
Get = (fun id -> if store.ContainsKey id then Some store[id] else None)
Save = (fun id value -> store[id] <- value)
ListAll = (fun () -> store.Values |> List.ofSeq)
}
[<Tests>]
let tests =
testList "ExpectoTests" [
testList "Wrapped test methods" [
testCase' "Methods that call other test methods still show in test explorer" <| fun _ ->
()
test' "That includes test builder wrappers" {
()
}
theoryWithResult "This repeats a test with different specified data" [
(1,2),3
(2,2),4
(1,2),4
] <| fun (n,m) ->
n + m
testProperty' "A property tests with shared custom config" <| fun (i: int) ->
true
]
reusableTestSuite "InMemoryCrudAPI" InMemoryCrudAPIFactory
testCase "Contains+separator.characters" <| fun _ ->
let subject = true
Expect.isTrue subject "I compute, therefore I am."
testCase "universe exists (╭ರᴥ•́)" <| fun _ ->
let subject = true
Expect.isTrue subject "I compute, therefore I am."
testCase "when true is not (should fail)" <| fun _ ->
let subject = false
Expect.isTrue subject "I should fail because the subject is false"
testCase "I'm always fail (should fail)" <| fun _ ->
Tests.failtest "This was expected..."
testCase "I'm skipped (should skip)" <| fun _ ->
Tests.skiptest "Yup, waiting for a sunny day..."
testCase "contains things" <| fun _ ->
Expect.containsAll [| 2; 3; 4 |] [| 2; 4 |]
"This is the case; {2,3,4} contains {2,4}"
testCase "contains things (should fail)" <| fun _ ->
Expect.containsAll [| 2; 3; 4 |] [| 2; 4; 1 |]
"Expecting we have one (1) in there"
testCase "Sometimes I want to ༼ノಠل͟ಠ༽ノ ︵ ┻━┻" <| fun _ ->
Expect.equal "abcdëf" "abcdef" "These should equal"
test "I am (should fail)" {
"╰〳 ಠ 益 ಠೃ 〵╯" |> Expect.equal true false
}
testList "Deeply nest" [
testList "Another layer" [
testCase "Bottom of nesting" (fun _ -> ())
]
]
]
let compose1 = testList "Not directly in parent" [
testCase "I pass " (fun _ -> ())
test "Me too" { ()}
]
let compose2 = testList "Also not directly in parent" [
testCase "Nya aa" (fun _ -> ())
test "Woof" { ()}
testList "Deeper nesting boi" [
testCase "Baaa" (fun _ -> ())
testList "So deep" [
testCase "Baaaaaaa" (fun _ -> ())
]
]
]
[<Tests>]
let t = testList "Composed test lists?" [
compose1
compose2
]
[<Tests>]
let t2 = testList "Composed test lists - 2" [
compose1
compose2
]