mirror of
https://github.com/octoleo/plantuml.git
synced 2024-11-17 02:35:09 +00:00
Merge pull request #1624 from The-Lum/PatchBranch
fix and test refactor: on `GetJsonKey`, `Chr` and `Ord`
This commit is contained in:
commit
c20153a841
@ -59,8 +59,8 @@ public class Chr 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 value = (char) values.get(0).toInt();
|
final String value = String.valueOf(Character.toChars(values.get(0).toInt()));
|
||||||
return TValue.fromString("" + value);
|
return TValue.fromString(value);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
return TValue.fromString("\0");
|
return TValue.fromString("\0");
|
||||||
}
|
}
|
||||||
|
@ -76,9 +76,11 @@ public class GetJsonKey extends SimpleReturnFunction {
|
|||||||
final JsonArray array = (JsonArray) json;
|
final JsonArray array = (JsonArray) json;
|
||||||
final JsonArray result = new JsonArray();
|
final JsonArray result = new JsonArray();
|
||||||
for (JsonValue tmp : array) {
|
for (JsonValue tmp : array) {
|
||||||
final JsonObject object = (JsonObject) tmp;
|
if (tmp.isObject()) {
|
||||||
for (String key : object.names())
|
final JsonObject object = (JsonObject) tmp;
|
||||||
result.add(key);
|
for (String key : object.names())
|
||||||
|
result.add(key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return TValue.fromJson(result);
|
return TValue.fromJson(result);
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,31 @@
|
|||||||
package net.sourceforge.plantuml.tim.stdlib;
|
package net.sourceforge.plantuml.tim.stdlib;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static net.sourceforge.plantuml.tim.TimTestUtils.assertTimExpectedOutputFromInput;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
|
||||||
|
import org.junit.jupiter.api.IndicativeSentencesGeneration;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.CsvSource;
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
|
||||||
import net.sourceforge.plantuml.tim.EaterException;
|
import net.sourceforge.plantuml.tim.EaterException;
|
||||||
import net.sourceforge.plantuml.tim.EaterExceptionLocated;
|
import net.sourceforge.plantuml.tim.EaterExceptionLocated;
|
||||||
import net.sourceforge.plantuml.tim.expression.TValue;
|
import net.sourceforge.plantuml.tim.TFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests the builtin function %chr.
|
* 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
|
* Tests chr according to a list of input / expected output
|
||||||
*
|
*
|
||||||
* @throws EaterException should not
|
* @throws EaterException should not
|
||||||
* @throws EaterExceptionLocated should not
|
* @throws EaterExceptionLocated should not
|
||||||
*/
|
*/
|
||||||
@ParameterizedTest
|
@ParameterizedTest(name = "[{index}] " + cutName + "({0}) = ''{1}''")
|
||||||
@CsvSource(nullValues = "null", value = {
|
@CsvSource(nullValues = "null", value = {
|
||||||
" 65 , A ",
|
" 65 , A ",
|
||||||
" 9 , '\t' ",
|
" 9 , '\t' ",
|
||||||
@ -32,15 +34,11 @@ class ChrTest {
|
|||||||
" 34 , '\"' ",
|
" 34 , '\"' ",
|
||||||
" 224 , à ",
|
" 224 , à ",
|
||||||
" 233 , é ",
|
" 233 , é ",
|
||||||
// TODO: fix `%chr` to allow Unicode chars, the corresponding tests are here:
|
// DONE: fix `%chr` to allow Unicode chars, the corresponding tests are here:
|
||||||
// " 128512 , 😀 ",
|
" 128512 , 😀 ",
|
||||||
// " 128512 , \uD83D\uDE00 ",
|
" 128512 , \uD83D\uDE00 ",
|
||||||
})
|
})
|
||||||
void executeReturnFunctionChrTest(Integer input, String expected) throws EaterException, EaterExceptionLocated {
|
void Test_with_Integer(Integer input, String expected) throws EaterException, EaterExceptionLocated {
|
||||||
Chr cut = new Chr();
|
assertTimExpectedOutputFromInput(cut, input, expected);
|
||||||
|
|
||||||
List<TValue> values = Collections.singletonList(TValue.fromInt(input));
|
|
||||||
TValue tValue = cut.executeReturnFunction(null, null, null, values, null);
|
|
||||||
assertEquals(expected, tValue.toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -61,11 +61,11 @@ class GetJsonKeyTest {
|
|||||||
" '{\"a\":\"abc\"}' , [\"a\"]",
|
" '{\"a\":\"abc\"}' , [\"a\"]",
|
||||||
" '[{\"a\":[1, 2]}, {\"b\":[3, 4]}]' , '[\"a\",\"b\"]'",
|
" '[{\"a\":[1, 2]}, {\"b\":[3, 4]}]' , '[\"a\",\"b\"]'",
|
||||||
" '{\"a\":[1, 2], \"b\":\"abc\", \"b\":true}' , '[\"a\",\"b\",\"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.:
|
// Ref.:
|
||||||
// - https://datatracker.ietf.org/doc/html/rfc8259#section-5
|
// - https://datatracker.ietf.org/doc/html/rfc8259#section-5
|
||||||
// - https://json-schema.org/understanding-json-schema/reference/array.html
|
// - 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 {
|
void Test_with_Json(@ConvertWith(StringJsonConverter.class) JsonValue input, String expected) throws EaterException, EaterExceptionLocated {
|
||||||
assertTimExpectedOutputFromInput(cut, input, expected);
|
assertTimExpectedOutputFromInput(cut, input, expected);
|
||||||
|
@ -1,29 +1,31 @@
|
|||||||
package net.sourceforge.plantuml.tim.stdlib;
|
package net.sourceforge.plantuml.tim.stdlib;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static net.sourceforge.plantuml.tim.TimTestUtils.assertTimExpectedOutputFromInput;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
|
||||||
|
import org.junit.jupiter.api.IndicativeSentencesGeneration;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.CsvSource;
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
|
||||||
import net.sourceforge.plantuml.tim.EaterException;
|
import net.sourceforge.plantuml.tim.EaterException;
|
||||||
import net.sourceforge.plantuml.tim.EaterExceptionLocated;
|
import net.sourceforge.plantuml.tim.EaterExceptionLocated;
|
||||||
import net.sourceforge.plantuml.tim.expression.TValue;
|
import net.sourceforge.plantuml.tim.TFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests the builtin function %ord.
|
* 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
|
* Tests ord according to a list of input / expected output
|
||||||
*
|
*
|
||||||
* @throws EaterException should not
|
* @throws EaterException should not
|
||||||
* @throws EaterExceptionLocated should not
|
* @throws EaterExceptionLocated should not
|
||||||
*/
|
*/
|
||||||
@ParameterizedTest
|
@ParameterizedTest(name = "[{index}] " + cutName + "(''{0}'') = {1}")
|
||||||
@CsvSource(nullValues = "null", value = {
|
@CsvSource(nullValues = "null", value = {
|
||||||
" A , 65 ",
|
" A , 65 ",
|
||||||
" ABC , 65 ",
|
" ABC , 65 ",
|
||||||
@ -39,11 +41,7 @@ class OrdTest {
|
|||||||
" 😀 , 128512 ",
|
" 😀 , 128512 ",
|
||||||
" \uD83D\uDE00 , 128512 ",
|
" \uD83D\uDE00 , 128512 ",
|
||||||
})
|
})
|
||||||
void executeReturnFunctionOrdTest(String input, String expected) throws EaterException, EaterExceptionLocated {
|
void Test_with_String(String input, String expected) throws EaterException, EaterExceptionLocated {
|
||||||
Ord cut = new Ord();
|
assertTimExpectedOutputFromInput(cut, input, expected);
|
||||||
|
|
||||||
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