Browse Source

main_test: refactor output comparison into separate function

This shouldn't affect anything, just make the code a bit better
(especially for the next commit).
pull/4523/head
Ayke van Laethem 1 month ago
committed by Ron Evans
parent
commit
6016d0c739
  1. 50
      main_test.go

50
main_test.go

@ -396,17 +396,13 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
// of the path.
path := TESTDATA + "/" + name
// Get the expected output for this test.
txtpath := path[:len(path)-3] + ".txt"
expectedOutputPath := path[:len(path)-3] + ".txt"
pkgName := "./" + path
if path[len(path)-1] == '/' {
txtpath = path + "out.txt"
expectedOutputPath = path + "out.txt"
options.Directory = path
pkgName = "."
}
expected, err := os.ReadFile(txtpath)
if err != nil {
t.Fatal("could not read expected output file:", err)
}
config, err := builder.NewConfig(&options)
if err != nil {
@ -428,10 +424,7 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
return
}
// putchar() prints CRLF, convert it to LF.
actual := bytes.Replace(stdout.Bytes(), []byte{'\r', '\n'}, []byte{'\n'}, -1)
expected = bytes.Replace(expected, []byte{'\r', '\n'}, []byte{'\n'}, -1) // for Windows
actual := stdout.Bytes()
if config.EmulatorName() == "simavr" {
// Strip simavr log formatting.
actual = bytes.Replace(actual, []byte{0x1b, '[', '3', '2', 'm'}, nil, -1)
@ -446,17 +439,12 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
}
// Check whether the command ran successfully.
fail := false
if err != nil {
t.Log("failed to run:", err)
fail = true
} else if !bytes.Equal(expected, actual) {
t.Logf("output did not match (expected %d bytes, got %d bytes):", len(expected), len(actual))
t.Logf(string(Diff("expected", expected, "actual", actual)))
fail = true
t.Error("failed to run:", err)
}
checkOutput(t, expectedOutputPath, actual)
if fail {
if t.Failed() {
r := bufio.NewReader(bytes.NewReader(actual))
for {
line, err := r.ReadString('\n')
@ -696,21 +684,27 @@ func TestWasmExport(t *testing.T) {
// Check that the output matches the expected output.
// (Skip this for wasm-unknown because it can't produce output).
if !tc.noOutput {
expectedOutput, err := os.ReadFile("testdata/wasmexport.txt")
if err != nil {
t.Fatal("could not read output file:", err)
}
actual := output.Bytes()
expectedOutput = bytes.ReplaceAll(expectedOutput, []byte("\r\n"), []byte("\n"))
actual = bytes.ReplaceAll(actual, []byte("\r\n"), []byte("\n"))
if !bytes.Equal(actual, expectedOutput) {
t.Error(string(Diff("expected", expectedOutput, "actual", actual)))
}
checkOutput(t, "testdata/wasmexport.txt", output.Bytes())
}
})
}
}
// Check whether the output of a test equals the expected output.
func checkOutput(t *testing.T, filename string, actual []byte) {
expectedOutput, err := os.ReadFile(filename)
if err != nil {
t.Fatal("could not read output file:", err)
}
expectedOutput = bytes.ReplaceAll(expectedOutput, []byte("\r\n"), []byte("\n"))
actual = bytes.ReplaceAll(actual, []byte("\r\n"), []byte("\n"))
if !bytes.Equal(actual, expectedOutput) {
t.Errorf("output did not match (expected %d bytes, got %d bytes):", len(expectedOutput), len(actual))
t.Error(string(Diff("expected", expectedOutput, "actual", actual)))
}
}
func TestTest(t *testing.T) {
t.Parallel()

Loading…
Cancel
Save