Skip to content

Commit

Permalink
test: add test cases for transpiler module #2710
Browse files Browse the repository at this point in the history
  • Loading branch information
jgomer2001 committed Oct 25, 2022
1 parent 12b0b0a commit 428dba0
Show file tree
Hide file tree
Showing 54 changed files with 599 additions and 0 deletions.
48 changes: 48 additions & 0 deletions agama/transpiler/src/test/java/io/jans/agama/test/FlowUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.jans.agama.test;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class FlowUtil {

private static final String EXT = "txt";

private static Logger LOG = LogManager.getLogger(FlowUtil.class);

private FlowUtil() {}

public static Map<String, String> sourcesOfFolder(String parent) {

try {
Path path = Paths.get(parent);
LOG.debug("Reading files under {}", path.toAbsolutePath());

return Files.walk(path).filter(p -> p.toString().endsWith("." + EXT))
.map(p -> {

String code = null;
try {
code = Files.readString(p);
} catch (IOException e) {
LOG.error("Unable to read contents of {}: {}", p, e.getMessage());
}
return new SimpleEntry(p.toString(), code);

}).collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString()));

} catch (IOException e) {
LOG.error(e.getMessage(), e);
return null;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.jans.agama.test;

import io.jans.agama.dsl.Transpiler;
import io.jans.agama.dsl.TranspilerException;
import io.jans.agama.dsl.error.SyntaxException;

import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.Test;

import static org.testng.Assert.fail;

public class MalformedFlowsTest {

private Logger logger = LogManager.getLogger(getClass());

@Test
public void test() {

Map<String, String> map = new TreeMap<>(FlowUtil.sourcesOfFolder("target/test-classes/fail"));
//Sonar check likes Map.Entry instead of nicer ways to iterate like map.keySet()
for (Map.Entry<String, String> entry: map.entrySet()) {
String file = entry.getKey();

try {
logger.info("Checking syntax of '{}'", file);
Transpiler.runSyntaxCheck(entry.getValue());

fail(file + " is expected to have errors but it passed validation");
} catch(SyntaxException | TranspilerException e) {
String msg = e.getMessage();
logger.info("{}\n", msg);
}
}
logger.info("{} files examined", map.size());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.jans.agama.test;

import io.jans.agama.dsl.Transpiler;
import io.jans.agama.dsl.TranspilerException;
import io.jans.agama.dsl.error.SyntaxException;

import java.util.Map;
import java.util.Map.Entry;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.Test;

import static org.testng.Assert.fail;

public class ValidFlowsTest {

private Logger logger = LogManager.getLogger(getClass());

@Test
public void test() {

Map<String, String> map = FlowUtil.sourcesOfFolder("target/test-classes/pass");
//Sonar check likes Map.Entry instead of nicer ways to iterate like map.keySet()
for (Map.Entry<String, String> entry: map.entrySet()) {
String file = entry.getKey();

try {
logger.info("Checking syntax of '{}'", file);
Transpiler.runSyntaxCheck(entry.getValue());
} catch(SyntaxException | TranspilerException e) {
String msg = e.getMessage();
logger.error("{}\n", msg);

fail(file + " has errors", e);
}
}
logger.info("{} files examined", map.size());

}

}
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/1-literals/01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = "\""

//Failure reason: string literals cannot contain double quotes inside; last quote is hanging
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/1-literals/02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = .5

//Failure reason: a digit is expected before decimal separator
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/1-literals/03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = 'hello'

//Failure reason: string literals are surrounded by double quotes only
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/1-literals/04.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = { "year": 2022, "color": "blue" }

//Failure reason: map literals are not JSON-like but more like Javascript objects
8 changes: 8 additions & 0 deletions agama/transpiler/src/test/resources/fail/1-literals/05.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Flow flow
Basepath ""

x = [
1,
2 ]

//Failure reason: in arrays, only commas can be surrounded by new lines
9 changes: 9 additions & 0 deletions agama/transpiler/src/test/resources/fail/1-literals/06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Flow flow
Basepath ""

x = { for: "you",
function: [ "properly" ], goto: {}

}

//Failure reason: in maps, only commas can be surrounded by new lines
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/2-variables/01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

_hey = "you"

//Failure reason: invalid variable name (underscored prefixed)
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/2-variables/02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

yO0_ = { weird.Key = false }

//Failure reason: invalid key name (period character)
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/2-variables/03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = y.oh-me

//Failure reason: invalid access to key with special character in name (hyphen)
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/2-variables/04.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = y["hi"]

//Failure reason: only a positive number or variable name is allowed for list indexing
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/2-variables/05.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = y[ z[0] ]

//Failure reason: only a positive number or variable name is allowed for list indexing
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/2-variables/06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = y[ z.w ]

//Failure reason: only a positive number or variable name is allowed for list indexing
7 changes: 7 additions & 0 deletions agama/transpiler/src/test/resources/fail/3-indentation/01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Flow flow
Basepath ""

When day is rainy
Log me

//Failure reason: at least one statement should be indented after When
8 changes: 8 additions & 0 deletions agama/transpiler/src/test/resources/fail/3-indentation/02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Flow flow
Basepath ""

When day is rainy
Log me
Log "cowboy music"

//Failure reason: second log statement mis-aligned
9 changes: 9 additions & 0 deletions agama/transpiler/src/test/resources/fail/3-indentation/03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Flow flow
Basepath ""

When day is rainy
Log me
Otherwise
Log "cowboy music"

//Failure reason: Otherwise block statement mis-aligned
9 changes: 9 additions & 0 deletions agama/transpiler/src/test/resources/fail/3-indentation/04.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Flow flow
Basepath ""

When day is rainy
Log me
Log "cowboy music"
Log "By Cerce"

//Failure reason: second log statement mis-aligned
9 changes: 9 additions & 0 deletions agama/transpiler/src/test/resources/fail/3-indentation/05.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Flow flow
Basepath ""

When day is rainy
Log me
Log "cowboy music"
Log "By Cerce"

//Failure reason: second log statement mis-aligned
7 changes: 7 additions & 0 deletions agama/transpiler/src/test/resources/fail/3-indentation/06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Flow flow
Basepath ""
Configs conf

Log "Eye hate god"

//Failure reason: Configs statement mis-aligned
7 changes: 7 additions & 0 deletions agama/transpiler/src/test/resources/fail/3-indentation/07.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Flow flow
Basepath ""

When day is rainy
Log me

//Failure reason: When expected to be aligned to Flow
9 changes: 9 additions & 0 deletions agama/transpiler/src/test/resources/fail/3-indentation/08.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Flow flow
Basepath ""

When day is rainy
Log me
Log "cowboy music"
Log "By Cerce"

//Failure reason: second and third log statements mis-aligned
10 changes: 10 additions & 0 deletions agama/transpiler/src/test/resources/fail/3-indentation/09.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Flow flow
Basepath ""

Match day to
1
Log "cowboy music"
2
Log "By Cerce"

//Failure reason: second case for Match is mis-aligned
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/3-indentation/10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

Log me

//Failure reason: Flow declaration not aligned to column 1
7 changes: 7 additions & 0 deletions agama/transpiler/src/test/resources/fail/4-structure/01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Flow flow
Basepath ""

When day is rainy
Log me

//Failure reason: When expected to be aligned to Flow
11 changes: 11 additions & 0 deletions agama/transpiler/src/test/resources/fail/4-structure/02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Flow flow
Basepath ""

When day is rainy
Log me
Otherwise
Log "cowboy music"
Otherwise
Log "By Cerce"

//Failure reason: unexpected second Otherwise block
11 changes: 11 additions & 0 deletions agama/transpiler/src/test/resources/fail/4-structure/03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Flow flow
Basepath ""

When day is rainy
Log me
Otherwise When there is hope
Log "cowboy music"
Otherwise
Log "By Cerce"

//Failure reason: Otherwise-When does not exist
12 changes: 12 additions & 0 deletions agama/transpiler/src/test/resources/fail/4-structure/04.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Flow flow
Basepath ""

When day is rainy
When there is cold
Log me
Otherwise
Log "cowboy music"
Otherwise
Log "By Cerce"

//Failure reason: unexpected second Otherwise block
13 changes: 13 additions & 0 deletions agama/transpiler/src/test/resources/fail/4-structure/05.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Flow flow
Basepath ""

When day is rainy
Repeat k times max
When there is cold
Log me
Otherwise
Log "cowboy music"
Otherwise
Log "By Cerce"

//Failure reason: unexpected Otherwise block for Repeat
8 changes: 8 additions & 0 deletions agama/transpiler/src/test/resources/fail/4-structure/06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Flow flow
Basepath ""

Repeat k times max
Quit When there is cold
Log me

//Failure reason: unexpected indented block under Quit-When
9 changes: 9 additions & 0 deletions agama/transpiler/src/test/resources/fail/4-structure/07.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Flow flow
Basepath ""

Repeat k times max
Quit When there is cold
Otherwise
Log me

//Failure reason: unexpected Otherwise block
Loading

0 comments on commit 428dba0

Please sign in to comment.