src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/parse/pprint_test.go (about)

     1  package parse
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"src.elv.sh/pkg/tt"
     8  )
     9  
    10  var n = mustParse("ls $x[0]$y[1];echo done >/redir-dest")
    11  
    12  var pprintASTTests = []*tt.Case{
    13  	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  	pprintAST := func(n Node) string {
    34  		var b strings.Builder
    35  		pprintAST(n, &b)
    36  		return b.String()
    37  	}
    38  	tt.Test(t, tt.Fn(pprintAST).Named("pprintAST"), pprintASTTests...)
    39  }
    40  
    41  var pprintParseTreeTests = []*tt.Case{
    42  	Args(n).Rets(
    43  		`Chunk "ls $x[0]$y...redir-dest" 0-36
    44    Pipeline/Form "ls $x[0]$y[1]" 0-13
    45      Compound/Indexing/Primary "ls" 0-2
    46      Sep " " 2-3
    47      Compound "$x[0]$y[1]" 3-13
    48        Indexing "$x[0]" 3-8
    49          Primary "$x" 3-5
    50          Sep "[" 5-6
    51          Array/Compound/Indexing/Primary "0" 6-7
    52          Sep "]" 7-8
    53        Indexing "$y[1]" 8-13
    54          Primary "$y" 8-10
    55          Sep "[" 10-11
    56          Array/Compound/Indexing/Primary "1" 11-12
    57          Sep "]" 12-13
    58    Sep ";" 13-14
    59    Pipeline/Form "echo done >/redir-dest" 14-36
    60      Compound/Indexing/Primary "echo" 14-18
    61      Sep " " 18-19
    62      Compound/Indexing/Primary "done" 19-23
    63      Sep " " 23-24
    64      Redir ">/redir-dest" 24-36
    65        Sep ">" 24-25
    66        Compound/Indexing/Primary "/redir-dest" 25-36
    67  `),
    68  }
    69  
    70  func TestPPrintParseTree(t *testing.T) {
    71  	pprintParseTree := func(n Node) string {
    72  		var b strings.Builder
    73  		pprintParseTree(n, &b)
    74  		return b.String()
    75  	}
    76  	tt.Test(t, tt.Fn(pprintParseTree).Named("pprintParseTree"), pprintParseTreeTests...)
    77  }
    78  
    79  func mustParse(src string) Node {
    80  	tree, err := Parse(SourceForTest(src), Config{})
    81  	if err != nil {
    82  		panic(err)
    83  	}
    84  	return tree.Root
    85  }