summaryrefslogtreecommitdiff
path: root/methods_test.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@datawire.io>2022-08-21 21:39:59 -0600
committerLuke Shumaker <lukeshu@datawire.io>2022-08-21 21:39:59 -0600
commit325838f35ce90080aa6c892a998d960c06c1c144 (patch)
tree60e87ee9a622a0261b93ef1e47dbca632c588276 /methods_test.go
parentd25456172946e5921747cd57fb04eb5b6da72fb6 (diff)
Add tests for the actual usability of the Decodable and Encodable interfaces
Diffstat (limited to 'methods_test.go')
-rw-r--r--methods_test.go115
1 files changed, 115 insertions, 0 deletions
diff --git a/methods_test.go b/methods_test.go
new file mode 100644
index 0000000..8280a94
--- /dev/null
+++ b/methods_test.go
@@ -0,0 +1,115 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package lowmemjson_test
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+
+ "git.lukeshu.com/go/lowmemjson"
+)
+
+type SumRun struct {
+ ChecksumSize int `json:",omitempty"`
+ Addr int64 `json:",omitempty"`
+ Sums string
+}
+
+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.Encode(w, 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.Decode(r, &name)
+ },
+ func(r io.RuneScanner) error {
+ switch name {
+ case "Addr":
+ return lowmemjson.Decode(r, &sg.Addr)
+ case "Size":
+ return lowmemjson.Decode(r, &sg.Size)
+ case "Runs":
+ return lowmemjson.DecodeArray(r, func(r io.RuneScanner) error {
+ var run SumRun
+ if err := lowmemjson.Decode(r, &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) {
+ in := SumRunWithGaps{
+ Addr: 13631488,
+ Size: 416033783808,
+ Runs: []SumRun{
+ {
+ ChecksumSize: 4,
+ Addr: 1095761920,
+ Sums: "c160817cb5c72bbbe",
+ },
+ },
+ }
+ var buf bytes.Buffer
+ assert.NoError(t, lowmemjson.Encode(&buf, 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.Decode(&buf, &out))
+ assert.Equal(t, in, out)
+}