Skip to content

Commit

Permalink
test: update Parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MlkMahmud committed Aug 21, 2023
1 parent 6d9c913 commit 589e30e
Show file tree
Hide file tree
Showing 6 changed files with 1,243 additions and 11 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/MlkMahmud/jack-compiler

go 1.19

require github.com/nsf/jsondiff v0.0.0-20230430225905-43f6cf3098c1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/nsf/jsondiff v0.0.0-20230430225905-43f6cf3098c1 h1:dOYG7LS/WK00RWZc8XGgcUTlTxpp3mKhdR2Q9z9HbXM=
github.com/nsf/jsondiff v0.0.0-20230430225905-43f6cf3098c1/go.mod h1:mpRZBD8SJ55OIICQ3iWH0Yz3cjzA61JdqMLoWXeB2+8=
49 changes: 38 additions & 11 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,56 @@ package parser_test

import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"path"
"strings"
"testing"

. "github.com/MlkMahmud/jack-compiler/lexer"
. "github.com/MlkMahmud/jack-compiler/parser"
. "github.com/nsf/jsondiff"
)

const TEST_DATA_PATH = "../testdata"

func readFileContent(filename string) []byte {
file, _ := os.Open(filename)
bytes, err := io.ReadAll(file)

if err != nil {
log.Fatal(err)
}
return bytes
}

func TestParser(t *testing.T) {
files := []string{"Array", "Square", "SquareGame"}
lexer := NewLexer()
filePath := path.Join(TEST_DATA_PATH, "SquareGame.jack")
tokens := lexer.Tokenize(filePath)

parser := NewParser()
class := parser.Parse(tokens)

jsonData, err := json.Marshal(class)
if err != nil {
fmt.Println(err)
t.Fatal()
}
for _, file := range files {
t.Run(file, func(t *testing.T) {
filePath := path.Join(TEST_DATA_PATH, strings.Join([]string{file, "jack"}, "."))
cmpFilePath := path.Join(TEST_DATA_PATH, "expected", strings.Join([]string{file, "json"}, "."))

tokens := lexer.Tokenize(filePath)
class := parser.Parse(tokens)

expected := readFileContent(cmpFilePath)

actual, err := json.Marshal(class)

fmt.Print(string(jsonData))
if err != nil {
t.Fatal(err)
}

difference, desc := Compare(expected, actual, &Options{ SkipMatches: true })

if difference != FullMatch {
t.Fatal(desc)
}
})
}
}
143 changes: 143 additions & 0 deletions testdata/expected/Array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"Name": { "Name": "Main" },
"Subroutines": [
{
"Name": { "Name": "main" },
"Params": null,
"Kind": 1,
"Type": "void",
"Body": {
"Statements": [
{
"Target": { "Name": "length" },
"Value": {
"Arguments": [{ "Type": 3, "Value": "HOW MANY NUMBERS? " }],
"Callee": {
"Object": { "Name": "Keyboard" },
"Property": { "Name": "readInt" }
}
}
},
{
"Target": { "Name": "a" },
"Value": {
"Arguments": [{ "Name": "length" }],
"Callee": {
"Object": { "Name": "Array" },
"Property": { "Name": "new" }
}
}
},
{ "Target": { "Name": "i" }, "Value": { "Type": 1, "Value": "0" } },
{
"Body": {
"Statements": [
{
"Target": {
"Indexer": { "Name": "i" },
"Object": { "Name": "a" }
},
"Value": {
"Arguments": [
{ "Type": 3, "Value": "ENTER THE NEXT NUMBER: " }
],
"Callee": {
"Object": { "Name": "Keyboard" },
"Property": { "Name": "readInt" }
}
}
},
{
"Target": { "Name": "i" },
"Value": {
"Operator": 0,
"Left": { "Name": "i" },
"Right": { "Type": 1, "Value": "1" }
}
}
]
},
"Condition": {
"Operator": 4,
"Left": { "Name": "i" },
"Right": { "Name": "length" }
}
},
{ "Target": { "Name": "i" }, "Value": { "Type": 1, "Value": "0" } },
{ "Target": { "Name": "sum" }, "Value": { "Type": 1, "Value": "0" } },
{
"Body": {
"Statements": [
{
"Target": { "Name": "sum" },
"Value": {
"Operator": 0,
"Left": { "Name": "sum" },
"Right": {
"Indexer": { "Name": "i" },
"Object": { "Name": "a" }
}
}
},
{
"Target": { "Name": "i" },
"Value": {
"Operator": 0,
"Left": { "Name": "i" },
"Right": { "Type": 1, "Value": "1" }
}
}
]
},
"Condition": {
"Operator": 4,
"Left": { "Name": "i" },
"Right": { "Name": "length" }
}
},
{
"Expression": {
"Arguments": [{ "Type": 3, "Value": "THE AVERAGE IS: " }],
"Callee": {
"Object": { "Name": "Output" },
"Property": { "Name": "printString" }
}
}
},
{
"Expression": {
"Arguments": [
{
"Operator": 3,
"Left": { "Name": "sum" },
"Right": { "Name": "length" }
}
],
"Callee": {
"Object": { "Name": "Output" },
"Property": { "Name": "printInt" }
}
}
},
{
"Expression": {
"Arguments": null,
"Callee": {
"Object": { "Name": "Output" },
"Property": { "Name": "println" }
}
}
},
{ "Expression": null }
],
"Vars": [
{ "Name": "a", "Kind": 2, "Type": "Array" },
{ "Name": "length", "Kind": 2, "Type": "int" },
{ "Name": "i", "Kind": 2, "Type": "int" },
{ "Name": "sum", "Kind": 2, "Type": "int" }
]
}
}
],
"Vars": null
}
Loading

0 comments on commit 589e30e

Please sign in to comment.