mirror of
https://github.com/octoleo/plantuml.git
synced 2024-11-05 21:17:52 +00:00
Merge pull request #1497 from The-Lum/BuiltinOrd
fix: `ord` builtin function: allow Unicode values, add test
This commit is contained in:
commit
44ba32d053
@ -59,9 +59,8 @@ public class Ord extends SimpleReturnFunction {
|
||||
public TValue executeReturnFunction(TContext context, TMemory memory, LineLocation location, List<TValue> values,
|
||||
Map<String, TValue> 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);
|
||||
}
|
||||
|
49
test/net/sourceforge/plantuml/tim/stdlib/OrdTest.java
Normal file
49
test/net/sourceforge/plantuml/tim/stdlib/OrdTest.java
Normal 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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user