Skip to content

Commit 6f3416f

Browse files
authored
Add TomlVisitorTest after seeing ClassCastException in arrays (#4892)
* Add TomlVisitorTest after seeing ClassCastException in arrays * Have Toml.Literal implement TomlValue * Drop unnecessary cast instead
1 parent 6a818cf commit 6f3416f

File tree

4 files changed

+199
-127
lines changed

4 files changed

+199
-127
lines changed

rewrite-toml/src/main/java/org/openrewrite/toml/TomlVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public Toml visitArray(Toml.Array array, P p) {
3838
Toml.Array a = array;
3939
a = a.withPrefix(visitSpace(a.getPrefix(), p));
4040
a = a.withMarkers(visitMarkers(a.getMarkers(), p));
41-
a = a.withValues(ListUtils.map(a.getValues(), v -> (TomlValue) visit(v, p)));
41+
a = a.withValues(ListUtils.map(a.getValues(), v -> visit(v, p)));
4242
return a;
4343
}
4444

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
root = true
2+
3+
[*.java]
4+
indent_size = 4
5+
ij_continuation_indent_size = 2

rewrite-toml/src/test/java/org/openrewrite/toml/TomlParserTest.java

Lines changed: 126 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ void keyValueString() {
2626
rewriteRun(
2727
toml(
2828
"""
29-
str = "I'm a string. \\"You can quote me\\". Name\\tJos\\u00E9\\nLocation\\tSF."
30-
"""
29+
str = "I'm a string. \\"You can quote me\\". Name\\tJos\\u00E9\\nLocation\\tSF."
30+
"""
3131
)
3232
);
3333
}
@@ -37,23 +37,23 @@ void keyValueInteger() {
3737
rewriteRun(
3838
toml(
3939
"""
40-
int1 = +99
41-
int2 = 42
42-
int3 = 0
43-
int4 = -17
44-
int5 = 1_000
45-
46-
# hexadecimal with prefix `0x`
47-
hex1 = 0xDEADBEEF
48-
hex2 = 0xdeadbeef
49-
hex3 = 0xdead_beef
50-
# octal with prefix `0o`
51-
oct1 = 0o01234567
52-
oct2 = 0o755 # useful for Unix file permissions
53-
54-
# binary with prefix `0b`
55-
bin1 = 0b11010110
56-
"""
40+
int1 = +99
41+
int2 = 42
42+
int3 = 0
43+
int4 = -17
44+
int5 = 1_000
45+
46+
# hexadecimal with prefix `0x`
47+
hex1 = 0xDEADBEEF
48+
hex2 = 0xdeadbeef
49+
hex3 = 0xdead_beef
50+
# octal with prefix `0o`
51+
oct1 = 0o01234567
52+
oct2 = 0o755 # useful for Unix file permissions
53+
54+
# binary with prefix `0b`
55+
bin1 = 0b11010110
56+
"""
5757
)
5858
);
5959
}
@@ -63,31 +63,31 @@ void keyValueFloat() {
6363
rewriteRun(
6464
toml(
6565
"""
66-
# fractional
67-
flt1 = +1.0
68-
flt2 = 3.1415
69-
flt3 = -0.01
70-
71-
# exponent
72-
flt4 = 5e+22
73-
flt5 = 1e06
74-
flt6 = -2E-2
75-
76-
# both
77-
flt7 = 6.626e-34
78-
79-
flt8 = 224_617.445_991_228
80-
81-
# infinity
82-
sf1 = inf # positive infinity
83-
sf2 = +inf # positive infinity
84-
sf3 = -inf # negative infinity
85-
86-
# not a number
87-
sf4 = nan # actual sNaN/qNaN encoding is implementation-specific
88-
sf5 = +nan # same as `nan`
89-
sf6 = -nan # valid, actual encoding is implementation-specific
90-
"""
66+
# fractional
67+
flt1 = +1.0
68+
flt2 = 3.1415
69+
flt3 = -0.01
70+
71+
# exponent
72+
flt4 = 5e+22
73+
flt5 = 1e06
74+
flt6 = -2E-2
75+
76+
# both
77+
flt7 = 6.626e-34
78+
79+
flt8 = 224_617.445_991_228
80+
81+
# infinity
82+
sf1 = inf # positive infinity
83+
sf2 = +inf # positive infinity
84+
sf3 = -inf # negative infinity
85+
86+
# not a number
87+
sf4 = nan # actual sNaN/qNaN encoding is implementation-specific
88+
sf5 = +nan # same as `nan`
89+
sf6 = -nan # valid, actual encoding is implementation-specific
90+
"""
9191
)
9292
);
9393
}
@@ -97,9 +97,9 @@ void keyValueBool() {
9797
rewriteRun(
9898
toml(
9999
"""
100-
bool1 = true
101-
bool2 = false
102-
"""
100+
bool1 = true
101+
bool2 = false
102+
"""
103103
)
104104
);
105105
}
@@ -109,11 +109,11 @@ void keyValueOffsetDateTime() {
109109
rewriteRun(
110110
toml(
111111
"""
112-
odt1 = 1979-05-27T07:32:00Z
113-
odt2 = 1979-05-27T00:32:00-07:00
114-
odt3 = 1979-05-27T00:32:00.999999-07:00
115-
odt4 = 1979-05-27 07:32:00Z
116-
"""
112+
odt1 = 1979-05-27T07:32:00Z
113+
odt2 = 1979-05-27T00:32:00-07:00
114+
odt3 = 1979-05-27T00:32:00.999999-07:00
115+
odt4 = 1979-05-27 07:32:00Z
116+
"""
117117
)
118118
);
119119
}
@@ -123,9 +123,9 @@ void keyValueLocalDateTime() {
123123
rewriteRun(
124124
toml(
125125
"""
126-
ldt1 = 1979-05-27T07:32:00
127-
ldt2 = 1979-05-27T00:32:00.999999
128-
"""
126+
ldt1 = 1979-05-27T07:32:00
127+
ldt2 = 1979-05-27T00:32:00.999999
128+
"""
129129
)
130130
);
131131
}
@@ -135,8 +135,8 @@ void keyValueLocalDate() {
135135
rewriteRun(
136136
toml(
137137
"""
138-
ld1 = 1979-05-27
139-
"""
138+
ld1 = 1979-05-27
139+
"""
140140
)
141141
);
142142
}
@@ -146,9 +146,9 @@ void keyValueLocalTime() {
146146
rewriteRun(
147147
toml(
148148
"""
149-
lt1 = 07:32:00
150-
lt2 = 00:32:00.999999
151-
"""
149+
lt1 = 07:32:00
150+
lt2 = 00:32:00.999999
151+
"""
152152
)
153153
);
154154
}
@@ -158,27 +158,27 @@ void keyValueArray() {
158158
rewriteRun(
159159
toml(
160160
"""
161-
integers = [ 1, 2, 3 ]
162-
colors = [ "red", "yellow", "green" ]
163-
nested_arrays_of_ints = [ [ 1, 2 ], [3, 4, 5] ]
164-
nested_mixed_array = [ [ 1, 2 ], ["a", "b", "c"] ]
165-
string_array = [ "all", 'strings', ""\"are the same""\", '''type''' ]
166-
167-
# Mixed-type arrays are allowed
168-
numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ]
169-
contributors = [
170-
"Foo Bar <[email protected]>",
171-
{ name = "Baz Qux", email = "[email protected]", url = "https://example.com/bazqux" }
172-
]
173-
integers2 = [
174-
1, 2, 3
175-
]
176-
177-
integers3 = [
178-
1,
179-
2, # this is ok
180-
]
181-
"""
161+
integers = [ 1, 2, 3 ]
162+
colors = [ "red", "yellow", "green" ]
163+
nested_arrays_of_ints = [ [ 1, 2 ], [3, 4, 5] ]
164+
nested_mixed_array = [ [ 1, 2 ], ["a", "b", "c"] ]
165+
string_array = [ "all", 'strings', ""\"are the same""\", '''type''' ]
166+
167+
# Mixed-type arrays are allowed
168+
numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ]
169+
contributors = [
170+
"Foo Bar <[email protected]>",
171+
{ name = "Baz Qux", email = "[email protected]", url = "https://example.com/bazqux" }
172+
]
173+
integers2 = [
174+
1, 2, 3
175+
]
176+
177+
integers3 = [
178+
1,
179+
2, # this is ok
180+
]
181+
"""
182182
)
183183
);
184184
}
@@ -188,17 +188,17 @@ void table() {
188188
rewriteRun(
189189
toml(
190190
"""
191-
[table-1]
192-
key1 = "some string"
193-
key2 = 123
194-
195-
[table-2]
196-
key1 = "another string"
197-
key2 = 456
198-
199-
[dog."tater.man"]
200-
type.name = "pug"
201-
"""
191+
[table-1]
192+
key1 = "some string"
193+
key2 = 123
194+
195+
[table-2]
196+
key1 = "another string"
197+
key2 = 456
198+
199+
[dog."tater.man"]
200+
type.name = "pug"
201+
"""
202202
)
203203
);
204204
}
@@ -208,18 +208,18 @@ void arrayTable() {
208208
rewriteRun(
209209
toml(
210210
"""
211-
[[products]]
212-
name = "Hammer"
213-
sku = 738594937
214-
215-
[[products]] # empty table within the array
216-
217-
[[products]]
218-
name = "Nail"
219-
sku = 284758393
220-
221-
color = "gray"
222-
"""
211+
[[products]]
212+
name = "Hammer"
213+
sku = 738594937
214+
215+
[[products]] # empty table within the array
216+
217+
[[products]]
218+
name = "Nail"
219+
sku = 284758393
220+
221+
color = "gray"
222+
"""
223223
)
224224
);
225225
}
@@ -229,11 +229,11 @@ void bareKeys() {
229229
rewriteRun(
230230
toml(
231231
"""
232-
key = "value"
233-
bare_key = "value"
234-
bare-key = "value"
235-
1234 = "value"
236-
"""
232+
key = "value"
233+
bare_key = "value"
234+
bare-key = "value"
235+
1234 = "value"
236+
"""
237237
)
238238
);
239239
}
@@ -243,12 +243,12 @@ void quotedKeys() {
243243
rewriteRun(
244244
toml(
245245
"""
246-
"127.0.0.1" = "value"
247-
"character encoding" = "value"
248-
"ʎǝʞ" = "value"
249-
'key2' = "value"
250-
'quoted "value"' = "value"
251-
"""
246+
"127.0.0.1" = "value"
247+
"character encoding" = "value"
248+
"ʎǝʞ" = "value"
249+
'key2' = "value"
250+
'quoted "value"' = "value"
251+
"""
252252
)
253253
);
254254
}
@@ -258,10 +258,10 @@ void dottedKeys() {
258258
rewriteRun(
259259
toml(
260260
"""
261-
physical.color = "orange"
262-
physical.shape = "round"
263-
site."google.com" = true
264-
"""
261+
physical.color = "orange"
262+
physical.shape = "round"
263+
site."google.com" = true
264+
"""
265265
)
266266
);
267267
}
@@ -271,10 +271,10 @@ void extraWhitespaceDottedKeys() {
271271
rewriteRun(
272272
toml(
273273
"""
274-
fruit.name = "banana" # this is best practice
275-
fruit. color = "yellow" # same as fruit.color
276-
fruit . flavor = "banana" # same as fruit.flavor
277-
"""
274+
fruit.name = "banana" # this is best practice
275+
fruit. color = "yellow" # same as fruit.color
276+
fruit . flavor = "banana" # same as fruit.flavor
277+
"""
278278
)
279279
);
280280
}
@@ -321,9 +321,9 @@ void trailingComment() {
321321
rewriteRun(
322322
toml(
323323
"""
324-
str = "I'm a string. \\"You can quote me\\". Name\\tJos\\u00E9\\nLocation\\tSF."
325-
# trailing comment
326-
"""
324+
str = "I'm a string. \\"You can quote me\\". Name\\tJos\\u00E9\\nLocation\\tSF."
325+
# trailing comment
326+
"""
327327
)
328328
);
329329
}

0 commit comments

Comments
 (0)