main: add tests
Add testing infrastructure and one initial test (for src/runtime/print.go). More tests to be added later.
Этот коммит содержится в:
родитель
ed227b8fd3
коммит
d8f0ddf3fa
3 изменённых файлов: 150 добавлений и 0 удалений
91
main_test.go
Обычный файл
91
main_test.go
Обычный файл
|
@ -0,0 +1,91 @@
|
|||
package main
|
||||
|
||||
// This file tests the compiler by running Go files in testdata/*.go and
|
||||
// comparing their output with the expected output in testdata/*.txt.
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const TESTDATA = "testdata"
|
||||
|
||||
func TestCompiler(t *testing.T) {
|
||||
matches, err := filepath.Glob(TESTDATA + "/*.go")
|
||||
if err != nil {
|
||||
t.Fatal("could not read test files:", err)
|
||||
}
|
||||
if len(matches) == 0 {
|
||||
t.Fatal("no test files found")
|
||||
}
|
||||
|
||||
// Create a temporary directory for test output files.
|
||||
tmpdir, err := ioutil.TempDir("", "tinygo-test")
|
||||
if err != nil {
|
||||
t.Fatal("could not create temporary directory:", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpdir)
|
||||
|
||||
for _, path := range matches {
|
||||
t.Run(path, func(t *testing.T) {
|
||||
runTest(path, tmpdir, t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func runTest(path, tmpdir string, t *testing.T) {
|
||||
// Get the expected output for this test.
|
||||
txtpath := path[:len(path)-3] + ".txt"
|
||||
f, err := os.Open(txtpath)
|
||||
if err != nil {
|
||||
t.Fatal("could not open expected output file:", err)
|
||||
}
|
||||
expected, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
t.Fatal("could not read expected output file:", err)
|
||||
}
|
||||
|
||||
// Build the test binary.
|
||||
binary := filepath.Join(tmpdir, "test")
|
||||
err = Build(path, binary, "", false, false, "")
|
||||
if err != nil {
|
||||
t.Log("failed to build:", err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Run the test.
|
||||
cmd := exec.Command(binary)
|
||||
stdout := &bytes.Buffer{}
|
||||
cmd.Stdout = stdout
|
||||
err = cmd.Run()
|
||||
|
||||
// putchar() prints CRLF, convert it to LF.
|
||||
actual := bytes.Replace(stdout.Bytes(), []byte{'\r', '\n'}, []byte{'\n'}, -1)
|
||||
|
||||
// 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.Log("output did not match")
|
||||
fail = true
|
||||
}
|
||||
|
||||
if fail {
|
||||
r := bufio.NewReader(bytes.NewReader(actual))
|
||||
for {
|
||||
line, err := r.ReadString('\n')
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
t.Log("stdout:", line[:len(line)-1])
|
||||
}
|
||||
t.Fail()
|
||||
}
|
||||
}
|
39
testdata/print.go
предоставленный
Обычный файл
39
testdata/print.go
предоставленный
Обычный файл
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
|
||||
func main() {
|
||||
// test basic printing
|
||||
println("hello world!")
|
||||
println(42)
|
||||
println(100000000)
|
||||
|
||||
// check that this one doesn't print an extra space between args
|
||||
print("a", "b", "c")
|
||||
println()
|
||||
// ..but this one does
|
||||
println("a", "b", "c")
|
||||
|
||||
// print integers
|
||||
println(uint8(123))
|
||||
println(int8(123))
|
||||
println(int8(-123))
|
||||
println(uint16(12345))
|
||||
println(int16(12345))
|
||||
println(int16(-12345))
|
||||
println(uint32(12345678))
|
||||
println(int32(12345678))
|
||||
println(int32(-12345678))
|
||||
println(uint64(123456789012))
|
||||
println(int64(123456789012))
|
||||
println(int64(-123456789012))
|
||||
|
||||
// print float64
|
||||
println(3.14)
|
||||
|
||||
// print map
|
||||
println(map[string]int{"three": 3, "five": 5})
|
||||
|
||||
// TODO: print pointer
|
||||
|
||||
// print bool
|
||||
println(true, false)
|
||||
}
|
20
testdata/print.txt
предоставленный
Обычный файл
20
testdata/print.txt
предоставленный
Обычный файл
|
@ -0,0 +1,20 @@
|
|||
hello world!
|
||||
42
|
||||
100000000
|
||||
abc
|
||||
a b c
|
||||
123
|
||||
123
|
||||
-123
|
||||
12345
|
||||
12345
|
||||
-12345
|
||||
12345678
|
||||
12345678
|
||||
-12345678
|
||||
123456789012
|
||||
123456789012
|
||||
-123456789012
|
||||
+3.140000e+000
|
||||
map[2]
|
||||
true false
|
Загрузка…
Создание таблицы
Сослаться в новой задаче