-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestExamples.fs
162 lines (136 loc) · 5.32 KB
/
TestExamples.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
namespace ShapeSifter.Test
open NUnit.Framework
open HCollections
open ShapeSifter
open ShapeSifter.Patterns
open TypeEquality
open FsUnitTyped
[<TestFixture>]
module TestExamples =
[<Test>]
let ``Simple example`` () =
let tryString (a : 'a) : string option =
match tType<'a> with
| String (teq : Teq<'a, string>) -> Teq.castTo teq a |> Some
| _ -> None
tryString 1234 |> shouldEqual None
tryString "hello" |> shouldEqual (Some "hello")
[<Test>]
let ``List example 1`` () =
let tryListLength (a : 'a) : int option =
match tType<'a> with
| List crate ->
{ new ListTeqEvaluator<_, _> with
member _.Eval (teq : Teq<'a, 'b list>) =
a |> Teq.castTo teq |> List.length |> Some
}
|> crate.Apply
| _ -> None
tryListLength "hello" |> shouldEqual None
tryListLength [ 'a' .. 'z' ] |> shouldEqual (Some 26)
[<Test>]
let ``List example 2`` () =
let tryListSomeCount (a : 'a) : int option =
match tType<'a> with
| List crate ->
{ new ListTeqEvaluator<_, _> with
member _.Eval (teq1 : Teq<'a, 'b list>) =
match tType<'b> with
| Option crate ->
{ new OptionTeqEvaluator<_, _> with
member _.Eval (teq2 : Teq<'b, 'c option>) =
let teq : Teq<'a, 'c option list> = Teq.transitivity teq1 (Teq.Cong.list teq2)
let xs : 'c option list = Teq.castTo teq a
xs |> List.filter Option.isSome |> List.length |> Some
}
|> crate.Apply
| _ -> None
}
|> crate.Apply
| _ -> None
tryListSomeCount [ None ; Some 'a' ; None ; Some 'b' ; Some 'c' ]
|> shouldEqual (Some 3)
[<Test>]
let ``Tuple example 1`` () =
let tryTupleLength (a : 'a) : int option =
match tType<'a> with
| Tuple crate ->
{ new TupleConvEvaluator<_, _> with
member _.Eval (ts : 'ts TypeList) (conv : Conv<'a, 'ts HList>) =
a |> conv.To |> HList.length |> Some
}
|> crate.Apply
| _ -> None
tryTupleLength ("hello", false) |> shouldEqual (Some 2)
tryTupleLength (5, 5, 5, 5) |> shouldEqual (Some 4)
[<Test>]
let ``Tuple example 2`` () =
let trySumTupleInts (a : 'a) : int option =
match tType<'a> with
| Tuple crate ->
{ new TupleConvEvaluator<_, _> with
member _.Eval _ (conv : Conv<'a, 'ts HList>) =
let xs : 'ts HList = a |> conv.To
let folder =
{ new HListFolder<int> with
member _.Folder sum (x : 'b) =
match tType<'b> with
| Int teq -> sum + (x |> Teq.castTo teq)
| _ -> sum
}
HList.fold folder 0 xs |> Some
}
|> crate.Apply
| _ -> None
trySumTupleInts (5, 5, 5, 5) |> shouldEqual (Some 20)
trySumTupleInts (5, false, 3, "hello") |> shouldEqual (Some 8)
trySumTupleInts ("hello", false) |> shouldEqual (Some 0)
let rec shoutify<'ts> (xs : 'ts HList) : 'ts HList =
match xs |> HList.toTypeList |> TypeList.split with
| Choice1Of2 _ -> xs
| Choice2Of2 crate ->
{ new TypeListConsEvaluator<_, _> with
member _.Eval _ (teq : Teq<'ts, 'u -> 'us>) =
let xs : ('u -> 'us) HList = xs |> Teq.castTo (HList.cong teq)
let head =
match tType<'u> with
| String teq -> (xs |> HList.head |> Teq.castTo teq).ToUpper () |> Teq.castFrom teq
| _ -> xs |> HList.head
let tail = xs |> HList.tail |> shoutify
HList.cons head tail |> Teq.castFrom (HList.cong teq)
}
|> crate.Apply
let tryShoutifyRecord (a : 'a) : 'a option =
match tType<'a> with
| Record crate ->
{ new RecordConvEvaluator<_, _> with
member _.Eval _ _ (conv : Conv<'a, 'ts HList>) =
let xs : 'ts HList = a |> conv.To
shoutify xs |> conv.From |> Some
}
|> crate.Apply
| _ -> None
type MyRecord =
{
FirstName : string
LastName : string
Age : int
Location : string
}
[<Test>]
let ``Record example`` () =
let sample =
{
FirstName = "Bob"
LastName = "Sample"
Age = 35
Location = "London"
}
let expected =
{
FirstName = "BOB"
LastName = "SAMPLE"
Age = 35
Location = "LONDON"
}
tryShoutifyRecord sample |> shouldEqual (Some expected)