summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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) {