func TestJSONScannerNextPeekRune(t *testing.T) { testCaseList := []testCaseForJSONScannerPeekRune{ {data: "12345678", expected: "12345678"}, } for testIndex, testCase := range testCaseList { scanner := scanner.NewJSONScanner(strings.NewReader(testCase.data)) for expectedIndex, expected := range testCase.expected { beforePosition := scanner.CurrentPosition() actual, err := scanner.PeekRune() afterPosition := scanner.CurrentPosition() if err != nil { t.Error(err) } if actual != expected { t.Errorf("testCase[%d:%d] expected=%#v, actual=%#v", testIndex, expectedIndex, expected, actual) } if beforePosition != afterPosition { t.Errorf("testCase[%d:%d] position is wrong", testIndex, expectedIndex) } scanner.NextRune() } actual, err := scanner.NextNonWhiteSpaceRune() if err != io.EOF { t.Logf("%s", string([]rune{actual})) t.Errorf("testCase[%d] EOF not found", testIndex) } actual, err = scanner.NextRune() if err != io.EOF { t.Logf("%s", string([]rune{actual})) t.Errorf("testCase[%d] EOF not found, current:%d", testIndex, scanner.CurrentPosition()) } } }
func TestJSONScannerNextCurrentPosition(t *testing.T) { testCaseList := []testCaseForJSONScannerCurrentPosition{ {data: "12345678", expected: []int{0, 1, 2, 3, 4, 5, 6, 7, 8}}, } for testIndex, testCase := range testCaseList { scanner := scanner.NewJSONScanner(strings.NewReader(testCase.data)) for expectedIndex, expected := range testCase.expected { actual := scanner.CurrentPosition() if actual != expected { t.Errorf("testCase[%d:%d] expected=%#v, actual=%#v", testIndex, expectedIndex, expected, actual) } scanner.NextRune() } actual, err := scanner.NextNonWhiteSpaceRune() if err != io.EOF { t.Logf("%s", string([]rune{actual})) t.Errorf("testCase[%d] EOF not found", testIndex) } actual, err = scanner.NextRune() if err != io.EOF { t.Logf("%s", string([]rune{actual})) t.Errorf("testCase[%d] EOF not found, current:%d", testIndex, scanner.CurrentPosition()) } } }
func TestJSONScannerNextPeekRuneWithOffset(t *testing.T) { testCaseList := []testCaseForJSONScannerPeekRuneWithOffset{ {data: "12345678", expected: []rune{'1', '2', '3', '4', '5', '6', '7', '8'}}, } for testIndex, testCase := range testCaseList { scanner := scanner.NewJSONScanner(strings.NewReader(testCase.data)) index := 0 for expectedIndex, expected := range testCase.expected { actual, err := scanner.PeekRuneWithOffset(index) index++ if err != nil { t.Fatal(err) } if actual != expected { t.Errorf("testCase[%d:%d] expected=%#v, actual=%#v", testIndex, expectedIndex, expected, actual) } } for expectedIndex, expected := range testCase.expected { actual, err := scanner.NextRune() if err != nil { t.Fatal(err) } if actual != expected { t.Errorf("testCase[%d:%d] expected=%#v, actual=%#v", testIndex, expectedIndex, expected, actual) } } actual, err := scanner.NextNonWhiteSpaceRune() if err != io.EOF { t.Logf("%s", string([]rune{actual})) t.Errorf("testCase[%d] EOF not found", testIndex) } actual, err = scanner.NextRune() if err != io.EOF { t.Logf("%s", string([]rune{actual})) t.Errorf("testCase[%d] EOF not found, current:%d", testIndex, scanner.CurrentPosition()) } } }