From 8742e77f3c5baa29014e0af0a4d7a5d6bed17019 Mon Sep 17 00:00:00 2001 From: The-Lum <86879521+The-Lum@users.noreply.github.com> Date: Wed, 26 Jul 2023 11:27:56 +0000 Subject: [PATCH] fix: `ord` builtin function: allow Unicode values, add test By this indication: - https://github.com/plantuml/plantuml/pull/1487#issuecomment-1648082049 Fix `ord` builtin function: - allow Unicode values - add tests --- .../sourceforge/plantuml/tim/stdlib/Ord.java | 5 +- .../plantuml/tim/stdlib/OrdTest.java | 49 +++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 test/net/sourceforge/plantuml/tim/stdlib/OrdTest.java diff --git a/src/net/sourceforge/plantuml/tim/stdlib/Ord.java b/src/net/sourceforge/plantuml/tim/stdlib/Ord.java index d45bf6d62..6e5262189 100644 --- a/src/net/sourceforge/plantuml/tim/stdlib/Ord.java +++ b/src/net/sourceforge/plantuml/tim/stdlib/Ord.java @@ -59,9 +59,8 @@ public class Ord extends SimpleReturnFunction { public TValue executeReturnFunction(TContext context, TMemory memory, LineLocation location, List values, Map named) throws EaterException, EaterExceptionLocated { try { - final char firstChar = values.get(0).toString().charAt(0); - final int value = (int) firstChar; - return TValue.fromInt(value); + final int codePoint = values.get(0).toString().codePointAt(0); + return TValue.fromInt(codePoint); } catch (Throwable t) { return TValue.fromInt(0); } diff --git a/test/net/sourceforge/plantuml/tim/stdlib/OrdTest.java b/test/net/sourceforge/plantuml/tim/stdlib/OrdTest.java new file mode 100644 index 000000000..3d4c2f203 --- /dev/null +++ b/test/net/sourceforge/plantuml/tim/stdlib/OrdTest.java @@ -0,0 +1,49 @@ +package net.sourceforge.plantuml.tim.stdlib; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Collections; +import java.util.List; + +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; + +/** + * Tests the builtin function %ord. + */ +class OrdTest { + + /** + * Tests ord according to a list of input / expected output + * + * @throws EaterException should not + * @throws EaterExceptionLocated should not + */ + @ParameterizedTest + @CsvSource(nullValues = "null", value = { + " A , 65 ", + " ABC , 65 ", + " 'A' , 65 ", + " '\t' , 9 ", + " ' ' , 32 ", + " '!' , 33 ", + " '\"' , 34 ", + " à , 224 ", + " 'à' , 224 ", + " é , 233 ", + " 'é' , 233 ", + " 😀 , 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()); + } +} \ No newline at end of file