File: functest_test.go

package info (click to toggle)
golang-go4 0.0~git20190313.94abd69-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 760 kB
  • sloc: asm: 20; makefile: 4
file content (137 lines) | stat: -rw-r--r-- 3,029 bytes parent folder | download | duplicates (4)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package functest

import (
	"bytes"
	"fmt"
	"testing"
)

// trec is a testing.TB which logs Errorf calls to buf
type trec struct {
	testing.TB // crash on unimplemented methods
	buf        bytes.Buffer
}

func (t *trec) Errorf(format string, args ...interface{}) {
	t.buf.WriteString("ERR: ")
	fmt.Fprintf(&t.buf, format, args...)
	t.buf.WriteByte('\n')
}

func (t *trec) Logf(format string, args ...interface{}) {
	t.buf.WriteString("LOG: ")
	fmt.Fprintf(&t.buf, format, args...)
	t.buf.WriteByte('\n')
}

func (t *trec) String() string { return t.buf.String() }

func add(a, b int) int { return a + b }

func TestBasic(t *testing.T) {
	f := New(add)
	trec := new(trec)
	f.Test(trec,
		f.In(1, 2).Want(3),
		f.In(5, 6).Want(100),
		f.Case("also wrong").In(5, 6).Want(101),
	)
	want := `ERR: add(5, 6) = 11; want 100
ERR: also wrong: add(5, 6) = 11; want 101
`
	if got := trec.String(); got != want {
		t.Errorf("Output mismatch.\nGot:\n%v\nWant:\n%v\n", got, want)
	}
}

func TestBasic_Strings(t *testing.T) {
	concat := func(a, b string) string { return a + b }
	f := New(concat)
	f.Name = "concat"
	trec := new(trec)
	f.Test(trec,
		f.In("a", "b").Want("ab"),
		f.In("a", "b\x00").Want("ab"),
	)
	want := `ERR: concat("a", "b\x00") = "ab\x00"; want "ab"
`
	if got := trec.String(); got != want {
		t.Errorf("Output mismatch.\nGot:\n%v\nWant:\n%v\n", got, want)
	}
}

func TestVariadic(t *testing.T) {
	sumVar := func(vals ...int) (sum int) {
		for _, v := range vals {
			sum += v
		}
		return
	}

	f := New(sumVar)
	f.Name = "sumVar"
	trec := new(trec)
	f.Test(trec,
		f.In().Want(0),
		f.In().Want(100),
		f.In(1).Want(1),
		f.In(1).Want(100),
		f.In(1, 2).Want(3),
		f.In(1, 2, 3).Want(6),
		f.In(1, 2, 3).Want(100),
	)
	want := `ERR: sumVar() = 0; want 100
ERR: sumVar(1) = 1; want 100
ERR: sumVar(1, 2, 3) = 6; want 100
`
	if got := trec.String(); got != want {
		t.Errorf("Output mismatch.\nGot:\n%v\nWant:\n%v\n", got, want)
	}
}

func condPanic(doPanic bool, panicValue interface{}) {
	if doPanic {
		panic(panicValue)
	}
}

func TestPanic(t *testing.T) {
	f := New(condPanic)
	f.Name = "condPanic"
	trec := new(trec)
	f.Test(trec,
		f.In(false, nil),
		f.In(true, "boom").Check(func(res Result) error {
			trec.Logf("Got res: %+v", res)
			if res.Panic != "boom" {
				return fmt.Errorf("panic = %v; want boom", res.Panic)
			}
			return nil
		}),
		f.Case("panic with nil").In(true, nil),
	)
	want := `LOG: Got res: {Result:[] Panic:boom Panicked:true}
ERR: panic with nil: condPanic(true, <nil>): panicked with <nil>
`
	if got := trec.String(); got != want {
		t.Errorf("Output mismatch.\nGot:\n%v\nWant:\n%v\n", got, want)
	}
}

func TestName_AutoFunc(t *testing.T) {
	testName(t, New(add), "add")
}

type SomeType struct{}

func (t *SomeType) SomeMethod(int) int { return 123 }

func TestName_AutoMethod(t *testing.T) {
	testName(t, New((*SomeType).SomeMethod), "SomeType.SomeMethod")
}

func testName(t *testing.T, f *Func, want string) {
	if f.Name != want {
		t.Errorf("name = %q; want %q", f.Name, want)
	}
}