From 1cb61d56099d3fddd5f0ea33812160fb939b576d Mon Sep 17 00:00:00 2001 From: The-Lum <86879521+The-Lum@users.noreply.github.com> Date: Fri, 8 Dec 2023 21:08:36 +0000 Subject: [PATCH 1/3] fix: allow different type inside array for `GetJsonKey` builtin fct --- src/net/sourceforge/plantuml/tim/stdlib/GetJsonKey.java | 8 +++++--- .../sourceforge/plantuml/tim/stdlib/GetJsonKeyTest.java | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/net/sourceforge/plantuml/tim/stdlib/GetJsonKey.java b/src/net/sourceforge/plantuml/tim/stdlib/GetJsonKey.java index 77824aabc..4df4bf5b5 100644 --- a/src/net/sourceforge/plantuml/tim/stdlib/GetJsonKey.java +++ b/src/net/sourceforge/plantuml/tim/stdlib/GetJsonKey.java @@ -76,9 +76,11 @@ public class GetJsonKey extends SimpleReturnFunction { final JsonArray array = (JsonArray) json; final JsonArray result = new JsonArray(); for (JsonValue tmp : array) { - final JsonObject object = (JsonObject) tmp; - for (String key : object.names()) - result.add(key); + if (tmp.isObject()) { + final JsonObject object = (JsonObject) tmp; + for (String key : object.names()) + result.add(key); + } } return TValue.fromJson(result); } diff --git a/test/net/sourceforge/plantuml/tim/stdlib/GetJsonKeyTest.java b/test/net/sourceforge/plantuml/tim/stdlib/GetJsonKeyTest.java index c9985befd..c84f32bd8 100644 --- a/test/net/sourceforge/plantuml/tim/stdlib/GetJsonKeyTest.java +++ b/test/net/sourceforge/plantuml/tim/stdlib/GetJsonKeyTest.java @@ -61,11 +61,11 @@ class GetJsonKeyTest { " '{\"a\":\"abc\"}' , [\"a\"]", " '[{\"a\":[1, 2]}, {\"b\":[3, 4]}]' , '[\"a\",\"b\"]'", " '{\"a\":[1, 2], \"b\":\"abc\", \"b\":true}' , '[\"a\",\"b\",\"b\"]'", - // TODO: Manage Array with different type inside + // DONE: Manage Array with different type inside // Ref.: // - https://datatracker.ietf.org/doc/html/rfc8259#section-5 // - https://json-schema.org/understanding-json-schema/reference/array.html - //" '[3, \"different\", { \"types\" : \"of values\" }]', [\"types\"]", + " '[3, \"different\", { \"types\" : \"of values\" }]', [\"types\"]", }) void Test_with_Json(@ConvertWith(StringJsonConverter.class) JsonValue input, String expected) throws EaterException, EaterExceptionLocated { assertTimExpectedOutputFromInput(cut, input, expected); From 3f836a44ccc507713b4cfcc87149e5853e02185a Mon Sep 17 00:00:00 2001 From: The-Lum <86879521+The-Lum@users.noreply.github.com> Date: Fri, 8 Dec 2023 21:20:22 +0000 Subject: [PATCH 2/3] fix: allow (`int` corresponding of) unicode value for `Chr` builtin fct That fixes partialy #1571. --- src/net/sourceforge/plantuml/tim/stdlib/Chr.java | 4 ++-- test/net/sourceforge/plantuml/tim/stdlib/ChrTest.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/net/sourceforge/plantuml/tim/stdlib/Chr.java b/src/net/sourceforge/plantuml/tim/stdlib/Chr.java index 622230500..7fdc13f2b 100644 --- a/src/net/sourceforge/plantuml/tim/stdlib/Chr.java +++ b/src/net/sourceforge/plantuml/tim/stdlib/Chr.java @@ -59,8 +59,8 @@ public class Chr extends SimpleReturnFunction { public TValue executeReturnFunction(TContext context, TMemory memory, LineLocation location, List values, Map named) throws EaterException, EaterExceptionLocated { try { - final char value = (char) values.get(0).toInt(); - return TValue.fromString("" + value); + final String value = String.valueOf(Character.toChars(values.get(0).toInt())); + return TValue.fromString(value); } catch (Throwable t) { return TValue.fromString("\0"); } diff --git a/test/net/sourceforge/plantuml/tim/stdlib/ChrTest.java b/test/net/sourceforge/plantuml/tim/stdlib/ChrTest.java index cf3886aaa..97eb1ece3 100644 --- a/test/net/sourceforge/plantuml/tim/stdlib/ChrTest.java +++ b/test/net/sourceforge/plantuml/tim/stdlib/ChrTest.java @@ -32,9 +32,9 @@ class ChrTest { " 34 , '\"' ", " 224 , à ", " 233 , é ", -// TODO: fix `%chr` to allow Unicode chars, the corresponding tests are here: -// " 128512 , 😀 ", -// " 128512 , \uD83D\uDE00 ", +// DONE: fix `%chr` to allow Unicode chars, the corresponding tests are here: + " 128512 , 😀 ", + " 128512 , \uD83D\uDE00 ", }) void executeReturnFunctionChrTest(Integer input, String expected) throws EaterException, EaterExceptionLocated { Chr cut = new Chr(); From 5c7245623a1c8cd700b7aaa30bbc44399a2307d3 Mon Sep 17 00:00:00 2001 From: The-Lum <86879521+The-Lum@users.noreply.github.com> Date: Fri, 8 Dec 2023 21:44:05 +0000 Subject: [PATCH 3/3] test refactor: simplify test using `TimTestUtils` For: `Chr` and `Ord` --- .../plantuml/tim/stdlib/ChrTest.java | 24 +++++++++---------- .../plantuml/tim/stdlib/OrdTest.java | 24 +++++++++---------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/test/net/sourceforge/plantuml/tim/stdlib/ChrTest.java b/test/net/sourceforge/plantuml/tim/stdlib/ChrTest.java index 97eb1ece3..6659ecd95 100644 --- a/test/net/sourceforge/plantuml/tim/stdlib/ChrTest.java +++ b/test/net/sourceforge/plantuml/tim/stdlib/ChrTest.java @@ -1,29 +1,31 @@ package net.sourceforge.plantuml.tim.stdlib; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.Collections; -import java.util.List; +import static net.sourceforge.plantuml.tim.TimTestUtils.assertTimExpectedOutputFromInput; +import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; +import org.junit.jupiter.api.IndicativeSentencesGeneration; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import net.sourceforge.plantuml.tim.EaterException; import net.sourceforge.plantuml.tim.EaterExceptionLocated; -import net.sourceforge.plantuml.tim.expression.TValue; +import net.sourceforge.plantuml.tim.TFunction; /** * Tests the builtin function %chr. */ -class ChrTest { +@IndicativeSentencesGeneration(separator = ": ", generator = ReplaceUnderscores.class) +class ChrTest { + TFunction cut = new Chr(); + final String cutName = "Chr"; /** * Tests chr according to a list of input / expected output * * @throws EaterException should not * @throws EaterExceptionLocated should not */ - @ParameterizedTest + @ParameterizedTest(name = "[{index}] " + cutName + "({0}) = ''{1}''") @CsvSource(nullValues = "null", value = { " 65 , A ", " 9 , '\t' ", @@ -36,11 +38,7 @@ class ChrTest { " 128512 , 😀 ", " 128512 , \uD83D\uDE00 ", }) - void executeReturnFunctionChrTest(Integer input, String expected) throws EaterException, EaterExceptionLocated { - Chr cut = new Chr(); - - List values = Collections.singletonList(TValue.fromInt(input)); - TValue tValue = cut.executeReturnFunction(null, null, null, values, null); - assertEquals(expected, tValue.toString()); + void Test_with_Integer(Integer input, String expected) throws EaterException, EaterExceptionLocated { + assertTimExpectedOutputFromInput(cut, input, expected); } } \ No newline at end of file diff --git a/test/net/sourceforge/plantuml/tim/stdlib/OrdTest.java b/test/net/sourceforge/plantuml/tim/stdlib/OrdTest.java index 3d4c2f203..4fd0411ab 100644 --- a/test/net/sourceforge/plantuml/tim/stdlib/OrdTest.java +++ b/test/net/sourceforge/plantuml/tim/stdlib/OrdTest.java @@ -1,29 +1,31 @@ package net.sourceforge.plantuml.tim.stdlib; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.Collections; -import java.util.List; +import static net.sourceforge.plantuml.tim.TimTestUtils.assertTimExpectedOutputFromInput; +import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; +import org.junit.jupiter.api.IndicativeSentencesGeneration; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import net.sourceforge.plantuml.tim.EaterException; import net.sourceforge.plantuml.tim.EaterExceptionLocated; -import net.sourceforge.plantuml.tim.expression.TValue; +import net.sourceforge.plantuml.tim.TFunction; /** * Tests the builtin function %ord. */ -class OrdTest { +@IndicativeSentencesGeneration(separator = ": ", generator = ReplaceUnderscores.class) +class OrdTest { + TFunction cut = new Ord(); + final String cutName = "Ord"; /** * Tests ord according to a list of input / expected output * * @throws EaterException should not * @throws EaterExceptionLocated should not */ - @ParameterizedTest + @ParameterizedTest(name = "[{index}] " + cutName + "(''{0}'') = {1}") @CsvSource(nullValues = "null", value = { " A , 65 ", " ABC , 65 ", @@ -39,11 +41,7 @@ class OrdTest { " 😀 , 128512 ", " \uD83D\uDE00 , 128512 ", }) - void executeReturnFunctionOrdTest(String input, String expected) throws EaterException, EaterExceptionLocated { - Ord cut = new Ord(); - - List values = Collections.singletonList(TValue.fromString(input)); - TValue tValue = cut.executeReturnFunction(null, null, null, values, null); - assertEquals(expected, tValue.toString()); + void Test_with_String(String input, String expected) throws EaterException, EaterExceptionLocated { + assertTimExpectedOutputFromInput(cut, input, expected); } } \ No newline at end of file