Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add int and string management on %json_add #1757

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/net/sourceforge/plantuml/tim/expression/TValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import java.util.Objects;

import net.sourceforge.plantuml.json.Json;
import net.sourceforge.plantuml.json.JsonValue;

public final class TValue {
Expand Down Expand Up @@ -221,4 +222,13 @@ public JsonValue toJson() {
return jsonValue;
}

public JsonValue toJsonValue() {
if (isNumber()) {
return Json.value(this.intValue);
}
if (isString()) {
return Json.value(this.stringValue);
}
return this.jsonValue;
}
}
14 changes: 6 additions & 8 deletions src/net/sourceforge/plantuml/tim/stdlib/JsonAdd.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,15 @@ public TValue executeReturnFunction(TContext context, TMemory memory, StringLoca
if (!json.isArray() && !json.isObject())
return data;
if (json.isArray()) {
final JsonValue value = values.get(1).toJson();
final JsonArray array = (JsonArray) json;
array.add(value);
return TValue.fromJson(array);
final JsonValue value = values.get(1).toJsonValue();
json.asArray().add(value);
return TValue.fromJson(json);
}
if (json.isObject()) {
final String name = values.get(1).toString();
final JsonValue value = values.get(2).toJson();
final JsonObject object = (JsonObject) json;
object.add(name, value);
return TValue.fromJson(object);
final JsonValue value = values.get(2).toJsonValue();
json.asObject().add(name, value);
return TValue.fromJson(json);
}
throw new EaterException("Bad JSON type", location);
}
Expand Down
14 changes: 14 additions & 0 deletions test/net/sourceforge/plantuml/tim/TimTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,18 @@ public static void assertTimExpectedOutputFromInput(TFunction func, JsonValue in
assertEquals(expected, tValue.toString());
}

// Tfunc: (JsonValue, String, String) -> (String)
public static void assertTimExpectedOutputFromInput(TFunction func, JsonValue input1, String input2, String input3, String expected) throws EaterException {
final List<TValue> values = Arrays.asList(TValue.fromJson(input1), TValue.fromString(input2), TValue.fromString(input3));
final TValue tValue = func.executeReturnFunction(null, null, null, values, null);
assertEquals(expected, tValue.toString());
}

// Tfunc: (JsonValue, String, Int) -> (String)
public static void assertTimExpectedOutputFromInput(TFunction func, JsonValue input1, String input2, Integer input3, String expected) throws EaterException {
final List<TValue> values = Arrays.asList(TValue.fromJson(input1), TValue.fromString(input2), TValue.fromInt(input3));
final TValue tValue = func.executeReturnFunction(null, null, null, values, null);
assertEquals(expected, tValue.toString());
}

}
51 changes: 50 additions & 1 deletion test/net/sourceforge/plantuml/tim/stdlib/JsonAddTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,35 @@ class JsonAddTest {
void Test_with_Array_Json(@ConvertWith(StringJsonConverter.class) JsonValue input1, @ConvertWith(StringJsonConverter.class) JsonValue input2, String expected) throws EaterException {
assertTimExpectedOutputFromInput(cut, input1, input2, expected);
}


@ParameterizedTest(name = paramTestName)
@CsvSource(value = {
" [], -1, '[\"-1\"]' ",
" [], 1, '[\"1\"]' ",
" [0], 123, '[0,\"123\"]' ",
" [0], a, '[0,\"a\"]' ",
" [0], \"a\", '[0,\"\\\"a\\\"\"]' ",
" [0], a b c, '[0,\"a b c\"]' ",
" [0], \"a b c\", '[0,\"\\\"a b c\\\"\"]' ",
" '[{\"a\":[1, 2]}]', 1, '[{\"a\":[1,2]},\"1\"]' ",
" '[{\"a\":[1, 2]}]', a, '[{\"a\":[1,2]},\"a\"]' ",
})
void Test_with_Array_Json_add_Str(@ConvertWith(StringJsonConverter.class) JsonValue input1, String input2, String expected) throws EaterException {
assertTimExpectedOutputFromInput(cut, input1, input2, expected);
}

@ParameterizedTest(name = paramTestName)
@CsvSource(value = {
" [], -1, [-1]",
" [], 1, [1]",
" [0], 123, '[0,123]' ",
" '[{\"a\":[1, 2]}]', 1, '[{\"a\":[1,2]},1]' ",
" '[{\"a\":[1, 2]}]', 123, '[{\"a\":[1,2]},123]' ",
})
void Test_with_Array_Json_add_Int(@ConvertWith(StringJsonConverter.class) JsonValue input1, Integer input2, String expected) throws EaterException {
assertTimExpectedOutputFromInput(cut, input1, input2, expected);
}

@ParameterizedTest(name = "[{index}] " + cutName + "({0}, {1}, {2}) = {3}")
@CsvSource(value = {
" {}, a, 1, {\"a\":1}",
Expand All @@ -57,6 +85,27 @@ void Test_with_Object_Json(@ConvertWith(StringJsonConverter.class) JsonValue inp
assertTimExpectedOutputFromInput(cut, input1, input2, input3, expected);
}

@ParameterizedTest(name = "[{index}] " + cutName + "({0}, {1}, {2}) = {3}")
@CsvSource(value = {
" {}, a, 1, {\"a\":\"1\"}",
" {}, a, 'abc', '{\"a\":\"abc\"}'",
" {}, a, 'a b c', '{\"a\":\"a b c\"}'",
" {\"age\" : 30}, name, Sally, '{\"age\":30,\"name\":\"Sally\"}'",
})
void Test_with_Object_Json_add_Str(@ConvertWith(StringJsonConverter.class) JsonValue input1, String input2, String input3, String expected) throws EaterException {
assertTimExpectedOutputFromInput(cut, input1, input2, input3, expected);
}

@ParameterizedTest(name = "[{index}] " + cutName + "({0}, {1}, {2}) = {3}")
@CsvSource(value = {
" {}, a, 1, {\"a\":1}",
" {}, a, 123, '{\"a\":123}'",
" {\"age\" : 30}, name, 123, '{\"age\":30,\"name\":123}'",
})
void Test_with_Object_Json_add_Int(@ConvertWith(StringJsonConverter.class) JsonValue input1, String input2, Integer input3, String expected) throws EaterException {
assertTimExpectedOutputFromInput(cut, input1, input2, input3, expected);
}

@Nested
class Not_Nominal_Test {
@ParameterizedTest(name = paramTestName)
Expand Down