Beispiel #1
0
func testLexer(input string, t *testing.T) {
	l := new(json.Lexer)
	l.Init(input, PanicOnError)
	spacesRE := regexp.MustCompile(`^\s+$`)

	next := l.Next()
	offset := 0
	for next != json.EOI {
		s, e := l.Pos()
		if s > offset && !spacesRE.MatchString(input[offset:s]) {
			t.Errorf("Spaces expected: %s", input[offset:s])
		}
		offset = e
		token := string(input[s:e])
		switch next {
		case json.LBRACE, json.RBRACE, json.LBRACK, json.RBRACK,
			json.COLON, json.COMMA, json.NULL, json.TRUE, json.FALSE:
			if token != next.String() {
				t.Errorf("Bad token %v: %s", next, token)
			}
		case json.JSONSTRING:
			if !strings.HasPrefix(token, `"`) || !strings.HasSuffix(token, `"`) {
				t.Errorf("Bad string literal: %s", token)
			}
		}
		next = l.Next()
	}
}
Beispiel #2
0
func BenchmarkLexer(b *testing.B) {
	l := new(json.Lexer)
	for i := 0; i < b.N; i++ {
		l.Init(jsonExample, PanicOnError)
		next := l.Next()
		for next != json.EOI {
			next = l.Next()
		}
	}
	b.SetBytes(int64(len(jsonExample)))
}