| 1 |
// Copyright (c) 2017, Daniel Martà <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package syntax |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
type simplifyTest struct { |
| 13 |
in, want string |
| 14 |
} |
| 15 |
|
| 16 |
func noSimple(in string) simplifyTest { |
| 17 |
return simplifyTest{in: in, want: in} |
| 18 |
} |
| 19 |
|
| 20 |
var simplifyTests = [...]simplifyTest{ |
| 21 |
// arithmetic exprs |
| 22 |
{"$((a + ((b - c))))", "$((a + (b - c)))"}, |
| 23 |
{"$((a + (((b - c)))))", "$((a + (b - c)))"}, |
| 24 |
{"$(((b - c)))", "$((b - c))"}, |
| 25 |
{"(((b - c)))", "((b - c))"}, |
| 26 |
{"${foo[(1)]}", "${foo[1]}"}, |
| 27 |
{"${foo:(1):(2)}", "${foo:1:2}"}, |
| 28 |
{"a[(1)]=2", "a[1]=2"}, |
| 29 |
{"$(($a + ${b}))", "$((a + b))"}, |
| 30 |
noSimple("$((${!a} + ${#b}))"), |
| 31 |
noSimple("a[$b]=2"), |
| 32 |
noSimple("${a[$b]}"), |
| 33 |
noSimple("${a[x,$b]}"), |
| 34 |
noSimple("${a[$b + 1]}"), |
| 35 |
noSimple("a=([x,$b]=v)"), |
| 36 |
noSimple("${a[@]}"), |
| 37 |
noSimple("((${a[@]}))"), |
| 38 |
noSimple("((${a[*]}))"), |
| 39 |
noSimple("((${a[0]}))"), |
| 40 |
noSimple("(($3 == $#))"), |
| 41 |
|
| 42 |
// test exprs |
| 43 |
{`[[ "$foo" == "bar" ]]`, `[[ $foo == "bar" ]]`}, |
| 44 |
{`[[ (-z "$foo") ]]`, `[[ -z $foo ]]`}, |
| 45 |
{`[[ "a b" > "$c" ]]`, `[[ "a b" > $c ]]`}, |
| 46 |
{`[[ ! -n $foo ]]`, `[[ -z $foo ]]`}, |
| 47 |
{`[[ ! ! -e a && ! -z $b ]]`, `[[ -e a && -n $b ]]`}, |
| 48 |
{`[[ (! a == b) || (! c != d) ]]`, `[[ (a != b) || (c == d) ]]`}, |
| 49 |
noSimple(`[[ -n a$b && -n $c ]]`), |
| 50 |
noSimple(`[[ ! -e foo ]]`), |
| 51 |
noSimple(`[[ foo == bar ]]`), |
| 52 |
{`[[ foo = bar ]]`, `[[ foo == bar ]]`}, |
| 53 |
noSimple(`[[ foo =~ "$bar" ]]`), |
| 54 |
{`[[ "$foo" =~ bar ]]`, `[[ $foo =~ bar ]]`}, |
| 55 |
|
| 56 |
// stmts |
| 57 |
{"$( (sts))", "$(sts)"}, |
| 58 |
{"( ( (sts)))", "(sts)"}, |
| 59 |
noSimple("( (sts) >f)"), |
| 60 |
noSimple("(\n\tx\n\t(sts)\n)"), |
| 61 |
|
| 62 |
// strings |
| 63 |
noSimple(`"foo"`), |
| 64 |
noSimple(`"foo$bar"`), |
| 65 |
noSimple(`"$bar"`), |
| 66 |
noSimple(`"f'o\\o"`), |
| 67 |
noSimple(`"fo\'o"`), |
| 68 |
noSimple(`"fo\\'o"`), |
| 69 |
noSimple(`"fo\no"`), |
| 70 |
{`"fo\$o"`, `'fo$o'`}, |
| 71 |
{`"fo\"o"`, `'fo"o'`}, |
| 72 |
{"\"fo\\`o\"", "'fo`o'"}, |
| 73 |
noSimple(`fo"o"bar`), |
| 74 |
noSimple(`foo""bar`), |
| 75 |
} |
| 76 |
|
| 77 |
func TestSimplify(t *testing.T) { |
| 78 |
t.Parallel() |
| 79 |
parser := NewParser() |
| 80 |
printer := NewPrinter() |
| 81 |
for _, tc := range simplifyTests { |
| 82 |
t.Run("", func(t *testing.T) { |
| 83 |
prog, err := parser.Parse(strings.NewReader(tc.in), "") |
| 84 |
if err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
simplified := Simplify(prog) |
| 88 |
var buf bytes.Buffer |
| 89 |
printer.Print(&buf, prog) |
| 90 |
want := tc.want + "\n" |
| 91 |
if got := buf.String(); got != want { |
| 92 |
t.Fatalf("Simplify mismatch of %q\nwant: %q\ngot: %q", |
| 93 |
tc.in, want, got) |
| 94 |
} |
| 95 |
if simplified && tc.in == tc.want { |
| 96 |
t.Fatalf("returned true but did not simplify") |
| 97 |
} else if !simplified && tc.in != tc.want { |
| 98 |
t.Fatalf("returned false but did simplify") |
| 99 |
} |
| 100 |
}) |
| 101 |
} |
| 102 |
} |