How to use parseString method of ast Package

Best Syzkaller code snippet using ast.parseString

parser_test.go

Source:parser_test.go Github

copy

Full Screen

1package participle_test2import (3 "errors"4 "fmt"5 "math"6 "net"7 "reflect"8 "strconv"9 "strings"10 "testing"11 "text/scanner"12 require "github.com/alecthomas/assert/v2"13 "github.com/alecthomas/participle/v2"14 "github.com/alecthomas/participle/v2/lexer"15)16func TestProductionCapture(t *testing.T) {17 type testCapture struct {18 A string `@Test`19 }20 _, err := participle.Build[testCapture]()21 require.Error(t, err)22}23func TestTermCapture(t *testing.T) {24 type grammar struct {25 A string `@"."*`26 }27 parser := mustTestParser[grammar](t)28 expected := &grammar{"..."}29 actual, err := parser.ParseString("", "...")30 require.NoError(t, err)31 require.Equal(t, expected, actual)32}33func TestParseScalar(t *testing.T) {34 type grammar struct {35 A string `@"one"`36 }37 parser := mustTestParser[grammar](t)38 actual, err := parser.ParseString("", "one")39 require.NoError(t, err)40 require.Equal(t, &grammar{"one"}, actual)41}42func TestParseGroup(t *testing.T) {43 type grammar struct {44 A string `@("one" | "two")`45 }46 parser := mustTestParser[grammar](t)47 actual, err := parser.ParseString("", "one")48 require.NoError(t, err)49 require.Equal(t, &grammar{"one"}, actual)50 actual, err = parser.ParseString("", "two")51 require.NoError(t, err)52 require.Equal(t, &grammar{"two"}, actual)53}54func TestParseAlternative(t *testing.T) {55 type grammar struct {56 A string `@"one" |`57 B string `@"two"`58 }59 parser := mustTestParser[grammar](t)60 actual, err := parser.ParseString("", "one")61 require.NoError(t, err)62 require.Equal(t, &grammar{A: "one"}, actual)63 actual, err = parser.ParseString("", "two")64 require.NoError(t, err)65 require.Equal(t, &grammar{B: "two"}, actual)66}67func TestParseSequence(t *testing.T) {68 type grammar struct {69 A string `@"one"`70 B string `@"two"`71 C string `@"three"`72 }73 parser := mustTestParser[grammar](t)74 expected := &grammar{"one", "two", "three"}75 actual, err := parser.ParseString("", "one two three")76 require.NoError(t, err)77 require.Equal(t, expected, actual)78 expected = &grammar{}79 actual, err = parser.ParseString("", "moo")80 require.Error(t, err)81 require.Equal(t, expected, actual)82}83func TestNested(t *testing.T) {84 type nestedInner struct {85 B string `@"one"`86 C string `@"two"`87 }88 type testNested struct {89 A *nestedInner `@@`90 }91 parser := mustTestParser[testNested](t)92 expected := &testNested{A: &nestedInner{B: "one", C: "two"}}93 actual, err := parser.ParseString("", "one two")94 require.NoError(t, err)95 require.Equal(t, expected, actual)96}97func TestAccumulateNested(t *testing.T) {98 type nestedInner struct {99 B string `@"one"`100 C string `@"two"`101 }102 type testAccumulateNested struct {103 A []*nestedInner `@@+`104 }105 parser := mustTestParser[testAccumulateNested](t)106 expected := &testAccumulateNested{A: []*nestedInner{{B: "one", C: "two"}, {B: "one", C: "two"}}}107 actual, err := parser.ParseString("", "one two one two")108 require.NoError(t, err)109 require.Equal(t, expected, actual)110}111func TestRepetitionNoMatch(t *testing.T) {112 type grammar struct {113 A []string `@"."*`114 }115 parser := mustTestParser[grammar](t)116 expected := &grammar{}117 actual, err := parser.ParseString("", ``)118 require.NoError(t, err)119 require.Equal(t, expected, actual)120}121func TestRepetition(t *testing.T) {122 type grammar struct {123 A []string `@"."*`124 }125 parser := mustTestParser[grammar](t)126 expected := &grammar{A: []string{".", ".", "."}}127 actual, err := parser.ParseString("", `...`)128 require.NoError(t, err)129 require.Equal(t, expected, actual)130}131func TestRepetitionAcrossFields(t *testing.T) {132 type testRepetition struct {133 A []string `@"."*`134 B *string `(@"b" |`135 C *string ` @"c")`136 }137 parser := mustTestParser[testRepetition](t)138 b := "b"139 c := "c"140 expected := &testRepetition{141 A: []string{".", ".", "."},142 B: &b,143 }144 actual, err := parser.ParseString("", "...b")145 require.NoError(t, err)146 require.Equal(t, expected, actual)147 expected = &testRepetition{148 A: []string{".", ".", "."},149 C: &c,150 }151 actual, err = parser.ParseString("", "...c")152 require.NoError(t, err)153 require.Equal(t, expected, actual)154 expected = &testRepetition{155 B: &b,156 }157 actual, err = parser.ParseString("", "b")158 require.NoError(t, err)159 require.Equal(t, expected, actual)160}161func TestAccumulateString(t *testing.T) {162 type testAccumulateString struct {163 A string `@"."+`164 }165 parser := mustTestParser[testAccumulateString](t)166 expected := &testAccumulateString{167 A: "...",168 }169 actual, err := parser.ParseString("", "...")170 require.NoError(t, err)171 require.Equal(t, expected, actual)172}173type Group struct {174 Expression *Expression `"(" @@ ")"`175}176type LookaheadGroup struct {177 Expression *Expression `"(" "?" ("=" | "!") @@ ")"`178}179type EBNFOption struct {180 Expression *Expression `"[" @@ "]"`181}182type Repetition struct {183 Expression *Expression `"{" @@ "}"`184}185type Negation struct {186 Expression *Expression `"!" @@`187}188type Literal struct {189 Start string `@String`190}191type Range struct {192 Start string `@String`193 End string `"…" @String`194}195type Term struct {196 Name string `@Ident |`197 Literal *Literal `@@ |`198 Range *Range `@@ |`199 Group *Group `@@ |`200 LookaheadGroup *LookaheadGroup `@@ |`201 Option *EBNFOption `@@ |`202 Repetition *Repetition `@@ |`203 Negation *Negation `@@`204}205type Sequence struct {206 Terms []*Term `@@+`207}208type Expression struct {209 Alternatives []*Sequence `@@ ( "|" @@ )*`210}211type Production struct {212 Name string `@Ident "="`213 Expression []*Expression `@@+ "."`214}215type EBNF struct {216 Productions []*Production `@@*`217}218func TestEBNFParser(t *testing.T) {219 parser := mustTestParser[EBNF](t, participle.Unquote())220 expected := &EBNF{221 Productions: []*Production{222 {223 Name: "Production",224 Expression: []*Expression{225 {226 Alternatives: []*Sequence{227 {228 Terms: []*Term{229 {Name: "name"},230 {Literal: &Literal{Start: "="}},231 {232 Option: &EBNFOption{233 Expression: &Expression{234 Alternatives: []*Sequence{235 {236 Terms: []*Term{237 {Name: "Expression"},238 },239 },240 },241 },242 },243 },244 {Literal: &Literal{Start: "."}},245 },246 },247 },248 },249 },250 },251 {252 Name: "Expression",253 Expression: []*Expression{254 {255 Alternatives: []*Sequence{256 {257 Terms: []*Term{258 {Name: "Alternative"},259 {260 Repetition: &Repetition{261 Expression: &Expression{262 Alternatives: []*Sequence{263 {264 Terms: []*Term{265 {Literal: &Literal{Start: "|"}},266 {Name: "Alternative"},267 },268 },269 },270 },271 },272 },273 },274 },275 },276 },277 },278 },279 {280 Name: "Alternative",281 Expression: []*Expression{282 {283 Alternatives: []*Sequence{284 {285 Terms: []*Term{286 {Name: "Term"},287 {288 Repetition: &Repetition{289 Expression: &Expression{290 Alternatives: []*Sequence{291 {292 Terms: []*Term{293 {Name: "Term"},294 },295 },296 },297 },298 },299 },300 },301 },302 },303 },304 },305 },306 {307 Name: "Term",308 Expression: []*Expression{309 {310 Alternatives: []*Sequence{311 {Terms: []*Term{{Name: "name"}}},312 {313 Terms: []*Term{314 {Name: "token"},315 {316 Option: &EBNFOption{317 Expression: &Expression{318 Alternatives: []*Sequence{319 {320 Terms: []*Term{321 {Literal: &Literal{Start: "…"}},322 {Name: "token"},323 },324 },325 },326 },327 },328 },329 },330 },331 {Terms: []*Term{{Literal: &Literal{Start: "@@"}}}},332 {Terms: []*Term{{Name: "Group"}}},333 {Terms: []*Term{{Name: "EBNFOption"}}},334 {Terms: []*Term{{Name: "Repetition"}}},335 },336 },337 },338 },339 {340 Name: "Group",341 Expression: []*Expression{342 {343 Alternatives: []*Sequence{344 {345 Terms: []*Term{346 {Literal: &Literal{Start: "("}},347 {Name: "Expression"},348 {Literal: &Literal{Start: ")"}},349 },350 },351 },352 },353 },354 },355 {356 Name: "EBNFOption",357 Expression: []*Expression{358 {359 Alternatives: []*Sequence{360 {361 Terms: []*Term{362 {Literal: &Literal{Start: "["}},363 {Name: "Expression"},364 {Literal: &Literal{Start: "]"}},365 },366 },367 },368 },369 },370 },371 {372 Name: "Repetition",373 Expression: []*Expression{374 {375 Alternatives: []*Sequence{376 {377 Terms: []*Term{378 {Literal: &Literal{Start: "{"}},379 {Name: "Expression"},380 {Literal: &Literal{Start: "}"}},381 },382 },383 },384 },385 },386 },387 },388 }389 actual, err := parser.ParseString("", strings.TrimSpace(`390Production = name "=" [ Expression ] "." .391Expression = Alternative { "|" Alternative } .392Alternative = Term { Term } .393Term = name | token [ "…" token ] | "@@" | Group | EBNFOption | Repetition .394Group = "(" Expression ")" .395EBNFOption = "[" Expression "]" .396Repetition = "{" Expression "}" .397`))398 require.NoError(t, err)399 require.Equal(t, expected, actual)400}401func TestParseExpression(t *testing.T) {402 type testNestA struct {403 A string `":" @"a"*`404 }405 type testNestB struct {406 B string `";" @"b"*`407 }408 type testExpression struct {409 A *testNestA `@@ |`410 B *testNestB `@@`411 }412 parser := mustTestParser[testExpression](t)413 expected := &testExpression{414 B: &testNestB{415 B: "b",416 },417 }418 actual, err := parser.ParseString("", ";b")419 require.NoError(t, err)420 require.Equal(t, expected, actual)421}422func TestParseOptional(t *testing.T) {423 type testOptional struct {424 A string `( @"a" @"b" )?`425 B string `@"c"`426 }427 parser := mustTestParser[testOptional](t)428 expected := &testOptional{B: "c"}429 actual, err := parser.ParseString("", `c`)430 require.NoError(t, err)431 require.Equal(t, expected, actual)432}433func TestHello(t *testing.T) {434 type testHello struct {435 Hello string `@"hello"`436 To string `@String`437 }438 parser := mustTestParser[testHello](t, participle.Unquote())439 expected := &testHello{"hello", `Bobby Brown`}440 actual, err := parser.ParseString("", `hello "Bobby Brown"`)441 require.NoError(t, err)442 require.Equal(t, expected, actual)443}444func mustTestParser[G any](t *testing.T, options ...participle.Option) *participle.Parser[G] {445 t.Helper()446 parser, err := participle.Build[G](options...)447 require.NoError(t, err)448 return parser449}450func BenchmarkEBNFParser(b *testing.B) {451 parser, err := participle.Build[EBNF]()452 require.NoError(b, err)453 b.ResetTimer()454 source := strings.TrimSpace(`455Production = name "=" [ Expression ] "." .456Expression = Alternative { "|" Alternative } .457Alternative = Term { Term } .458Term = name | token [ "…" token ] | "@@" | Group | EBNFOption | Repetition .459Group = "(" Expression ")" .460EBNFOption = "[" Expression "]" .461Repetition = "{" Expression "}" .462`)463 for i := 0; i < b.N; i++ {464 _, _ = parser.ParseString("", source)465 }466}467func TestRepeatAcrossFields(t *testing.T) {468 type grammar struct {469 A string `( @("." ">") |`470 B string ` @("," "<") )*`471 }472 parser := mustTestParser[grammar](t)473 expected := &grammar{A: ".>.>.>.>", B: ",<,<,<"}474 actual, err := parser.ParseString("", ".>,<.>.>,<.>,<")475 require.NoError(t, err)476 require.Equal(t, expected, actual)477}478func TestPosInjection(t *testing.T) {479 type subgrammar struct {480 Pos lexer.Position481 B string `@","*`482 EndPos lexer.Position483 }484 type grammar struct {485 Pos lexer.Position486 A string `@"."*`487 B *subgrammar `@@`488 C string `@"."`489 EndPos lexer.Position490 }491 parser := mustTestParser[grammar](t)492 expected := &grammar{493 Pos: lexer.Position{494 Offset: 3,495 Line: 1,496 Column: 4,497 },498 A: "...",499 B: &subgrammar{500 B: ",,,",501 Pos: lexer.Position{502 Offset: 6,503 Line: 1,504 Column: 7,505 },506 EndPos: lexer.Position{507 Offset: 9,508 Line: 1,509 Column: 10,510 },511 },512 C: ".",513 EndPos: lexer.Position{514 Offset: 10,515 Line: 1,516 Column: 11,517 },518 }519 actual, err := parser.ParseString("", " ...,,,.")520 require.NoError(t, err)521 require.Equal(t, expected, actual)522}523type parseableCount int524func (c *parseableCount) Capture(values []string) error {525 *c += parseableCount(len(values))526 return nil527}528func TestCaptureInterface(t *testing.T) {529 type grammar struct {530 Count parseableCount `@"a"*`531 }532 parser := mustTestParser[grammar](t)533 expected := &grammar{Count: 3}534 actual, err := parser.ParseString("", "a a a")535 require.NoError(t, err)536 require.Equal(t, expected, actual)537}538type unmarshallableCount int539func (u *unmarshallableCount) UnmarshalText(text []byte) error {540 *u += unmarshallableCount(len(text))541 return nil542}543func TestTextUnmarshalerInterface(t *testing.T) {544 type grammar struct {545 Count unmarshallableCount `{ @"a" }`546 }547 parser := mustTestParser[grammar](t)548 expected := &grammar{Count: 3}549 actual, err := parser.ParseString("", "a a a")550 require.NoError(t, err)551 require.Equal(t, expected, actual)552}553func TestLiteralTypeConstraint(t *testing.T) {554 type grammar struct {555 Literal string `@"123456":String`556 }557 parser := mustTestParser[grammar](t, participle.Unquote())558 expected := &grammar{Literal: "123456"}559 actual, err := parser.ParseString("", `"123456"`)560 require.NoError(t, err)561 require.Equal(t, expected, actual)562 _, err = parser.ParseString("", `123456`)563 require.Error(t, err)564}565type nestedCapture struct {566 Tokens []string567}568func (n *nestedCapture) Capture(tokens []string) error {569 n.Tokens = tokens570 return nil571}572func TestStructCaptureInterface(t *testing.T) {573 type grammar struct {574 Capture *nestedCapture `@String`575 }576 parser, err := participle.Build[grammar](participle.Unquote())577 require.NoError(t, err)578 expected := &grammar{Capture: &nestedCapture{Tokens: []string{"hello"}}}579 actual, err := parser.ParseString("", `"hello"`)580 require.NoError(t, err)581 require.Equal(t, expected, actual)582}583type parseableStruct struct {584 Tokens []string585}586func (p *parseableStruct) Parse(lex *lexer.PeekingLexer) error {587 for {588 tok := lex.Next()589 if tok.EOF() {590 break591 }592 p.Tokens = append(p.Tokens, tok.Value)593 }594 return nil595}596func TestParseable(t *testing.T) {597 type grammar struct {598 Inner *parseableStruct `@@`599 }600 parser, err := participle.Build[grammar](participle.Unquote())601 require.NoError(t, err)602 expected := &grammar{Inner: &parseableStruct{Tokens: []string{"hello", "123", "world"}}}603 actual, err := parser.ParseString("", `hello 123 "world"`)604 require.NoError(t, err)605 require.Equal(t, expected, actual)606}607func TestStringConcat(t *testing.T) {608 type grammar struct {609 Field string `@"."+`610 }611 parser, err := participle.Build[grammar]()612 require.NoError(t, err)613 expected := &grammar{"...."}614 actual, err := parser.ParseString("", `. . . .`)615 require.NoError(t, err)616 require.Equal(t, expected, actual)617}618func TestParseIntSlice(t *testing.T) {619 type grammar struct {620 Field []int `@Int+`621 }622 parser := mustTestParser[grammar](t)623 expected := &grammar{[]int{1, 2, 3, 4}}624 actual, err := parser.ParseString("", `1 2 3 4`)625 require.NoError(t, err)626 require.Equal(t, expected, actual)627}628func TestEmptyStructErrorsNotPanicsIssue21(t *testing.T) {629 type grammar struct {630 Foo struct{} `@@`631 }632 _, err := participle.Build[grammar]()633 require.Error(t, err)634}635func TestMultipleTokensIntoScalar(t *testing.T) {636 type grammar struct {637 Field int `@("-" Int)`638 }639 p, err := participle.Build[grammar]()640 require.NoError(t, err)641 actual, err := p.ParseString("", `- 10`)642 require.NoError(t, err)643 require.Equal(t, -10, actual.Field)644}645type posMixin struct {646 Pos lexer.Position647}648func TestMixinPosIsPopulated(t *testing.T) {649 type grammar struct {650 posMixin651 Int int `@Int`652 }653 p := mustTestParser[grammar](t)654 actual, err := p.ParseString("", "10")655 require.NoError(t, err)656 require.Equal(t, 10, actual.Int)657 require.Equal(t, 1, actual.Pos.Column)658 require.Equal(t, 1, actual.Pos.Line)659}660type testParserMixin struct {661 A string `@Ident`662 B string `@Ident`663}664func TestMixinFieldsAreParsed(t *testing.T) {665 type grammar struct {666 testParserMixin667 C string `@Ident`668 }669 p := mustTestParser[grammar](t)670 actual, err := p.ParseString("", "one two three")671 require.NoError(t, err)672 require.Equal(t, "one", actual.A)673 require.Equal(t, "two", actual.B)674 require.Equal(t, "three", actual.C)675}676func TestNestedOptional(t *testing.T) {677 type grammar struct {678 Args []string `"(" [ @Ident ( "," @Ident )* ] ")"`679 }680 p := mustTestParser[grammar](t)681 _, err := p.ParseString("", `()`)682 require.NoError(t, err)683 _, err = p.ParseString("", `(a)`)684 require.NoError(t, err)685 _, err = p.ParseString("", `(a, b, c)`)686 require.NoError(t, err)687 _, err = p.ParseString("", `(1)`)688 require.Error(t, err)689}690func TestInvalidNumbers(t *testing.T) {691 type grammar struct {692 Int8 int8 ` "int8" @Int`693 Int16 int16 `| "int16" @Int`694 Int32 int32 `| "int32" @Int`695 Int64 int64 `| "int64" @Int`696 Uint8 uint8 `| "uint8" @Int`697 Uint16 uint16 `| "uint16" @Int`698 Uint32 uint32 `| "uint32" @Int`699 Uint64 uint64 `| "uint64" @Int`700 Float32 float32 `| "float32" @Float`701 Float64 float64 `| "float64" @Float`702 }703 p := mustTestParser[grammar](t)704 tests := []struct {705 name string706 input string707 expected *grammar708 err bool709 }{710 {name: "ValidInt8", input: "int8 127", expected: &grammar{Int8: 127}},711 {name: "InvalidInt8", input: "int8 129", err: true},712 {name: "ValidInt16", input: "int16 32767", expected: &grammar{Int16: 32767}},713 {name: "InvalidInt16", input: "int16 32768", err: true},714 {name: "ValidInt32", input: fmt.Sprintf("int32 %d", math.MaxInt32), expected: &grammar{Int32: math.MaxInt32}},715 {name: "InvalidInt32", input: fmt.Sprintf("int32 %d", int64(math.MaxInt32+1)), err: true},716 {name: "ValidInt64", input: fmt.Sprintf("int64 %d", int64(math.MaxInt64)), expected: &grammar{Int64: math.MaxInt64}},717 {name: "InvalidInt64", input: "int64 9223372036854775808", err: true},718 {name: "ValidFloat64", input: "float64 1234.5", expected: &grammar{Float64: 1234.5}},719 {name: "InvalidFloat64", input: "float64 asdf", err: true},720 }721 for _, test := range tests {722 // nolint: scopelint723 t.Run(test.name, func(t *testing.T) {724 actual, err := p.ParseString("", test.input)725 if test.err {726 require.Error(t, err, fmt.Sprintf("%#v", actual))727 } else {728 require.NoError(t, err)729 require.Equal(t, test.expected, actual)730 }731 })732 }733}734// We'd like this to work, but it can wait.735func TestPartialAST(t *testing.T) {736 type grammar struct {737 Succeed string `@Ident`738 Fail string `@"foo"`739 }740 p := mustTestParser[grammar](t)741 actual, err := p.ParseString("", `foo bar`)742 require.Error(t, err)743 expected := &grammar{Succeed: "foo"}744 require.Equal(t, expected, actual)745}746func TestCaseInsensitive(t *testing.T) {747 type grammar struct {748 Select string `"select":Keyword @Ident`749 }750 // lex := lexer.MustStateful(lexer.Regexp(751 // `(?i)(?P<Keyword>SELECT)` +752 // `|(?P<Ident>\w+)` +753 // `|(\s+)`,754 // ))755 lex := lexer.MustSimple([]lexer.SimpleRule{756 {"Keyword", `(?i)SELECT`},757 {"Ident", `\w+`},758 {"whitespace", `\s+`},759 })760 p := mustTestParser[grammar](t, participle.Lexer(lex), participle.CaseInsensitive("Keyword"))761 actual, err := p.ParseString("", `SELECT foo`)762 expected := &grammar{"foo"}763 require.NoError(t, err)764 require.Equal(t, expected, actual)765 actual, err = p.ParseString("", `select foo`)766 require.NoError(t, err)767 require.Equal(t, expected, actual)768}769func TestTokenAfterRepeatErrors(t *testing.T) {770 type grammar struct {771 Text string `@Ident* "foo"`772 }773 p := mustTestParser[grammar](t)774 _, err := p.ParseString("", ``)775 require.Error(t, err)776}777func TestEOFAfterRepeat(t *testing.T) {778 type grammar struct {779 Text string `@Ident*`780 }781 p := mustTestParser[grammar](t)782 _, err := p.ParseString("", ``)783 require.NoError(t, err)784}785func TestTrailing(t *testing.T) {786 type grammar struct {787 Text string `@Ident`788 }789 p := mustTestParser[grammar](t)790 _, err := p.ParseString("", `foo bar`)791 require.Error(t, err)792}793type modifierTest[G any] struct {794 name string795 input string796 expected string797 fail bool798}799func (test modifierTest[G]) test(t *testing.T) {800 t.Helper()801 t.Run(test.name, func(t *testing.T) {802 p := mustTestParser[G](t)803 grammar, err := p.ParseString("", test.input)804 if test.fail {805 require.Error(t, err)806 } else {807 require.NoError(t, err)808 actual := reflect.ValueOf(grammar).Elem().FieldByName("A").String()809 require.Equal(t, test.expected, actual)810 }811 })812}813func TestModifiers(t *testing.T) {814 type nonEmptyGrammar struct {815 A string `@( ("x"? "y"? "z"?)! "b" )`816 }817 tests := []interface{ test(t *testing.T) }{818 modifierTest[nonEmptyGrammar]{name: "NonMatchingOptionalNonEmpty",819 input: "b",820 fail: true,821 },822 modifierTest[nonEmptyGrammar]{name: "NonEmptyMatch",823 input: "x b",824 expected: "xb",825 },826 modifierTest[nonEmptyGrammar]{name: "NonEmptyMatchAll",827 input: "x y z b",828 expected: "xyzb",829 },830 modifierTest[nonEmptyGrammar]{name: "NonEmptyMatchSome",831 input: "x z b",832 expected: "xzb",833 },834 modifierTest[struct {835 A string `@( "a"? "b" )`836 }]{name: "MatchingOptional",837 input: "a b",838 expected: "ab",839 },840 modifierTest[struct {841 A string `@( "a"? "b" )`842 }]{name: "NonMatchingOptionalIsSkipped",843 input: "b",844 expected: "b",845 },846 modifierTest[struct {847 A string `@( "a"+ )`848 }]{name: "MatchingOneOrMore",849 input: "a a a a a",850 expected: "aaaaa",851 },852 modifierTest[struct {853 A string `@( "a"+ )`854 }]{name: "NonMatchingOneOrMore",855 input: "",856 fail: true,857 },858 modifierTest[struct {859 A string `@( "a"* )`860 }]{name: "MatchingZeroOrMore",861 input: "aaaaaaa",862 fail: true,863 },864 modifierTest[struct {865 A string `@( "a"* )`866 }]{name: "NonMatchingZeroOrMore",867 input: "",868 },869 }870 for _, test := range tests {871 test.test(t)872 }873}874func TestNonEmptyMatchWithOptionalGroup(t *testing.T) {875 type term struct {876 Minus bool `@'-'?`877 Name string `@Ident`878 }879 type grammar struct {880 Start term `parser:"'[' (@@?"`881 End term `parser:" (':' @@)?)! ']'"`882 }883 p := mustTestParser[grammar](t)884 result, err := p.ParseString("", "[-x]")885 require.NoError(t, err)886 require.Equal(t, &grammar{Start: term{Minus: true, Name: "x"}}, result)887 result, err = p.ParseString("", "[a:-b]")888 require.NoError(t, err)889 require.Equal(t, &grammar{Start: term{Name: "a"}, End: term{Minus: true, Name: "b"}}, result)890 result, err = p.ParseString("", "[:end]")891 require.NoError(t, err)892 require.Equal(t, &grammar{End: term{Name: "end"}}, result)893 _, err = p.ParseString("", "[]")894 require.EqualError(t, err, `1:2: sub-expression (Term? (":" Term)?)! cannot be empty`)895}896func TestIssue60(t *testing.T) {897 type grammar struct {898 A string `@("one" | | "two")`899 }900 _, err := participle.Build[grammar]()901 require.Error(t, err)902}903type Issue62Bar struct {904 A int905}906func (x *Issue62Bar) Parse(lex *lexer.PeekingLexer) error {907 token := lex.Next()908 var err error909 x.A, err = strconv.Atoi(token.Value)910 return err911}912type Issue62Foo struct {913 Bars []Issue62Bar `parser:"@@+"`914}915func TestIssue62(t *testing.T) {916 _, err := participle.Build[Issue62Foo]()917 require.NoError(t, err)918}919// nolint: structcheck, unused920func TestIssue71(t *testing.T) {921 type Sub struct {922 name string `@Ident`923 }924 type grammar struct {925 pattern *Sub `@@`926 }927 _, err := participle.Build[grammar]()928 require.Error(t, err)929}930func TestAllowTrailing(t *testing.T) {931 type G struct {932 Name string `@Ident`933 }934 p, err := participle.Build[G]()935 require.NoError(t, err)936 _, err = p.ParseString("", `hello world`)937 require.Error(t, err)938 g, err := p.ParseString("", `hello world`, participle.AllowTrailing(true))939 require.NoError(t, err)940 require.Equal(t, &G{"hello"}, g)941}942func TestDisjunctionErrorReporting(t *testing.T) {943 type statement struct {944 Add bool ` @"add"`945 Remove bool `| @"remove"`946 }947 type grammar struct {948 Statements []*statement `"{" ( @@ )* "}"`949 }950 p := mustTestParser[grammar](t)951 _, err := p.ParseString("", `{ add foo }`)952 // TODO: This should produce a more useful error. This is returned by sequence.Parse().953 require.EqualError(t, err, `1:7: unexpected token "foo" (expected "}")`)954}955func TestCustomInt(t *testing.T) {956 type MyInt int957 type G struct {958 Value MyInt `@Int`959 }960 p, err := participle.Build[G]()961 require.NoError(t, err)962 g, err := p.ParseString("", `42`)963 require.NoError(t, err)964 require.Equal(t, &G{42}, g)965}966func TestBoolIfSet(t *testing.T) {967 type G struct {968 Value bool `@"true"?`969 }970 p, err := participle.Build[G]()971 require.NoError(t, err)972 g, err := p.ParseString("", `true`)973 require.NoError(t, err)974 require.Equal(t, &G{true}, g)975 g, err = p.ParseString("", ``)976 require.NoError(t, err)977 require.Equal(t, &G{false}, g)978}979func TestCustomBoolIfSet(t *testing.T) {980 type MyBool bool981 type G struct {982 Value MyBool `@"true"?`983 }984 p, err := participle.Build[G]()985 require.NoError(t, err)986 g, err := p.ParseString("", `true`)987 require.NoError(t, err)988 require.Equal(t, &G{true}, g)989 g, err = p.ParseString("", ``)990 require.NoError(t, err)991 require.Equal(t, &G{false}, g)992}993func TestPointerToList(t *testing.T) {994 type grammar struct {995 List *[]string `@Ident*`996 }997 p := mustTestParser[grammar](t)998 ast, err := p.ParseString("", `foo bar`)999 require.NoError(t, err)1000 l := []string{"foo", "bar"}1001 require.Equal(t, &grammar{List: &l}, ast)1002}1003// I'm not sure if this is a problem that should be solved like this.1004// func TestMatchHydratesNullFields(t *testing.T) {1005// type grammar struct {1006// List []string `"{" @Ident* "}"`1007// }1008// p := mustTestParser[grammar](t)1009// ast := &grammar{}1010// err := p.ParseString(`{}`, ast)1011// require.NoError(t, err)1012// require.NotNil(t, ast.List)1013// }1014func TestNegation(t *testing.T) {1015 type grammar struct {1016 EverythingUntilSemicolon *[]string `@!';'* @';'`1017 }1018 p := mustTestParser[grammar](t)1019 ast, err := p.ParseString("", `hello world ;`)1020 require.NoError(t, err)1021 require.Equal(t, &[]string{"hello", "world", ";"}, ast.EverythingUntilSemicolon)1022 _, err = p.ParseString("", `hello world`)1023 require.Error(t, err)1024}1025func TestNegationWithPattern(t *testing.T) {1026 type grammar struct {1027 EverythingMoreComplex *[]string `@!(';' String)* @';' @String`1028 }1029 p := mustTestParser[grammar](t, participle.Unquote())1030 // j, err := json.MarshalIndent(p.root, "", " ")1031 // log.Print(j)1032 // log.Print(ebnf(p.root))1033 ast, err := p.ParseString("", `hello world ; "some-str"`)1034 require.NoError(t, err)1035 require.Equal(t, &[]string{"hello", "world", ";", `some-str`}, ast.EverythingMoreComplex)1036 ast, err = p.ParseString("", `hello ; world ; "hey"`)1037 require.NoError(t, err)1038 require.Equal(t, &[]string{"hello", ";", "world", ";", `hey`}, ast.EverythingMoreComplex)1039 _, err = p.ParseString("", `hello ; world ;`)1040 require.Error(t, err)1041}1042func TestNegationWithDisjunction(t *testing.T) {1043 type grammar struct {1044 EverythingMoreComplex *[]string `@!(';' | ',')* @(';' | ',')`1045 }1046 // Note: we need more lookahead since (';' String) needs some before failing to match1047 p := mustTestParser[grammar](t)1048 ast, err := p.ParseString("", `hello world ;`)1049 require.NoError(t, err)1050 require.Equal(t, &[]string{"hello", "world", ";"}, ast.EverythingMoreComplex)1051 ast, err = p.ParseString("", `hello world , `)1052 require.NoError(t, err)1053 require.Equal(t, &[]string{"hello", "world", ","}, ast.EverythingMoreComplex)1054}1055func TestLookaheadGroup_Positive_SingleToken(t *testing.T) {1056 type val struct {1057 Str string ` @String`1058 Int int `| @Int`1059 }1060 type op struct {1061 Op string `@('+' | '*' (?= @Int))`1062 Operand val `@@`1063 }1064 type sum struct {1065 Left val `@@`1066 Ops []op `@@*`1067 }1068 p := mustTestParser[sum](t)1069 ast, err := p.ParseString("", `"x" + "y" + 4`)1070 require.NoError(t, err)1071 require.Equal(t, &sum{Left: val{Str: `"x"`}, Ops: []op{{"+", val{Str: `"y"`}}, {"+", val{Int: 4}}}}, ast)1072 ast, err = p.ParseString("", `"a" * 4 + "b"`)1073 require.NoError(t, err)1074 require.Equal(t, &sum{Left: val{Str: `"a"`}, Ops: []op{{"*", val{Int: 4}}, {"+", val{Str: `"b"`}}}}, ast)1075 ast, err = p.ParseString("", `1 * 2 * 3`)1076 require.NoError(t, err)1077 require.Equal(t, &sum{Left: val{Int: 1}, Ops: []op{{"*", val{Int: 2}}, {"*", val{Int: 3}}}}, ast)1078 _, err = p.ParseString("", `"a" * "x" + "b"`)1079 require.EqualError(t, err, `1:7: unexpected '"x"'`)1080 _, err = p.ParseString("", `4 * 2 + 0 * "b"`)1081 require.EqualError(t, err, `1:13: unexpected '"b"'`)1082}1083func TestLookaheadGroup_Negative_SingleToken(t *testing.T) {1084 type variable struct {1085 Name string `@Ident`1086 }1087 type grammar struct {1088 Identifiers []variable `((?! 'except'|'end') @@)*`1089 Except *variable `('except' @@)? 'end'`1090 }1091 p := mustTestParser[grammar](t)1092 ast, err := p.ParseString("", `one two three exception end`)1093 require.NoError(t, err)1094 require.Equal(t, []variable{{"one"}, {"two"}, {"three"}, {"exception"}}, ast.Identifiers)1095 require.Zero(t, ast.Except)1096 ast, err = p.ParseString("", `anything except this end`)1097 require.NoError(t, err)1098 require.Equal(t, []variable{{"anything"}}, ast.Identifiers)1099 require.Equal(t, &variable{"this"}, ast.Except)1100 ast, err = p.ParseString("", `except the end`)1101 require.NoError(t, err)1102 require.Zero(t, ast.Identifiers)1103 require.Equal(t, &variable{"the"}, ast.Except)1104 _, err = p.ParseString("", `no ending`)1105 require.EqualError(t, err, `1:10: unexpected token "<EOF>" (expected "end")`)1106 _, err = p.ParseString("", `no end in sight`)1107 require.EqualError(t, err, `1:8: unexpected token "in"`)1108}1109func TestLookaheadGroup_Negative_MultipleTokens(t *testing.T) {1110 type grammar struct {1111 Parts []string `((?! '.' '.' '.') @(Ident | '.'))*`1112 }1113 p := mustTestParser[grammar](t)1114 ast, err := p.ParseString("", `x.y.z.`)1115 require.NoError(t, err)1116 require.Equal(t, []string{"x", ".", "y", ".", "z", "."}, ast.Parts)1117 ast, err = p.ParseString("", `..x..`)1118 require.NoError(t, err)1119 require.Equal(t, []string{".", ".", "x", ".", "."}, ast.Parts)1120 ast, err = p.ParseString("", `two.. are fine`)1121 require.NoError(t, err)1122 require.Equal(t, []string{"two", ".", ".", "are", "fine"}, ast.Parts)1123 _, err = p.ParseString("", `but this... is just wrong`)1124 require.EqualError(t, err, `1:9: unexpected token "."`)1125}1126func TestASTTokens(t *testing.T) {1127 type subject struct {1128 Tokens []lexer.Token1129 Word string `@Ident`1130 }1131 type hello struct {1132 Tokens []lexer.Token1133 Subject subject `"hello" @@`1134 }1135 p := mustTestParser[hello](t,1136 participle.Elide("Whitespace"),1137 participle.Lexer(lexer.MustSimple([]lexer.SimpleRule{1138 {"Ident", `\w+`},1139 {"Whitespace", `\s+`},1140 })))1141 actual, err := p.ParseString("", "hello world")1142 require.NoError(t, err)1143 tokens := []lexer.Token{1144 {-2, "hello", lexer.Position{Line: 1, Column: 1}},1145 {-3, " ", lexer.Position{Offset: 5, Line: 1, Column: 6}},1146 {-2, "world", lexer.Position{Offset: 6, Line: 1, Column: 7}},1147 }1148 expected := &hello{1149 Tokens: tokens,1150 Subject: subject{1151 Tokens: tokens[1:],1152 Word: "world",1153 },1154 }1155 require.Equal(t, expected, actual)1156}1157func TestCaptureIntoToken(t *testing.T) {1158 type ast struct {1159 Head lexer.Token `@Ident`1160 Tail []lexer.Token `@(Ident*)`1161 }1162 p := mustTestParser[ast](t)1163 actual, err := p.ParseString("", "hello waz baz")1164 require.NoError(t, err)1165 expected := &ast{1166 Head: lexer.Token{-2, "hello", lexer.Position{Line: 1, Column: 1}},1167 Tail: []lexer.Token{1168 {-2, "waz", lexer.Position{Offset: 6, Line: 1, Column: 7}},1169 {-2, "baz", lexer.Position{Offset: 10, Line: 1, Column: 11}},1170 },1171 }1172 require.Equal(t, expected, actual)1173}1174func TestEndPos(t *testing.T) {1175 type Ident struct {1176 Pos lexer.Position1177 EndPos lexer.Position1178 Text string `parser:"@Ident"`1179 }1180 type AST struct {1181 First *Ident `parser:"@@"`1182 Second *Ident `parser:"@@"`1183 }1184 var (1185 Lexer = lexer.Must(lexer.New(lexer.Rules{1186 "Root": {1187 {"Ident", `[\w:]+`, nil},1188 {"Whitespace", `[\r\t ]+`, nil},1189 },1190 }))1191 Parser = participle.MustBuild[AST](1192 participle.Lexer(Lexer),1193 participle.Elide("Whitespace"),1194 )1195 )1196 mod, err := Parser.Parse("", strings.NewReader("foo bar"))1197 require.NoError(t, err)1198 require.Equal(t, 0, mod.First.Pos.Offset)1199 require.Equal(t, 3, mod.First.EndPos.Offset)1200}1201func TestBug(t *testing.T) {1202 type A struct {1203 Shared string `parser:"@'1'"`1204 Diff string `parser:"@A"`1205 }1206 type B struct {1207 Shared string `parser:"@'1'"`1208 Diff string `parser:"@B"`1209 }1210 type AST struct {1211 Branch string `parser:"@'branch'"`1212 A *A `parser:"( @@"`1213 B *B `parser:"| @@ )"`1214 }1215 var (1216 lexer = lexer.Must(lexer.New(lexer.Rules{1217 "Root": {1218 {"A", `@`, nil},1219 {"B", `!`, nil},1220 {"Ident", `[\w:]+`, nil},1221 {"Whitespace", `[\r\t ]+`, nil},1222 },1223 }))1224 parser = participle.MustBuild[AST](1225 participle.Lexer(lexer),1226 participle.Elide("Whitespace"),1227 )1228 )1229 expected := &AST{1230 Branch: "branch",1231 B: &B{1232 Shared: "1",1233 Diff: "!",1234 },1235 }1236 actual, err := parser.Parse("name", strings.NewReader(`branch 1!`))1237 require.NoError(t, err)1238 require.Equal(t, expected, actual)1239}1240type sliceCapture string1241func (c *sliceCapture) Capture(values []string) error {1242 *c = sliceCapture(strings.ToUpper(values[0]))1243 return nil1244}1245func TestCaptureOnSliceElements(t *testing.T) { // nolint:dupl1246 type capture struct {1247 Single *sliceCapture `@Capture`1248 Slice []sliceCapture `@Capture @Capture`1249 SlicePtr []*sliceCapture `@Capture @Capture`1250 }1251 parser := participle.MustBuild[capture]([]participle.Option{1252 participle.Lexer(lexer.MustSimple([]lexer.SimpleRule{1253 {Name: "Capture", Pattern: `[a-z]{3}`},1254 {Name: "Whitespace", Pattern: `[\s|\n]+`},1255 })),1256 participle.Elide("Whitespace"),1257 }...)1258 captured, err := parser.ParseString("capture_slice", `abc def ijk lmn opq`)1259 require.NoError(t, err)1260 expectedSingle := sliceCapture("ABC")1261 expectedSlicePtr1 := sliceCapture("LMN")1262 expectedSlicePtr2 := sliceCapture("OPQ")1263 expected := &capture{1264 Single: &expectedSingle,1265 Slice: []sliceCapture{"DEF", "IJK"},1266 SlicePtr: []*sliceCapture{&expectedSlicePtr1, &expectedSlicePtr2},1267 }1268 require.Equal(t, expected, captured)1269}1270type sliceParse string1271func (s *sliceParse) Parse(lex *lexer.PeekingLexer) error {1272 token := lex.Peek()1273 if len(token.Value) != 3 {1274 return participle.NextMatch1275 }1276 lex.Next()1277 *s = sliceParse(strings.Repeat(token.Value, 2))1278 return nil1279}1280func TestParseOnSliceElements(t *testing.T) { // nolint:dupl1281 type parse struct {1282 Single *sliceParse `@@`1283 Slice []sliceParse `@@+`1284 }1285 parser := participle.MustBuild[parse]([]participle.Option{1286 participle.Lexer(lexer.MustSimple([]lexer.SimpleRule{1287 {Name: "Element", Pattern: `[a-z]{3}`},1288 {Name: "Whitespace", Pattern: `[\s|\n]+`},1289 })),1290 participle.Elide("Whitespace"),1291 }...)1292 parsed, err := parser.ParseString("parse_slice", `abc def ijk`)1293 require.NoError(t, err)1294 expectedSingle := sliceParse("abcabc")1295 expected := &parse{1296 Single: &expectedSingle,1297 Slice: []sliceParse{"defdef", "ijkijk"},1298 }1299 require.Equal(t, expected, parsed)1300}1301func TestUnmarshalNetIP(t *testing.T) {1302 type grammar struct {1303 IP net.IP `@IP`1304 }1305 parser := mustTestParser[grammar](t, participle.Lexer(lexer.MustSimple([]lexer.SimpleRule{1306 {"IP", `[\d.]+`},1307 })))1308 ast, err := parser.ParseString("", "10.2.3.4")1309 require.NoError(t, err)1310 require.Equal(t, "10.2.3.4", ast.IP.String())1311}1312type Address net.IP1313func (a *Address) Capture(values []string) error {1314 fmt.Println("does not run at all")1315 *a = Address(net.ParseIP(values[0]))1316 return nil1317}1318func TestCaptureIP(t *testing.T) {1319 type grammar struct {1320 IP Address `@IP`1321 }1322 parser := mustTestParser[grammar](t, participle.Lexer(lexer.MustSimple([]lexer.SimpleRule{1323 {"IP", `[\d.]+`},1324 })))1325 ast, err := parser.ParseString("", "10.2.3.4")1326 require.NoError(t, err)1327 require.Equal(t, "10.2.3.4", (net.IP)(ast.IP).String())1328}1329func BenchmarkIssue143(b *testing.B) {1330 type Disjunction struct {1331 Long1 bool `parser:" '<' '1' ' ' 'l' 'o' 'n' 'g' ' ' 'r' 'u' 'l' 'e' ' ' 't' 'o' ' ' 'f' 'o' 'r' 'm' 'a' 't' '>'"`1332 Long2 bool `parser:"| '<' '2' ' ' 'l' 'o' 'n' 'g' ' ' 'r' 'u' 'l' 'e' ' ' 't' 'o' ' ' 'f' 'o' 'r' 'm' 'a' 't' '>'"`1333 Long3 bool `parser:"| '<' '3' ' ' 'l' 'o' 'n' 'g' ' ' 'r' 'u' 'l' 'e' ' ' 't' 'o' ' ' 'f' 'o' 'r' 'm' 'a' 't' '>'"`1334 Long4 bool `parser:"| '<' '4' ' ' 'l' 'o' 'n' 'g' ' ' 'r' 'u' 'l' 'e' ' ' 't' 'o' ' ' 'f' 'o' 'r' 'm' 'a' 't' '>'"`1335 Real bool `parser:"| '<' 'x' '>'"`1336 }1337 type Disjunctions struct {1338 List []Disjunction `parser:"@@*"`1339 }1340 var disjunctionParser = participle.MustBuild[Disjunctions]()1341 input := "<x> <x> <x> <x> <x> <x> <x> <x> <x> <x> <x> <x> <x> <x> <x> <x> <x> <x> <x> <x>"1342 b.ResetTimer()1343 b.ReportAllocs()1344 for i := 0; i < b.N; i++ {1345 if _, err := disjunctionParser.ParseString("", input); err != nil {1346 panic(err)1347 }1348 }1349}1350type Boxes struct {1351 Pos lexer.Position1352 Boxes Box `@Ident`1353}1354type Box struct {1355 Pos lexer.Position1356 Val string `@Ident`1357}1358func (b *Box) Capture(values []string) error {1359 b.Val = values[0]1360 return nil1361}1362func TestBoxedCapture(t *testing.T) {1363 lex := lexer.MustSimple([]lexer.SimpleRule{1364 {"Ident", `[a-zA-Z](\w|\.|/|:|-)*`},1365 {"whitespace", `\s+`},1366 })1367 parser := participle.MustBuild[Boxes](1368 participle.Lexer(lex),1369 participle.UseLookahead(2),1370 )1371 if _, err := parser.ParseString("test", "abc::cdef.abc"); err != nil {1372 t.Fatal(err)1373 }1374}1375func TestMatchEOF(t *testing.T) {1376 type testMatchNewlineOrEOF struct {1377 Text []string `@Ident+ ("\n" | EOF)`1378 }1379 p := mustTestParser[testMatchNewlineOrEOF](t)1380 _, err := p.ParseString("", "hell world")1381 require.NoError(t, err)1382 _, err = p.ParseString("", "hell world\n")1383 require.NoError(t, err)1384}1385func TestParseExplicitElidedIdent(t *testing.T) { // nolint1386 lex := lexer.MustSimple([]lexer.SimpleRule{1387 {"Ident", `[a-zA-Z](\w|\.|/|:|-)*`},1388 {"Comment", `/\*[^*]*\*/`},1389 {"whitespace", `\s+`},1390 })1391 type grammar struct {1392 Comment string `@Comment?`1393 Ident string `@Ident`1394 }1395 p := mustTestParser[grammar](t, participle.Lexer(lex), participle.Elide("Comment"))1396 actual, err := p.ParseString("", `hello`)1397 require.NoError(t, err)1398 require.Equal(t, &grammar{Ident: "hello"}, actual)1399 actual, err = p.ParseString("", `/* Comment */ hello`)1400 require.NoError(t, err)1401 require.Equal(t, &grammar{Comment: `/* Comment */`, Ident: "hello"}, actual)1402}1403func TestParseExplicitElidedTypedLiteral(t *testing.T) { // nolint1404 lex := lexer.MustSimple([]lexer.SimpleRule{1405 {"Ident", `[a-zA-Z](\w|\.|/|:|-)*`},1406 {"Comment", `/\*[^*]*\*/`},1407 {"whitespace", `\s+`},1408 })1409 type grammar struct {1410 Comment string `@"/* Comment */":Comment?`1411 Ident string `@Ident`1412 }1413 p := mustTestParser[grammar](t, participle.Lexer(lex), participle.Elide("Comment"))1414 actual, err := p.ParseString("", `hello`)1415 require.NoError(t, err)1416 require.Equal(t, &grammar{Ident: "hello"}, actual)1417 actual, err = p.ParseString("", `/* Comment */ hello`)1418 require.NoError(t, err)1419 require.Equal(t, &grammar{Comment: `/* Comment */`, Ident: "hello"}, actual)1420}1421func TestEmptySequenceMatches(t *testing.T) {1422 lex := lexer.MustSimple([]lexer.SimpleRule{1423 {"Ident", `[a-zA-Z](\w|\.|/|:|-)*`},1424 {"Comment", `/\*[^*]*\*/`},1425 {"Whitespace", `\s+`},1426 })1427 type grammar struct {1428 Ident []string `@Ident*`1429 Comments []string `@Comment*`1430 }1431 p := mustTestParser[grammar](t, participle.Lexer(lex), participle.Elide("Whitespace"))1432 expected := &grammar{}1433 actual, err := p.ParseString("", "")1434 require.NoError(t, err)1435 require.Equal(t, expected, actual)1436}1437type RootParseableFail struct{}1438func (*RootParseableFail) String() string { return "" }1439func (*RootParseableFail) GoString() string { return "" }1440func (*RootParseableFail) Parse(lex *lexer.PeekingLexer) error {1441 return errors.New("always fail immediately")1442}1443func TestRootParseableFail(t *testing.T) {1444 p := mustTestParser[RootParseableFail](t)1445 _, err := p.ParseString("<test>", "blah")1446 require.EqualError(t, err, "<test>:1:1: always fail immediately")1447}1448type (1449 TestCustom interface{ isTestCustom() }1450 CustomIdent string1451 CustomNumber float641452 CustomBoolean bool1453)1454func (CustomIdent) isTestCustom() {}1455func (CustomNumber) isTestCustom() {}1456func (CustomBoolean) isTestCustom() {}1457func TestParserWithCustomProduction(t *testing.T) {1458 type grammar struct {1459 Custom TestCustom `@@`1460 }1461 p := mustTestParser[grammar](t, participle.ParseTypeWith(func(lex *lexer.PeekingLexer) (TestCustom, error) {1462 switch peek := lex.Peek(); {1463 case peek.Type == scanner.Int || peek.Type == scanner.Float:1464 v, err := strconv.ParseFloat(lex.Next().Value, 64)1465 if err != nil {1466 return nil, err1467 }1468 return CustomNumber(v), nil1469 case peek.Type == scanner.Ident:1470 name := lex.Next().Value1471 if name == "true" || name == "false" {1472 return CustomBoolean(name == "true"), nil1473 }1474 return CustomIdent(name), nil1475 default:1476 return nil, participle.NextMatch1477 }1478 }))1479 type testCase struct {1480 src string1481 expected TestCustom1482 }1483 for _, c := range []testCase{1484 {"a", CustomIdent("a")},1485 {"12.5", CustomNumber(12.5)},1486 {"true", CustomBoolean(true)},1487 {"false", CustomBoolean(false)},1488 } {1489 actual, err := p.ParseString("", c.src)1490 require.NoError(t, err)1491 require.Equal(t, c.expected, actual.Custom)1492 }1493 require.Equal(t, `Grammar = TestCustom .`, p.String())1494}1495type (1496 TestUnionA interface{ isTestUnionA() }1497 TestUnionB interface{ isTestUnionB() }1498 AMember1 struct {1499 V string `@Ident`1500 }1501 AMember2 struct {1502 V TestUnionB `"[" @@ "]"`1503 }1504 BMember1 struct {1505 V float64 `@Int | @Float`1506 }1507 BMember2 struct {1508 V TestUnionA `"{" @@ "}"`1509 }1510)1511func (AMember1) isTestUnionA() {}1512func (AMember2) isTestUnionA() {}1513func (BMember1) isTestUnionB() {}1514func (BMember2) isTestUnionB() {}1515func TestParserWithUnion(t *testing.T) {1516 type grammar struct {1517 A TestUnionA `@@`1518 B TestUnionB `| @@`1519 }1520 parser := mustTestParser[grammar](t, participle.UseLookahead(10),1521 participle.Union[TestUnionA](AMember1{}, AMember2{}),1522 participle.Union[TestUnionB](BMember1{}, BMember2{}))1523 type testCase struct {1524 src string1525 expected grammar1526 }1527 for _, c := range []testCase{1528 {`a`, grammar{A: AMember1{"a"}}},1529 {`1.5`, grammar{B: BMember1{1.5}}},1530 {`[2.5]`, grammar{A: AMember2{BMember1{2.5}}}},1531 {`{x}`, grammar{B: BMember2{AMember1{"x"}}}},1532 {`{ [ { [12] } ] }`, grammar{B: BMember2{AMember2{BMember2{AMember2{BMember1{12}}}}}}},1533 } {1534 var trace strings.Builder1535 actual, err := parser.ParseString("", c.src, participle.Trace(&trace))1536 require.NoError(t, err)1537 require.Equal(t, &c.expected, actual)1538 require.NotEqual(t, "", trace.String())1539 }1540 require.Equal(t, strings.TrimSpace(`1541Grammar = TestUnionA | TestUnionB .1542TestUnionA = AMember1 | AMember2 .1543AMember1 = <ident> .1544AMember2 = "[" TestUnionB "]" .1545TestUnionB = BMember1 | BMember2 .1546BMember1 = <int> | <float> .1547BMember2 = "{" TestUnionA "}" .1548 `), parser.String())1549}1550func TestParseSubProduction(t *testing.T) {1551 type (1552 ListItem struct {1553 Number *float64 `(@Int | @Float)`1554 String *string `| @String`1555 }1556 Grammar struct {1557 List []ListItem `"[" @@ ("," @@)* "]"`1558 }1559 )1560 numberItem := func(n float64) ListItem { return ListItem{Number: &n} }1561 stringItem := func(s string) ListItem { return ListItem{String: &s} }1562 p := mustTestParser[Grammar](t, participle.Unquote())1563 expected := &Grammar{List: []ListItem{numberItem(1), stringItem("test")}}1564 actual, err := p.ParseString("", `[ 1, "test" ]`)1565 require.NoError(t, err)1566 require.Equal(t, expected, actual)1567 expectedItem := numberItem(1.234e5)1568 ip, err := participle.ParserForProduction[ListItem](p)1569 require.NoError(t, err)1570 actualItem, err := ip.ParseString("", `1.234e5`)1571 require.NoError(t, err)1572 require.Equal(t, &expectedItem, actualItem)1573 expectedItem2 := stringItem("\t\ttest\t\t")1574 actualItem2, err := ip.ParseString("", `"\t\ttest\t\t"`)1575 require.NoError(t, err)1576 require.Equal(t, &expectedItem2, actualItem2)1577}1578type I255Grammar struct {1579 Union I255Union `@@`1580}1581type I255Union interface{ union() }1582type I255String struct {1583 Value string `@String`1584}1585func (*I255String) union() {}1586func TestIssue255(t *testing.T) {1587 parser, err := participle.Build[I255Grammar](1588 participle.Union[I255Union](&I255String{}),1589 )1590 require.NoError(t, err)1591 g, err := parser.ParseString("", `"Hello, World!"`)1592 require.NoError(t, err)1593 require.Equal(t, &I255Grammar{Union: &I255String{Value: `"Hello, World!"`}}, g)1594}...

Full Screen

Full Screen

statements_test.go

Source:statements_test.go Github

copy

Full Screen

1package parser2import (3 "fmt"4 "testing"5 "github.com/end-r/guardian/ast"6 "github.com/end-r/goutil"7)8func TestParseAssignmentStatementSingleConstant(t *testing.T) {9 p, _ := ParseString("x = 6")10 goutil.AssertNow(t, p != nil, "scope is nil")11 goutil.AssertLength(t, len(p.Sequence), 1)12 first := p.Next()13 goutil.AssertNow(t, first.Type() == ast.AssignmentStatement, "wrong node type")14 assignmentStmt := first.(*ast.AssignmentStatementNode)15 goutil.AssertNow(t, len(assignmentStmt.Left) == 1, "wrong left length")16 goutil.AssertNow(t, len(assignmentStmt.Right) == 1, "wrong right length")17 left := assignmentStmt.Left[0]18 right := assignmentStmt.Right[0]19 goutil.AssertNow(t, left != nil, "left is nil")20 goutil.AssertNow(t, left.Type() == ast.Identifier, "wrong left type")21 goutil.AssertNow(t, right != nil, "right is nil")22 goutil.AssertNow(t, right.Type() == ast.Literal, "wrong right type")23}24func TestParseAssignmentStatementIncrement(t *testing.T) {25 _, errs := ParseString("x++")26 goutil.AssertNow(t, len(errs) == 0, errs.Format())27}28func TestParseIfStatement(t *testing.T) {29 p := createParser(`if x == 2 {30 }`)31 goutil.Assert(t, isIfStatement(p), "should detect if statement")32 parseIfStatement(p)33 goutil.AssertNow(t, len(p.errs) == 0, fmt.Sprintln(p.errs))34 first := p.scope.Next()35 goutil.Assert(t, first.Type() == ast.IfStatement, "wrong node type")36}37func TestParseIfStatementComplex(t *testing.T) {38 p := createParser(`if proposals[p].voteCount > winningVoteCount {39 }`)40 goutil.Assert(t, isIfStatement(p), "should detect if statement")41 parseIfStatement(p)42 goutil.AssertNow(t, len(p.errs) == 0, p.errs.Format())43 first := p.scope.Next()44 goutil.Assert(t, first.Type() == ast.IfStatement, "wrong node type")45}46func TestParseIfStatementInit(t *testing.T) {47 p := createParser(`if x = 0; x > 5 {48 }`)49 goutil.Assert(t, isIfStatement(p), "should detect if statement")50 parseIfStatement(p)51 goutil.AssertNow(t, len(p.errs) == 0, p.errs.Format())52 first := p.scope.Next()53 goutil.Assert(t, first.Type() == ast.IfStatement, "wrong node type")54}55func TestParseIfStatementElse(t *testing.T) {56 p := createParser(`if x == 2 {57 } else {58 }`)59 goutil.Assert(t, isIfStatement(p), "should detect if statement")60 parseIfStatement(p)61 goutil.AssertNow(t, len(p.errs) == 0, p.errs.Format())62 first := p.scope.Next()63 goutil.Assert(t, first.Type() == ast.IfStatement, "wrong node type")64 ifStat := first.(*ast.IfStatementNode)65 goutil.Assert(t, ifStat.Init == nil, "init should be nil")66}67func TestParseIfStatementInitElse(t *testing.T) {68 p := createParser(`if x = 0; x > 5 {69 } else {70 }`)71 goutil.Assert(t, isIfStatement(p), "should detect if statement")72 parseIfStatement(p)73 goutil.AssertNow(t, len(p.errs) == 0, p.errs.Format())74 first := p.scope.Next()75 goutil.Assert(t, first.Type() == ast.IfStatement, "wrong node type")76 ifStat := first.(*ast.IfStatementNode)77 goutil.Assert(t, ifStat.Init != nil, "init shouldn't be nil")78}79func TestParseIfStatementElifElse(t *testing.T) {80 p := createParser(`if x > 4 {81 } elif x < 4 {82 } else {83 }`)84 goutil.Assert(t, isIfStatement(p), "should detect if statement")85 parseIfStatement(p)86 goutil.AssertNow(t, len(p.errs) == 0, p.errs.Format())87 first := p.scope.Next()88 goutil.Assert(t, first.Type() == ast.IfStatement, "wrong node type")89 ifStat := first.(*ast.IfStatementNode)90 goutil.Assert(t, ifStat.Init == nil, "init should be nil")91 goutil.AssertNow(t, len(ifStat.Conditions) == 2, "should have two conditions")92 nextFirst := ifStat.Conditions[0]93 goutil.AssertNow(t, nextFirst.Condition.Type() == ast.BinaryExpression, "first binary expr not recognised")94 nextSecond := ifStat.Conditions[1]95 goutil.AssertNow(t, nextSecond.Condition.Type() == ast.BinaryExpression, "second binary expr not recognised")96 goutil.AssertNow(t, ifStat.Else != nil, "else should not be nil")97}98func TestParseForStatementCondition(t *testing.T) {99 p := createParser(`for x < 5 {}`)100 goutil.Assert(t, isForStatement(p), "should detect for statement")101 parseForStatement(p)102 goutil.Assert(t, len(p.errs) == 0, "should be error-free")103 first := p.scope.Next()104 goutil.Assert(t, first.Type() == ast.ForStatement, "wrong node type")105 forStat := first.(*ast.ForStatementNode)106 goutil.Assert(t, forStat.Init == nil, "init should be nil")107 goutil.Assert(t, forStat.Cond != nil, "cond should not be nil")108 goutil.Assert(t, forStat.Post == nil, "post should be nil")109}110func TestParseForStatementInitCondition(t *testing.T) {111 p := createParser(`for x = 0; x < 5 {}`)112 goutil.Assert(t, isForStatement(p), "should detect for statement")113 parseForStatement(p)114 goutil.AssertNow(t, len(p.errs) == 0, p.errs.Format())115 first := p.scope.Next()116 goutil.Assert(t, first.Type() == ast.ForStatement, "wrong node type")117 forStat := first.(*ast.ForStatementNode)118 goutil.Assert(t, forStat.Init != nil, "init should not be nil")119 goutil.Assert(t, forStat.Cond != nil, "cond should not be nil")120 goutil.Assert(t, forStat.Post == nil, "post should be nil")121}122func TestParseForStatementInitConditionStatement(t *testing.T) {123 p := createParser(`for x = 0; x < 5; x++ {}`)124 goutil.Assert(t, isForStatement(p), "should detect for statement")125 parseForStatement(p)126 goutil.Assert(t, len(p.errs) == 0, "should be error-free")127 goutil.AssertNow(t, p.scope != nil, "scope should not be nil")128 goutil.AssertNow(t, len(p.scope.Sequence) == 1, fmt.Sprintf("wrong sequence length: %d", len(p.scope.Sequence)))129 first := p.scope.Next()130 goutil.Assert(t, first.Type() == ast.ForStatement, "wrong node type")131 forStat := first.(*ast.ForStatementNode)132 goutil.Assert(t, forStat.Init != nil, "init should not be nil")133 goutil.Assert(t, forStat.Cond != nil, "cond should not be nil")134 goutil.Assert(t, forStat.Post != nil, "post should not be nil")135}136func TestParseSwitchStatement(t *testing.T) {137 p := createParser(`switch x {}`)138 goutil.AssertNow(t, isSwitchStatement(p), "should detect switch statement")139 parseSwitchStatement(p)140}141func TestParseSwitchStatementSingleCase(t *testing.T) {142 p := createParser(`switch x { case 5{}}`)143 goutil.Assert(t, isSwitchStatement(p), "should detect switch statement")144 parseSwitchStatement(p)145}146func TestParseSwitchStatementMultiCase(t *testing.T) {147 p := createParser(`switch x {148 case 5:149 x += 2150 break151 case 4:152 x *= 2153 break154 }`)155 goutil.Assert(t, isSwitchStatement(p), "should detect switch statement")156 parseSwitchStatement(p)157}158func TestParseSwitchStatementExclusive(t *testing.T) {159 p := createParser(`exclusive switch x {}160 `)161 goutil.Assert(t, isSwitchStatement(p), "should detect switch statement")162 parseSwitchStatement(p)163}164func TestParseCaseStatementSingle(t *testing.T) {165 p := createParser(`case 5:`)166 goutil.Assert(t, isCaseStatement(p), "should detect case statement")167 parseCaseStatement(p)168}169func TestParseCaseStatementMultiple(t *testing.T) {170 p := createParser(`case 5, 8, 9 {}`)171 goutil.Assert(t, isCaseStatement(p), "should detect case statement")172 parseCaseStatement(p)173}174func TestEmptyReturnStatement(t *testing.T) {175 p := createParser("return")176 goutil.Assert(t, isReturnStatement(p), "should detect return statement")177 parseReturnStatement(p)178}179func TestSingleLiteralReturnStatement(t *testing.T) {180 p := createParser(`return "twenty"`)181 goutil.Assert(t, isReturnStatement(p), "should detect return statement")182 parseReturnStatement(p)183 u := p.scope.Next()184 goutil.AssertNow(t, u.Type() == ast.ReturnStatement, "wrong return type")185 r := u.(*ast.ReturnStatementNode)186 goutil.AssertNow(t, len(r.Results) == 1, "wrong result length")187 goutil.AssertNow(t, r.Results[0].Type() == ast.Literal, "wrong literal type")188}189func TestMultipleLiteralReturnStatement(t *testing.T) {190 p := createParser(`return "twenty", "thirty"`)191 goutil.Assert(t, isReturnStatement(p), "should detect return statement")192 parseReturnStatement(p)193 u := p.scope.Next()194 goutil.AssertNow(t, u.Type() == ast.ReturnStatement, "wrong return type")195 r := u.(*ast.ReturnStatementNode)196 goutil.AssertNow(t, len(r.Results) == 2, "wrong result length")197 goutil.AssertNow(t, r.Results[0].Type() == ast.Literal, "wrong result 0 type")198 goutil.AssertNow(t, r.Results[1].Type() == ast.Literal, "wrong result 1 type")199}200func TestSingleReferenceReturnStatement(t *testing.T) {201 p := createParser(`return twenty`)202 goutil.Assert(t, isReturnStatement(p), "should detect return statement")203 parseReturnStatement(p)204 u := p.scope.Next()205 goutil.AssertNow(t, u.Type() == ast.ReturnStatement, "wrong return type")206 r := u.(*ast.ReturnStatementNode)207 goutil.AssertNow(t, len(r.Results) == 1, "wrong result length")208 goutil.AssertNow(t, r.Results[0].Type() == ast.Identifier, "wrong result 0 type")209}210func TestMultipleReferenceReturnStatement(t *testing.T) {211 p := createParser(`return twenty, thirty`)212 goutil.Assert(t, isReturnStatement(p), "should detect return statement")213 parseReturnStatement(p)214 u := p.scope.Next()215 goutil.AssertNow(t, u.Type() == ast.ReturnStatement, "wrong return type")216 r := u.(*ast.ReturnStatementNode)217 goutil.AssertNow(t, len(r.Results) == 2, "wrong result length")218 goutil.AssertNow(t, r.Results[0].Type() == ast.Identifier, "wrong result 0 type")219 goutil.AssertNow(t, r.Results[1].Type() == ast.Identifier, "wrong result 1 type")220}221func TestSingleCallReturnStatement(t *testing.T) {222 p := createParser(`return param()`)223 goutil.Assert(t, isReturnStatement(p), "should detect return statement")224 parseReturnStatement(p)225 u := p.scope.Next()226 goutil.AssertNow(t, u.Type() == ast.ReturnStatement, "wrong return type")227 r := u.(*ast.ReturnStatementNode)228 goutil.AssertNow(t, len(r.Results) == 1, "wrong result length")229 goutil.AssertNow(t, r.Results[0].Type() == ast.CallExpression, "wrong result 0 type")230}231func TestMultipleCallReturnStatement(t *testing.T) {232 p := createParser(`return a(param, "param"), b()`)233 goutil.Assert(t, isReturnStatement(p), "should detect return statement")234 parseReturnStatement(p)235 u := p.scope.Next()236 goutil.AssertNow(t, u.Type() == ast.ReturnStatement, "wrong return type")237 r := u.(*ast.ReturnStatementNode)238 goutil.AssertNow(t, len(r.Results) == 2, fmt.Sprintf("wrong result length: %d", len(r.Results)))239 goutil.AssertNow(t, r.Results[0].Type() == ast.CallExpression, "wrong result 0 type")240 c1 := r.Results[0].(*ast.CallExpressionNode)241 goutil.AssertNow(t, len(c1.Arguments) == 2, fmt.Sprintf("wrong c1 args length: %d", len(c1.Arguments)))242 goutil.AssertNow(t, r.Results[1].Type() == ast.CallExpression, "wrong result 1 type")243}244func TestSingleArrayLiteralReturnStatement(t *testing.T) {245 p := createParser(`return []int{}`)246 goutil.Assert(t, isReturnStatement(p), "should detect return statement")247 parseReturnStatement(p)248 u := p.scope.Next()249 goutil.AssertNow(t, u.Type() == ast.ReturnStatement, "wrong return type")250 r := u.(*ast.ReturnStatementNode)251 goutil.AssertNow(t, len(r.Results) == 1, "wrong result length")252 goutil.AssertNow(t, r.Results[0].Type() == ast.ArrayLiteral, "wrong result 0 type")253}254func TestMultipleArrayLiteralReturnStatement(t *testing.T) {255 p := createParser(`return []string{"one", "two"}, []Dog{}`)256 goutil.Assert(t, isReturnStatement(p), "should detect return statement")257 parseReturnStatement(p)258 u := p.scope.Next()259 goutil.AssertNow(t, u.Type() == ast.ReturnStatement, "wrong return type")260 r := u.(*ast.ReturnStatementNode)261 goutil.AssertNow(t, len(r.Results) == 2, "wrong result length")262 goutil.AssertNow(t, r.Results[0].Type() == ast.ArrayLiteral, "wrong result 0 type")263 goutil.AssertNow(t, r.Results[1].Type() == ast.ArrayLiteral, "wrong result 1 type")264}265func TestSingleMapLiteralReturnStatement(t *testing.T) {266 p := createParser(`return map[string]int{"one":2, "two":3}`)267 goutil.Assert(t, isReturnStatement(p), "should detect return statement")268 parseReturnStatement(p)269 u := p.scope.Next()270 goutil.AssertNow(t, u.Type() == ast.ReturnStatement, "wrong return type")271 r := u.(*ast.ReturnStatementNode)272 goutil.AssertNow(t, len(r.Results) == 1, "wrong result length")273 goutil.AssertNow(t, r.Results[0].Type() == ast.MapLiteral, "wrong result 0 type")274}275func TestMultipleMapLiteralReturnStatement(t *testing.T) {276 p := createParser(`return map[string]int{"one":2, "two":3}, map[int]Dog{}`)277 goutil.Assert(t, isReturnStatement(p), "should detect return statement")278 parseReturnStatement(p)279 u := p.scope.Next()280 goutil.AssertNow(t, u.Type() == ast.ReturnStatement, "wrong return type")281 r := u.(*ast.ReturnStatementNode)282 goutil.AssertNow(t, len(r.Results) == 2, "wrong result length")283 goutil.AssertNow(t, r.Results[0].Type() == ast.MapLiteral, "wrong result 0 type")284 goutil.AssertNow(t, r.Results[1].Type() == ast.MapLiteral, "wrong result 1 type")285}286func TestSingleCompositeLiteralReturnStatement(t *testing.T) {287 p := createParser(`return Dog{}`)288 goutil.Assert(t, isReturnStatement(p), "should detect return statement")289 parseReturnStatement(p)290 u := p.scope.Next()291 goutil.AssertNow(t, u.Type() == ast.ReturnStatement, "wrong return type")292 r := u.(*ast.ReturnStatementNode)293 goutil.AssertNow(t, len(r.Results) == 1, "wrong result length")294 goutil.AssertNow(t, r.Results[0].Type() == ast.CompositeLiteral, "wrong result 0 type")295}296func TestMultipleCompositeLiteralReturnStatement(t *testing.T) {297 p := createParser(`return Cat{name:"Doggo", age:"Five"}, Dog{name:"Katter"}`)298 goutil.Assert(t, isReturnStatement(p), "should detect return statement")299 parseReturnStatement(p)300 u := p.scope.Next()301 goutil.AssertNow(t, u.Type() == ast.ReturnStatement, "wrong return type")302 r := u.(*ast.ReturnStatementNode)303 goutil.AssertNow(t, len(r.Results) == 2, fmt.Sprintf("wrong result length: %d", len(r.Results)))304 goutil.AssertNow(t, r.Results[0].Type() == ast.CompositeLiteral, "wrong result 0 type")305 goutil.AssertNow(t, r.Results[1].Type() == ast.CompositeLiteral, "wrong result 1 type")306}307func TestSimpleLiteralAssignmentStatement(t *testing.T) {308 p, _ := ParseString("x = 5")309 n := p.Next()310 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")311 a := n.(*ast.AssignmentStatementNode)312 goutil.AssertNow(t, len(a.Left) == 1, "should be one left value")313 goutil.AssertNow(t, a.Left[0].Type() == ast.Identifier, "wrong left type")314}315func TestIncrementReferenceAssignmentStatement(t *testing.T) {316 p, _ := ParseString("x++")317 n := p.Next()318 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")319 a := n.(*ast.AssignmentStatementNode)320 goutil.AssertNow(t, len(a.Left) == 1, "should be one left value")321 goutil.AssertNow(t, a.Left[0].Type() == ast.Identifier, "wrong left type")322}323func TestMultiToSingleLiteralAssignmentStatement(t *testing.T) {324 p, _ := ParseString("x, y = 5")325 n := p.Next()326 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")327 a := n.(*ast.AssignmentStatementNode)328 goutil.AssertNow(t, len(a.Left) == 2, "should be two left values")329 goutil.AssertNow(t, a.Left[0].Type() == ast.Identifier, "wrong left type")330}331func TestIndexReferenceAssignmentStatement(t *testing.T) {332 a, _ := ParseString("voters[chairperson].weight = 1")333 goutil.AssertNow(t, a != nil, "ast should not be nil")334 n := a.Next()335 goutil.AssertNow(t, len(a.Sequence) == 1, fmt.Sprintf("wrong sequence length: %d", len(a.Sequence)))336 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong type")337}338func TestCompositeLiteralAssignmentStatement(t *testing.T) {339 a, _ := ParseString(`proposals = append(proposals, Proposal{340 name: proposalNames[i],341 voteCount: 0,342 })`)343 n := a.Next()344 goutil.AssertNow(t, len(a.Sequence) == 1, fmt.Sprintf("wrong sequence length: %d\n", len(a.Sequence)))345 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong type")346}347func TestMultiLiteralAssignmentStatement(t *testing.T) {348 p, _ := ParseString("x, y = 5, 3")349 n := p.Next()350 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")351 a := n.(*ast.AssignmentStatementNode)352 goutil.AssertNow(t, len(a.Left) == 2, "should be two left values")353}354func TestSimpleReferenceAssignmentStatement(t *testing.T) {355 p, _ := ParseString("x = a")356 n := p.Next()357 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")358 a := n.(*ast.AssignmentStatementNode)359 goutil.AssertNow(t, len(a.Left) == 1, "should be 1 left value")360}361func TestMultiToSingleReferenceAssignmentStatement(t *testing.T) {362 p, _ := ParseString("x, y = a")363 n := p.Next()364 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")365 a := n.(*ast.AssignmentStatementNode)366 goutil.AssertNow(t, len(a.Left) == 2, "should be two left values")367}368func TestMultiReferenceAssignmentStatement(t *testing.T) {369 p, _ := ParseString("x, y = a, b")370 n := p.Next()371 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")372 a := n.(*ast.AssignmentStatementNode)373 goutil.AssertNow(t, len(a.Left) == 2, "should be two left values")374}375func TestSimpleCallAssignmentStatement(t *testing.T) {376 p, _ := ParseString("x = a()")377 n := p.Next()378 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")379 a := n.(*ast.AssignmentStatementNode)380 goutil.AssertNow(t, len(a.Left) == 1, "should be one left values")381}382func TestMultiToSingleCallAssignmentStatement(t *testing.T) {383 p, _ := ParseString("x, y = ab()")384 n := p.Next()385 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")386 a := n.(*ast.AssignmentStatementNode)387 goutil.AssertNow(t, len(a.Left) == 2, "should be two left values")388}389func TestMultiCallAssignmentStatement(t *testing.T) {390 p, _ := ParseString("x, y = a(), b()")391 n := p.Next()392 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")393 a := n.(*ast.AssignmentStatementNode)394 goutil.AssertNow(t, len(a.Left) == 2, "should be two left values")395}396func TestSimpleCompositeLiteralAssignmentStatement(t *testing.T) {397 p, _ := ParseString("x = Dog{}")398 n := p.Next()399 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")400 a := n.(*ast.AssignmentStatementNode)401 goutil.AssertNow(t, len(a.Left) == 1, "should be two left values")402}403func TestMultiToSingleCompositeLiteralAssignmentStatement(t *testing.T) {404 p, _ := ParseString("x, y = Dog{}")405 n := p.Next()406 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")407 a := n.(*ast.AssignmentStatementNode)408 goutil.AssertNow(t, len(a.Left) == 2, "should be two left values")409}410func TestMultiCompositeLiteralAssignmentStatement(t *testing.T) {411 p, _ := ParseString("x, y = Dog{}, Cat{}")412 n := p.Next()413 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")414 a := n.(*ast.AssignmentStatementNode)415 goutil.AssertNow(t, len(a.Left) == 2, "should be two left values")416}417func TestSimpleArrayLiteralAssignmentStatement(t *testing.T) {418 p, _ := ParseString("x = []int{3, 5}")419 n := p.Next()420 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")421 a := n.(*ast.AssignmentStatementNode)422 goutil.AssertNow(t, len(a.Left) == 1, "should be 1 left values")423}424func TestMultiToSingleArrayLiteralAssignmentStatement(t *testing.T) {425 p, _ := ParseString("x, y = []int{3, 5}")426 n := p.Next()427 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")428 a := n.(*ast.AssignmentStatementNode)429 goutil.AssertNow(t, len(a.Left) == 2, "should be two left values")430}431func TestMultiArrayLiteralAssignmentStatement(t *testing.T) {432 p, _ := ParseString("x, y = []int{1, 2}, [int]{}")433 n := p.Next()434 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")435 a := n.(*ast.AssignmentStatementNode)436 goutil.AssertNow(t, len(a.Left) == 2, "should be two left values")437}438func TestSimpleMapLiteralAssignmentStatement(t *testing.T) {439 p, _ := ParseString("x = []int{3, 5}")440 n := p.Next()441 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")442 a := n.(*ast.AssignmentStatementNode)443 goutil.AssertNow(t, len(a.Left) == 1, "should be 1 left values")444}445func TestMultiToSingleMapLiteralAssignmentStatement(t *testing.T) {446 p, _ := ParseString("x, y = []int{3, 5}")447 n := p.Next()448 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")449 a := n.(*ast.AssignmentStatementNode)450 goutil.AssertNow(t, len(a.Left) == 2, "should be two left values")451}452func TestMultiMapLiteralAssignmentStatement(t *testing.T) {453 p, _ := ParseString(`x, y = map[int]string{1:"A", 2:"B"}, map[string]int{"A":3, "B": 4}`)454 n := p.Next()455 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")456 a := n.(*ast.AssignmentStatementNode)457 goutil.AssertNow(t, len(a.Left) == 2, "should be two left values")458}459func TestAssignmentStatementSingleAdd(t *testing.T) {460 p, _ := ParseString(`x += 5`)461 n := p.Next()462 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")463 a := n.(*ast.AssignmentStatementNode)464 goutil.AssertNow(t, len(a.Left) == 1, "should be one left value")465}466func TestAssignmentStatementArrayLiteral(t *testing.T) {467 p, errs := ParseString(`x = []string{"a"}`)468 n := p.Next()469 goutil.AssertNow(t, n.Type() == ast.AssignmentStatement, "wrong assignment type")470 a := n.(*ast.AssignmentStatementNode)471 goutil.AssertNow(t, len(a.Left) == 1, "should be one left value")472 goutil.AssertNow(t, errs == nil, errs.Format())473}474func TestImportStatementPath(t *testing.T) {475 p := createParser(`import "dog"`)476 goutil.Assert(t, isImportStatement(p), "should detect import statement")477 parseImportStatement(p)478 n := p.scope.Next()479 goutil.AssertNow(t, n.Type() == ast.ImportStatement, "wrong import type")480 a := n.(*ast.ImportStatementNode)481 goutil.AssertNow(t, a.Path == "dog", "wrong import path value")482}483func TestImportStatementAlias(t *testing.T) {484 p := createParser(`import "dog" as d`)485 goutil.Assert(t, isImportStatement(p), "should detect import statement")486 parseImportStatement(p)487 n := p.scope.Next()488 goutil.AssertNow(t, n.Type() == ast.ImportStatement, "wrong import type")489 a := n.(*ast.ImportStatementNode)490 goutil.AssertNow(t, a.Path == "dog", "wrong import path value")491 goutil.AssertNow(t, a.Alias == "d", "wrong import alias value")492}493func TestPackageStatement(t *testing.T) {494 p := createParser(`package dog at 0.0.1`)495 goutil.Assert(t, isPackageStatement(p), "should detect package statement")496 parsePackageStatement(p)497 n := p.scope.Next()498 goutil.AssertNow(t, n.Type() == ast.PackageStatement, "wrong package type")499 a := n.(*ast.PackageStatementNode)500 goutil.AssertNow(t, a.Name == "dog", "wrong package name value")501}502func TestForEachStatement(t *testing.T) {503 p := createParser(`for x, y in a {}504 `)505 goutil.Assert(t, isForEachStatement(p), "should detect for statement")506 parseForEachStatement(p)507 goutil.Assert(t, p.errs == nil, p.errs.Format())508 n := p.scope.Next()509 goutil.AssertNow(t, len(p.scope.Sequence) == 1, fmt.Sprintf("wrong sequence len: %d", len(p.scope.Sequence)))510 goutil.AssertNow(t, n.Type() == ast.ForEachStatement, "wrong node type")511 a := n.(*ast.ForEachStatementNode)512 goutil.AssertNow(t, len(a.Variables) == 2, "wrong var length")513}514func TestDeclaredForEachStatement(t *testing.T) {515 a, errs := ParseString(`516 a = []string{"a", "b"}517 for x, y in a {}518 `)519 goutil.Assert(t, errs == nil, errs.Format())520 goutil.AssertNow(t, len(a.Sequence) == 2, fmt.Sprintf("wrong sequence len: %d", len(a.Sequence)))521 a.Next()522 n := a.Next()523 goutil.AssertNow(t, n.Type() == ast.ForEachStatement, "wrong node type")524 p := n.(*ast.ForEachStatementNode)525 goutil.AssertNow(t, len(p.Variables) == 2, "wrong var length")526 goutil.AssertNow(t, p.Producer.Type() == ast.Identifier, "wrong producer")527}528func TestParseCaseStatementDouble(t *testing.T) {529 a, errs := ParseString(`530 case x > 5:531 break532 case x == 10:533 break534 `)535 goutil.AssertNow(t, len(errs) == 0, errs.Format())536 goutil.AssertNow(t, a != nil, "nil scope")537 goutil.AssertLength(t, len(a.Sequence), 2)538 one := a.Sequence[0]539 goutil.AssertNow(t, one.Type() == ast.CaseStatement, "1 not case statement")540 c1 := one.(*ast.CaseStatementNode)541 goutil.AssertLength(t, len(c1.Block.Sequence), 1)542 two := a.Sequence[1]543 goutil.AssertNow(t, two.Type() == ast.CaseStatement, "2 not case statement")544 c2 := two.(*ast.CaseStatementNode)545 goutil.AssertLength(t, len(c2.Block.Sequence), 1)546}547func TestParseSimpleAssignmentStatement(t *testing.T) {548 a, errs := ParseString(`549 if x = 0; x > 5 {550 }551 `)552 goutil.AssertNow(t, len(errs) == 0, errs.Format())553 goutil.AssertNow(t, a != nil, "nil scope")554 goutil.AssertLength(t, len(a.Sequence), 1)555 one := a.Sequence[0]556 goutil.AssertNow(t, one.Type() == ast.IfStatement, "1 not if statement")557 i := one.(*ast.IfStatementNode)558 goutil.AssertNow(t, i.Init != nil, "nil init")559 goutil.AssertNow(t, i.Init.Type() == ast.AssignmentStatement, "not assignment statement")560 as := i.Init.(*ast.AssignmentStatementNode)561 goutil.AssertLength(t, len(as.Left), 1)562 goutil.AssertLength(t, len(as.Right), 1)563 goutil.Assert(t, as.Left[0] != nil, "left is nil")564 goutil.Assert(t, as.Right[0] != nil, "right is nil")565}566func TestParseOrIfCondition(t *testing.T) {567 a, errs := ParseString(`568 if a == 0 or b == 0 {569 return 0570 }571 `)572 goutil.AssertNow(t, len(errs) == 0, errs.Format())573 goutil.AssertNow(t, a != nil, "nil scope")574 goutil.AssertLength(t, len(a.Sequence), 1)575}576func TestGenericStatementPlainType(t *testing.T) {577 a, errs := ParseString(`578 x = new List<string>()579 `)580 goutil.AssertNow(t, len(errs) == 0, errs.Format())581 goutil.AssertNow(t, a != nil, "nil scope")582 goutil.AssertLength(t, len(a.Sequence), 1)583}584func TestGenericStatementArrayType(t *testing.T) {585 a, errs := ParseString(`586 x = []List<string> {587 new List<string>([]string{"hi"}),588 }589 `)590 goutil.AssertNow(t, len(errs) == 0, errs.Format())591 goutil.AssertNow(t, a != nil, "nil scope")592 goutil.AssertLength(t, len(a.Sequence), 1)593}594func TestMultiLineMapTypeAssignment(t *testing.T) {595 a, errs := ParseString(`596 x = map[int]int{597 5: 4,598 3: 1,599 }600 `)601 goutil.AssertNow(t, len(errs) == 0, errs.Format())602 goutil.AssertNow(t, a != nil, "nil scope")603 goutil.AssertLength(t, len(a.Sequence), 1)604}605func TestParseForStatementMultipleSimples(t *testing.T) {606 a, errs := ParseString(`607 for i, j, k = 0, 0, 0; i < j < k; j++ {608 }609 `)610 goutil.AssertNow(t, len(errs) == 0, errs.Format())611 goutil.AssertNow(t, a != nil, "nil scope")612 goutil.AssertLength(t, len(a.Sequence), 1)613}614func TestParseMultiplePostInvalid(t *testing.T) {615 a, errs := ParseString(`616 i, j++617 `)618 goutil.AssertNow(t, len(errs) == 1, errs.Format())619 goutil.AssertNow(t, a != nil, "nil scope")620 goutil.AssertLength(t, len(a.Sequence), 1)621}622func TestParseDecrement(t *testing.T) {623 a, errs := ParseString(`624 i--625 `)626 goutil.AssertNow(t, len(errs) == 0, errs.Format())627 goutil.AssertNow(t, a != nil, "nil scope")628 goutil.AssertLength(t, len(a.Sequence), 1)629}630func TestParseEmptyReturn(t *testing.T) {631 a, errs := ParseString(`632 return633 `)634 goutil.AssertNow(t, len(errs) == 0, errs.Format())635 goutil.AssertNow(t, a != nil, "nil scope")636 goutil.AssertLength(t, len(a.Sequence), 1)637 r := a.Sequence[0].(*ast.ReturnStatementNode)638 goutil.AssertLength(t, len(r.Results), 0)639}640func TestImportGroup(t *testing.T) {641 _, errs := ParseString(`642 import (643 "dog" as d644 "cat" as c645 )646 `)647 goutil.AssertNow(t, len(errs) == 0, errs.Format())648}649func TestStrangeAssignmentSameLine(t *testing.T) {650 _, errs := ParseString(`x = 7 y = 6`)651 goutil.AssertNow(t, len(errs) == 1, errs.Format())652}653func TestStrangeAssignmentWithLineComment(t *testing.T) {654 _, errs := ParseString(`x = 7 // hi655 y = 6`)656 goutil.AssertNow(t, len(errs) == 0, errs.Format())657}658func TestStrangeAssignmentWithMultilineComment(t *testing.T) {659 _, errs := ParseString(`x = 7 /* hi */660 y = 6`)661 goutil.AssertNow(t, len(errs) == 0, errs.Format())662}...

