Example #1
0
func TestJSONScannerNextNonWhiteSpaceRune(t *testing.T) {
	testCaseList := []testCaseForJSONScannerNextNonWhiteSpaceRune{
		{data: "      1      2\n3\t45\r678", expected: "12345678"},
	}

	for testIndex, testCase := range testCaseList {
		scanner := scanner.NewJSONScanner(strings.NewReader(testCase.data))
		for expectedIndex, expected := range testCase.expected {
			actual, err := scanner.NextNonWhiteSpaceRune()
			if err != nil {
				t.Error(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())
		}
	}
}
Example #2
0
func TestJSONScannerNextPeekNonWhiteSpaceRune(t *testing.T) {
	testCaseList := []testCaseForJSONScannerPeekRuneWithOffset{
		{data: `1
		2 3 4 5 6 	7	 8`, 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.PeekNonWhiteSpaceRune()
			index++
			if err != nil {
				t.Fatal(err)
			}

			if actual != expected {
				t.Errorf("testCase[%d:%d] expected=%#v, actual=%#v", testIndex, expectedIndex, string(expected), string(actual))
			}
			actual, err = scanner.NextNonWhiteSpaceRune()
			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())
		}
	}
}