forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnionTests.fs
138 lines (125 loc) · 3.26 KB
/
UnionTests.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
module Fantomas.Tests.UnionsTests
open NUnit.Framework
open FsUnit
open Fantomas.CodeFormatter
open Fantomas.Tests.TestHelper
[<Test>]
let ``enums declaration``() =
formatSourceString false """
type FontVariant =
| [<Description("small-caps")>] SmallCaps = 0""" config
|> prepend newline
|> should equal """
type FontVariant =
| [<Description("small-caps")>] SmallCaps = 0
"""
[<Test>]
let ``discriminated unions declaration``() =
formatSourceString false "type X = private | A of AParameters | B" config
|> prepend newline
|> should equal """
type X =
private
| A of AParameters
| B
"""
[<Test>]
let ``enums conversion``() =
formatSourceString false """
type uColor =
| Red = 0u
| Green = 1u
| Blue = 2u
let col3 = Microsoft.FSharp.Core.LanguagePrimitives.EnumOfValue<uint32, uColor>(2u)""" config
|> prepend newline
|> should equal """
type uColor =
| Red = 0u
| Green = 1u
| Blue = 2u
let col3 =
Microsoft.FSharp.Core.LanguagePrimitives.EnumOfValue<uint32, uColor>(2u)
"""
[<Test>]
let ``discriminated unions with members``() =
formatSourceString false """
type Type
= TyLam of Type * Type
| TyVar of string
| TyCon of string * Type list
with override this.ToString() =
match this with
| TyLam (t1, t2) -> sprintf "(%s -> %s)" (t1.ToString()) (t2.ToString())
| TyVar a -> a
| TyCon (s, ts) -> s""" config
|> prepend newline
|> should equal """
type Type =
| TyLam of Type * Type
| TyVar of string
| TyCon of string * Type list
override this.ToString() =
match this with
| TyLam(t1, t2) -> sprintf "(%s -> %s)" (t1.ToString()) (t2.ToString())
| TyVar a -> a
| TyCon(s, ts) -> s
"""
[<Test>]
let ``should keep attributes on union cases``() =
formatSourceString false """
type Argument =
| [<MandatoryAttribute>] Action of string
| [<MandatoryAttribute>] ProjectFile of string
| PackageId of string
| Version of string""" config
|> prepend newline
|> should equal """
type Argument =
| [<MandatoryAttribute>] Action of string
| [<MandatoryAttribute>] ProjectFile of string
| PackageId of string
| Version of string
"""
[<Test>]
let ``should be able to define named unions``() =
formatSourceString false """
type Thing =
| Human of Name:string * Age:int
| Cat of Name:string * HoursSleptADay:int
type Strategy =
| Adaptive
| Fundamental
| ShortAR of p:int // F# 3.1 syntax
| BuyHold""" config
|> prepend newline
|> should equal """
type Thing =
| Human of Name : string * Age : int
| Cat of Name : string * HoursSleptADay : int
type Strategy =
| Adaptive
| Fundamental
| ShortAR of p : int // F# 3.1 syntax
| BuyHold
"""
[<Test>]
let ``should be able to pattern match on unions``() =
formatSourceString false """
type TestUnion = Test of A : int * B : int
[<EntryPoint>]
let main argv =
let d = Test(B = 1, A = 2)
match d with
| Test(A = a; B = b) -> a + b
| _ -> 0""" config
|> prepend newline
|> should equal """
type TestUnion =
| Test of A : int * B : int
[<EntryPoint>]
let main argv =
let d = Test(B = 1, A = 2)
match d with
| Test (A = a; B = b) -> a + b
| _ -> 0
"""