Skip to content

Commit

Permalink
Unit test and fix magic list variable
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarian2 committed Jan 9, 2024
1 parent a6f1ff3 commit 441c4d7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/mimecast/robin/util/Magic.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,18 @@ public static String magicReplace(String magicString, Session session, boolean n
magicArgs = magicArgs.replaceAll("[()]", "");
}
String magicName = matcher.group(3);
String magicRow = matcher.group(5);
String resultColumn = matcher.group(7);
String value = null;

// Magic variables.
if (session.hasMagic(magicName)) {
value = getMagicValue(magicName, matcher.group(4), session);
value = getMagicValue(magicName, magicRow, session);
}

// Saved results.
if (resultColumn != null && session.getSavedResults().containsKey(magicName)) {
int resultRow = Integer.parseInt(matcher.group(5));
int resultRow = Integer.parseInt(magicRow);

if (session.getSavedResults().get(magicName) != null &&
session.getSavedResults().get(magicName).get(resultRow) != null) {
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/mimecast/robin/util/MagicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class MagicTest {

Expand All @@ -17,6 +18,12 @@ void magicReplace() {
session.putMagic("port", "25");
assertEquals("25", Magic.magicReplace("{$port}", session, false));

session.putMagic("hostnames", List.of("example.com"));
assertEquals("example.com", Magic.magicReplace("{$hostnames[0]}", session, false));

session.saveResults("hostnames", List.of("example.com"));
assertNotNull(Magic.magicReplace("{$hostnames[?]}", session, false));

session.saveResults("host", List.of(Map.of("com", "example.com")));
assertEquals("example.com", Magic.magicReplace("{$host[0][com]}", session, false));

Expand Down

0 comments on commit 441c4d7

Please sign in to comment.