1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-12-22 02:49:06 +00:00

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
This commit is contained in:
The-Lum 2023-07-26 11:27:56 +00:00
parent a327d636a7
commit 8742e77f3c
2 changed files with 51 additions and 3 deletions

View File

@ -59,9 +59,8 @@ public class Ord extends SimpleReturnFunction {
public TValue executeReturnFunction(TContext context, TMemory memory, LineLocation location, List<TValue> values, public TValue executeReturnFunction(TContext context, TMemory memory, LineLocation location, List<TValue> values,
Map<String, TValue> named) throws EaterException, EaterExceptionLocated { Map<String, TValue> named) throws EaterException, EaterExceptionLocated {
try { try {
final char firstChar = values.get(0).toString().charAt(0); final int codePoint = values.get(0).toString().codePointAt(0);
final int value = (int) firstChar; return TValue.fromInt(codePoint);
return TValue.fromInt(value);
} catch (Throwable t) { } catch (Throwable t) {
return TValue.fromInt(0); return TValue.fromInt(0);
} }

View File

@ -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<TValue> values = Collections.singletonList(TValue.fromString(input));
TValue tValue = cut.executeReturnFunction(null, null, null, values, null);
assertEquals(expected, tValue.toString());
}
}