forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormattingSelectionOnlyTests.fs
155 lines (134 loc) · 4.45 KB
/
FormattingSelectionOnlyTests.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
module Fantomas.Tests.FormattingSelectionOnlyTests
open NUnit.Framework
open FsUnit
open Fantomas.CodeFormatter
open Fantomas.Tests.TestHelper
[<Test>]
let ``should format a part of a line correctly``() =
formatSelectionOnly false (makeRange 3 8 3 10) """
let x = 2 + 3
let y = 1+2
let z = x + y""" config
|> should equal """1 + 2"""
[<Test>]
let ``should format a whole line correctly and preserve indentation``() =
formatSelectionOnly false (makeRange 3 0 3 36) """
let base1 = d1 :> Base1
let derived1 = base1 :?> Derived1""" config
|> should equal """ let derived1 = base1 :?> Derived1"""
[<Test>]
let ``should format a few lines correctly and preserve indentation``() =
formatSelectionOnly false (makeRange 3 4 5 51) """
let rangeTest testValue mid size =
match testValue with
| var1 when var1 >= mid - size/2 && var1 <= mid + size/2 -> printfn "The test value is in range."
| _ -> printfn "The test value is out of range."
let (var1, var2) as tuple1 = (1, 2)""" config
|> append newline
|> should equal """match testValue with
| var1 when var1 >= mid - size / 2 && var1 <= mid + size / 2 ->
printfn "The test value is in range."
| _ -> printfn "The test value is out of range."
"""
[<Test>]
let ``should format a top-level let correctly``() =
formatSelectionOnly false (makeRange 3 0 3 10) """
let x = 2 + 3
let y = 1+2
let z = x + y""" config
|> should equal """let y = 1 + 2"""
[<Test>]
let ``should skip whitespace at the beginning of lines``() =
formatSelectionOnly false (makeRange 3 3 3 27) """
type Product' (backlogItemId) =
let mutable ordering = 0
let mutable version = 0
let backlogItems = []""" config
|> should equal """ let mutable ordering = 0"""
[<Test>]
let ``should parse a complete expression correctly``() =
formatSelectionOnly false (makeRange 4 0 5 35) """
open Fantomas.CodeFormatter
let config = { FormatConfig.Default with
IndentSpaceNum = 2 }
let source = "
let Multiple9x9 () =
for i in 1 .. 9 do
printf \"\\n\";
for j in 1 .. 9 do
let k = i * j in
printf \"%d x %d = %2d \" i j k;
done;
done;;
Multiple9x9 ();;"
""" config
|> should equal """let config = { FormatConfig.Default with IndentSpaceNum = 2 }"""
[<Test>]
let ``should format the selected pipeline correctly``() =
formatSelectionOnly false (makeRange 3 4 7 18) """
let r =
[ "abc"
"a"
"b"
"" ]
|> List.map id""" config
|> should equal """[ "abc"; "a"; "b"; "" ] |> List.map id"""
[<Test>]
let ``should preserve line breaks before and after selection``() =
formatSelectionOnly false (makeRange 3 0 4 25) """
assert (3 > 2)
let result = lazy (x + 10)
do printfn "Hello world"
""" config
|> should equal """let result = lazy (x + 10)"""
[<Test>]
let ``should detect members and format appropriately``() =
formatSelectionOnly false (makeRange 4 0 5 32) """
type T () =
let items = []
override x.Reorder () =
items |> List.iter ignore
""" config
|> should equal """ override x.Reorder() = items |> List.iter ignore"""
[<Test>]
let ``should format the and branch of recursive functions``() =
formatSelectionOnly false (makeRange 3 0 4 34) """
let rec createJArray x = createJObject
and createJObject y = createJArray
""" config
|> should equal """and createJObject y = createJArray
"""
[<Test>]
let ``should format recursive types correctly``() =
formatSelectionOnly false (makeRange 7 0 10 48) """
type Folder(pathIn : string) =
let path = pathIn
let filenameArray : string array = System.IO.Directory.GetFiles(path)
member this.FileArray =
Array.map (fun elem -> new File(elem, this)) filenameArray
and File(filename: string, containingFolder: Folder) =
member __.Name = filename
member __.ContainingFolder = containingFolder
""" config
|> prepend newline
|> should equal """
and File(filename : string, containingFolder : Folder) =
member __.Name = filename
member __.ContainingFolder = containingFolder
"""
[<Test>]
let ``should not add trailing whitespaces and preserve indentation``() =
formatSelectionOnly false (makeRange 4 0 7 15) """
module Enums =
// Declaration of an enumeration.
type Colour =
| Red = 0
| Green = 1
| Blue = 2
""" config
|> prepend newline
|> should equal """
type Colour =
| Red = 0
| Green = 1
| Blue = 2"""