Skip to content

Commit

Permalink
fixed issue with deserialize new lined in VariableObjective data
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolf2323 authored and SaltyAimbOtter committed Dec 24, 2023
1 parent f256ed4 commit 15cc6d2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,6 @@ public VariableData(final String instruction, final Profile profile, final Strin
variables = deserializeData(instruction);
}

public String get(final String key) {
return variables.get(key);
}

public void add(final String key, final String value) {
if (value == null || value.isEmpty()) {
variables.remove(key);
} else {
variables.put(key, value);
}
update();
}

@Override
public String toString() {
return serializeData(variables);
}

public static String serializeData(final Map<String, String> values) {
final StringBuilder builder = new StringBuilder();
for (final Entry<String, String> entry : values.entrySet()) {
Expand Down Expand Up @@ -167,9 +149,27 @@ public static Map<String, String> deserializeData(final String data) {

private static String deserializePart(final String part) {
return part
.replace("\\n", "\n")
.replace("\\\\n", "\n")
.replace("\\:", ":")
.replace("\\\\", "\\");
}

public String get(final String key) {
return variables.get(key);
}

public void add(final String key, final String value) {
if (value == null || value.isEmpty()) {
variables.remove(key);
} else {
variables.put(key, value);
}
update();
}

@Override
public String toString() {
return serializeData(variables);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@

@ExtendWith(MockitoExtension.class)
class VariableObjectiveTest {
@ParameterizedTest
@MethodSource("serializedVariableObjectiveData")
void loading_variables_from_serialized_data(final String serializedData, final String expectedKey, final String expectedValue, @Mock final Profile profile) {
final VariableObjective.VariableData data = new VariableObjective.VariableData(serializedData, profile, "");
final String value = data.get(expectedKey);
assertEquals(expectedValue, value, "Values from deserialized variable objective instruction should be correct.");
}

@ParameterizedTest
@MethodSource("serializableVariableObjectiveData")
void storing_variables_as_serialized_data(final Map<String, String> variables, final String expectedData) {
final String serialized = VariableObjective.VariableData.serializeData(variables);
assertEquals(expectedData, serialized, "Serialized variable objective data instruction should be correct.");
}

public static Stream<Arguments> serializedVariableObjectiveData() {
return Stream.of(
Arguments.of("", "any", null),
Expand All @@ -41,15 +26,15 @@ public static Stream<Arguments> serializedVariableObjectiveData() {
Arguments.of("one:1\ntwo:22\nthree:333", "two", "22"),
Arguments.of("one:1\ntwo:22\nthree:333", "three", "333"),
Arguments.of("one:1\ntwo:22\nthree:333", "four", null),
Arguments.of("newline:This is a\\nnewline test!", "newline", "This is a\nnewline test!"),
Arguments.of("newline:This is a\\nnewline test!", "newline", "This is a\\nnewline test!"),
Arguments.of("newline:This is a\nnewline test!", "newline", "This is a\nnewline test!"),
Arguments.of("newline:This is a\nnewline test!", "newline test!", null),
Arguments.of("multi-newline:This\\nis\\na\\nmulti\\nnewline\\ntest!", "multi-newline", "This\nis\na\nmulti\nnewline\ntest!"),
Arguments.of("multi-newline:This\\nis\\na\\nmulti\\nnewline\\ntest!", "multi-newline", "This\\nis\\na\\nmulti\\nnewline\\ntest!"),
Arguments.of("multi-newline:This\nis\na\nmulti\nnewline\ntest!", "multi-newline", "This\nis\na\nmulti\nnewline\ntest!"),
Arguments.of("first-newliner:This is a\nnewline test!\nsecond-newliner:This also\ncontains a newline!", "first-newliner", "This is a\nnewline test!"),
Arguments.of("first-newliner:This is a\nnewline test!\nsecond-newliner:This also\ncontains a newline!", "second-newliner", "This also\ncontains a newline!"),
Arguments.of("first-newliner:This is a\\nnewline test!\nsecond-newliner:This also\\ncontains a newline!", "first-newliner", "This is a\nnewline test!"),
Arguments.of("first-newliner:This is a\\nnewline test!\nsecond-newliner:This also\\ncontains a newline!", "second-newliner", "This also\ncontains a newline!"),
Arguments.of("first-newliner:This is a\\nnewline test!\nsecond-newliner:This also\\ncontains a newline!", "first-newliner", "This is a\\nnewline test!"),
Arguments.of("first-newliner:This is a\\nnewline test!\nsecond-newliner:This also\\ncontains a newline!", "second-newliner", "This also\\ncontains a newline!"),
Arguments.of("contains_colon:This: Is a test.", "contains_colon", "This: Is a test."),
Arguments.of("contains_colon:This\\: Is a test.", "contains_colon", "This: Is a test."),
Arguments.of("rou\\ge:backslash", "rou\\ge", "backslash"),
Expand All @@ -63,9 +48,9 @@ public static Stream<Arguments> serializedVariableObjectiveData() {
Arguments.of("ending\\::\\:beginning", "ending:", ":beginning"),
Arguments.of("escaped_escape\\\\:does_not_escape", "escaped_escape\\", "does_not_escape"),
Arguments.of(" starts_with_space : ends with space ", " starts_with_space ", " ends with space "),
Arguments.of("newline\\nin_key?:That's fancy!", "newline\nin_key?", "That's fancy!"),
Arguments.of("newline\\nin_key?:That's fancy!", "newline\\nin_key?", "That's fancy!"),
Arguments.of("test:works\nnewline_in\\nsecond_key?:That's fancy!", "test", "works"),
Arguments.of("test:works\nnewline_in\\nsecond_key?:That's fancy!", "newline_in\nsecond_key?", "That's fancy!"),
Arguments.of("test:works\nnewline_in\\nsecond_key?:That's fancy!", "newline_in\\nsecond_key?", "That's fancy!"),
Arguments.of("test:works\n\\\\:Only backslash key!", "test", "works"),
Arguments.of("test:works\n\\\\:Only backslash key!", "\\", "Only backslash key!"),
Arguments.of("test:works\n\\\\\\\\\\\\:Multi backslash key!", "test", "works"),
Expand Down Expand Up @@ -104,4 +89,19 @@ private static Map<String, String> linkedMapOf(final String... values) {
}
return map;
}

@ParameterizedTest
@MethodSource("serializedVariableObjectiveData")
void loading_variables_from_serialized_data(final String serializedData, final String expectedKey, final String expectedValue, @Mock final Profile profile) {
final VariableObjective.VariableData data = new VariableObjective.VariableData(serializedData, profile, "");
final String value = data.get(expectedKey);
assertEquals(expectedValue, value, "Values from deserialized variable objective instruction should be correct.");
}

@ParameterizedTest
@MethodSource("serializableVariableObjectiveData")
void storing_variables_as_serialized_data(final Map<String, String> variables, final String expectedData) {
final String serialized = VariableObjective.VariableData.serializeData(variables);
assertEquals(expectedData, serialized, "Serialized variable objective data instruction should be correct.");
}
}

0 comments on commit 15cc6d2

Please sign in to comment.