-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert_test.go
75 lines (57 loc) · 2.26 KB
/
assert_test.go
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
package protocmp
import (
"io/ioutil"
"os"
"strings"
"testing"
"github.com/nbaztec/protocmp/protos/sample"
)
func TestAssertEqual(t *testing.T) {
actual := makeInput(func(v *sample.Outer) {})
AssertEqual(t, makeInput(nil), actual)
AssertEqual(t, actual, makeInput(nil))
}
func TestAssertEqualNil(t *testing.T) {
AssertEqual(t, nil, nil)
}
func TestAssertEqualFails(t *testing.T) {
old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w
AssertEqual(&testingT{}, nil, makeInput(nil))
_ = w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = old
expectedOutput := `TestXYZ: assert_test.go:27
Outer: value mismatch
+ <nil>
- <str_val:"foo" int_val:1 bool_val:true double_val:1.1 bytes_val:[1 2] repeated_type:[<id:"1"> <id:"2"> <nil>] map_type:map[A:<id:"AA"> B:<id:"BB"> C:<nil>] enum_type:NOT_OK oneof_string:"1" timestamp_type:<seconds:1598814300> duration_type:<seconds:1> any_type:<type_url:"mytype/v1" value:[5]> repeated_type_simple:[9 10 11] map_type_simple:map[A:20 B:30 C:40] nested_message:<inner:<id:"123">>>`
actualOutput := strings.TrimSpace(string(out))
if expectedOutput != actualOutput {
t.Errorf("output mismatch want\n%s\ngot\n%s", expectedOutput, actualOutput)
}
}
func TestAssertEqualFailsInverse(t *testing.T) {
old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w
AssertEqual(&testingT{}, makeInput(nil), nil)
_ = w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = old
expectedOutput := `TestXYZ: assert_test.go:49
Outer: value mismatch
+ <str_val:"foo" int_val:1 bool_val:true double_val:1.1 bytes_val:[1 2] repeated_type:[<id:"1"> <id:"2"> <nil>] map_type:map[A:<id:"AA"> B:<id:"BB"> C:<nil>] enum_type:NOT_OK oneof_string:"1" timestamp_type:<seconds:1598814300> duration_type:<seconds:1> any_type:<type_url:"mytype/v1" value:[5]> repeated_type_simple:[9 10 11] map_type_simple:map[A:20 B:30 C:40] nested_message:<inner:<id:"123">>>
- <nil>`
actualOutput := strings.TrimSpace(string(out))
if expectedOutput != actualOutput {
t.Errorf("output mismatch want\n%s\ngot\n%s", expectedOutput, actualOutput)
}
}
type testingT struct {
}
func (t *testingT) Name() string {
return "TestXYZ"
}
func (t *testingT) Fail() {
}