-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
integrationutil_test.go
110 lines (100 loc) · 2.64 KB
/
integrationutil_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
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
package capnp_test
import (
"bytes"
"encoding/hex"
"fmt"
"path/filepath"
"runtime"
"testing"
"capnproto.org/go/capnp/v3"
air "capnproto.org/go/capnp/v3/internal/aircraftlib"
"capnproto.org/go/capnp/v3/internal/capnptool"
)
const schemaPath = "internal/aircraftlib/aircraft.capnp"
func initNester(t *testing.T, n air.Nester1Capn, strs ...string) {
tl, err := n.NewStrs(int32(len(strs)))
if err != nil {
t.Fatalf("initNester(..., %q): NewStrs: %v", strs, err)
}
for i, s := range strs {
if err := tl.Set(i, s); err != nil {
t.Fatalf("initNester(..., %q): set strs[%d]: %v", strs, i, err)
}
}
}
func zdateFilledMessage(t testing.TB, n int32) *capnp.Message {
msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
if err != nil {
t.Fatal(err)
}
z, err := air.NewRootZ(seg)
if err != nil {
t.Fatal(err)
}
list, err := z.NewZdatevec(n)
if err != nil {
t.Fatal(err)
}
for i := 0; i < int(n); i++ {
d, err := air.NewZdate(seg)
if err != nil {
t.Fatal(err)
}
d.SetMonth(12)
d.SetDay(7)
d.SetYear(int16(2004 + i))
list.Set(i, d)
}
return msg
}
func zdataFilledMessage(t testing.TB, n int) *capnp.Message {
msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
if err != nil {
t.Fatal(err)
}
z, err := air.NewRootZ(seg)
if err != nil {
t.Fatal(err)
}
d, err := air.NewZdata(seg)
if err != nil {
t.Fatal(err)
}
b := make([]byte, n)
for i := 0; i < len(b); i++ {
b[i] = byte(i)
}
d.SetData(b)
z.SetZdata(d)
return msg
}
// encodeTestMessage encodes the textual Cap'n Proto message to unpacked
// binary using the capnp tool, or returns the fallback if the tool fails.
func encodeTestMessage(t testing.TB, typ string, text string, fallback []byte) ([]byte, error) {
tool, err := capnptool.Find()
if err != nil {
t.Log("capnp tool not found; using fallback")
return fallback, nil
}
b, err := tool.Encode(capnptool.Type{SchemaPath: schemaPath, Name: typ}, text)
if err != nil {
return nil, fmt.Errorf("%s value %q encode failed: %v", typ, text, err)
}
if !bytes.Equal(b, fallback) {
return nil, fmt.Errorf("%s value %q =\n%s; fallback is\n%s\nFallback out of date?", typ, text, hex.Dump(b), hex.Dump(fallback))
}
return b, nil
}
// mustEncodeTestMessage encodes the textual Cap'n Proto message to unpacked
// binary using the capnp tool, or returns the fallback if the tool fails.
func mustEncodeTestMessage(t testing.TB, typ string, text string, fallback []byte) []byte {
b, err := encodeTestMessage(t, typ, text, fallback)
if err != nil {
if _, fname, line, ok := runtime.Caller(1); ok {
t.Fatalf("%s:%d: %v", filepath.Base(fname), line, err)
} else {
t.Fatal(err)
}
}
return b
}