summaryrefslogtreecommitdiff
path: root/methods_test.go
blob: 46e2601d0b5afc0b3703513032dca7ed86d52180 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package lowmemjson_test

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"

	"git.lukeshu.com/go/lowmemjson"
)

type ShortSum string

func (s ShortSum) EncodeJSON(w io.Writer) error {
	// Test that it's OK to call lowmemjson.Encoder.Encode for the top-level value in a method.
	return lowmemjson.NewEncoder(w).Encode(string(s))
}

type SumRun struct {
	ChecksumSize int   `json:",omitempty"`
	Addr         int64 `json:",omitempty"`
	Sums         ShortSum
}

type SumRunWithGaps struct {
	Addr int64
	Size int64
	Runs []SumRun
}

func (sg SumRunWithGaps) EncodeJSON(w io.Writer) error {
	if _, err := fmt.Fprintf(w, `{"Addr":%d,"Size":%d,"Runs":[`, sg.Addr, sg.Size); err != nil {
		return err
	}
	cur := sg.Addr
	for i, run := range sg.Runs {
		if i > 0 {
			if _, err := w.Write([]byte{','}); err != nil {
				return err
			}
		}
		if run.Addr > cur {
			if _, err := fmt.Fprintf(w, `{"Gap":%d},`, run.Addr-cur); err != nil {
				return err
			}
		}
		if err := lowmemjson.NewEncoder(w).Encode(run); err != nil {
			return err
		}
	}
	end := sg.Addr + sg.Size
	switch {
	case end < cur:
		return fmt.Errorf("invalid %T: addr went backwards: %v < %v", sg, end, cur)
	case end > cur:
		if _, err := fmt.Fprintf(w, `,{"Gap":%d}`, end-cur); err != nil {
			return err
		}
	}
	if _, err := w.Write([]byte("]}")); err != nil {
		return err
	}
	return nil
}

func (sg *SumRunWithGaps) DecodeJSON(r io.RuneScanner) error {
	*sg = SumRunWithGaps{}
	var name string
	return lowmemjson.DecodeObject(r,
		func(r io.RuneScanner) error {
			return lowmemjson.NewDecoder(r).Decode(&name)
		},
		func(r io.RuneScanner) error {
			switch name {
			case "Addr":
				return lowmemjson.NewDecoder(r).Decode(&sg.Addr)
			case "Size":
				return lowmemjson.NewDecoder(r).Decode(&sg.Size)
			case "Runs":
				return lowmemjson.DecodeArray(r, func(r io.RuneScanner) error {
					var run SumRun
					if err := lowmemjson.NewDecoder(r).Decode(&run); err != nil {
						return err
					}
					if run.ChecksumSize > 0 {
						sg.Runs = append(sg.Runs, run)
					}
					return nil
				})
			default:
				return fmt.Errorf("unknown key %q", name)
			}
		})
}

func TestMethods(t *testing.T) {
	t.Parallel()
	in := SumRunWithGaps{
		Addr: 13631488,
		Size: 416033783808,
		Runs: []SumRun{
			{
				ChecksumSize: 4,
				Addr:         1095761920,
				Sums:         "c160817cb5c72bbbe",
			},
		},
	}
	var buf bytes.Buffer
	assert.NoError(t, lowmemjson.NewEncoder(&buf).Encode(in))
	assert.Equal(t,
		`{"Addr":13631488,"Size":416033783808,"Runs":[{"Gap":1082130432},{"ChecksumSize":4,"Addr":1095761920,"Sums":"c160817cb5c72bbbe"},{"Gap":416033783808}]}`,
		buf.String())
	var out SumRunWithGaps
	assert.NoError(t, lowmemjson.NewDecoder(&buf).Decode(&out))
	assert.Equal(t, in, out)
}

type strEncoder string

func (s strEncoder) EncodeJSON(w io.Writer) error {
	_, err := io.WriteString(w, string(s))
	return err
}

type strMarshaler string

func (s strMarshaler) MarshalJSON() ([]byte, error) {
	return []byte(s), nil
}

type strTextMarshaler struct {
	str string
	err string
}

func (m strTextMarshaler) MarshalText() (txt []byte, err error) {
	if len(m.str) > 0 {
		txt = []byte(m.str)
	}
	if len(m.err) > 0 {
		err = errors.New(m.err)
	}
	return
}

func TestMethodsEncode(t *testing.T) {
	t.Parallel()
	type testcase struct {
		In          string
		ExpectedErr string
	}
	testcases := map[string]testcase{
		"basic": {In: `{}`},
		"empty": {In: ``, ExpectedErr: `syntax error at input byte 0: EOF`},
		"short": {In: `{`, ExpectedErr: `syntax error at input byte 1: unexpected EOF`},
		"long":  {In: `{}{}`, ExpectedErr: `syntax error at input byte 2: invalid character '{' after top-level value`},
	}
	t.Run("encodable", func(t *testing.T) {
		t.Parallel()
		for tcName, tc := range testcases {
			tc := tc
			t.Run(tcName, func(t *testing.T) {
				t.Parallel()
				var buf strings.Builder
				err := lowmemjson.NewEncoder(&buf).Encode([]any{strEncoder(tc.In)})
				if tc.ExpectedErr == "" {
					assert.NoError(t, err)
					assert.Equal(t, "["+tc.In+"]", buf.String())
				} else {
					assert.EqualError(t, err,
						`json: error calling EncodeJSON for type lowmemjson_test.strEncoder: `+
							tc.ExpectedErr)
				}
			})
		}
	})
	t.Run("marshaler", func(t *testing.T) {
		t.Parallel()
		for tcName, tc := range testcases {
			tc := tc
			t.Run(tcName, func(t *testing.T) {
				t.Parallel()
				var buf strings.Builder
				err := lowmemjson.NewEncoder(&buf).Encode([]any{strMarshaler(tc.In)})
				if tc.ExpectedErr == "" {
					assert.NoError(t, err)
					assert.Equal(t, "["+tc.In+"]", buf.String())
				} else {
					assert.EqualError(t, err,
						`json: error calling MarshalJSON for type lowmemjson_test.strMarshaler: `+
							tc.ExpectedErr)
				}
			})
		}
	})
	t.Run("text", func(t *testing.T) {
		t.Parallel()
		type testcase struct {
			Str string
			Err string
		}
		testcases := map[string]testcase{
			"basic": {Str: `a`},
			"err":   {Err: `xxx`},
			"both":  {Str: `a`, Err: `xxx`},
		}
		for tcName, tc := range testcases {
			tc := tc
			t.Run(tcName, func(t *testing.T) {
				t.Parallel()
				var buf strings.Builder
				err := lowmemjson.NewEncoder(&buf).Encode([]any{strTextMarshaler{str: tc.Str, err: tc.Err}})
				if tc.Err == "" {
					assert.NoError(t, err)
					assert.Equal(t, `["`+tc.Str+`"]`, buf.String())
				} else {
					assert.EqualError(t, err,
						`json: error calling MarshalText for type lowmemjson_test.strTextMarshaler: `+
							tc.Err)
					assert.Equal(t, "[", buf.String())
				}
			})
		}
	})
}