Exemplo n.º 1
0
func TestStringScanner1(t *testing.T) {
	testData := `
	"teststring"
	`
	reader := strings.NewReader(testData)
	jsonScanner := scanner.NewJSONScanner(reader)
	scanner := tokenscanner.NewStringScanner(jsonScanner).(*tokenscanner.StringScanner)
	if !scanner.CanProcess() {
		t.Error("testData can process")
	}
	if scanner.RuneOfToken() != '"' {
		t.Error("StringScanner.RunOfToken must be '\"'")
	}
	if !scanner.CanProcessRune('"') {
		t.Error("StringScanner.CanProcessRune can process rune '\"'")
	}

	expectedToken := token.NewToken(token.KindString, "teststring")
	token, err := scanner.Scan()
	if err != nil {
		t.Error(err)
	}
	if !expectedToken.IsEqualTo(token) {
		t.Errorf("expected=%#v, actual=%#v", expectedToken, token)
	}
}
Exemplo n.º 2
0
func TestMultiCharacterScannerRuneOfToken(t *testing.T) {
	testCaseList := []testCaseForTestMultiCharacterScannerRuneOfToken{
		{data: " null ", tokenKind: token.KindNull, expected: 'n'},
		{data: " true ", tokenKind: token.KindTrue, expected: 't'},
		{data: " false ", tokenKind: token.KindFalse, expected: 'f'},
	}
	for testIndex, testCase := range testCaseList {
		stringReader := strings.NewReader(testCase.data)
		jsonScanner := scanner.NewJSONScanner(stringReader)
		scanner := tokenscanner.NewMultiCharacterScanner(testCase.tokenKind, jsonScanner).(*tokenscanner.MultiCharacterScanner)

		actual := scanner.RuneOfToken()
		expected := testCase.expected

		if actual != expected {
			t.Errorf("testCase[%d] expected=%#v, actual=%#v", testIndex, string(expected), string(actual))
		}
	}
}