From 87b02577e50b76d373e3c6b921d776e39cb83346 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 16 Aug 2022 22:54:13 -0600 Subject: decode_scan_test.go: Factor out a common test runner --- decode_scan_test.go | 93 +++++++++++++++++++++++------------------------------ 1 file 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) { -- cgit v1.1-4-g5e80