summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@datawire.io>2022-08-16 22:54:13 -0600
committerLuke Shumaker <lukeshu@datawire.io>2022-08-16 22:56:38 -0600
commit87b02577e50b76d373e3c6b921d776e39cb83346 (patch)
tree8a820194fb48f8ee197a763e1a0119ea6a22eaa1
parentf4546a23bf7dddeb082747a362bc5a96f0562f8b (diff)
decode_scan_test.go: Factor out a common test runner
-rw-r--r--decode_scan_test.go93
1 files changed, 41 insertions, 52 deletions
diff --git a/decode_scan_test.go b/decode_scan_test.go
index 1f04ead..8bc33e3 100644
--- a/decode_scan_test.go
+++ b/decode_scan_test.go
@@ -6,6 +6,7 @@ package lowmemjson
import (
"fmt"
+ "io"
"strings"
"testing"
@@ -24,12 +25,34 @@ func (r ReadRuneTypeResult) String() string {
return fmt.Sprintf("{%q, %d, %#v, %v}", r.r, r.s, r.t, r.e)
}
-func TestRuneTypeScanner(t *testing.T) {
- type testcase struct {
- Input string
- Exp []ReadRuneTypeResult
+type runeTypeScannerTestcase struct {
+ Input string
+ Exp []ReadRuneTypeResult
+}
+
+func testRuneTypeScanner(t *testing.T, testcases map[string]runeTypeScannerTestcase, factory func(io.RuneReader) runeTypeScanner) {
+ for tcName, tc := range testcases {
+ t.Run(tcName, func(t *testing.T) {
+ sc := factory(strings.NewReader(tc.Input))
+ var exp, act []string
+ for _, iExp := range tc.Exp {
+ var iAct ReadRuneTypeResult
+ if iExp.s < 0 {
+ iAct.s = iExp.s
+ iAct.e = sc.UnreadRune()
+ } else {
+ iAct.r, iAct.s, iAct.t, iAct.e = sc.ReadRuneType()
+ }
+ exp = append(exp, iExp.String())
+ act = append(act, iAct.String())
+ }
+ assert.Equal(t, exp, act)
+ })
}
- testcases := map[string]testcase{
+}
+
+func TestRuneTypeScanner(t *testing.T) {
+ testcases := map[string]runeTypeScannerTestcase{
"basic": {`{"foo": 12.0}`, []ReadRuneTypeResult{
{'{', 1, RuneTypeObjectBeg, nil},
{'"', 1, RuneTypeStringBeg, nil},
@@ -113,34 +136,15 @@ func TestRuneTypeScanner(t *testing.T) {
{']', 1, RuneTypeError, &DecodeSyntaxError{Offset: 5, Err: fmt.Errorf("invalid character %q looking for beginning of value", ']')}},
}},
}
- for tcName, tc := range testcases {
- t.Run(tcName, func(t *testing.T) {
- sc := &runeTypeScannerImpl{
- inner: strings.NewReader(tc.Input),
- }
- var exp, act []string
- for _, iExp := range tc.Exp {
- var iAct ReadRuneTypeResult
- if iExp.s < 0 {
- iAct.s = iExp.s
- iAct.e = sc.UnreadRune()
- } else {
- iAct.r, iAct.s, iAct.t, iAct.e = sc.ReadRuneType()
- }
- exp = append(exp, iExp.String())
- act = append(act, iAct.String())
- }
- assert.Equal(t, exp, act)
- })
- }
+ testRuneTypeScanner(t, testcases, func(reader io.RuneReader) runeTypeScanner {
+ return &runeTypeScannerImpl{
+ inner: reader,
+ }
+ })
}
func TestNoWSRuneTypeScanner(t *testing.T) {
- type testcase struct {
- Input string
- Exp []ReadRuneTypeResult
- }
- testcases := map[string]testcase{
+ testcases := map[string]runeTypeScannerTestcase{
"basic": {`{"foo": 12.0}`, []ReadRuneTypeResult{
{'{', 1, RuneTypeObjectBeg, nil},
{'"', 1, RuneTypeStringBeg, nil},
@@ -192,28 +196,13 @@ func TestNoWSRuneTypeScanner(t *testing.T) {
{0, 0, RuneTypeEOF, nil},
}},
}
- for tcName, tc := range testcases {
- t.Run(tcName, func(t *testing.T) {
- sc := &noWSRuneTypeScanner{
- inner: &runeTypeScannerImpl{
- inner: strings.NewReader(tc.Input),
- },
- }
- var exp, act []string
- for _, iExp := range tc.Exp {
- var iAct ReadRuneTypeResult
- if iExp.s < 0 {
- iAct.s = iExp.s
- iAct.e = sc.UnreadRune()
- } else {
- iAct.r, iAct.s, iAct.t, iAct.e = sc.ReadRuneType()
- }
- exp = append(exp, iExp.String())
- act = append(act, iAct.String())
- }
- assert.Equal(t, exp, act)
- })
- }
+ testRuneTypeScanner(t, testcases, func(reader io.RuneReader) runeTypeScanner {
+ return &noWSRuneTypeScanner{
+ inner: &runeTypeScannerImpl{
+ inner: reader,
+ },
+ }
+ })
}
func TestElemRuneTypeScanner(t *testing.T) {