2016-02-17 08:05:51 +08:00
|
|
|
package parse
|
|
|
|
|
|
|
|
import (
|
2018-10-13 00:54:33 +08:00
|
|
|
"strings"
|
2016-02-17 08:05:51 +08:00
|
|
|
"testing"
|
2018-03-17 07:06:23 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/tt"
|
2016-02-17 08:05:51 +08:00
|
|
|
)
|
|
|
|
|
2021-05-29 12:42:38 +08:00
|
|
|
var n = mustParse("ls $x[0]$y[1];echo done >/redir-dest")
|
2018-03-17 07:06:23 +08:00
|
|
|
|
|
|
|
var pprintASTTests = tt.Table{
|
2022-03-25 10:41:49 +08:00
|
|
|
Args(n).Rets(
|
2016-02-17 08:05:51 +08:00
|
|
|
`Chunk
|
|
|
|
Pipeline/Form
|
2022-01-03 08:45:15 +08:00
|
|
|
Compound/Indexing/Primary ExprCtx=CmdExpr Type=Bareword Value="ls"
|
2018-10-13 23:02:19 +08:00
|
|
|
Compound ExprCtx=NormalExpr
|
|
|
|
Indexing ExprCtx=NormalExpr
|
2022-01-03 08:45:15 +08:00
|
|
|
Primary ExprCtx=NormalExpr Type=Variable Value="x"
|
|
|
|
Array/Compound/Indexing/Primary ExprCtx=NormalExpr Type=Bareword Value="0"
|
2018-10-13 23:02:19 +08:00
|
|
|
Indexing ExprCtx=NormalExpr
|
2022-01-03 08:45:15 +08:00
|
|
|
Primary ExprCtx=NormalExpr Type=Variable Value="y"
|
|
|
|
Array/Compound/Indexing/Primary ExprCtx=NormalExpr Type=Bareword Value="1"
|
2021-05-29 12:42:38 +08:00
|
|
|
Pipeline/Form
|
2022-01-03 08:45:15 +08:00
|
|
|
Compound/Indexing/Primary ExprCtx=CmdExpr Type=Bareword Value="echo"
|
|
|
|
Compound/Indexing/Primary ExprCtx=NormalExpr Type=Bareword Value="done"
|
2021-05-29 12:42:38 +08:00
|
|
|
Redir Mode=Write RightIsFd=false
|
2022-01-03 08:45:15 +08:00
|
|
|
Compound/Indexing/Primary ExprCtx=NormalExpr Type=Bareword Value="/redir-dest"
|
2018-03-17 07:06:23 +08:00
|
|
|
`),
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPPrintAST(t *testing.T) {
|
2018-10-13 00:54:33 +08:00
|
|
|
tt.Test(t, tt.Fn("PPrintAST (to string)", func(n Node) string {
|
|
|
|
var b strings.Builder
|
2020-04-26 01:31:49 +08:00
|
|
|
pprintAST(n, &b)
|
2018-10-13 00:54:33 +08:00
|
|
|
return b.String()
|
|
|
|
}), pprintASTTests)
|
2018-03-17 07:06:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var pprintParseTreeTests = tt.Table{
|
2022-03-25 10:41:49 +08:00
|
|
|
Args(n).Rets(
|
2021-05-29 12:42:38 +08:00
|
|
|
`Chunk "ls $x[0]$y...redir-dest" 0-36
|
2016-02-17 08:05:51 +08:00
|
|
|
Pipeline/Form "ls $x[0]$y[1]" 0-13
|
|
|
|
Compound/Indexing/Primary "ls" 0-2
|
|
|
|
Sep " " 2-3
|
|
|
|
Compound "$x[0]$y[1]" 3-13
|
|
|
|
Indexing "$x[0]" 3-8
|
|
|
|
Primary "$x" 3-5
|
|
|
|
Sep "[" 5-6
|
|
|
|
Array/Compound/Indexing/Primary "0" 6-7
|
|
|
|
Sep "]" 7-8
|
|
|
|
Indexing "$y[1]" 8-13
|
|
|
|
Primary "$y" 8-10
|
|
|
|
Sep "[" 10-11
|
|
|
|
Array/Compound/Indexing/Primary "1" 11-12
|
|
|
|
Sep "]" 12-13
|
|
|
|
Sep ";" 13-14
|
2021-05-29 12:42:38 +08:00
|
|
|
Pipeline/Form "echo done >/redir-dest" 14-36
|
|
|
|
Compound/Indexing/Primary "echo" 14-18
|
|
|
|
Sep " " 18-19
|
|
|
|
Compound/Indexing/Primary "done" 19-23
|
|
|
|
Sep " " 23-24
|
|
|
|
Redir ">/redir-dest" 24-36
|
|
|
|
Sep ">" 24-25
|
|
|
|
Compound/Indexing/Primary "/redir-dest" 25-36
|
2018-03-17 07:06:23 +08:00
|
|
|
`),
|
|
|
|
}
|
|
|
|
|
2018-10-13 23:51:00 +08:00
|
|
|
func TestPPrintParseTree(t *testing.T) {
|
2018-10-13 00:54:33 +08:00
|
|
|
tt.Test(t, tt.Fn("PPrintParseTree (to string)", func(n Node) string {
|
|
|
|
var b strings.Builder
|
2020-04-26 01:31:49 +08:00
|
|
|
pprintParseTree(n, &b)
|
2018-10-13 00:54:33 +08:00
|
|
|
return b.String()
|
|
|
|
}), pprintParseTreeTests)
|
2016-02-17 08:05:51 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 07:06:23 +08:00
|
|
|
func mustParse(src string) Node {
|
2021-02-01 22:17:57 +08:00
|
|
|
tree, err := Parse(SourceForTest(src), Config{})
|
2018-03-17 07:06:23 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2016-02-17 08:05:51 +08:00
|
|
|
}
|
2020-04-26 02:15:37 +08:00
|
|
|
return tree.Root
|
2016-02-17 08:05:51 +08:00
|
|
|
}
|