| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
var gEvalTests = []struct { |
| 11 |
inp string |
| 12 |
toks []string |
| 13 |
exprs []expr |
| 14 |
}{ |
| 15 |
{ |
| 16 |
"", |
| 17 |
[]string{}, |
| 18 |
nil, |
| 19 |
}, |
| 20 |
|
| 21 |
{ |
| 22 |
"# comments start with '#'", |
| 23 |
[]string{}, |
| 24 |
nil, |
| 25 |
}, |
| 26 |
|
| 27 |
{ |
| 28 |
"echo hello", |
| 29 |
[]string{"echo", "hello", "\n"}, |
| 30 |
[]expr{&callExpr{"echo", []string{"hello"}, 1}}, |
| 31 |
}, |
| 32 |
|
| 33 |
{ |
| 34 |
"echo hello world", |
| 35 |
[]string{"echo", "hello", "world", "\n"}, |
| 36 |
[]expr{&callExpr{"echo", []string{"hello", "world"}, 1}}, |
| 37 |
}, |
| 38 |
|
| 39 |
{ |
| 40 |
"echo 'hello world'", |
| 41 |
[]string{"echo", "hello world", "\n"}, |
| 42 |
[]expr{&callExpr{"echo", []string{"hello world"}, 1}}, |
| 43 |
}, |
| 44 |
|
| 45 |
{ |
| 46 |
`echo "hello world"`, |
| 47 |
[]string{"echo", "hello world", "\n"}, |
| 48 |
[]expr{&callExpr{"echo", []string{"hello world"}, 1}}, |
| 49 |
}, |
| 50 |
|
| 51 |
{ |
| 52 |
`echo "hello\"world"`, |
| 53 |
[]string{"echo", `hello"world`, "\n"}, |
| 54 |
[]expr{&callExpr{"echo", []string{`hello"world`}, 1}}, |
| 55 |
}, |
| 56 |
|
| 57 |
{ |
| 58 |
`echo "hello\tworld"`, |
| 59 |
[]string{"echo", "hello\tworld", "\n"}, |
| 60 |
[]expr{&callExpr{"echo", []string{"hello\tworld"}, 1}}, |
| 61 |
}, |
| 62 |
|
| 63 |
{ |
| 64 |
`echo "hello\nworld"`, |
| 65 |
[]string{"echo", "hello\nworld", "\n"}, |
| 66 |
[]expr{&callExpr{"echo", []string{"hello\nworld"}, 1}}, |
| 67 |
}, |
| 68 |
|
| 69 |
{ |
| 70 |
`echo "hello\zworld"`, |
| 71 |
[]string{"echo", `hello\zworld`, "\n"}, |
| 72 |
[]expr{&callExpr{"echo", []string{`hello\zworld`}, 1}}, |
| 73 |
}, |
| 74 |
|
| 75 |
{ |
| 76 |
`echo "hello\0world"`, |
| 77 |
[]string{"echo", "hello\000world", "\n"}, |
| 78 |
[]expr{&callExpr{"echo", []string{"hello\000world"}, 1}}, |
| 79 |
}, |
| 80 |
|
| 81 |
{ |
| 82 |
`echo "hello\101world"`, |
| 83 |
[]string{"echo", "hello\101world", "\n"}, |
| 84 |
[]expr{&callExpr{"echo", []string{"hello\101world"}, 1}}, |
| 85 |
}, |
| 86 |
|
| 87 |
{ |
| 88 |
`echo hello\ world`, |
| 89 |
[]string{"echo", "hello world", "\n"}, |
| 90 |
[]expr{&callExpr{"echo", []string{"hello world"}, 1}}, |
| 91 |
}, |
| 92 |
|
| 93 |
{ |
| 94 |
"echo hello\\\tworld", |
| 95 |
[]string{"echo", "hello\tworld", "\n"}, |
| 96 |
[]expr{&callExpr{"echo", []string{"hello\tworld"}, 1}}, |
| 97 |
}, |
| 98 |
|
| 99 |
{ |
| 100 |
"echo hello\\\nworld", |
| 101 |
[]string{"echo", "hello\nworld", "\n"}, |
| 102 |
[]expr{&callExpr{"echo", []string{"hello\nworld"}, 1}}, |
| 103 |
}, |
| 104 |
|
| 105 |
{ |
| 106 |
`echo hello\\world`, |
| 107 |
[]string{"echo", `hello\world`, "\n"}, |
| 108 |
[]expr{&callExpr{"echo", []string{`hello\world`}, 1}}, |
| 109 |
}, |
| 110 |
|
| 111 |
{ |
| 112 |
`echo hello\zworld`, |
| 113 |
[]string{"echo", "hellozworld", "\n"}, |
| 114 |
[]expr{&callExpr{"echo", []string{"hellozworld"}, 1}}, |
| 115 |
}, |
| 116 |
|
| 117 |
{ |
| 118 |
"set hidden # trailing comments are allowed", |
| 119 |
[]string{"set", "hidden", "\n"}, |
| 120 |
[]expr{&setExpr{"hidden", ""}}, |
| 121 |
}, |
| 122 |
|
| 123 |
{ |
| 124 |
"set hidden; set preview", |
| 125 |
[]string{"set", "hidden", ";", "set", "preview", "\n"}, |
| 126 |
[]expr{&setExpr{"hidden", ""}, &setExpr{"preview", ""}}, |
| 127 |
}, |
| 128 |
|
| 129 |
{ |
| 130 |
"set hidden\nset preview", |
| 131 |
[]string{"set", "hidden", "\n", "set", "preview", "\n"}, |
| 132 |
[]expr{&setExpr{"hidden", ""}, &setExpr{"preview", ""}}, |
| 133 |
}, |
| 134 |
|
| 135 |
{ |
| 136 |
`set ifs ""`, |
| 137 |
[]string{"set", "ifs", "", "\n"}, |
| 138 |
[]expr{&setExpr{"ifs", ""}}, |
| 139 |
}, |
| 140 |
|
| 141 |
{ |
| 142 |
`set ifs "\n"`, |
| 143 |
[]string{"set", "ifs", "\n", "\n"}, |
| 144 |
[]expr{&setExpr{"ifs", "\n"}}, |
| 145 |
}, |
| 146 |
|
| 147 |
{ |
| 148 |
"set ratios 1:2:3", |
| 149 |
[]string{"set", "ratios", "1:2:3", "\n"}, |
| 150 |
[]expr{&setExpr{"ratios", "1:2:3"}}, |
| 151 |
}, |
| 152 |
|
| 153 |
{ |
| 154 |
"set ratios 1:2:3;", |
| 155 |
[]string{"set", "ratios", "1:2:3", ";"}, |
| 156 |
[]expr{&setExpr{"ratios", "1:2:3"}}, |
| 157 |
}, |
| 158 |
|
| 159 |
{ |
| 160 |
":set ratios 1:2:3", |
| 161 |
[]string{":", "set", "ratios", "1:2:3", "\n", "\n"}, |
| 162 |
[]expr{&listExpr{[]expr{&setExpr{"ratios", "1:2:3"}}, 1}}, |
| 163 |
}, |
| 164 |
|
| 165 |
{ |
| 166 |
":set ratios 1:2:3\nset hidden", |
| 167 |
[]string{":", "set", "ratios", "1:2:3", "\n", "\n", "set", "hidden", "\n"}, |
| 168 |
[]expr{&listExpr{[]expr{&setExpr{"ratios", "1:2:3"}}, 1}, &setExpr{"hidden", ""}}, |
| 169 |
}, |
| 170 |
|
| 171 |
{ |
| 172 |
":set ratios 1:2:3;", |
| 173 |
[]string{":", "set", "ratios", "1:2:3", ";", "\n"}, |
| 174 |
[]expr{&listExpr{[]expr{&setExpr{"ratios", "1:2:3"}}, 1}}, |
| 175 |
}, |
| 176 |
|
| 177 |
{ |
| 178 |
":set ratios 1:2:3;\nset hidden", |
| 179 |
[]string{":", "set", "ratios", "1:2:3", ";", "\n", "set", "hidden", "\n"}, |
| 180 |
[]expr{&listExpr{[]expr{&setExpr{"ratios", "1:2:3"}}, 1}, &setExpr{"hidden", ""}}, |
| 181 |
}, |
| 182 |
|
| 183 |
{ |
| 184 |
"set ratios 1:2:3\n set hidden", |
| 185 |
[]string{"set", "ratios", "1:2:3", "\n", "set", "hidden", "\n"}, |
| 186 |
[]expr{&setExpr{"ratios", "1:2:3"}, &setExpr{"hidden", ""}}, |
| 187 |
}, |
| 188 |
|
| 189 |
{ |
| 190 |
"set ratios 1:2:3 \nset hidden", |
| 191 |
[]string{"set", "ratios", "1:2:3", "\n", "set", "hidden", "\n"}, |
| 192 |
[]expr{&setExpr{"ratios", "1:2:3"}, &setExpr{"hidden", ""}}, |
| 193 |
}, |
| 194 |
|
| 195 |
{ |
| 196 |
"setlocal /foo/bar hidden # trailing comments are allowed", |
| 197 |
[]string{"setlocal", "/foo/bar", "hidden", "\n"}, |
| 198 |
[]expr{&setLocalExpr{"/foo/bar", "hidden", ""}}, |
| 199 |
}, |
| 200 |
|
| 201 |
{ |
| 202 |
"setlocal /foo/bar hidden; setlocal /foo/bar reverse", |
| 203 |
[]string{"setlocal", "/foo/bar", "hidden", ";", "setlocal", "/foo/bar", "reverse", "\n"}, |
| 204 |
[]expr{&setLocalExpr{"/foo/bar", "hidden", ""}, &setLocalExpr{"/foo/bar", "reverse", ""}}, |
| 205 |
}, |
| 206 |
|
| 207 |
{ |
| 208 |
"setlocal /foo/bar hidden\nsetlocal /foo/bar reverse", |
| 209 |
[]string{"setlocal", "/foo/bar", "hidden", "\n", "setlocal", "/foo/bar", "reverse", "\n"}, |
| 210 |
[]expr{&setLocalExpr{"/foo/bar", "hidden", ""}, &setLocalExpr{"/foo/bar", "reverse", ""}}, |
| 211 |
}, |
| 212 |
|
| 213 |
{ |
| 214 |
`setlocal /foo/bar info ""`, |
| 215 |
[]string{"setlocal", "/foo/bar", "info", "", "\n"}, |
| 216 |
[]expr{&setLocalExpr{"/foo/bar", "info", ""}}, |
| 217 |
}, |
| 218 |
|
| 219 |
{ |
| 220 |
`setlocal /foo/bar info "size"`, |
| 221 |
[]string{"setlocal", "/foo/bar", "info", "size", "\n"}, |
| 222 |
[]expr{&setLocalExpr{"/foo/bar", "info", "size"}}, |
| 223 |
}, |
| 224 |
|
| 225 |
{ |
| 226 |
"setlocal /foo/bar info size:time", |
| 227 |
[]string{"setlocal", "/foo/bar", "info", "size:time", "\n"}, |
| 228 |
[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, |
| 229 |
}, |
| 230 |
|
| 231 |
{ |
| 232 |
"setlocal /foo/bar info size:time;", |
| 233 |
[]string{"setlocal", "/foo/bar", "info", "size:time", ";"}, |
| 234 |
[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, |
| 235 |
}, |
| 236 |
|
| 237 |
{ |
| 238 |
":setlocal /foo/bar info size:time", |
| 239 |
[]string{":", "setlocal", "/foo/bar", "info", "size:time", "\n", "\n"}, |
| 240 |
[]expr{&listExpr{[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, 1}}, |
| 241 |
}, |
| 242 |
|
| 243 |
{ |
| 244 |
":setlocal /foo/bar info size:time\nsetlocal /foo/bar hidden", |
| 245 |
[]string{":", "setlocal", "/foo/bar", "info", "size:time", "\n", "\n", "setlocal", "/foo/bar", "hidden", "\n"}, |
| 246 |
[]expr{&listExpr{[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, 1}, &setLocalExpr{"/foo/bar", "hidden", ""}}, |
| 247 |
}, |
| 248 |
|
| 249 |
{ |
| 250 |
":setlocal /foo/bar info size:time;", |
| 251 |
[]string{":", "setlocal", "/foo/bar", "info", "size:time", ";", "\n"}, |
| 252 |
[]expr{&listExpr{[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, 1}}, |
| 253 |
}, |
| 254 |
|
| 255 |
{ |
| 256 |
":setlocal /foo/bar info size:time;\nsetlocal /foo/bar hidden", |
| 257 |
[]string{":", "setlocal", "/foo/bar", "info", "size:time", ";", "\n", "setlocal", "/foo/bar", "hidden", "\n"}, |
| 258 |
[]expr{&listExpr{[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, 1}, &setLocalExpr{"/foo/bar", "hidden", ""}}, |
| 259 |
}, |
| 260 |
|
| 261 |
{ |
| 262 |
"setlocal /foo/bar info size:time\n setlocal /foo/bar hidden", |
| 263 |
[]string{"setlocal", "/foo/bar", "info", "size:time", "\n", "setlocal", "/foo/bar", "hidden", "\n"}, |
| 264 |
[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}, &setLocalExpr{"/foo/bar", "hidden", ""}}, |
| 265 |
}, |
| 266 |
|
| 267 |
{ |
| 268 |
"setlocal /foo/bar info size:time \nsetlocal /foo/bar hidden", |
| 269 |
[]string{"setlocal", "/foo/bar", "info", "size:time", "\n", "setlocal", "/foo/bar", "hidden", "\n"}, |
| 270 |
[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}, &setLocalExpr{"/foo/bar", "hidden", ""}}, |
| 271 |
}, |
| 272 |
|
| 273 |
{ |
| 274 |
"map gh cd ~", |
| 275 |
[]string{"map", "gh", "cd", "~", "\n"}, |
| 276 |
[]expr{&mapExpr{"gh", &callExpr{"cd", []string{"~"}, 1}}}, |
| 277 |
}, |
| 278 |
|
| 279 |
{ |
| 280 |
"map gh cd ~;", |
| 281 |
[]string{"map", "gh", "cd", "~", ";"}, |
| 282 |
[]expr{&mapExpr{"gh", &callExpr{"cd", []string{"~"}, 1}}}, |
| 283 |
}, |
| 284 |
|
| 285 |
{ |
| 286 |
"map gh :cd ~", |
| 287 |
[]string{"map", "gh", ":", "cd", "~", "\n", "\n"}, |
| 288 |
[]expr{&mapExpr{"gh", &listExpr{[]expr{&callExpr{"cd", []string{"~"}, 1}}, 1}}}, |
| 289 |
}, |
| 290 |
|
| 291 |
{ |
| 292 |
"map gh :cd ~;", |
| 293 |
[]string{"map", "gh", ":", "cd", "~", ";", "\n"}, |
| 294 |
[]expr{&mapExpr{"gh", &listExpr{[]expr{&callExpr{"cd", []string{"~"}, 1}}, 1}}}, |
| 295 |
}, |
| 296 |
|
| 297 |
{ |
| 298 |
"nmap <space> :toggle; down", |
| 299 |
[]string{"nmap", "<space>", ":", "toggle", ";", "down", "\n", "\n"}, |
| 300 |
[]expr{&nmapExpr{"<space>", &listExpr{[]expr{&callExpr{"toggle", nil, 1}, &callExpr{"down", nil, 1}}, 1}}}, |
| 301 |
}, |
| 302 |
|
| 303 |
{ |
| 304 |
"vmap <esc> visual-accept", |
| 305 |
[]string{"vmap", "<esc>", "visual-accept", "\n"}, |
| 306 |
[]expr{&vmapExpr{"<esc>", &callExpr{"visual-accept", nil, 1}}}, |
| 307 |
}, |
| 308 |
|
| 309 |
{ |
| 310 |
"cmap <c-g> cmd-escape", |
| 311 |
[]string{"cmap", "<c-g>", "cmd-escape", "\n"}, |
| 312 |
[]expr{&cmapExpr{"<c-g>", &callExpr{"cmd-escape", nil, 1}}}, |
| 313 |
}, |
| 314 |
|
| 315 |
{ |
| 316 |
"cmd usage $du -h . | less", |
| 317 |
[]string{"cmd", "usage", "$", "du -h . | less", "\n"}, |
| 318 |
[]expr{&cmdExpr{"usage", &execExpr{"$", "du -h . | less"}}}, |
| 319 |
}, |
| 320 |
|
| 321 |
{ |
| 322 |
"cmd 世界 $echo 世界", |
| 323 |
[]string{"cmd", "世界", "$", "echo 世界", "\n"}, |
| 324 |
[]expr{&cmdExpr{"世界", &execExpr{"$", "echo 世界"}}}, |
| 325 |
}, |
| 326 |
|
| 327 |
{ |
| 328 |
"map u usage", |
| 329 |
[]string{"map", "u", "usage", "\n"}, |
| 330 |
[]expr{&mapExpr{"u", &callExpr{"usage", nil, 1}}}, |
| 331 |
}, |
| 332 |
|
| 333 |
{ |
| 334 |
"map u usage;", |
| 335 |
[]string{"map", "u", "usage", ";"}, |
| 336 |
[]expr{&mapExpr{"u", &callExpr{"usage", nil, 1}}}, |
| 337 |
}, |
| 338 |
|
| 339 |
{ |
| 340 |
"map u :usage", |
| 341 |
[]string{"map", "u", ":", "usage", "\n", "\n"}, |
| 342 |
[]expr{&mapExpr{"u", &listExpr{[]expr{&callExpr{"usage", nil, 1}}, 1}}}, |
| 343 |
}, |
| 344 |
|
| 345 |
{ |
| 346 |
"map u :usage;", |
| 347 |
[]string{"map", "u", ":", "usage", ";", "\n"}, |
| 348 |
[]expr{&mapExpr{"u", &listExpr{[]expr{&callExpr{"usage", nil, 1}}, 1}}}, |
| 349 |
}, |
| 350 |
|
| 351 |
{ |
| 352 |
"map r push :rename<space>", |
| 353 |
[]string{"map", "r", "push", ":rename<space>", "\n"}, |
| 354 |
[]expr{&mapExpr{"r", &callExpr{"push", []string{":rename<space>"}, 1}}}, |
| 355 |
}, |
| 356 |
|
| 357 |
{ |
| 358 |
"map r push :rename<space>;", |
| 359 |
[]string{"map", "r", "push", ":rename<space>;", "\n"}, |
| 360 |
[]expr{&mapExpr{"r", &callExpr{"push", []string{":rename<space>;"}, 1}}}, |
| 361 |
}, |
| 362 |
|
| 363 |
{ |
| 364 |
"map r push :rename<space> # trailing comments are allowed after a space", |
| 365 |
[]string{"map", "r", "push", ":rename<space>", "\n"}, |
| 366 |
[]expr{&mapExpr{"r", &callExpr{"push", []string{":rename<space>"}, 1}}}, |
| 367 |
}, |
| 368 |
|
| 369 |
{ |
| 370 |
"map r :push :rename<space>", |
| 371 |
[]string{"map", "r", ":", "push", ":rename<space>", "\n", "\n"}, |
| 372 |
[]expr{&mapExpr{"r", &listExpr{[]expr{&callExpr{"push", []string{":rename<space>"}, 1}}, 1}}}, |
| 373 |
}, |
| 374 |
|
| 375 |
{ |
| 376 |
"map r :push :rename<space> ; set hidden", |
| 377 |
[]string{"map", "r", ":", "push", ":rename<space>", ";", "set", "hidden", "\n", "\n"}, |
| 378 |
[]expr{&mapExpr{"r", &listExpr{[]expr{&callExpr{"push", []string{":rename<space>"}, 1}, &setExpr{"hidden", ""}}, 1}}}, |
| 379 |
}, |
| 380 |
|
| 381 |
{ |
| 382 |
"map u $du -h . | less", |
| 383 |
[]string{"map", "u", "$", "du -h . | less", "\n"}, |
| 384 |
[]expr{&mapExpr{"u", &execExpr{"$", "du -h . | less"}}}, |
| 385 |
}, |
| 386 |
|
| 387 |
{ |
| 388 |
"cmd usage $du -h $1 | less", |
| 389 |
[]string{"cmd", "usage", "$", "du -h $1 | less", "\n"}, |
| 390 |
[]expr{&cmdExpr{"usage", &execExpr{"$", "du -h $1 | less"}}}, |
| 391 |
}, |
| 392 |
|
| 393 |
{ |
| 394 |
"map u usage /", |
| 395 |
[]string{"map", "u", "usage", "/", "\n"}, |
| 396 |
[]expr{&mapExpr{"u", &callExpr{"usage", []string{"/"}, 1}}}, |
| 397 |
}, |
| 398 |
|
| 399 |
{ |
| 400 |
"map ss :set sortby size; set info size", |
| 401 |
[]string{"map", "ss", ":", "set", "sortby", "size", ";", "set", "info", "size", "\n", "\n"}, |
| 402 |
[]expr{&mapExpr{"ss", &listExpr{[]expr{&setExpr{"sortby", "size"}, &setExpr{"info", "size"}}, 1}}}, |
| 403 |
}, |
| 404 |
|
| 405 |
{ |
| 406 |
"map ss :set sortby size; set info size;", |
| 407 |
[]string{"map", "ss", ":", "set", "sortby", "size", ";", "set", "info", "size", ";", "\n"}, |
| 408 |
[]expr{&mapExpr{"ss", &listExpr{[]expr{&setExpr{"sortby", "size"}, &setExpr{"info", "size"}}, 1}}}, |
| 409 |
}, |
| 410 |
|
| 411 |
{ |
| 412 |
`cmd gohome :{{ |
| 413 |
cd ~ |
| 414 |
set hidden |
| 415 |
}}`, |
| 416 |
[]string{ |
| 417 |
"cmd", "gohome", ":", "{{", |
| 418 |
"cd", "~", "\n", |
| 419 |
"set", "hidden", "\n", |
| 420 |
"}}", "\n", |
| 421 |
}, |
| 422 |
[]expr{&cmdExpr{ |
| 423 |
"gohome", &listExpr{[]expr{ |
| 424 |
&callExpr{"cd", []string{"~"}, 1}, |
| 425 |
&setExpr{"hidden", ""}, |
| 426 |
}, 1}, |
| 427 |
}}, |
| 428 |
}, |
| 429 |
|
| 430 |
{ |
| 431 |
`map gh :{{ |
| 432 |
cd ~ |
| 433 |
set hidden |
| 434 |
}}`, |
| 435 |
[]string{ |
| 436 |
"map", "gh", ":", "{{", |
| 437 |
"cd", "~", "\n", |
| 438 |
"set", "hidden", "\n", |
| 439 |
"}}", "\n", |
| 440 |
}, |
| 441 |
[]expr{&mapExpr{ |
| 442 |
"gh", &listExpr{[]expr{ |
| 443 |
&callExpr{"cd", []string{"~"}, 1}, |
| 444 |
&setExpr{"hidden", ""}, |
| 445 |
}, 1}, |
| 446 |
}}, |
| 447 |
}, |
| 448 |
|
| 449 |
{ |
| 450 |
`map c ${{ |
| 451 |
mkdir foo |
| 452 |
cp $fs foo |
| 453 |
tar -czvf foo.tar.gz foo |
| 454 |
rm -rf foo |
| 455 |
}}`, |
| 456 |
[]string{"map", "c", "$", "{{", ` |
| 457 |
mkdir foo |
| 458 |
cp $fs foo |
| 459 |
tar -czvf foo.tar.gz foo |
| 460 |
rm -rf foo |
| 461 |
`, "}}", "\n"}, |
| 462 |
[]expr{&mapExpr{"c", &execExpr{"$", ` |
| 463 |
mkdir foo |
| 464 |
cp $fs foo |
| 465 |
tar -czvf foo.tar.gz foo |
| 466 |
rm -rf foo |
| 467 |
`}}}, |
| 468 |
}, |
| 469 |
|
| 470 |
{ |
| 471 |
`cmd compress ${{ |
| 472 |
mkdir $1 |
| 473 |
cp $fs $1 |
| 474 |
tar -czvf $1.tar.gz $1 |
| 475 |
rm -rf $1 |
| 476 |
}}`, |
| 477 |
[]string{"cmd", "compress", "$", "{{", ` |
| 478 |
mkdir $1 |
| 479 |
cp $fs $1 |
| 480 |
tar -czvf $1.tar.gz $1 |
| 481 |
rm -rf $1 |
| 482 |
`, "}}", "\n"}, |
| 483 |
[]expr{&cmdExpr{"compress", &execExpr{"$", ` |
| 484 |
mkdir $1 |
| 485 |
cp $fs $1 |
| 486 |
tar -czvf $1.tar.gz $1 |
| 487 |
rm -rf $1 |
| 488 |
`}}}, |
| 489 |
}, |
| 490 |
} |
| 491 |
|
| 492 |
func TestScan(t *testing.T) { |
| 493 |
for _, test := range gEvalTests { |
| 494 |
s := newScanner(strings.NewReader(test.inp)) |
| 495 |
|
| 496 |
for _, tok := range test.toks { |
| 497 |
if s.scan(); s.tok != tok { |
| 498 |
t.Errorf("at input '%s' expected '%s' but scanned '%s'", test.inp, tok, s.tok) |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
if s.scan() { |
| 503 |
t.Errorf("at input '%s' unexpected '%s'", test.inp, s.tok) |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
func TestParse(t *testing.T) { |
| 509 |
for _, test := range gEvalTests { |
| 510 |
p := newParser(strings.NewReader(test.inp)) |
| 511 |
|
| 512 |
for _, expr := range test.exprs { |
| 513 |
if p.parse(); !reflect.DeepEqual(p.expr, expr) { |
| 514 |
t.Errorf("at input '%s' expected '%s' but parsed '%s'", test.inp, expr, p.expr) |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
if p.parse(); p.expr != nil { |
| 519 |
t.Errorf("at input '%s' unexpected '%s'", test.inp, p.expr) |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
func TestExprString(t *testing.T) { |
| 525 |
tests := []struct { |
| 526 |
e expr |
| 527 |
exp string |
| 528 |
}{ |
| 529 |
{&setExpr{"hidden!", ""}, "set hidden!"}, |
| 530 |
{&setExpr{"hidden", "true"}, "set hidden true"}, |
| 531 |
{&setLocalExpr{"~", "hidden!", ""}, "setlocal ~ hidden!"}, |
| 532 |
{&setLocalExpr{"~", "hidden", "true"}, "setlocal ~ hidden true"}, |
| 533 |
{&mapExpr{"q", nil}, "map q"}, |
| 534 |
{&mapExpr{"q", &callExpr{"quit", nil, 1}}, "map q quit"}, |
| 535 |
{&nmapExpr{"q", nil}, "nmap q"}, |
| 536 |
{&nmapExpr{"q", &callExpr{"quit", nil, 1}}, "nmap q quit"}, |
| 537 |
{&vmapExpr{"q", nil}, "vmap q"}, |
| 538 |
{&vmapExpr{"q", &callExpr{"quit", nil, 1}}, "vmap q quit"}, |
| 539 |
{&cmapExpr{"q", nil}, "cmap q"}, |
| 540 |
{&cmapExpr{"q", &callExpr{"quit", nil, 1}}, "cmap q quit"}, |
| 541 |
{&cmdExpr{"foo", nil}, "cmd foo"}, |
| 542 |
{&cmdExpr{"foo", &callExpr{"quit", nil, 1}}, "cmd foo quit"}, |
| 543 |
{&callExpr{"quit", nil, 1}, "quit"}, |
| 544 |
{&callExpr{"cd", []string{"~"}, 1}, "cd ~"}, |
| 545 |
{&execExpr{"$", "du -h . | less"}, "${{ du -h . | less }}"}, |
| 546 |
{ |
| 547 |
&execExpr{"$", ` |
| 548 |
mkdir foo |
| 549 |
cp $fs foo |
| 550 |
tar -czvf foo.tar.gz foo |
| 551 |
rm -rf foo |
| 552 |
`}, |
| 553 |
"${{ mkdir foo ... }}", |
| 554 |
}, |
| 555 |
{&listExpr{[]expr{&callExpr{"toggle", nil, 1}, &callExpr{"down", nil, 1}}, 1}, ":{{ toggle; down; }}"}, |
| 556 |
} |
| 557 |
|
| 558 |
for _, test := range tests { |
| 559 |
if got := test.e.String(); got != test.exp { |
| 560 |
t.Errorf("at test '%#v' expected '%s' but got '%s'", test.e, test.exp, got) |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
func TestSplitKeys(t *testing.T) { |
| 566 |
inps := []struct { |
| 567 |
s string |
| 568 |
keys []string |
| 569 |
}{ |
| 570 |
{"", nil}, |
| 571 |
{"j", []string{"j"}}, |
| 572 |
{"jk", []string{"j", "k"}}, |
| 573 |
{"1j", []string{"1", "j"}}, |
| 574 |
{"42j", []string{"4", "2", "j"}}, |
| 575 |
{"<space>", []string{"<space>"}}, |
| 576 |
{"j<space>", []string{"j", "<space>"}}, |
| 577 |
{"j<space>k", []string{"j", "<space>", "k"}}, |
| 578 |
{"1j<space>k", []string{"1", "j", "<space>", "k"}}, |
| 579 |
{"1j<space>1k", []string{"1", "j", "<space>", "1", "k"}}, |
| 580 |
{"<>", []string{"<>"}}, |
| 581 |
{"<space", []string{"<space"}}, |
| 582 |
{"<space<", []string{"<space<"}}, |
| 583 |
{"<space<>", []string{"<space<>"}}, |
| 584 |
{"><space>", []string{">", "<space>"}}, |
| 585 |
{"><space>>", []string{">", "<space>", ">"}}, |
| 586 |
} |
| 587 |
|
| 588 |
for _, inp := range inps { |
| 589 |
if keys := splitKeys(inp.s); !reflect.DeepEqual(keys, inp.keys) { |
| 590 |
t.Errorf("at input '%s' expected '%v' but got '%v'", inp.s, inp.keys, keys) |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
func TestApplyBoolOpt(t *testing.T) { |
| 596 |
tests := []struct { |
| 597 |
opt bool |
| 598 |
e setExpr |
| 599 |
exp bool |
| 600 |
}{ |
| 601 |
{true, setExpr{"feature", ""}, true}, |
| 602 |
{true, setExpr{"feature", "true"}, true}, |
| 603 |
{true, setExpr{"feature", "false"}, false}, |
| 604 |
{false, setExpr{"feature", ""}, true}, |
| 605 |
{false, setExpr{"feature", "true"}, true}, |
| 606 |
{false, setExpr{"feature", "false"}, false}, |
| 607 |
{true, setExpr{"nofeature", ""}, false}, |
| 608 |
{false, setExpr{"nofeature", ""}, false}, |
| 609 |
{true, setExpr{"feature!", ""}, false}, |
| 610 |
{false, setExpr{"feature!", ""}, true}, |
| 611 |
} |
| 612 |
|
| 613 |
for _, test := range tests { |
| 614 |
testStr := fmt.Sprintf("%v", test) |
| 615 |
if err := applyBoolOpt(&test.opt, &test.e); err != nil { |
| 616 |
t.Errorf("at test '%s' expected '%t' but got an error '%s'", testStr, test.exp, err) |
| 617 |
continue |
| 618 |
} |
| 619 |
if test.opt != test.exp { |
| 620 |
t.Errorf("at test '%s' expected '%t' but got '%t'", testStr, test.exp, test.opt) |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
func TestApplyLocalBoolOpt(t *testing.T) { |
| 626 |
tests := []struct { |
| 627 |
localOpt map[string]bool |
| 628 |
globalOpt bool |
| 629 |
e setLocalExpr |
| 630 |
exp bool |
| 631 |
}{ |
| 632 |
{map[string]bool{}, false, setLocalExpr{"/", "feature", ""}, true}, |
| 633 |
{map[string]bool{}, false, setLocalExpr{"/", "feature", "true"}, true}, |
| 634 |
{map[string]bool{}, false, setLocalExpr{"/", "feature", "false"}, false}, |
| 635 |
{map[string]bool{}, false, setLocalExpr{"/", "nofeature", ""}, false}, |
| 636 |
{map[string]bool{}, true, setLocalExpr{"/", "feature!", ""}, false}, |
| 637 |
{map[string]bool{}, false, setLocalExpr{"/", "feature!", ""}, true}, |
| 638 |
{map[string]bool{"/": true}, false, setLocalExpr{"/", "feature", ""}, true}, |
| 639 |
{map[string]bool{"/": true}, false, setLocalExpr{"/", "feature", "true"}, true}, |
| 640 |
{map[string]bool{"/": true}, false, setLocalExpr{"/", "feature", "false"}, false}, |
| 641 |
{map[string]bool{"/": true}, false, setLocalExpr{"/", "nofeature", ""}, false}, |
| 642 |
{map[string]bool{"/": true}, true, setLocalExpr{"/", "feature!", ""}, false}, |
| 643 |
{map[string]bool{"/": true}, false, setLocalExpr{"/", "feature!", ""}, false}, |
| 644 |
{map[string]bool{"/": false}, false, setLocalExpr{"/", "feature", ""}, true}, |
| 645 |
{map[string]bool{"/": false}, false, setLocalExpr{"/", "feature", "true"}, true}, |
| 646 |
{map[string]bool{"/": false}, false, setLocalExpr{"/", "feature", "false"}, false}, |
| 647 |
{map[string]bool{"/": false}, false, setLocalExpr{"/", "nofeature", ""}, false}, |
| 648 |
{map[string]bool{"/": false}, true, setLocalExpr{"/", "feature!", ""}, true}, |
| 649 |
{map[string]bool{"/": false}, false, setLocalExpr{"/", "feature!", ""}, true}, |
| 650 |
} |
| 651 |
|
| 652 |
for _, test := range tests { |
| 653 |
testStr := fmt.Sprintf("%v", test) |
| 654 |
if err := applyLocalBoolOpt(test.localOpt, test.globalOpt, &test.e); err != nil { |
| 655 |
t.Errorf("at test '%s' expected '%t' but got an error '%s'", testStr, test.exp, err) |
| 656 |
continue |
| 657 |
} |
| 658 |
result := test.localOpt[test.e.path] |
| 659 |
if result != test.exp { |
| 660 |
t.Errorf("at test '%s' expected '%t' but got '%t'", testStr, test.exp, result) |
| 661 |
} |
| 662 |
} |
| 663 |
} |