aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/proto_test.go
blob: 7ed2266d3d281d25834e59aaf9a3550a1e3b6fe0 (plain) (blame)
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
package proto

import (
	"bytes"
	"encoding/binary"
	"os"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/ClickHouse/ch-go/internal/gold"
)

func TestMain(m *testing.M) {
	// Explicitly registering flags for golden files.
	gold.Init()

	os.Exit(m.Run())
}

type staticAware struct {
	AwareDecoder
}

func (s staticAware) Decode(r *Reader) error {
	return s.AwareDecoder.DecodeAware(r, Version)
}

func aware(v AwareDecoder) Decoder {
	return staticAware{AwareDecoder: v}
}

type columnAware struct {
	Column
	rows int
}

func (c columnAware) Decode(r *Reader) error {
	return c.DecodeColumn(r, c.rows)
}

func colAware(v Column, rows int) Decoder {
	return columnAware{
		Column: v,
		rows:   rows,
	}
}

func requireNoShortRead(t testing.TB, buf []byte, v Decoder) {
	t.Helper()

	for i := 0; i < len(buf); i++ {
		b := buf[:i]
		r := NewReader(bytes.NewReader(b))
		require.Error(t, v.Decode(r), "decode on short buffer should fail")
	}
}

func skipCode(t testing.TB, buf []byte, code int) []byte {
	t.Helper()

	v, n := binary.Uvarint(buf)
	if int(v) != code {
		t.Fatalf("code mismatch: %d (got) != %d (expected)", v, code)
	}

	return buf[n:]
}

func requireDecode(t testing.TB, buf []byte, v Decoder) {
	t.Helper()

	r := NewReader(bytes.NewReader(buf))
	require.NoError(t, v.Decode(r))
}