This repository has been archived on 2024-02-27. You can view files and clone it, but cannot push or open issues or pull requests.
arf/parser/test-common.go

79 lines
1.5 KiB
Go
Raw Normal View History

package parser
import "io"
2022-09-05 17:42:17 +00:00
import "os"
import "strings"
import "testing"
2022-09-05 17:42:17 +00:00
import "path/filepath"
2022-09-05 18:52:37 +00:00
func checkTree (modulePath string, skim bool, correct string, test *testing.T) {
2022-09-05 17:42:17 +00:00
cwd, _ := os.Getwd()
modulePath = filepath.Join(cwd, modulePath)
2022-09-05 18:52:37 +00:00
tree, err := Fetch(modulePath, skim)
2022-09-01 22:02:14 +00:00
treeString := tree.ToString(0)
treeRunes := []rune(treeString)
test.Log("CORRECT TREE:")
logWithLineNumbers(correct, test)
test.Log("WHAT WAS PARSED:")
logWithLineNumbers(treeString, test)
if err != io.EOF && err != nil {
test.Log("returned error:")
test.Log(err.Error())
test.Fail()
return
}
equal := true
line := 0
column := 0
for index, correctChar := range correct {
if index >= len(treeRunes) {
test.Log (
"parsed is too short at line", line + 1,
"col", column + 1)
test.Fail()
return
}
if correctChar != treeRunes[index] {
test.Log (
"trees not equal at line", line + 1,
"col", column + 1)
test.Log("correct: [" + string(correctChar) + "]")
test.Log("got: [" + string(treeRunes[index]) + "]")
test.Fail()
return
}
if correctChar == '\n' {
line ++
column = 0
} else {
column ++
}
}
if len(treeString) > len(correct) {
test.Log("parsed is too long")
test.Fail()
return
}
if !equal {
return
}
}
func logWithLineNumbers (bigString string, test *testing.T) {
lines := strings.Split (
strings.Replace(bigString, "\t", " ", -1), "\n")
for index, line := range lines {
test.Logf("%3d | %s", index + 1, line)
}
}