github.com/ladydascalie/elvish@v0.0.0-20170703214355-2964dd3ece7f/parse/pprint_test.go (about) 1 package parse 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 var pprintCases = []struct { 9 src string 10 wantAST string 11 wantParseTree string 12 }{ 13 {"ls $x[0]$y[1];echo", 14 `Chunk 15 Pipeline/Form 16 Compound/Indexing/Primary Type=Bareword Value="ls" IsRange=[] 17 Compound 18 Indexing 19 Primary Type=Variable Value="x" IsRange=[] 20 Array/Compound/Indexing/Primary Type=Bareword Value="0" IsRange=[] 21 Indexing 22 Primary Type=Variable Value="y" IsRange=[] 23 Array/Compound/Indexing/Primary Type=Bareword Value="1" IsRange=[] 24 Pipeline/Form/Compound/Indexing/Primary Type=Bareword Value="echo" IsRange=[] 25 `, 26 `Chunk "ls $x[0]$y[1];echo" 0-18 27 Pipeline/Form "ls $x[0]$y[1]" 0-13 28 Compound/Indexing/Primary "ls" 0-2 29 Sep " " 2-3 30 Compound "$x[0]$y[1]" 3-13 31 Indexing "$x[0]" 3-8 32 Primary "$x" 3-5 33 Sep "[" 5-6 34 Array/Compound/Indexing/Primary "0" 6-7 35 Sep "]" 7-8 36 Indexing "$y[1]" 8-13 37 Primary "$y" 8-10 38 Sep "[" 10-11 39 Array/Compound/Indexing/Primary "1" 11-12 40 Sep "]" 12-13 41 Sep ";" 13-14 42 Pipeline/Form/Compound/Indexing/Primary "echo" 14-18 43 `}, 44 } 45 46 func TestPprint(t *testing.T) { 47 for _, tc := range pprintCases { 48 n, err := Parse("[test]", tc.src) 49 if err != nil { 50 t.Error(err) 51 } 52 var b bytes.Buffer 53 PprintAST(n, &b) 54 ast := b.String() 55 if b.String() != tc.wantAST { 56 t.Errorf("PprintAST(%q):\n%s\nwant:\n%s", tc.src, ast, tc.wantAST) 57 } 58 b = bytes.Buffer{} 59 PprintParseTree(n, &b) 60 pt := b.String() 61 if pt != tc.wantParseTree { 62 t.Errorf("PprintParseTree(%q):\n%s\nwant:\n%s", tc.src, pt, tc.wantParseTree) 63 } 64 } 65 }