forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OperatorTests.fs
178 lines (156 loc) · 5.54 KB
/
OperatorTests.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
module Fantomas.Tests.OperatorTests
open NUnit.Framework
open FsUnit
open Fantomas.CodeFormatter
open Fantomas.Tests.TestHelper
[<Test>]
let ``should format prefix operators``() =
formatSourceString false """let x = -y
let z = !!x
""" config
|> should equal """let x = -y
let z = !!x
"""
[<Test>]
let ``should keep triple ~~~ operator``() =
formatSourceString false """x ~~~FileAttributes.ReadOnly
""" config
|> should equal """x ~~~FileAttributes.ReadOnly
"""
[<Test>]
let ``should keep single triple ~~~ operator``() =
formatSourceString false """~~~FileAttributes.ReadOnly
""" config
|> should equal """~~~FileAttributes.ReadOnly
"""
[<Test>]
let ``should keep parens around ? operator definition``() =
formatSourceString false """let (?) f s = f s
""" config
|> should equal """let (?) f s = f s
"""
[<Test>]
let ``should keep parens around ?<- operator definition``() =
formatSourceString false """let (?<-) f s = f s
""" config
|> should equal """let (?<-) f s = f s
"""
[<Test>]
let ``should keep parens around !+ prefix operator definition``() =
formatSourceString false """let (!+) x = Include x
""" config
|> should equal """let (!+) x = Include x
"""
[<Test>]
let ``should keep parens around ++ infix operator definition``() =
formatSourceString false """let (++) x y = { x with Includes = y :: x.Includes }
""" config
|> should equal """let (++) x y = { x with Includes = y :: x.Includes }
"""
[<Test>]
let ``should keep parens around inlined ==> operator definition``() =
formatSourceString false """let inline (==>) x y = f x y
""" config
|> should equal """let inline (==>) x y = f x y
"""
[<Test>]
let ``should keep parens around inlined operator definition``() =
formatSourceString false """let inline (@@) path1 path2 = Path.Combine(path1, path2)
""" config
|> should equal """let inline (@@) path1 path2 = Path.Combine(path1, path2)
"""
[<Test>]
let ``should pattern match on quotation expression``() =
formatSourceString false """let rec print expr =
match expr with
| SpecificCall <@@ (+) @@> (_, _, exprList) ->
print exprList.Head
printf " + "
print exprList.Tail.Head
| _ -> ()""" config
|> should equal """let rec print expr =
match expr with
| SpecificCall <@@ (+) @@> (_, _, exprList) ->
print exprList.Head
printf " + "
print exprList.Tail.Head
| _ -> ()
"""
[<Test>]
let ``should break on . operator``() =
formatSourceString false """pattern.Replace(".", @"\.").Replace("$", @"\$").Replace("^", @"\^").Replace("{", @"\{").Replace("[", @"\[").Replace("(", @"\(").Replace(")", @"\)").Replace("+", @"\+")
""" config
|> prepend newline
|> should equal """
pattern.Replace(".", @"\.").Replace("$", @"\$").Replace("^", @"\^")
.Replace("{", @"\{").Replace("[", @"\[").Replace("(", @"\(")
.Replace(")", @"\)").Replace("+", @"\+")
"""
// the current behavior results in a compile error since line break is before the parens and not before the .
[<Test>]
let ``should break on . operator and keep indentation``() =
formatSourceString false """let pattern =
(x + y)
.Replace(seperator + "**" + seperator, replacementSeparator + "(.|?" + replacementSeparator + ")?" )
.Replace("**" + seperator, ".|(?<=^|" + replacementSeparator + ")" )
""" config
|> should equal """let pattern =
(x + y)
.Replace(seperator + "**" + seperator,
replacementSeparator + "(.|?" + replacementSeparator + ")?")
.Replace("**" + seperator, ".|(?<=^|" + replacementSeparator + ")")
"""
[<Test>]
let ``should keep space between ( and * in *** operator definition``() =
formatSourceString false """let inline ( ***) l1 l2 = pair l2 l1
""" config
|> should equal """let inline ( *** ) l1 l2 = pair l2 l1
"""
[<Test>]
let ``should keep space between ( and * in *= operator definition``() =
formatSourceString false """let inline ( *=) l v = update (( *) v) l
""" config
|> should equal """let inline ( *= ) l v = update ((*) v) l
"""
[<Test>]
let ``should not add space around ? operator``() =
formatSourceString false """let x = y?z.d?c.[2]?d.xpto()""" config
|> should equal """let x = y?z.d?c.[2]?d.xpto()
"""
[<Test>]
let ``should understand ? as an infix operator``() =
formatSourceString false """try
item.MethodInfo.Method.Invoke(null, ipa)
|> (fun x -> x?Invoke (true))
|> fun (t : Task) -> t.Wait()
with _ -> ()""" config
|> should equal """try
item.MethodInfo.Method.Invoke(null, ipa)
|> (fun x -> x?Invoke (true))
|> fun (t : Task) -> t.Wait()
with _ -> ()
"""
[<Test>]
let ``should not mess up ?<- operator``() =
formatSourceString false """x?v <- 2""" config
|> should equal """x?v <- 2
"""
[<Test>]
let ``should pipeline monadic bind``() =
formatSourceString false """strToInt "1"
>>= strAddLong "A long argument that is ignored" "2"
>>= strAddLong "A long argument that is ignored" "2"
>>= strAddLong "A long argument that is ignored" "2"
>>= strAddLong "A long argument that is ignored" "2"
>>= strAddLong "A long argument that is ignored" "2"
>>= strAddLong "A long argument that is ignored" "2"
"""
config
|> should equal """strToInt "1"
>>= strAddLong "A long argument that is ignored" "2"
>>= strAddLong "A long argument that is ignored" "2"
>>= strAddLong "A long argument that is ignored" "2"
>>= strAddLong "A long argument that is ignored" "2"
>>= strAddLong "A long argument that is ignored" "2"
>>= strAddLong "A long argument that is ignored" "2"
"""