-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathTestUnionType.fs
More file actions
227 lines (191 loc) · 5.68 KB
/
Copy pathTestUnionType.fs
File metadata and controls
227 lines (191 loc) · 5.68 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
module Fable.Tests.UnionTypes
open Util.Testing
type Gender = Male | Female
type Either<'TL,'TR> =
| Left of 'TL
| Right of 'TR
member x.AsString() =
match x with
| Left y -> y.ToString()
| Right y -> y.ToString()
type MyUnion =
| Case0
| Case1 of string
| Case2 of string * string
| Case3 of string * string * string
type MyUnion2 =
| Tag of string
| NewTag of string
let (|Functional|NonFunctional|) (s: string) =
match s with
| "fsharp" | "haskell" | "ocaml" -> Functional
| _ -> NonFunctional
let (|Small|Medium|Large|) i =
if i < 3 then Small 5
elif i >= 3 && i < 6 then Medium "foo"
else Large
let (|FSharp|_|) (document : string) =
if document = "fsharp" then Some FSharp else None
let (|A|) n = n
// type JsonTypeInner = {
// Prop1: string
// Prop2: int
// }
// type JsonTestUnion =
// | IntType of int
// | StringType of string
// | TupleType of string * int
// | ObjectType of JsonTypeInner
// type Tree =
// | Leaf of int
// | Branch of Tree[]
// member this.Sum() =
// match this with
// | Leaf i -> i
// | Branch trees -> trees |> Seq.map (fun x -> x.Sum()) |> Seq.sum
type MyExUnion = MyExUnionCase of exn
type Wrapper(s: string) =
member x.Value = s |> Seq.rev |> Seq.map string |> String.concat ""
[<RequireQualifiedAccess>]
type MyUnion3 =
| Case1
| Case2
| Case3
type R = {
Name: string
Case: MyUnion3
}
#if FABLE_COMPILER
open Fable.Core
[<Erase>]
#endif
type DU = Int of int | Str of string
[<Fact>]
let ``test Union cases matches with no arguments can be generated`` () =
let x = Male
match x with
| Female -> true
| Male -> false
|> equal false
[<Fact>]
let ``test Union cases matches with one argument can be generated`` () =
let x = Left "abc"
match x with
| Left data -> data
| Right _ -> failwith "unexpected"
|> equal "abc"
[<Fact>]
let ``test Union methods can be generated`` () =
let x = Left 5
x.AsString()
|> equal "5"
[<Fact>]
let ``test Nested pattern matching works`` () =
let x = Right(Left 5)
match x with
| Left _ -> failwith "unexpected"
| Right x ->
match x with
| Left x -> x
| Right _ -> failwith "unexpected"
|> equal 5
[<Fact>]
let ``test Union cases matches with many arguments can be generated`` () =
let x = Case3("a", "b", "c")
match x with
| Case3(a, b, c) -> a + b + c
| _ -> failwith "unexpected"
|> equal "abc"
[<Fact>]
let ``test Pattern matching with common targets works`` () =
let x = MyUnion.Case2("a", "b")
match x with
| MyUnion.Case0 -> failwith "unexpected"
| MyUnion.Case1 _
| MyUnion.Case2 _ -> "a"
| MyUnion.Case3(a, b, c) -> a + b + c
|> equal "a"
[<Fact>]
let ``test Union cases called Tag still work (bug due to Tag field)`` () =
let x = Tag "abc"
match x with
| Tag x -> x
| _ -> failwith "unexpected"
|> equal "abc"
[<Fact>]
let ``test Comprehensive active patterns work`` () =
let isFunctional = function
| Functional -> true
| NonFunctional -> false
isFunctional "fsharp" |> equal true
isFunctional "csharp" |> equal false
isFunctional "haskell" |> equal true
[<Fact>]
let ``test Comprehensive active patterns can return values`` () =
let measure = function
| Small i -> string i
| Medium s -> s
| Large -> "bar"
measure 0 |> equal "5"
measure 10 |> equal "bar"
measure 5 |> equal "foo"
[<Fact>]
let ``test Partial active patterns which don't return values work`` () = // See #478
let isFunctional = function
| FSharp -> "yes"
| "scala" -> "fifty-fifty"
| _ -> "dunno"
isFunctional "scala" |> equal "fifty-fifty"
isFunctional "smalltalk" |> equal "dunno"
isFunctional "fsharp" |> equal "yes"
[<Fact>]
let ``test Active patterns can be combined with union case matching`` () = // See #306
let test = function
| Some(A n, Some(A m)) -> n + m
| _ -> 0
Some(5, Some 2) |> test |> equal 7
Some(5, None) |> test |> equal 0
None |> test |> equal 0
[<Fact>]
let ``test Types can have Exception fields`` () =
let (MyExUnionCase ex) =
try
exn "foo" |> raise
with ex -> MyExUnionCase ex
ex.Message |> equal "foo"
#if FABLE_COMPILER
[<Fact>]
let ``test Erased union type testing works`` () =
let toString (arg: U3<string, int, Wrapper>) =
match arg with
| U3.Case1 s -> s
| U3.Case2 i -> i * 2 |> string
| U3.Case3 t -> t.Value
U3.Case1 "HELLO" |> toString |> equal "HELLO"
U3.Case2 3 |> toString |> equal "6"
"HELLO" |> Wrapper |> U3.Case3 |> toString |> equal "OLLEH"
#endif
[<Fact>]
let ``test Equality works in filter`` () =
let original = [| { Name = "1"; Case = MyUnion3.Case1 } ; { Name = "2"; Case = MyUnion3.Case1 }; { Name = "3"; Case = MyUnion3.Case2 }; { Name = "4"; Case = MyUnion3.Case3 } |]
original
|> Array.filter (fun r -> r.Case = MyUnion3.Case1)
|> Array.length
|> equal 2
type S = S of string
[<Fact>]
let ``test sprintf formats strings cases correctly`` () =
let s = sprintf "%A" (S "1")
s |> equal "S \"1\""
// See https://github.com/fable-compiler/Fable/issues/4645
// A named union case field called `name` collided with the inherited `Union.name`
// property, which Python's @dataclass treated as a default value.
type NamedFieldUnion =
| NamedCase of name: string * optional: string
[<Fact>]
let ``test Named union case fields work when a field is called name`` () =
let value = NamedCase("v1", "v2")
match value with
| NamedCase(name, optional) ->
name |> equal "v1"
optional |> equal "v2"