MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestLexerPositionSaveRestore

Function TestLexerPositionSaveRestore

parser/lexer_test.go:489–538  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

487}
488
489func TestLexerPositionSaveRestore(t *testing.T) {
490 buf := runes.NewBuffer("foo + bar * 42")
491 lexer := newLexer(buf)
492
493 tok1 := lexer.Lex()
494 if tok1.kind != tokIdent {
495 t.Fatalf("tok1 = %v, want tokIdent", tok1.kind)
496 }
497
498 tok2 := lexer.Lex()
499 if tok2.kind != tokWhitespace {
500 t.Fatalf("tok2 = %v, want tokWhitespace", tok2.kind)
501 }
502
503 // Save position before '+'
504 saved := lexer.SavePosition()
505
506 tok3 := lexer.Lex()
507 if tok3.kind != tokPlus {
508 t.Fatalf("tok3 = %v, want tokPlus", tok3.kind)
509 }
510
511 tok4 := lexer.Lex()
512 if tok4.kind != tokWhitespace {
513 t.Fatalf("tok4 = %v, want tokWhitespace", tok4.kind)
514 }
515
516 tok5 := lexer.Lex()
517 if tok5.kind != tokIdent {
518 t.Fatalf("tok5 = %v, want tokIdent", tok5.kind)
519 }
520
521 // Restore position to before '+'
522 lexer.RestorePosition(saved)
523
524 tok3Restored := lexer.Lex()
525 if tok3Restored.kind != tokPlus || tok3Restored.start != tok3.start || tok3Restored.end != tok3.end {
526 t.Errorf("tok3Restored = %v (%d, %d), want tokPlus (%d, %d)", tok3Restored.kind, tok3Restored.start, tok3Restored.end, tok3.start, tok3.end)
527 }
528
529 tok4Restored := lexer.Lex()
530 if tok4Restored.kind != tokWhitespace {
531 t.Errorf("tok4Restored = %v, want tokWhitespace", tok4Restored.kind)
532 }
533
534 tok5Restored := lexer.Lex()
535 if tok5Restored.kind != tokIdent {
536 t.Errorf("tok5Restored = %v, want tokIdent", tok5Restored.kind)
537 }
538}
539
540func TestLexerErrorRecovery(t *testing.T) {
541 buf := runes.NewBuffer("1e, {2 3}")

Callers

nothing calls this directly

Calls 5

NewBufferFunction · 0.92
newLexerFunction · 0.85
LexMethod · 0.80
SavePositionMethod · 0.80
RestorePositionMethod · 0.80

Tested by

no test coverage detected