Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/main/java/utils/StringUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ else if (keyValue[0] == null)
* @return value depending on the context (could be from ContextStore, Properties, Random etc)
*/
public static String contextCheck(@NotNull String input){
if (input.contains("CONTEXT-"))
input = ContextStore.get(TextParser.parse("CONTEXT-", null, input));
if (input.contains("CONTEXT-")) {
if (input.startsWith("MATH")) input = evaluateMathExpression(input);
else input = ContextStore.get(TextParser.parse("CONTEXT-", null, input));
}
else if (input.contains("PROPERTY-")){
String propertyName = TextParser.parse("PROPERTY-", null, input);
input = PropertyUtilities.getProperty(propertyName, "NULL");
Expand Down Expand Up @@ -253,6 +255,36 @@ else if (input.contains("PROPERTY-")){
return input;
}

private static String evaluateMathExpression(String input) {
input = input.replaceAll("\\s+", "");
String expression = input.substring("MATH->".length());
String args = expression.substring(4, expression.length() - 1); //Scoop from inside ADD() or SUB()
String[] parts = args.split(",");
if (parts.length != 2) {
throw new IllegalArgumentException("Exactly two arguments required.");
}
int left = parseOperand(parts[0]);
int right = parseOperand(parts[1]);
if (expression.startsWith("ADD(") && expression.endsWith(")")) {
return String.valueOf(left + right);
} else if (expression.startsWith("SUB(") && expression.endsWith(")")) {
return String.valueOf(left - right);
} else {
throw new IllegalArgumentException("Unsupported math operation: " + expression);
}
}

private static int parseOperand(String operand) {
if (operand.startsWith("CONTEXT-")) {
String contextKey = TextParser.parse("CONTEXT-", null, operand);
String value = ContextStore.get(contextKey);
assert value != null;
return Integer.parseInt(value);
} else {
return Integer.parseInt(operand);
}
}

/**
* An enumeration of colors used to represent different color values.
*/
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;
import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static utils.arrays.ArrayUtilities.*;
import static utils.email.EmailUtilities.Inbox.EmailField.CONTENT;
import static utils.email.EmailUtilities.Inbox.EmailField.SUBJECT;
Expand Down Expand Up @@ -351,4 +352,56 @@ public void dateFormatTest() {
);
printer.success("The dateFormatTest() test pass!");
}

@Test
public void testSubtractionWithContextAndLiteral() {
ContextStore.put("val", "20");
String input = "MATH -> SUB ( CONTEXT-val , 5 )";
String result = contextCheck(input);
Assert.assertEquals("15", result);
printer.success("The testSubtractionWithContextAndLiteral() test pass!");
}

@Test
public void testAdditionWithContextOperands() {
ContextStore.put("a", "10");
ContextStore.put("b", "5");
String input = "MATH->ADD(CONTEXT-a, CONTEXT-b)";
String result = contextCheck(input);
Assert.assertEquals("15", result);
printer.success("The testAdditionWithContextOperands() test pass!");
}

@Test
public void testInvalidOperation() {
ContextStore.put("a", "3");
String input = "MATH->MUL(2,CONTEXT-a)";
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
contextCheck(input);
});
Assert.assertTrue(exception.getMessage().contains("Unsupported"));
printer.success("The testInvalidOperation() test pass!");
}

@Test
public void testNonNumericContextValue() {
ContextStore.put("bad", "abc");

String input = "MATH->ADD(CONTEXT-bad, 2)";
assertThrows(NumberFormatException.class, () -> {
contextCheck(input);
});
printer.success("The testNonNumericContextValue() test pass!");
}

@Test
public void testBadNumberOfArguments() {
ContextStore.put("abc", "10");
String input = "MATH->ADD(CONTEXT-abc,2,3)";
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
contextCheck(input);
});
Assert.assertTrue(exception.getMessage().contains("Exactly two arguments required"));
printer.success("The testBadNumberOfArguments() test pass!");
}
}