github.com/cilki/sh@v2.6.4+incompatible/syntax/simplify_test.go (about)

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