Repository navigation
Expand file tree
/
Copy pathjson_golden_test.go
More file actions
58 lines (51 loc) · 1.75 KB
/
Copy pathjson_golden_test.go
File metadata and controls
58 lines (51 loc) · 1.75 KB
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
package protocol
import (
"bufio"
"bytes"
"encoding/json"
"os"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/centrifugal/protocol/cfjson"
)
// testdata/json_golden.txt holds messages encoded by the easyjson based
// encoders this package had up to v0.22, one per line. Client SDKs were
// written against that output, so the encoders must keep producing it byte
// for byte.
//
// The file is generated (and kept honest) by internal/cfjsoncmp, the module
// which still has the easyjson code. Here a message is first decoded with
// encoding/json, which shares nothing with the code under test, and then
// encoded again.
func TestJSONGolden(t *testing.T) {
f, err := os.Open("testdata/json_golden.txt")
require.NoError(t, err)
defer func() { _ = f.Close() }()
scanner := bufio.NewScanner(f)
scanner.Buffer(nil, 1<<20)
lines := 0
for scanner.Scan() {
lines++
name, golden, ok := strings.Cut(scanner.Text(), "\t")
require.True(t, ok)
newMessage, ok := messageTypes[name]
require.True(t, ok, name)
msg := newMessage()
require.NoError(t, json.Unmarshal([]byte(golden), msg))
require.Equal(t, golden, string(msg.AppendJSON(nil)), name)
require.Equal(t, golden, string(encodeJSON(msg)), name)
// The decoders must make the same message of it as encoding/json.
data := []byte(golden)
for _, flags := range []cfjson.Flags{0, cfjson.Prescan(data, 0)} {
decoded := newMessage()
n := decoded.DecodeJSON(data, 0, flags)
require.Equal(t, len(data), n, "%s: %s", name, golden)
require.True(t, reflect.DeepEqual(msg, decoded), "%s: %s", name, golden)
require.True(t, bytes.Equal(data, []byte(golden)), "decoding modified its input")
}
}
require.NoError(t, scanner.Err())
require.Greater(t, lines, 1000)
}