Skip to content

Commit

Permalink
main_test: refactor output comparison into separate function
Browse files Browse the repository at this point in the history
This shouldn't affect anything, just make the code a bit better
(especially for the next commit).
  • Loading branch information
aykevl committed Oct 3, 2024
1 parent 4720f35 commit 107be65
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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')
Expand Down Expand Up @@ -695,21 +683,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()

Expand Down

0 comments on commit 107be65

Please sign in to comment.