-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetOptics.Test.fs
More file actions
121 lines (113 loc) · 3.76 KB
/
NetOptics.Test.fs
File metadata and controls
121 lines (113 loc) · 3.76 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
module NetOptics.Test
open NetOptics
type Node<'k, 'v> =
{ key: 'k
value: 'v
smaller: BST<'k, 'v>
greater: BST<'k, 'v> }
and BST<'k, 'v> = option<Node<'k, 'v>>
module Node =
let keyL p = Optic.lens (fun r -> r.key) (fun v r -> {r with key = v}) p
let valueL p = Optic.lens (fun r -> r.value) (fun v r -> {r with value = v}) p
let smallerL p =
Optic.lens (fun r -> r.smaller) (fun v r -> {r with smaller = v}) p
let greaterL p =
Optic.lens (fun r -> r.greater) (fun v r -> {r with greater = v}) p
module BST =
open Node
let rec valuesT p =
Optic.someP
<< (smallerL << valuesT
|> Optic.andAlso valueL
|> Optic.andAlso (greaterL << valuesT))
<| p
let rec nodeL key =
Optic.choose <| function
| None -> Optic.idI
| Some n ->
if key < n.key then Optic.someP << smallerL << nodeL key
elif n.key < key then Optic.someP << greaterL << nodeL key
else Optic.idI
let valueP key = nodeL key << Optic.someP << valueL
let [<EntryPoint>] main _ =
let mutable passed = 0
let mutable failed = 0
let testEq actual expected =
if actual <> expected
then failed <- failed + 1; printfn "Expected %A, but got %A" expected actual
else passed <- passed + 1
testEq
<| Optic.view (Optic.fstL << Optic.sndL) (("101", 42), true)
<| 42
testEq
<| Optic.canView (Optic.atP 5) [|3; 1; 4|]
<| false
testEq
<| Optic.fold 0 (+) Optic.elemsT [|3; 1; 4|]
<| 8
testEq
<| Optic.over (Optic.elemsT << Optic.fstL) (~-)
[|(3, "a"); (1, "b"); (4, "c")|]
<| upcast [|(-3, "a"); (-1, "b"); (-4, "c")|]
testEq
<| Optic.remove (Optic.atP 1) [|3; 1; 4|]
<| upcast [|3; 4|]
testEq
<| Optic.remove (Optic.elemsT << Optic.sndL << Optic.whereP (fun x -> x < 3))
[|(1, 3); (1, 1); (1, 4); (1, 1); (1, 5); (1, 9); (1, 2)|]
<| upcast [|(1, 3); (1, 4); (1, 5); (1, 9)|]
testEq
<| Optic.collect
(Optic.elemsT << Optic.sndL << Optic.whereP (fun x -> x < 3))
[|(1, 3); (1, 1); (1, 4); (1, 1); (1, 5); (1, 9); (1, 2)|]
<| upcast [|1; 1; 2|]
testEq
<| Optic.disperse
(Optic.elemsT
<< Optic.sndL
<< Optic.whereP (fun x -> x < 3))
[|-9; -1; -1|]
[|(1, 3); (1, 1); (1, 4); (1, 1); (1, 5); (1, 9); (1, 2)|]
<| upcast [|(1, 3); (1, -9); (1, 4); (1, -1); (1, 5); (1, 9); (1, -1)|]
testEq
<| Optic.over
(Optic.partsOf
(Optic.elemsT
<< Optic.sndL
<< Optic.whereP (fun x -> x < 3)))
(Optic.view Optic.revI)
[|(1, 3); (1, 1); (1, 4); (1, 1); (1, 5); (1, 9); (1, 2)|]
<| upcast [|(1, 3); (1, 2); (1, 4); (1, 1); (1, 5); (1, 9); (1, 1)|]
testEq
<| Optic.review Optic.revI [|1;2;3|]
<| upcast [|3; 2; 1|]
testEq
<| Optic.review
(Optic.splitI "-"
<< Optic.revI
<< Optic.invertI (Optic.splitI "-"))
"this-it-is"
<| "is-it-this"
testEq <| Optic.view (Optic.containsL 4) [|3; 1|] <| false
testEq <| Optic.set (Optic.containsL 4) true [|3; 1|] <| upcast [|3; 1; 4|]
testEq <| Optic.set (Optic.containsL 1) false [|3; 1; 4|] <| upcast [|3; 4|]
testEq
<| (History.init id 1
|> Optic.set History.present 2
|> Optic.set History.present 3
|> Optic.over History.undoIndex ((+) -1)
|> History.redoForget
|> History.undoForget
|> Optic.view History.present)
<| 2
testEq
<| Optic.view Optic.querystringI
"message=hello&message=world"
<| Map.ofArray [|("message", [|"hello"; "world"|] :> IROL<_>)|]
testEq
<| Optic.review Optic.querystringI
(Map.ofArray [|("message", [|"hello"; "world"|] :> IROL<_>)|])
<| "?message=hello&message=world"
if failed <> 0
then printfn "%d PASSED, %d FAILED" passed failed; 1
else printfn "%d PASSED" passed; 0