forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecordTests.fs
229 lines (203 loc) · 6.61 KB
/
RecordTests.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
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
228
229
module Fantomas.Tests.RecordTests
open NUnit.Framework
open FsUnit
open Fantomas.CodeFormatter
open Fantomas.Tests.TestHelper
[<Test>]
let ``record declaration``() =
formatSourceString false "type AParameters = { a : int }" config
|> prepend newline
|> should equal """
type AParameters =
{ a : int }
"""
[<Test>]
let ``record declaration with implementation visibility attribute``() =
formatSourceString false "type AParameters = private { a : int; b: float }" config
|> prepend newline
|> should equal """
type AParameters =
private { a : int
b : float }
"""
[<Test>]
let ``record signatures``() =
formatSourceString true """
module RecordSignature
/// Represents simple XML elements.
type Element =
{
/// The attribute collection.
Attributes : IDictionary<Name,string>
/// The children collection.
Children : seq<INode>
/// The qualified name.
Name : Name
}
interface INode
/// Constructs an new empty Element.
static member Create : name: string * ?uri: string -> Element
/// Replaces the children.
static member WithChildren : children: #seq<#INode> -> self: Element -> Element
/// Replaces the children.
static member ( - ) : self: Element * children: #seq<#INode> -> Element
/// Replaces the attributes.
static member WithAttributes : attrs: #seq<string*string> -> self: Element -> Element
/// Replaces the attributes.
static member ( + ) : self: Element * attrs: #seq<string*string> -> Element
/// Replaces the children with a single text node.
static member WithText : text: string -> self: Element-> Element
/// Replaces the children with a single text node.
static member ( -- ) : self: Element * text: string -> Element""" { config with SemicolonAtEndOfLine = true }
|> prepend newline
|> should equal """
module RecordSignature
/// Represents simple XML elements.
type Element =
{ /// The attribute collection.
Attributes : IDictionary<Name, string>;
/// The children collection.
Children : seq<INode>;
/// The qualified name.
Name : Name }
interface INode
/// Constructs an new empty Element.
static member Create : name:string * ?uri:string -> Element
/// Replaces the children.
static member WithChildren : children:#seq<#INode>
-> self:Element -> Element
/// Replaces the children.
static member (-) : self:Element * children:#seq<#INode> -> Element
/// Replaces the attributes.
static member WithAttributes : attrs:#seq<string * string>
-> self:Element -> Element
/// Replaces the attributes.
static member (+) : self:Element * attrs:#seq<string * string> -> Element
/// Replaces the children with a single text node.
static member WithText : text:string -> self:Element -> Element
/// Replaces the children with a single text node.
static member (--) : self:Element * text:string -> Element
"""
[<Test>]
let ``records with update``() =
formatSourceString false """
type Car = {
Make : string
Model : string
mutable Odometer : int
}
let myRecord3 = { myRecord2 with Y = 100; Z = 2 }""" config
|> prepend newline
|> should equal """
type Car =
{ Make : string
Model : string
mutable Odometer : int }
let myRecord3 =
{ myRecord2 with Y = 100
Z = 2 }
"""
// the current behavior results in a compile error since the if is not aligned properly
[<Test>]
let ``should not break inside of if statements in records``() =
formatSourceString false """let XpkgDefaults() =
{
ToolPath = "./tools/xpkg/xpkg.exe"
WorkingDir = "./";
TimeOut = TimeSpan.FromMinutes 5.
Package = null
Version = if not isLocalBuild then buildVersion else "0.1.0.0"
OutputPath = "./xpkg"
Project = null
Summary = null
Publisher = null
Website = null
Details = "Details.md"
License = "License.md"
GettingStarted = "GettingStarted.md"
Icons = []
Libraries = []
Samples = [];
}
""" { config with SemicolonAtEndOfLine = true }
|> should equal """let XpkgDefaults() =
{ ToolPath = "./tools/xpkg/xpkg.exe";
WorkingDir = "./";
TimeOut = TimeSpan.FromMinutes 5.;
Package = null;
Version =
if not isLocalBuild then buildVersion
else "0.1.0.0";
OutputPath = "./xpkg";
Project = null;
Summary = null;
Publisher = null;
Website = null;
Details = "Details.md";
License = "License.md";
GettingStarted = "GettingStarted.md";
Icons = [];
Libraries = [];
Samples = [] }
"""
[<Test>]
let ``should not add redundant newlines when using a record in a DU``() =
formatSourceString false """
let rec make item depth =
if depth > 0 then
Tree({ Left = make (2 * item - 1) (depth - 1)
Right = make (2 * item) (depth - 1) }, item)
else Tree(defaultof<_>, item)""" config
|> prepend newline
|> should equal """
let rec make item depth =
if depth > 0 then
Tree({ Left = make (2 * item - 1) (depth - 1)
Right = make (2 * item) (depth - 1) }, item)
else Tree(defaultof<_>, item)
"""
[<Test>]
let ``should keep unit of measures in record and DU declaration``() =
formatSourceString false """
type rate = {Rate:float<GBP*SGD/USD>}
type rate2 = Rate of float<GBP/SGD*USD>
""" config
|> prepend newline
|> should equal """
type rate =
{ Rate : float<GBP * SGD / USD> }
type rate2 =
| Rate of float<GBP / SGD * USD>
"""
[<Test>]
let ``should keep comments on records``() =
formatSourceString false """
let newDocument = //somecomment
{ program = Encoding.Default.GetBytes(document.Program) |> Encoding.UTF8.GetString
content = Encoding.Default.GetBytes(document.Content) |> Encoding.UTF8.GetString
created = document.Created.ToLocalTime() }
|> JsonConvert.SerializeObject
""" config
|> prepend newline
|> should equal """
let newDocument = //somecomment
{ program =
Encoding.Default.GetBytes(document.Program) |> Encoding.UTF8.GetString
content =
Encoding.Default.GetBytes(document.Content) |> Encoding.UTF8.GetString
created = document.Created.ToLocalTime() }
|> JsonConvert.SerializeObject
"""
[<Test>]
let ``should preserve inherit parts in records``() =
formatSourceString false """
type MyExc =
inherit Exception
new(msg) = {inherit Exception(msg)}
""" config
|> prepend newline
|> should equal """
type MyExc =
inherit Exception
new(msg) = { inherit Exception(msg) }
"""