implement-modules #43
@ -5,6 +5,7 @@ import "testing"
|
||||
import "strings"
|
||||
import "git.tebibyte.media/sashakoshka/fspl/parser"
|
||||
import "git.tebibyte.media/sashakoshka/fspl/analyzer"
|
||||
import "git.tebibyte.media/sashakoshka/fspl/testcommon"
|
||||
|
||||
func testString (test *testing.T, correct string, input string) {
|
||||
ast := parser.Tree { }
|
||||
@ -36,41 +37,10 @@ func testString (test *testing.T, correct string, input string) {
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
got := output.String()
|
||||
if got != correct {
|
||||
test.Logf("results do not match")
|
||||
|
||||
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(correct) { left = correct[index] }
|
||||
if index < len(got) { right = got[index] }
|
||||
|
||||
if left != right {
|
||||
printColumnsStyle(left, right, "31m")
|
||||
} else {
|
||||
printColumns(left, right)
|
||||
}
|
||||
|
||||
}
|
||||
testcommon.Compare(test, correct, got)
|
||||
test.Log("SOURCE FSPL CODE:")
|
||||
test.Log("\033[32m" + input + "\033[0m")
|
||||
test.Fail()
|
||||
|
54
testcommon/common.go
Normal file
54
testcommon/common.go
Normal file
@ -0,0 +1,54 @@
|
||||
package testcommon
|
||||
|
||||
import "testing"
|
||||
import "strings"
|
||||
|
||||
// LogColumns logs columns of text side by side, each column 80 characters wide.
|
||||
func LogColumns (test *testing.T, columns ...any) {
|
||||
LogColumnsStyle(test, "0m", columns...)
|
||||
}
|
||||
|
||||
// LogColumnsStyle is like LogColumns, but accepts an ANSI escape code as an
|
||||
// argument (without the \033[). For example, passing 31m causes the text to be
|
||||
// red.
|
||||
func LogColumnsStyle (test *testing.T, style string, columns ...any) {
|
||||
formatString := ""
|
||||
for index := range columns {
|
||||
if index > 0 { formatString += " | " }
|
||||
|
||||
if index == len(columns) {
|
||||
formatString += "%v"
|
||||
} else {
|
||||
formatString += "%-80v"
|
||||
}
|
||||
}
|
||||
test.Logf("\033[" + style + formatString + "\033[0m", columns...)
|
||||
}
|
||||
|
||||
// Compare compares a correct string with a result string and produces formatted
|
||||
// output showing them side-by-side with their differences highlighted.
|
||||
func Compare (test *testing.T, correct, got string) {
|
||||
got = strings.ReplaceAll(got, "\t", " ")
|
||||
correct = strings.ReplaceAll(correct, "\t", " ")
|
||||
|
||||
correctLines := strings.Split(correct, "\n")
|
||||
gotLines := strings.Split(got, "\n")
|
||||
length := len(gotLines)
|
||||
if len(correctLines) > len(gotLines) { length = len(correctLines) }
|
||||
|
||||
LogColumns(test, "CORRECT:", "GOT:")
|
||||
test.Log()
|
||||
for index := 0; index < length; index ++ {
|
||||
left := ""
|
||||
right := ""
|
||||
if index < len(correctLines) { left = correctLines[index] }
|
||||
if index < len(gotLines) { right = gotLines[index] }
|
||||
|
||||
if left != right {
|
||||
LogColumnsStyle(test, "31m", left, right)
|
||||
} else {
|
||||
LogColumns(test, left, right)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user