1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
package parse
import (
"fmt"
"os"
"testing"
)
func a(c ...interface{}) ast {
// Shorthand used for checking Compound and levels beneath.
return ast{"Chunk/Pipeline/Form", fs{"Head": "a", "Args": c}}
}
var goodCases = []struct {
src string
ast ast
}{
// Chunk
// Smoke test.
{"a;b;c\n;d", ast{"Chunk", fs{"Pipelines": []string{"a", "b", "c", "d"}}}},
// Empty chunk should have Pipelines=nil.
{"", ast{"Chunk", fs{"Pipelines": nil}}},
// Superfluous newlines and semicolons should not result in empty
// pipelines.
{" ;\n\n ls \t ;\n", ast{"Chunk", fs{"Pipelines": []string{"ls \t "}}}},
// Pipeline
{"a|b|c|d", ast{
"Chunk/Pipeline", fs{"Forms": []string{"a", "b", "c", "d"}}}},
// Newlines are allowed after pipes.
{"a| \n \n b", ast{
"Chunk/Pipeline", fs{"Forms": []string{"a", "b"}}}},
// Comments.
{"a#haha\nb#lala", ast{
"Chunk", fs{"Pipelines": []string{"a", "b"}}}},
// Form
// Smoke test.
{"ls x y", ast{"Chunk/Pipeline/Form", fs{
"Head": "ls",
"Args": []string{"x", "y"}}}},
// Assignments.
{"k=v k[a][b]=v {a,b[1]}=(ha)", ast{"Chunk/Pipeline/Form", fs{
"Assignments": []string{"k=v", "k[a][b]=v", "{a,b[1]}=(ha)"}}}},
// Temporary assignment.
{"k=v k[a][b]=v a", ast{"Chunk/Pipeline/Form", fs{
"Assignments": []string{"k=v", "k[a][b]=v"},
"Head": "a"}}},
// Spacey assignment.
{"k=v a b = c d", ast{"Chunk/Pipeline/Form", fs{
"Assignments": []string{"k=v"},
"Vars": []string{"a", "b"},
"Args": []string{"c", "d"}}}},
// Redirections
{"a >b", ast{"Chunk/Pipeline/Form", fs{
"Head": "a",
"Redirs": []ast{
{"Redir", fs{"Mode": Write, "Right": "b"}}},
}}},
// More redirections
{"a >>b 2>b 3>&- 4>&1 5<c 6<>d", ast{"Chunk/Pipeline/Form", fs{
"Head": "a",
"Redirs": []ast{
{"Redir", fs{"Mode": Append, "Right": "b"}},
{"Redir", fs{"Left": "2", "Mode": Write, "Right": "b"}},
{"Redir", fs{"Left": "3", "Mode": Write, "RightIsFd": true, "Right": "-"}},
{"Redir", fs{"Left": "4", "Mode": Write, "RightIsFd": true, "Right": "1"}},
{"Redir", fs{"Left": "5", "Mode": Read, "Right": "c"}},
{"Redir", fs{"Left": "6", "Mode": ReadWrite, "Right": "d"}},
},
}}},
// Exitus redirection
{"a ?>$e", ast{"Chunk/Pipeline/Form", fs{
"Head": "a",
"ExitusRedir": ast{"ExitusRedir", fs{"Dest": "$e"}},
}}},
// Options (structure of MapPair tested below with map)
{"a &a=1 x &b=2", ast{"Chunk/Pipeline/Form", fs{
"Head": "a",
"Args": []string{"x"},
"Opts": []string{"&a=1", "&b=2"},
}}},
// Compound
{`a b"foo"?$c*'xyz'`, a(ast{"Compound", fs{
"Indexings": []string{"b", `"foo"`, "?", "$c", "*", "'xyz'"}}})},
// Indexing
{"a $b[c][d][\ne\n]", a(ast{"Compound/Indexing", fs{
"Head": "$b", "Indicies": []string{"c", "d", "\ne\n"},
}})},
// Primary
//
// Single quote
{"a '''x''y'''", a(ast{"Compound/Indexing/Primary", fs{
"Type": SingleQuoted, "Value": "'x'y'",
}})},
// Double quote
{`a "b\^[\x1b\u548c\U0002CE23\123\n\t\\"`,
a(ast{"Compound/Indexing/Primary", fs{
"Type": DoubleQuoted,
"Value": "b\x1b\x1b\u548c\U0002CE23\123\n\t\\",
}})},
// Wildcard
{"a * ?", a(
ast{"Compound/Indexing/Primary", fs{"Type": Wildcard, "Value": "*"}},
ast{"Compound/Indexing/Primary", fs{"Type": Wildcard, "Value": "?"}},
)},
// Variable
{"a $x $&f", a(
ast{"Compound/Indexing/Primary", fs{"Type": Variable, "Value": "x"}},
ast{"Compound/Indexing/Primary", fs{"Type": Variable, "Value": "&f"}},
)},
// List
{"a [] [ ] [1] [ 2] [3 ] [\n 4 \n5\n 6 7 \n]", a(
ast{"Compound/Indexing/Primary", fs{
"Type": List,
"Elements": []ast{}}},
ast{"Compound/Indexing/Primary", fs{
"Type": List,
"Elements": []ast{}}},
ast{"Compound/Indexing/Primary", fs{
"Type": List,
"Elements": []string{"1"}}},
ast{"Compound/Indexing/Primary", fs{
"Type": List,
"Elements": []string{"2"}}},
ast{"Compound/Indexing/Primary", fs{
"Type": List,
"Elements": []string{"3"}}},
ast{"Compound/Indexing/Primary", fs{
"Type": List,
"Elements": []string{"4", "5", "6", "7"}}},
)},
// Map
{"a [&k=v] [ &k=v] [&k=v ] [ &k=v ] [ &k= v] [&k= \n v] [\n&a=b &c=d \n &e=f\n\n]", a(
ast{"Compound/Indexing/Primary", fs{
"Type": Map,
"MapPairs": []ast{{"MapPair", fs{"Key": "k", "Value": "v"}}}}},
ast{"Compound/Indexing/Primary", fs{
"Type": Map,
"MapPairs": []ast{{"MapPair", fs{"Key": "k", "Value": "v"}}}}},
ast{"Compound/Indexing/Primary", fs{
"Type": Map,
"MapPairs": []ast{{"MapPair", fs{"Key": "k", "Value": "v"}}}}},
ast{"Compound/Indexing/Primary", fs{
"Type": Map,
"MapPairs": []ast{{"MapPair", fs{"Key": "k", "Value": "v"}}}}},
ast{"Compound/Indexing/Primary", fs{
"Type": Map,
"MapPairs": []ast{{"MapPair", fs{"Key": "k", "Value": "v"}}}}},
ast{"Compound/Indexing/Primary", fs{
"Type": Map,
"MapPairs": []ast{{"MapPair", fs{"Key": "k", "Value": "v"}}}}},
ast{"Compound/Indexing/Primary", fs{
"Type": Map,
"MapPairs": []ast{
{"MapPair", fs{"Key": "a", "Value": "b"}},
{"MapPair", fs{"Key": "c", "Value": "d"}},
{"MapPair", fs{"Key": "e", "Value": "f"}},
}}},
)},
// Empty map
{"a [&] [ &] [& ] [ & ]", a(
ast{"Compound/Indexing/Primary", fs{"Type": Map, "MapPairs": nil}},
ast{"Compound/Indexing/Primary", fs{"Type": Map, "MapPairs": nil}},
ast{"Compound/Indexing/Primary", fs{"Type": Map, "MapPairs": nil}},
ast{"Compound/Indexing/Primary", fs{"Type": Map, "MapPairs": nil}},
)},
// Lambda
{"a []{} [ ]{ } []{ echo 233 } [ x y ]{puts $x $y} { put haha}", a(
ast{"Compound/Indexing/Primary", fs{
"Type": Lambda, "Elements": []ast{}, "Chunk": "",
}},
ast{"Compound/Indexing/Primary", fs{
"Type": Lambda, "Elements": []ast{}, "Chunk": " ",
}},
ast{"Compound/Indexing/Primary", fs{
"Type": Lambda, "Elements": []ast{}, "Chunk": " echo 233 ",
}},
ast{"Compound/Indexing/Primary", fs{
"Type": Lambda, "Elements": []string{"x", "y"}, "Chunk": "puts $x $y",
}},
ast{"Compound/Indexing/Primary", fs{
"Type": Lambda, "Elements": []ast{}, "Chunk": " put haha",
}},
)},
// Lambda with arguments and options
{"a [a b &k=v]{}", a(
ast{"Compound/Indexing/Primary", fs{
"Type": Lambda,
"Elements": []string{"a", "b"},
"MapPairs": []string{"&k=v"},
"Chunk": "",
}},
)},
// Output capture
{"a () (b;c) (c\nd)", a(
ast{"Compound/Indexing/Primary", fs{
"Type": OutputCapture, "Chunk": ""}},
ast{"Compound/Indexing/Primary", fs{
"Type": OutputCapture, "Chunk": ast{
"Chunk", fs{"Pipelines": []string{"b", "c"}},
}}},
ast{"Compound/Indexing/Primary", fs{
"Type": OutputCapture, "Chunk": ast{
"Chunk", fs{"Pipelines": []string{"c", "d"}},
}}},
)},
// Exitus capture
{"a ?() ?(b;c)", a(
ast{"Compound/Indexing/Primary", fs{
"Type": ExceptionCapture, "Chunk": ""}},
ast{"Compound/Indexing/Primary", fs{
"Type": ExceptionCapture, "Chunk": "b;c",
}})},
// Braced
{"a {,a,c\ng\n}", a(
ast{"Compound/Indexing/Primary", fs{
"Type": Braced,
"Braced": []string{"", "a", "c", "g", ""}}})},
// Tilde
{"a ~xiaq/go", a(
ast{"Compound", fs{
"Indexings": []ast{
{"Indexing/Primary", fs{"Type": Tilde, "Value": "~"}},
{"Indexing/Primary", fs{"Type": Bareword, "Value": "xiaq/go"}},
},
}},
)},
// Line continuation: "\\\n" is considered whitespace
{"a b\\\nc", ast{
"Chunk/Pipeline/Form", fs{"Head": "a", "Args": []string{"b", "c"}}}},
}
func TestParse(t *testing.T) {
for _, tc := range goodCases {
bn, err := Parse("[test]", tc.src)
if err != nil {
t.Errorf("Parse(%q) returns error: %v", tc.src, err)
}
err = checkParseTree(bn)
if err != nil {
t.Errorf("Parse(%q) returns bad parse tree: %v", tc.src, err)
fmt.Fprintf(os.Stderr, "Parse tree of %q:\n", tc.src)
PPrintParseTreeTo(bn, os.Stderr)
}
err = checkAST(bn, tc.ast)
if err != nil {
t.Errorf("Parse(%q) returns bad AST: %v", tc.src, err)
fmt.Fprintf(os.Stderr, "AST of %q:\n", tc.src)
PPrintASTTo(bn, os.Stderr)
}
}
}
var badCases = []struct {
src string
pos int // expected Begin position of first error
}{
// Empty form.
{"a|", 2},
// Unopened parens.
{")", 0}, {"]", 0}, {"}", 0},
// Unclosed parens.
{"a (", 3}, {"a [", 3}, {"a {", 3},
// Bogus ampersand.
{"a & &", 4}, {"a [&", 4},
}
func TestParseError(t *testing.T) {
for _, tc := range badCases {
_, err := Parse("[test]", tc.src)
if err == nil {
t.Errorf("Parse(%q) returns no error", tc.src)
continue
}
posErr0 := err.(*Error).Entries[0]
if posErr0.Context.Begin != tc.pos {
t.Errorf("Parse(%q) first error begins at %d, want %d. Errors are:%s\n", tc.src, posErr0.Context.Begin, tc.pos, err)
}
}
}
|