aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/testing
diff options
context:
space:
mode:
authorCarolyn Van Slyck <[email protected]>2019-06-18 03:23:59 -0700
committerRon Evans <[email protected]>2019-06-18 12:23:59 +0200
commit208e1719ad9a8e7ff94e8aac900909c37747a04f (patch)
tree0ec53ee94cc447f84e6bbfe51ef68ad428bf2bf4 /src/testing
parenta3d1f1a514aaaad6c2bfe6024ee48211cf6b7575 (diff)
downloadtinygo-208e1719ad9a8e7ff94e8aac900909c37747a04f.tar.gz
tinygo-208e1719ad9a8e7ff94e8aac900909c37747a04f.zip
Add test command to tinygo (#243)
* Add test command to tinygo
Diffstat (limited to 'src/testing')
-rw-r--r--src/testing/doc.go6
-rw-r--r--src/testing/testing.go77
2 files changed, 83 insertions, 0 deletions
diff --git a/src/testing/doc.go b/src/testing/doc.go
new file mode 100644
index 000000000..b5026d806
--- /dev/null
+++ b/src/testing/doc.go
@@ -0,0 +1,6 @@
+package testing
+
+/*
+ This is a sad stub of the upstream testing package because it doesn't compile
+ with tinygo right now.
+*/
diff --git a/src/testing/testing.go b/src/testing/testing.go
new file mode 100644
index 000000000..1d7ea051d
--- /dev/null
+++ b/src/testing/testing.go
@@ -0,0 +1,77 @@
+package testing
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "os"
+)
+
+// T is a test helper.
+type T struct {
+ name string
+ output io.Writer
+
+ // flags the test as having failed when non-zero
+ failed int
+}
+
+// TestToCall is a reference to a test that should be called during a test suite run.
+type TestToCall struct {
+ // Name of the test to call.
+ Name string
+ // Function reference to the test.
+ Func func(*T)
+}
+
+// M is a test suite.
+type M struct {
+ // tests is a list of the test names to execute
+ Tests []TestToCall
+}
+
+// Run the test suite.
+func (m *M) Run() int {
+ failures := 0
+ for _, test := range m.Tests {
+ t := &T{
+ name: test.Name,
+ output: &bytes.Buffer{},
+ }
+
+ fmt.Printf("=== RUN %s\n", test.Name)
+ test.Func(t)
+
+ if t.failed == 0 {
+ fmt.Printf("--- PASS: %s\n", test.Name)
+ } else {
+ fmt.Printf("--- FAIL: %s\n", test.Name)
+ }
+ fmt.Println(t.output)
+
+ failures += t.failed
+ }
+
+ if failures > 0 {
+ fmt.Printf("exit status %d\n", failures)
+ fmt.Println("FAIL")
+ }
+ return failures
+}
+
+func TestMain(m *M) {
+ os.Exit(m.Run())
+}
+
+// Error is equivalent to Log followed by Fail
+func (t *T) Error(args ...interface{}) {
+ // This doesn't print the same as in upstream go, but works good enough
+ // TODO: buffer test output like go does
+ fmt.Fprintf(t.output, "\t")
+ fmt.Fprintln(t.output, args...)
+ t.Fail()
+}
+
+func (t *T) Fail() {
+ t.failed = 1
+}