github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/parse/pprint_test.go (about)

     1  package parse
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/markusbkk/elvish/pkg/tt"
     8  )
     9  
    10  var n = mustParse("ls $x[0]$y[1];echo done >/redir-dest")
    11  
    12  var pprintASTTests = tt.Table{
    13  	tt.Args(n).Rets(
    14  		`Chunk
    15    Pipeline/Form
    16      Compound/Indexing/Primary ExprCtx=CmdExpr Type=Bareword Value="ls"
    17      Compound ExprCtx=NormalExpr
    18        Indexing ExprCtx=NormalExpr
    19          Primary ExprCtx=NormalExpr Type=Variable Value="x"
    20          Array/Compound/Indexing/Primary ExprCtx=NormalExpr Type=Bareword Value="0"
    21        Indexing ExprCtx=NormalExpr
    22          Primary ExprCtx=NormalExpr Type=Variable Value="y"
    23          Array/Compound/Indexing/Primary ExprCtx=NormalExpr Type=Bareword Value="1"
    24    Pipeline/Form
    25      Compound/Indexing/Primary ExprCtx=CmdExpr Type=Bareword Value="echo"
    26      Compound/Indexing/Primary ExprCtx=NormalExpr Type=Bareword Value="done"
    27      Redir Mode=Write RightIsFd=false
    28        Compound/Indexing/Primary ExprCtx=NormalExpr Type=Bareword Value="/redir-dest"
    29  `),
    30  }
    31  
    32  func TestPPrintAST(t *testing.T) {
    33  	tt.Test(t, tt.Fn("PPrintAST (to string)", func(n Node) string {
    34  		var b strings.Builder
    35  		pprintAST(n, &b)
    36  		return b.String()
    37  	}), pprintASTTests)
    38  }
    39  
    40  var pprintParseTreeTests = tt.Table{
    41  	tt.Args(n).Rets(
    42  		`Chunk "ls $x[0]$y...redir-dest" 0-36
    43    Pipeline/Form "ls $x[0]$y[1]" 0-13
    44      Compound/Indexing/Primary "ls" 0-2
    45      Sep " " 2-3
    46      Compound "$x[0]$y[1]" 3-13
    47        Indexing "$x[0]" 3-8
    48          Primary "$x" 3-5
    49          Sep "[" 5-6
    50          Array/Compound/Indexing/Primary "0" 6-7
    51          Sep "]" 7-8
    52        Indexing "$y[1]" 8-13
    53          Primary "$y" 8-10
    54          Sep "[" 10-11
    55          Array/Compound/Indexing/Primary "1" 11-12
    56          Sep "]" 12-13
    57    Sep ";" 13-14
    58    Pipeline/Form "echo done >/redir-dest" 14-36
    59      Compound/Indexing/Primary "echo" 14-18
    60      Sep " " 18-19
    61      Compound/Indexing/Primary "done" 19-23
    62      Sep " " 23-24
    63      Redir ">/redir-dest" 24-36
    64        Sep ">" 24-25
    65        Compound/Indexing/Primary "/redir-dest" 25-36
    66  `),
    67  }
    68  
    69  func TestPPrintParseTree(t *testing.T) {
    70  	tt.Test(t, tt.Fn("PPrintParseTree (to string)", func(n Node) string {
    71  		var b strings.Builder
    72  		pprintParseTree(n, &b)
    73  		return b.String()
    74  	}), pprintParseTreeTests)
    75  }
    76  
    77  func mustParse(src string) Node {
    78  	tree, err := Parse(SourceForTest(src), Config{})
    79  	if err != nil {
    80  		panic(err)
    81  	}
    82  	return tree.Root
    83  }