From 6f3416fdc692d5e96c2969413e86390a5e41fcec Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 13 Jan 2025 11:56:22 +0100 Subject: [PATCH] Add TomlVisitorTest after seeing ClassCastException in arrays (#4892) * Add TomlVisitorTest after seeing ClassCastException in arrays * Have Toml.Literal implement TomlValue * Drop unnecessary cast instead --- .../org/openrewrite/toml/TomlVisitor.java | 2 +- rewrite-toml/src/test/java/.editorconfig | 5 + .../org/openrewrite/toml/TomlParserTest.java | 252 +++++++++--------- .../org/openrewrite/toml/TomlVisitorTest.java | 67 +++++ 4 files changed, 199 insertions(+), 127 deletions(-) create mode 100644 rewrite-toml/src/test/java/.editorconfig create mode 100644 rewrite-toml/src/test/java/org/openrewrite/toml/TomlVisitorTest.java diff --git a/rewrite-toml/src/main/java/org/openrewrite/toml/TomlVisitor.java b/rewrite-toml/src/main/java/org/openrewrite/toml/TomlVisitor.java index b3707132e10..940d46db59c 100644 --- a/rewrite-toml/src/main/java/org/openrewrite/toml/TomlVisitor.java +++ b/rewrite-toml/src/main/java/org/openrewrite/toml/TomlVisitor.java @@ -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; } diff --git a/rewrite-toml/src/test/java/.editorconfig b/rewrite-toml/src/test/java/.editorconfig new file mode 100644 index 00000000000..a4824935e4f --- /dev/null +++ b/rewrite-toml/src/test/java/.editorconfig @@ -0,0 +1,5 @@ +root = true + +[*.java] +indent_size = 4 +ij_continuation_indent_size = 2 diff --git a/rewrite-toml/src/test/java/org/openrewrite/toml/TomlParserTest.java b/rewrite-toml/src/test/java/org/openrewrite/toml/TomlParserTest.java index fefa837ee9b..2693151c649 100644 --- a/rewrite-toml/src/test/java/org/openrewrite/toml/TomlParserTest.java +++ b/rewrite-toml/src/test/java/org/openrewrite/toml/TomlParserTest.java @@ -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." + """ ) ); } @@ -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 + """ ) ); } @@ -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 + """ ) ); } @@ -97,9 +97,9 @@ void keyValueBool() { rewriteRun( toml( """ - bool1 = true - bool2 = false - """ + bool1 = true + bool2 = false + """ ) ); } @@ -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 + """ ) ); } @@ -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 + """ ) ); } @@ -135,8 +135,8 @@ void keyValueLocalDate() { rewriteRun( toml( """ - ld1 = 1979-05-27 - """ + ld1 = 1979-05-27 + """ ) ); } @@ -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 + """ ) ); } @@ -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 ", - { name = "Baz Qux", email = "bazqux@example.com", 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 ", + { name = "Baz Qux", email = "bazqux@example.com", url = "https://example.com/bazqux" } + ] + integers2 = [ + 1, 2, 3 + ] + + integers3 = [ + 1, + 2, # this is ok + ] + """ ) ); } @@ -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" + """ ) ); } @@ -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" + """ ) ); } @@ -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" + """ ) ); } @@ -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" + """ ) ); } @@ -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 + """ ) ); } @@ -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 + """ ) ); } @@ -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 + """ ) ); } diff --git a/rewrite-toml/src/test/java/org/openrewrite/toml/TomlVisitorTest.java b/rewrite-toml/src/test/java/org/openrewrite/toml/TomlVisitorTest.java new file mode 100644 index 00000000000..f7698844a20 --- /dev/null +++ b/rewrite-toml/src/test/java/org/openrewrite/toml/TomlVisitorTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2025 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.toml; + +import org.junit.jupiter.api.Test; +import org.openrewrite.*; +import org.openrewrite.marker.Markup; +import org.openrewrite.test.RewriteTest; + +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.openrewrite.test.RewriteTest.toRecipe; +import static org.openrewrite.toml.Assertions.toml; + +class TomlVisitorTest implements RewriteTest { + + @Issue("https://github.com/openrewrite/rewrite-spring/issues/665") + @Test + void visitMarkupErrorMarkers() { + List exceptions = new ArrayList<>(); + rewriteRun( + spec -> spec.recipe(toRecipe(() -> new TreeVisitor<>() { + @Override + public Tree preVisit(Tree tree, ExecutionContext ctx) { + // Mimics what we do in the rewrite-gradle-plugin + tree.getMarkers().findFirst(Markup.Error.class).ifPresent(e -> { + Optional sourceFile = Optional.ofNullable(getCursor().firstEnclosing(SourceFile.class)); + String sourcePath = sourceFile.map(SourceFile::getSourcePath).map(Path::toString).orElse(""); + exceptions.add(new RuntimeException("Error while visiting " + sourcePath + ": " + e.getDetail())); + }); + return tree; + } + })), + toml( + """ + [versions] + jackson = '2.14.2' + + [libraries] + jackson-annotations = { module = 'com.fasterxml.jackson.core:jackson-annotations', version.ref = 'jackson' } + jackson-core = { module = 'com.fasterxml.jackson.core:jackson-core', version.ref = 'jackson' } + + [bundles] + jackson = ['jackson-annotations', 'jackson-core'] + """ + ) + ); + assertThat(exceptions).isEmpty(); + } +}