Full Screen

Full Screen

yaml_parser_test.go

Source:yaml_parser_test.go Github

copy

Full Screen

1package yamlParser2import (3 "fmt"4 "testing"5 "github.com/alecthomas/participle/v2"6)7func TestAddress(t *testing.T) {8 localParser := participle.MustBuild(&Arg{},9 participle.Lexer(yamlLexer),10 participle.Elide("Comment", "Whitespace"),11 participle.UseLookahead(2),12 )13 ast := &Arg{}14 fileContents := `0x000000000000000000000000000000000000dEaD`15 err := localParser.ParseString("fileName", fileContents, ast)16 if err != nil {17 fmt.Println(err)18 t.Fail()19 }20}21func TestUint256(t *testing.T) {22 localParser := participle.MustBuild(&Arg{},23 participle.Lexer(yamlLexer),24 participle.Elide("Comment", "Whitespace"),25 participle.UseLookahead(2),26 )27 ast := &Arg{}28 fileContents := "234092340923"29 err := localParser.ParseString("fileName", fileContents, ast)30 if err != nil {31 fmt.Println(err)32 t.Fail()33 }34}35func TestTriggerConditions(t *testing.T) {36 //test when block37 localParser := participle.MustBuild(&Trigger{},38 participle.Lexer(yamlLexer),39 participle.Elide("Comment", "Whitespace"),40 participle.UseLookahead(2),41 )42 ast := &Trigger{}43 fileContents := "WHEN BLOCK == 234034:"44 err := localParser.ParseString("fileName", fileContents, ast)45 if err != nil {46 fmt.Println(err)47 t.Fail()48 }49 //test on event50 ast = &Trigger{}51 fileContents = "ON EVENT 0x000000000000000000000000000000000000dEaD(0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c):"52 err = localParser.ParseString("fileName", fileContents, ast)53 if err != nil {54 fmt.Println(err)55 t.Fail()56 }57 //test every x interval58 ast = &Trigger{}59 fileContents = "EVERY BLOCK:"60 err = localParser.ParseString("fileName", fileContents, ast)61 if err != nil {62 fmt.Println(err)63 t.Fail()64 }65 //test every x blocks66 ast = &Trigger{}67 fileContents = "EVERY 309324 BLOCKS:"68 err = localParser.ParseString("fileName", fileContents, ast)69 if err != nil {70 fmt.Println(err)71 t.Fail()72 }73 //test every block74 fileContents = "EVERY BLOCK:"75 ast = &Trigger{}76 err = localParser.ParseString("fileName", fileContents, ast)77 if err != nil {78 fmt.Println(err)79 t.Fail()80 }81}82// func TestCall(t *testing.T) {83// localParser := participle.MustBuild(&Call{},84// participle.Lexer(yamlLexer),85// participle.Elide("Comment", "Whitespace"),86// participle.UseLookahead(2),87// )88// ast := &Call{}89// fileContents := "CALL 0x000000000000000000000000000000000000dEaD(functionSig(arg1,arg2), 0x000000000000000000000000000000000000dEaD, 2093490234)"90// err := localParser.ParseString("fileName", fileContents, ast)91// if err != nil {92// fmt.Println(err)93// t.Fail()94// }95// }96// func TestTx(t *testing.T) {97// localParser := participle.MustBuild(&Tx{},98// participle.Lexer(yamlLexer),99// participle.Elide("Comment", "Whitespace"),100// participle.UseLookahead(2),101// )102// ast := &Tx{}103// fileContents := "TX: 0x000000000000000000000000000000000000dEaD(functionSig())"104// // err := localParser.ParseString("fileName", fileContents, ast)105// // if err != nil {106// // fmt.Println(err)107// // t.Fail()108// // }109// }110func TestAction(t *testing.T) {111 localParser := participle.MustBuild(&Action{},112 participle.Lexer(yamlLexer),113 participle.Elide("Comment", "Whitespace"),114 participle.UseLookahead(2),115 )116 ast := &Action{}117 fileContents := "TX: 0x000000000000000000000000000000000000dEaD(functionSig())"118 err := localParser.ParseString("fileName", fileContents, ast)119 if err != nil {120 fmt.Println(err)121 t.Fail()122 }123}124func TestActions(t *testing.T) {125 localParser := participle.MustBuild(&Actions{},126 participle.Lexer(yamlLexer),127 participle.Elide("Comment", "Whitespace"),128 participle.UseLookahead(2),129 )130 ast := &Actions{}131 fileContents := `TX: 0x000000000000000000000000000000000000dEaD(functionSig())132 TX: 0x000000000000000000000000000000000000dEaD(functionSig())`133 err := localParser.ParseString("fileName", fileContents, ast)134 if err != nil {135 fmt.Println(err)136 t.Fail()137 }138}139func TestAutomationTask(t *testing.T) {140 localParser := participle.MustBuild(&AutomationTask{},141 participle.Lexer(yamlLexer),142 participle.Elide("Comment", "Whitespace"),143 participle.UseLookahead(2),144 )145 ast := &AutomationTask{}146 fileContents := `147 EVERY 10 BLOCKS:148 TX: 0x000000000000000000000000000000000000dEaD(functionSig())149 `150 err := localParser.ParseString("fileName", fileContents, ast)151 if err != nil {152 fmt.Println(err)153 t.Fail()154 }155}156func TestYamlFile(t *testing.T) {157 localParser := participle.MustBuild(&YamlFile{},158 participle.Lexer(yamlLexer),159 participle.Elide("Comment", "Whitespace"),160 participle.UseLookahead(2),161 )162 ast := &YamlFile{}163 fileContents := `164 165 EVERY 10 BLOCKS:166 TX: 0x000000000000000000000000000000000000dEaD(functionSig())167 WHEN BLOCK == 1000230493:168 TX: 0x000000000000000000000000000000000000dEaD(functionSig())169 170 EVERY BLOCK:171 TX: 0x000000000000000000000000000000000000dEaD(functionSig(address,uint256), 2923409234, 092309234)172 WHEN BLOCK == 1000230493:173 TX: 0x000000000000000000000000000000000000dEaD(functionSig())174 175 176 WHEN BLOCK == 23034093409234:177 TX: 0x000000000000000000000000000000000000dEaD(functionSig())178 WHEN BLOCK == 1000230493:179 TX: 0x000000000000000000000000000000000000dEaD(functionSig())180 181 182 ON EVENT 0x000000000000000000000000000000000000dEaD(0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c):183 TX: 0x000000000000000000000000000000000000dEaD(functionSig())184 185 WHEN BLOCK == 1000230493:186 TX: 0x000000000000000000000000000000000000dEaD(functionSig())187 188 `189 err := localParser.ParseString("fileName", fileContents, ast)190 if err != nil {191 fmt.Println(err)192 t.Fail()193 }194}...

Full Screen

Full Screen

parseString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)4 if err != nil {5 fmt.Println(err)6 }7 ast.Print(fset, f)8}9import (10func main() {11 fmt.Println("Hello, playground")12}13import "fmt"14func main() { fmt.Println("Hello, playground") }15Print(fset *token.FileSet, node Node)16import (17func main() {18 f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)19 if err != nil {20 fmt.Println(err)21 }22 ast.Print(fset, f)23}24import (25func main() {26 fmt.Println("Hello, playground")27}28import "fmt"29func main() { fmt.Println("Hello, playground") }

Full Screen

Full Screen

parseString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)4 if err != nil {5 fmt.Println(err)6 }7 ast.Print(fset, f)8}9import "fmt"10func main() {11 fmt.Println("Hello, playground")12}13import "fmt"14func main() {15 fmt.Println("Hello, playground")16}17import (18func main() {19 expr, err := parser.ParseExprFrom(fset, "1.go", "1+1", 0)20 if err != nil {21 fmt.Println(err)22 }23 ast.Print(fset, expr)24}25import (26func main() {27 expr, err := parser.ParseExpr("1+1")28 if err != nil {29 fmt.Println(err)30 }

Full Screen

Full Screen

parseString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 s := scan.NewScanner("1 + 2 * 3")4 root, err := ast.ParseString(s)5 if err != nil {6 panic(err)7 }8 fmt.Println(root)9 fmt.Println(root.Eval())10}11import (12func main() {13 s := scan.NewScanner("1 + 2 * 3")14 root, err := ast.ParseString(s)15 if err != nil {16 panic(err)17 }18 fmt.Println(root)19 fmt.Println(root.Eval())20}21import (22func main() {23 s := scan.NewScanner("1 + 2 * 3")24 root, err := ast.ParseString(s)25 if err != nil {26 panic(err)27 }28 fmt.Println(root)29 fmt.Println(root.Eval())30}31import (32func main() {33 s := scan.NewScanner("1 +

Full Screen

Full Screen

parseString

Using AI Code Generation

copy

Full Screen

1import java.io.*;2import java.util.*;3{4 static String str;5 static int i;6 static int j;7 static int k;8 static int l;9 static int m;10 static int n;11 static int o;12 static int p;13 static int q;14 static int r;15 static int s;16 static int t;17 static int u;18 static int v;19 static int w;20 static int x;21 static int y;22 static int z;23 static int aa;24 static int ab;25 static int ac;26 static int ad;27 static int ae;28 static int af;29 static int ag;30 static int ah;31 static int ai;32 static int aj;33 static int ak;34 static int al;35 static int am;36 static int an;37 static int ao;38 static int ap;39 static int aq;40 static int ar;41 static int as;42 static int at;43 static int au;44 static int av;45 static int aw;46 static int ax;47 static int ay;48 static int az;49 static int ba;50 static int bb;51 static int bc;52 static int bd;53 static int be;54 static int bf;55 static int bg;56 static int bh;57 static int bi;58 static int bj;59 static int bk;60 static int bl;61 static int bm;62 static int bn;63 static int bo;64 static int bp;65 static int bq;66 static int br;67 static int bs;68 static int bt;69 static int bu;70 static int bv;71 static int bw;72 static int bx;73 static int by;74 static int bz;75 static int ca;76 static int cb;77 static int cc;78 static int cd;79 static int ce;80 static int cf;81 static int cg;82 static int ch;83 static int ci;84 static int cj;85 static int ck;86 static int cl;87 static int cm;88 static int cn;89 static int co;90 static int cp;91 static int cq;92 static int cr;93 static int cs;94 static int ct;95 static int cu;96 static int cv;97 static int cw;98 static int cx;99 static int cy;100 static int cz;

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful