Skip to content

Commit 6016d0c

Browse files
aykevldeadprogram
authored andcommitted
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).
1 parent 07d23c9 commit 6016d0c

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

main_test.go

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -396,17 +396,13 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
396396
// of the path.
397397
path := TESTDATA + "/" + name
398398
// Get the expected output for this test.
399-
txtpath := path[:len(path)-3] + ".txt"
399+
expectedOutputPath := path[:len(path)-3] + ".txt"
400400
pkgName := "./" + path
401401
if path[len(path)-1] == '/' {
402-
txtpath = path + "out.txt"
402+
expectedOutputPath = path + "out.txt"
403403
options.Directory = path
404404
pkgName = "."
405405
}
406-
expected, err := os.ReadFile(txtpath)
407-
if err != nil {
408-
t.Fatal("could not read expected output file:", err)
409-
}
410406

411407
config, err := builder.NewConfig(&options)
412408
if err != nil {
@@ -428,10 +424,7 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
428424
return
429425
}
430426

431-
// putchar() prints CRLF, convert it to LF.
432-
actual := bytes.Replace(stdout.Bytes(), []byte{'\r', '\n'}, []byte{'\n'}, -1)
433-
expected = bytes.Replace(expected, []byte{'\r', '\n'}, []byte{'\n'}, -1) // for Windows
434-
427+
actual := stdout.Bytes()
435428
if config.EmulatorName() == "simavr" {
436429
// Strip simavr log formatting.
437430
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
446439
}
447440

448441
// Check whether the command ran successfully.
449-
fail := false
450442
if err != nil {
451-
t.Log("failed to run:", err)
452-
fail = true
453-
} else if !bytes.Equal(expected, actual) {
454-
t.Logf("output did not match (expected %d bytes, got %d bytes):", len(expected), len(actual))
455-
t.Logf(string(Diff("expected", expected, "actual", actual)))
456-
fail = true
443+
t.Error("failed to run:", err)
457444
}
445+
checkOutput(t, expectedOutputPath, actual)
458446

459-
if fail {
447+
if t.Failed() {
460448
r := bufio.NewReader(bytes.NewReader(actual))
461449
for {
462450
line, err := r.ReadString('\n')
@@ -696,21 +684,27 @@ func TestWasmExport(t *testing.T) {
696684
// Check that the output matches the expected output.
697685
// (Skip this for wasm-unknown because it can't produce output).
698686
if !tc.noOutput {
699-
expectedOutput, err := os.ReadFile("testdata/wasmexport.txt")
700-
if err != nil {
701-
t.Fatal("could not read output file:", err)
702-
}
703-
actual := output.Bytes()
704-
expectedOutput = bytes.ReplaceAll(expectedOutput, []byte("\r\n"), []byte("\n"))
705-
actual = bytes.ReplaceAll(actual, []byte("\r\n"), []byte("\n"))
706-
if !bytes.Equal(actual, expectedOutput) {
707-
t.Error(string(Diff("expected", expectedOutput, "actual", actual)))
708-
}
687+
checkOutput(t, "testdata/wasmexport.txt", output.Bytes())
709688
}
710689
})
711690
}
712691
}
713692

693+
// Check whether the output of a test equals the expected output.
694+
func checkOutput(t *testing.T, filename string, actual []byte) {
695+
expectedOutput, err := os.ReadFile(filename)
696+
if err != nil {
697+
t.Fatal("could not read output file:", err)
698+
}
699+
expectedOutput = bytes.ReplaceAll(expectedOutput, []byte("\r\n"), []byte("\n"))
700+
actual = bytes.ReplaceAll(actual, []byte("\r\n"), []byte("\n"))
701+
702+
if !bytes.Equal(actual, expectedOutput) {
703+
t.Errorf("output did not match (expected %d bytes, got %d bytes):", len(expectedOutput), len(actual))
704+
t.Error(string(Diff("expected", expectedOutput, "actual", actual)))
705+
}
706+
}
707+
714708
func TestTest(t *testing.T) {
715709
t.Parallel()
716710

0 commit comments

Comments
 (0)