mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-18 19:44:00 +00:00
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).
This commit is contained in:
committed by
Ron Evans
parent
07d23c9d83
commit
6016d0c739
+22
-28
@@ -396,17 +396,13 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
|
|||||||
// of the path.
|
// of the path.
|
||||||
path := TESTDATA + "/" + name
|
path := TESTDATA + "/" + name
|
||||||
// Get the expected output for this test.
|
// Get the expected output for this test.
|
||||||
txtpath := path[:len(path)-3] + ".txt"
|
expectedOutputPath := path[:len(path)-3] + ".txt"
|
||||||
pkgName := "./" + path
|
pkgName := "./" + path
|
||||||
if path[len(path)-1] == '/' {
|
if path[len(path)-1] == '/' {
|
||||||
txtpath = path + "out.txt"
|
expectedOutputPath = path + "out.txt"
|
||||||
options.Directory = path
|
options.Directory = path
|
||||||
pkgName = "."
|
pkgName = "."
|
||||||
}
|
}
|
||||||
expected, err := os.ReadFile(txtpath)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("could not read expected output file:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
config, err := builder.NewConfig(&options)
|
config, err := builder.NewConfig(&options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -428,10 +424,7 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// putchar() prints CRLF, convert it to LF.
|
actual := stdout.Bytes()
|
||||||
actual := bytes.Replace(stdout.Bytes(), []byte{'\r', '\n'}, []byte{'\n'}, -1)
|
|
||||||
expected = bytes.Replace(expected, []byte{'\r', '\n'}, []byte{'\n'}, -1) // for Windows
|
|
||||||
|
|
||||||
if config.EmulatorName() == "simavr" {
|
if config.EmulatorName() == "simavr" {
|
||||||
// Strip simavr log formatting.
|
// Strip simavr log formatting.
|
||||||
actual = bytes.Replace(actual, []byte{0x1b, '[', '3', '2', 'm'}, nil, -1)
|
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.
|
// Check whether the command ran successfully.
|
||||||
fail := false
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Log("failed to run:", err)
|
t.Error("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
|
|
||||||
}
|
}
|
||||||
|
checkOutput(t, expectedOutputPath, actual)
|
||||||
|
|
||||||
if fail {
|
if t.Failed() {
|
||||||
r := bufio.NewReader(bytes.NewReader(actual))
|
r := bufio.NewReader(bytes.NewReader(actual))
|
||||||
for {
|
for {
|
||||||
line, err := r.ReadString('\n')
|
line, err := r.ReadString('\n')
|
||||||
@@ -696,21 +684,27 @@ func TestWasmExport(t *testing.T) {
|
|||||||
// Check that the output matches the expected output.
|
// Check that the output matches the expected output.
|
||||||
// (Skip this for wasm-unknown because it can't produce output).
|
// (Skip this for wasm-unknown because it can't produce output).
|
||||||
if !tc.noOutput {
|
if !tc.noOutput {
|
||||||
expectedOutput, err := os.ReadFile("testdata/wasmexport.txt")
|
checkOutput(t, "testdata/wasmexport.txt", output.Bytes())
|
||||||
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)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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) {
|
func TestTest(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user