Skip to content

Commit 1b8f6a8

Browse files
Fix naming consistency and add comprehensive test coverage for formatter
Co-authored-by: felix-andreas <[email protected]>
1 parent 3e98742 commit 1b8f6a8

18 files changed

+254
-11
lines changed

crates/roughly/tests/format/base.R.test

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,22 @@ x; y
2929
#'comment'
3030
#"comment"
3131

32-
#---- coments_all_positions
32+
#---- comment_formatting_edge_cases
33+
# basic comments should get space after #
34+
#comment
35+
#foo
36+
# special comments should preserve format
37+
#' roxygen comment
38+
#* special comment
39+
#: special comment
40+
## double hash comment
41+
#! shebang-style comment
42+
# empty comment just becomes #
43+
#
44+
# comments with quotes should be preserved
45+
#'comment with 'quotes''
46+
47+
#---- comments_all_positions
3348
# 1
3449
program # program
3550
# 2
@@ -46,6 +61,12 @@ NA_real_
4661
NA_character_
4762
NA_complex_
4863

64+
#---- keywords
65+
return
66+
next
67+
break
68+
...
69+
4970
#---- integers
5071
42L
5172
0L
@@ -59,6 +80,13 @@ NA_complex_
5980
1e-3
6081
1.8e10
6182

83+
#---- complex
84+
1i
85+
1.5i
86+
1e2i
87+
1+2i
88+
1.5+2.5i
89+
6290
#---- strings
6391
""
6492
"x"
@@ -74,6 +102,16 @@ NA_complex_
74102
'\\"x\\"'
75103
"\\\"x\\\""
76104

105+
#---- strings_quote_selection
106+
# formatter should choose quotes to minimize escaping
107+
"simple"
108+
'simple'
109+
"string with 'single quotes'"
110+
'string with "double quotes"'
111+
'string with \'escaped single quotes\''
112+
"string with \"escaped double quotes\""
113+
'mixed \'single\' and "double" quotes'
114+
77115
#---- strings_multiline
78116
"x
79117
x"
@@ -110,7 +148,15 @@ x.y
110148
- x
111149
~x
112150

113-
#---- coments_all_positions
151+
#---- tilde_spacing
152+
# tilde should have space except when followed by identifier
153+
~x
154+
~identifier
155+
~ x
156+
~ (x + y)
157+
~ {x}
158+
159+
#---- comments_all_positions
114160
- # 1
115161
rhs
116162

@@ -156,6 +202,13 @@ x %in% y
156202
x %>% y
157203
x |> y
158204

205+
#---- spacing_special_operators
206+
# colon and caret should not have spacing
207+
x:y
208+
x^y
209+
x : y
210+
x ^ y
211+
159212
#---- multiline
160213
x +
161214
y
@@ -177,7 +230,7 @@ x + {
177230
y
178231
}
179232

180-
#---- coments_all_positions
233+
#---- comments_all_positions
181234
lhs + # 1
182235
rhs # 2
183236

@@ -272,7 +325,7 @@ x; y
272325
{ x; y +
273326
z }
274327

275-
#---- coments_all_positions
328+
#---- comments_all_positions
276329
{ # {
277330
# 1
278331
body # body
@@ -454,7 +507,7 @@ lhs$
454507
)$
455508
fn(x,y)
456509

457-
#---- coments_all_positions
510+
#---- comments_all_positions
458511
x$ # 1
459512
y # 2
460513

@@ -537,7 +590,7 @@ in
537590
for (item in sequence) # 1
538591
body
539592

540-
#---- coments_all_positions
593+
#---- comments_all_positions
541594
for
542595
# 1
543596
( # (
@@ -756,7 +809,7 @@ if ({
756809
# 1
757810
) {}
758811

759-
#---- coments_all_positions
812+
#---- comments_all_positions
760813
# for some weird reason this is only parsed correctly if in a parentheses
761814
(
762815
# before
@@ -908,7 +961,7 @@ repeat # 1
908961
repeat # 1
909962
{ body }
910963

911-
#---- coments_all_positions
964+
#---- comments_all_positions
912965
repeat # repeat
913966
# 1
914967
{
@@ -945,7 +998,7 @@ x[, x, y + call(
945998
arg
946999
)]
9471000

948-
#---- coments_all_positions
1001+
#---- comments_all_positions
9491002
fn[ # [
9501003
# 1
9511004
argument # argument
@@ -983,7 +1036,7 @@ x[[, x, y + call(
9831036
arg
9841037
)]]
9851038

986-
#---- coments_all_positions
1039+
#---- comments_all_positions
9871040
fn[[ # [[
9881041
# 1
9891042
argument # argument
@@ -1054,7 +1107,7 @@ while ({
10541107
# 1
10551108
) {}
10561109

1057-
#---- coments_all_positions
1110+
#---- comments_all_positions
10581111
while # while
10591112
# 1
10601113
( # (
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: crates/roughly/tests/test_format.rs
3+
expression: code
4+
---
5+
lhs + # 1
6+
rhs # 2
7+
8+
(
9+
lhs # lhs
10+
# 1
11+
+ # operator
12+
# 2
13+
rhs # rhs
14+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: crates/roughly/tests/test_format.rs
3+
expression: code
4+
---
5+
# colon and caret should not have spacing
6+
x:y
7+
x^y
8+
x:y
9+
x^y
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: crates/roughly/tests/test_format.rs
3+
expression: code
4+
---
5+
{ # {
6+
# 1
7+
body # body
8+
# 2
9+
} # }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
source: crates/roughly/tests/test_format.rs
3+
expression: code
4+
---
5+
x$ # 1
6+
y # 2
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
source: crates/roughly/tests/test_format.rs
3+
expression: code
4+
---
5+
for
6+
# 1
7+
( # (
8+
# 2
9+
item # item
10+
# 3
11+
in # in
12+
# 4
13+
sequence # sequence
14+
# 5
15+
) # )
16+
# 6
17+
{
18+
body
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
source: crates/roughly/tests/test_format.rs
3+
expression: code
4+
---
5+
# for some weird reason this is only parsed correctly if in a parentheses
6+
(
7+
# before
8+
if # if
9+
# 1
10+
( # open
11+
# 2
12+
condition # condition
13+
# 3
14+
) # close
15+
# 4
16+
{
17+
y
18+
}
19+
# 5
20+
else # else
21+
# 6
22+
{
23+
4
24+
}
25+
# after
26+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: crates/roughly/tests/test_format.rs
3+
expression: code
4+
---
5+
1i
6+
1.5i
7+
1e2i
8+
1 + 2i
9+
1.5 + 2.5i
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
source: crates/roughly/tests/test_format.rs
3+
expression: code
4+
---
5+
return
6+
next
7+
break
8+
...
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: crates/roughly/tests/test_format.rs
3+
expression: code
4+
---
5+
# formatter should choose quotes to minimize escaping
6+
"simple"
7+
"simple"
8+
"string with 'single quotes'"
9+
'string with "double quotes"'
10+
"string with \'escaped single quotes\'"
11+
"string with \"escaped double quotes\""
12+
'mixed \'single\' and "double" quotes'

0 commit comments

Comments
 (0)