fspl/generator/test-common.go

84 lines
1.9 KiB
Go
Raw Normal View History

2023-11-17 03:04:56 +00:00
package generator
import "io"
import "fmt"
import "testing"
import "strings"
import "git.tebibyte.media/sashakoshka/fspl/parser"
import "git.tebibyte.media/sashakoshka/fspl/analyzer"
func testString (test *testing.T, correct string, input string) {
testReader(test, correct, strings.NewReader(input))
}
func testReader (test *testing.T, correct string, inputs ...io.Reader) {
ast := parser.Tree { }
for index, stream := range inputs {
err := ast.Parse(fmt.Sprintf("stream%d.fspl", index), stream)
if err != nil && err != io.EOF{
2023-11-26 09:02:22 +00:00
test.Error("parser returned error:", err)
2023-11-17 03:04:56 +00:00
return
}
}
tree := analyzer.Tree { }
err := tree.Analyze(ast)
if err != nil {
2023-11-26 09:02:22 +00:00
test.Error("analyzer returned error:", err)
2023-11-17 03:04:56 +00:00
return
}
2023-12-01 06:18:10 +00:00
module, err := (&Target {
WordSize: 64,
Arch: "x86_64",
}).Generate(tree)
2023-11-23 06:26:14 +00:00
if err != nil {
2023-11-26 09:02:22 +00:00
test.Error("generator returned error:", err)
2023-11-23 06:26:14 +00:00
return
}
2023-11-17 03:04:56 +00:00
output := new(strings.Builder)
2023-11-23 06:26:14 +00:00
_, err = module.WriteTo(output)
2023-11-17 03:04:56 +00:00
if err != nil {
2023-11-26 09:02:22 +00:00
test.Error("generator returned error:", err)
2023-11-17 03:04:56 +00:00
return
}
2024-01-27 23:07:49 +00:00
printColumns := func (left, right string) {
test.Logf("%-80v | %v", left, right)
}
printColumnsStyle := func (left, right string, style string) {
test.Logf("\033[%s%-80v - %v\033[0m", style, left, right)
}
2023-11-17 03:04:56 +00:00
got := output.String()
2023-11-23 06:26:14 +00:00
if got != correct {
2023-11-17 03:04:56 +00:00
test.Logf("results do not match")
2024-01-27 23:07:49 +00:00
got = strings.ReplaceAll(got, "\t", " ")
correct = strings.ReplaceAll(correct, "\t", " ")
got := strings.Split(got, "\n")
correct := strings.Split(correct, "\n")
length := len(got)
if len(correct) > len(got) { length = len(correct) }
printColumns("CORRECT:", "GOT:")
test.Log()
for index := 0; index < length; index ++ {
left := ""
right := ""
if index < len(got) { left = got[index] }
if index < len(correct) { right = correct[index] }
if left != right {
printColumnsStyle(left, right, "31m")
} else {
printColumns(left, right)
}
}
2023-11-17 03:04:56 +00:00
test.Fail()
}
}