Skip to content

Commit

Permalink
Add TomlVisitorTest after seeing ClassCastException in arrays (#4892)
Browse files Browse the repository at this point in the history
* Add TomlVisitorTest after seeing ClassCastException in arrays

* Have Toml.Literal implement TomlValue

* Drop unnecessary cast instead
  • Loading branch information
timtebeek authored Jan 13, 2025
1 parent 6a818cf commit 6f3416f
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Toml visitArray(Toml.Array array, P p) {
Toml.Array a = array;
a = a.withPrefix(visitSpace(a.getPrefix(), p));
a = a.withMarkers(visitMarkers(a.getMarkers(), p));
a = a.withValues(ListUtils.map(a.getValues(), v -> (TomlValue) visit(v, p)));
a = a.withValues(ListUtils.map(a.getValues(), v -> visit(v, p)));
return a;
}

Expand Down
5 changes: 5 additions & 0 deletions rewrite-toml/src/test/java/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.java]
indent_size = 4
ij_continuation_indent_size = 2
252 changes: 126 additions & 126 deletions rewrite-toml/src/test/java/org/openrewrite/toml/TomlParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void keyValueString() {
rewriteRun(
toml(
"""
str = "I'm a string. \\"You can quote me\\". Name\\tJos\\u00E9\\nLocation\\tSF."
"""
str = "I'm a string. \\"You can quote me\\". Name\\tJos\\u00E9\\nLocation\\tSF."
"""
)
);
}
Expand All @@ -37,23 +37,23 @@ void keyValueInteger() {
rewriteRun(
toml(
"""
int1 = +99
int2 = 42
int3 = 0
int4 = -17
int5 = 1_000
# hexadecimal with prefix `0x`
hex1 = 0xDEADBEEF
hex2 = 0xdeadbeef
hex3 = 0xdead_beef
# octal with prefix `0o`
oct1 = 0o01234567
oct2 = 0o755 # useful for Unix file permissions
# binary with prefix `0b`
bin1 = 0b11010110
"""
int1 = +99
int2 = 42
int3 = 0
int4 = -17
int5 = 1_000
# hexadecimal with prefix `0x`
hex1 = 0xDEADBEEF
hex2 = 0xdeadbeef
hex3 = 0xdead_beef
# octal with prefix `0o`
oct1 = 0o01234567
oct2 = 0o755 # useful for Unix file permissions
# binary with prefix `0b`
bin1 = 0b11010110
"""
)
);
}
Expand All @@ -63,31 +63,31 @@ void keyValueFloat() {
rewriteRun(
toml(
"""
# fractional
flt1 = +1.0
flt2 = 3.1415
flt3 = -0.01
# exponent
flt4 = 5e+22
flt5 = 1e06
flt6 = -2E-2
# both
flt7 = 6.626e-34
flt8 = 224_617.445_991_228
# infinity
sf1 = inf # positive infinity
sf2 = +inf # positive infinity
sf3 = -inf # negative infinity
# not a number
sf4 = nan # actual sNaN/qNaN encoding is implementation-specific
sf5 = +nan # same as `nan`
sf6 = -nan # valid, actual encoding is implementation-specific
"""
# fractional
flt1 = +1.0
flt2 = 3.1415
flt3 = -0.01
# exponent
flt4 = 5e+22
flt5 = 1e06
flt6 = -2E-2
# both
flt7 = 6.626e-34
flt8 = 224_617.445_991_228
# infinity
sf1 = inf # positive infinity
sf2 = +inf # positive infinity
sf3 = -inf # negative infinity
# not a number
sf4 = nan # actual sNaN/qNaN encoding is implementation-specific
sf5 = +nan # same as `nan`
sf6 = -nan # valid, actual encoding is implementation-specific
"""
)
);
}
Expand All @@ -97,9 +97,9 @@ void keyValueBool() {
rewriteRun(
toml(
"""
bool1 = true
bool2 = false
"""
bool1 = true
bool2 = false
"""
)
);
}
Expand All @@ -109,11 +109,11 @@ void keyValueOffsetDateTime() {
rewriteRun(
toml(
"""
odt1 = 1979-05-27T07:32:00Z
odt2 = 1979-05-27T00:32:00-07:00
odt3 = 1979-05-27T00:32:00.999999-07:00
odt4 = 1979-05-27 07:32:00Z
"""
odt1 = 1979-05-27T07:32:00Z
odt2 = 1979-05-27T00:32:00-07:00
odt3 = 1979-05-27T00:32:00.999999-07:00
odt4 = 1979-05-27 07:32:00Z
"""
)
);
}
Expand All @@ -123,9 +123,9 @@ void keyValueLocalDateTime() {
rewriteRun(
toml(
"""
ldt1 = 1979-05-27T07:32:00
ldt2 = 1979-05-27T00:32:00.999999
"""
ldt1 = 1979-05-27T07:32:00
ldt2 = 1979-05-27T00:32:00.999999
"""
)
);
}
Expand All @@ -135,8 +135,8 @@ void keyValueLocalDate() {
rewriteRun(
toml(
"""
ld1 = 1979-05-27
"""
ld1 = 1979-05-27
"""
)
);
}
Expand All @@ -146,9 +146,9 @@ void keyValueLocalTime() {
rewriteRun(
toml(
"""
lt1 = 07:32:00
lt2 = 00:32:00.999999
"""
lt1 = 07:32:00
lt2 = 00:32:00.999999
"""
)
);
}
Expand All @@ -158,27 +158,27 @@ void keyValueArray() {
rewriteRun(
toml(
"""
integers = [ 1, 2, 3 ]
colors = [ "red", "yellow", "green" ]
nested_arrays_of_ints = [ [ 1, 2 ], [3, 4, 5] ]
nested_mixed_array = [ [ 1, 2 ], ["a", "b", "c"] ]
string_array = [ "all", 'strings', ""\"are the same""\", '''type''' ]
# Mixed-type arrays are allowed
numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ]
contributors = [
"Foo Bar <[email protected]>",
{ name = "Baz Qux", email = "[email protected]", url = "https://example.com/bazqux" }
]
integers2 = [
1, 2, 3
]
integers3 = [
1,
2, # this is ok
]
"""
integers = [ 1, 2, 3 ]
colors = [ "red", "yellow", "green" ]
nested_arrays_of_ints = [ [ 1, 2 ], [3, 4, 5] ]
nested_mixed_array = [ [ 1, 2 ], ["a", "b", "c"] ]
string_array = [ "all", 'strings', ""\"are the same""\", '''type''' ]
# Mixed-type arrays are allowed
numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ]
contributors = [
"Foo Bar <[email protected]>",
{ name = "Baz Qux", email = "[email protected]", url = "https://example.com/bazqux" }
]
integers2 = [
1, 2, 3
]
integers3 = [
1,
2, # this is ok
]
"""
)
);
}
Expand All @@ -188,17 +188,17 @@ void table() {
rewriteRun(
toml(
"""
[table-1]
key1 = "some string"
key2 = 123
[table-2]
key1 = "another string"
key2 = 456
[dog."tater.man"]
type.name = "pug"
"""
[table-1]
key1 = "some string"
key2 = 123
[table-2]
key1 = "another string"
key2 = 456
[dog."tater.man"]
type.name = "pug"
"""
)
);
}
Expand All @@ -208,18 +208,18 @@ void arrayTable() {
rewriteRun(
toml(
"""
[[products]]
name = "Hammer"
sku = 738594937
[[products]] # empty table within the array
[[products]]
name = "Nail"
sku = 284758393
color = "gray"
"""
[[products]]
name = "Hammer"
sku = 738594937
[[products]] # empty table within the array
[[products]]
name = "Nail"
sku = 284758393
color = "gray"
"""
)
);
}
Expand All @@ -229,11 +229,11 @@ void bareKeys() {
rewriteRun(
toml(
"""
key = "value"
bare_key = "value"
bare-key = "value"
1234 = "value"
"""
key = "value"
bare_key = "value"
bare-key = "value"
1234 = "value"
"""
)
);
}
Expand All @@ -243,12 +243,12 @@ void quotedKeys() {
rewriteRun(
toml(
"""
"127.0.0.1" = "value"
"character encoding" = "value"
"ʎǝʞ" = "value"
'key2' = "value"
'quoted "value"' = "value"
"""
"127.0.0.1" = "value"
"character encoding" = "value"
"ʎǝʞ" = "value"
'key2' = "value"
'quoted "value"' = "value"
"""
)
);
}
Expand All @@ -258,10 +258,10 @@ void dottedKeys() {
rewriteRun(
toml(
"""
physical.color = "orange"
physical.shape = "round"
site."google.com" = true
"""
physical.color = "orange"
physical.shape = "round"
site."google.com" = true
"""
)
);
}
Expand All @@ -271,10 +271,10 @@ void extraWhitespaceDottedKeys() {
rewriteRun(
toml(
"""
fruit.name = "banana" # this is best practice
fruit. color = "yellow" # same as fruit.color
fruit . flavor = "banana" # same as fruit.flavor
"""
fruit.name = "banana" # this is best practice
fruit. color = "yellow" # same as fruit.color
fruit . flavor = "banana" # same as fruit.flavor
"""
)
);
}
Expand Down Expand Up @@ -321,9 +321,9 @@ void trailingComment() {
rewriteRun(
toml(
"""
str = "I'm a string. \\"You can quote me\\". Name\\tJos\\u00E9\\nLocation\\tSF."
# trailing comment
"""
str = "I'm a string. \\"You can quote me\\". Name\\tJos\\u00E9\\nLocation\\tSF."
# trailing comment
"""
)
);
}
Expand Down
Loading

0 comments on commit 6f3416f

Please sign in to comment.