Started testing for parser.

This commit is contained in:
Marvin Blum
2015-10-10 17:00:11 +02:00
parent 18ab469808
commit e051b0ae42
8 changed files with 100 additions and 13 deletions

View File

@@ -4,7 +4,7 @@
* special cases (like if ... exitWith)
* sqf: ... sqf whitespace
* solution for build in commands which do not require left values
* pretty/minified printing
* ~~pretty/minified printing~~
* usage
* recursive compiling
* concurrent compiling

View File

@@ -2,4 +2,4 @@ func foo(a, b) {
return a > b;
}
var x = foo();
var x = foo(1, 2);

View File

@@ -151,7 +151,6 @@ func parseFor() {
expect(";")
appendOut("}, {", false)
parseExpression(true)
expect(";")
appendOut("}] do {", true)
expect("{")
parseBlock()

View File

@@ -1,5 +1,85 @@
package asl
import (
"testing"
"io/ioutil"
)
func TestParserDeclaration(t *testing.T) {
got := getCompiled(t, "test/tokenizer_var.asl")
want := "x = 1;\n"
equal(t, got, want)
}
func TestParserAssignment(t *testing.T) {
got := getCompiled(t, "test/parser_assignment.asl")
want := "x = 1;\n"
equal(t, got, want)
}
func TestParserIf(t *testing.T) {
got := getCompiled(t, "test/tokenizer_if.asl")
want := "if (a<b) then {\n};\n"
equal(t, got, want)
}
func TestParserWhile(t *testing.T) {
got := getCompiled(t, "test/tokenizer_while.asl")
want := "while {true} do {\n};"
equal(t, got, want)
}
func TestParserFor(t *testing.T) {
got := getCompiled(t, "test/tokenizer_for.asl")
want := "for [{i=0}, {i<100}, {i=i+1}] do {\n};\n"
equal(t, got, want)
}
func TestParserEach(t *testing.T) {
got := getCompiled(t, "test/tokenizer_each.asl")
want := "{\n} forEach (allUnits);\n"
equal(t, got, want)
}
func TestParserFunction(t *testing.T) {
got := getCompiled(t, "test/tokenizer_func.asl")
want := "TestFunction = {\nparam0 = _this select 0;\nparam1 = _this select 1;\nreturn true;\n};\n"
equal(t, got, want)
}
func TestParserAssignResult(t *testing.T) {
got := getCompiled(t, "test/parser_assign_result.asl")
want := "x = [1, 2, 3] call foo;\ny = [1, 2, 3] call bar;"
equal(t, got, want)
}
func getCompiled(t *testing.T, file string) string {
code, err := ioutil.ReadFile(file)
if err != nil {
t.Error("Could not read test file: "+file)
t.FailNow()
}
tokens := Tokenize(code)
return Parse(tokens, true)
}
func equal(t *testing.T, got, want string) {
if got != want {
t.Error("Results do not equal, got:")
t.Log(got)
t.Log("expected:")
t.Log(want)
t.FailNow()
}
}

View File

@@ -5,7 +5,7 @@ import (
"io/ioutil"
)
func TestVar(t *testing.T) {
func TestTokenizerVar(t *testing.T) {
got := getTokens(t, "test/tokenizer_var.asl")
want := []string{"var", "x", "=", "1", ";"}
@@ -13,7 +13,7 @@ func TestVar(t *testing.T) {
compareTokens(t, &got, &want)
}
func TestIf(t *testing.T) {
func TestTokenizerIf(t *testing.T) {
got := getTokens(t, "test/tokenizer_if.asl")
want := []string{"if", "a", "<", "b", "{", "}"}
@@ -21,7 +21,7 @@ func TestIf(t *testing.T) {
compareTokens(t, &got, &want)
}
func TestWhile(t *testing.T) {
func TestTokenizerWhile(t *testing.T) {
got := getTokens(t, "test/tokenizer_while.asl")
want := []string{"while", "true", "{", "}"}
@@ -29,9 +29,7 @@ func TestWhile(t *testing.T) {
compareTokens(t, &got, &want)
}
//for var i = 0; i < 100; i = i+1 {
//}
func TestFor(t *testing.T) {
func TestTokenizerFor(t *testing.T) {
got := getTokens(t, "test/tokenizer_for.asl")
want := []string{"for", "var", "i", "=", "0", ";", "i", "<", "100", ";", "i", "=", "i+1", "{", "}"}
@@ -39,7 +37,7 @@ func TestFor(t *testing.T) {
compareTokens(t, &got, &want)
}
func TestEach(t *testing.T) {
func TestTokenizerEach(t *testing.T) {
got := getTokens(t, "test/tokenizer_each.asl")
want := []string{"each", "allUnits", "{", "}"}
@@ -47,7 +45,7 @@ func TestEach(t *testing.T) {
compareTokens(t, &got, &want)
}
func TestFunction(t *testing.T) {
func TestTokenizerFunction(t *testing.T) {
got := getTokens(t, "test/tokenizer_func.asl")
want := []string{"func", "TestFunction", "(", "param0", ",", "param1", ")", "{", "return", "true", ";", "}"}

View File

@@ -6,8 +6,15 @@ import (
"io/ioutil"
)
const version = "0.1"
func usage() {
fmt.Println("Usage: asl [-v|-pretty]")
fmt.Println("Usage: asl [-v|-r|-pretty] <input file/folder> [<output file/folder>]")
fmt.Println("-v (optional) shows asl version")
fmt.Println("-r (optional) recursivly compile all asl files in folder")
fmt.Println("-pretty (optional) activates pretty printing")
fmt.Println("<input file/folder> file or directory to compile")
fmt.Println("<output file/folder> (optional) output file/folder, if not set, files will be created alongside their asl files")
}
func main() {

View File

@@ -0,0 +1,2 @@
var x = foo(1, 2, 3);
y = bar(1, 2, 3);

View File

@@ -0,0 +1 @@
x = 1;