-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test cases for transpiler module #2710
- Loading branch information
1 parent
12b0b0a
commit 428dba0
Showing
54 changed files
with
599 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
agama/transpiler/src/test/java/io/jans/agama/test/FlowUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
agama/transpiler/src/test/java/io/jans/agama/test/MalformedFlowsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
agama/transpiler/src/test/java/io/jans/agama/test/ValidFlowsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
7
agama/transpiler/src/test/resources/fail/3-indentation/01.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
8
agama/transpiler/src/test/resources/fail/3-indentation/02.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
agama/transpiler/src/test/resources/fail/3-indentation/03.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
agama/transpiler/src/test/resources/fail/3-indentation/04.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
agama/transpiler/src/test/resources/fail/3-indentation/05.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
7
agama/transpiler/src/test/resources/fail/3-indentation/06.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
7
agama/transpiler/src/test/resources/fail/3-indentation/07.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
agama/transpiler/src/test/resources/fail/3-indentation/08.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
agama/transpiler/src/test/resources/fail/3-indentation/09.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
6
agama/transpiler/src/test/resources/fail/3-indentation/10.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
agama/transpiler/src/test/resources/fail/4-structure/02.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
agama/transpiler/src/test/resources/fail/4-structure/03.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
agama/transpiler/src/test/resources/fail/4-structure/04.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
agama/transpiler/src/test/resources/fail/4-structure/05.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.