aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/testing/testing.go
blob: 1d7ea051dde50fc28f18cbd2f2c38d182c0d7e2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
}