| 1 |
package astrewrite |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"go/ast" |
| 7 |
"go/importer" |
| 8 |
"go/parser" |
| 9 |
"go/printer" |
| 10 |
"go/token" |
| 11 |
"go/types" |
| 12 |
"io/ioutil" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
func TestSimplify(t *testing.T) { |
| 17 |
simplifyAndCompareStmts(t, "-a()", "_1 := a(); -_1") |
| 18 |
simplifyAndCompareStmts(t, "a() + b()", "_1 := a(); _2 := b(); _1 + _2") |
| 19 |
simplifyAndCompareStmts(t, "f(g(), h())", "_1 := g(); _2 := h(); f(_1, _2)") |
| 20 |
simplifyAndCompareStmts(t, "f().x", "_1 := f(); _1.x") |
| 21 |
simplifyAndCompareStmts(t, "f()()", "_1 := f(); _1()") |
| 22 |
simplifyAndCompareStmts(t, "x.f()", "x.f()") |
| 23 |
simplifyAndCompareStmts(t, "f()[g()]", "_1 := f(); _2 := g(); _1[_2]") |
| 24 |
simplifyAndCompareStmts(t, "f()[g():h()]", "_1 := f(); _2 := g(); _3 := h(); _1[_2:_3]") |
| 25 |
simplifyAndCompareStmts(t, "f()[g():h():i()]", "_1 := f(); _2 := g(); _3 := h(); _4 := i(); _1[_2:_3:_4]") |
| 26 |
simplifyAndCompareStmts(t, "*f()", "_1 := f(); *_1") |
| 27 |
simplifyAndCompareStmts(t, "f().(t)", "_1 := f(); _1.(t)") |
| 28 |
simplifyAndCompareStmts(t, "func() { -a() }", "func() { _1 := a(); -_1 }") |
| 29 |
simplifyAndCompareStmts(t, "T{a(), b()}", "_1 := a(); _2 := b(); T{_1, _2}") |
| 30 |
simplifyAndCompareStmts(t, "T{A: a(), B: b()}", "_1 := a(); _2 := b(); T{A: _1, B: _2}") |
| 31 |
simplifyAndCompareStmts(t, "func() { a()() }", "func() { _1 := a(); _1() }") |
| 32 |
|
| 33 |
simplifyAndCompareStmts(t, "a() && b", "_1 := a(); _1 && b") |
| 34 |
simplifyAndCompareStmts(t, "a && b()", "_1 := a; if _1 { _1 = b() }; _1") |
| 35 |
simplifyAndCompareStmts(t, "a() && b()", "_1 := a(); if _1 { _1 = b() }; _1") |
| 36 |
|
| 37 |
simplifyAndCompareStmts(t, "a() || b", "_1 := a(); _1 || b") |
| 38 |
simplifyAndCompareStmts(t, "a || b()", "_1 := a; if !_1 { _1 = b() }; _1") |
| 39 |
simplifyAndCompareStmts(t, "a() || b()", "_1 := a(); if !_1 { _1 = b() }; _1") |
| 40 |
|
| 41 |
simplifyAndCompareStmts(t, "a && (b || c())", "_1 := a; if(_1) { _2 := b; if(!_2) { _2 = c() }; _1 = (_2) }; _1") |
| 42 |
|
| 43 |
simplifyAndCompareStmts(t, "a := b()()", "_1 := b(); a := _1()") |
| 44 |
simplifyAndCompareStmts(t, "a().f = b", "_1 := a(); _1.f = b") |
| 45 |
simplifyAndCompareStmts(t, "var a int = b()", "_1 := b(); var a int = _1") |
| 46 |
|
| 47 |
simplifyAndCompareStmts(t, "if a() { b }", "_1 := a(); if _1 { b }") |
| 48 |
simplifyAndCompareStmts(t, "if a := b(); a { c }", "{ a := b(); if a { c } }") |
| 49 |
simplifyAndCompareStmts(t, "if a { b()() }", "if a { _1 := b(); _1() }") |
| 50 |
simplifyAndCompareStmts(t, "if a { b } else { c()() }", "if a { b } else { _1 := c(); _1() }") |
| 51 |
simplifyAndCompareStmts(t, "if a { b } else if c { d()() }", "if a { b } else if c { _1 := d(); _1() }") |
| 52 |
simplifyAndCompareStmts(t, "if a { b } else if c() { d }", "if a { b } else { _1 := c(); if _1 { d } }") |
| 53 |
simplifyAndCompareStmts(t, "if a { b } else if c := d(); c { e }", "if a { b } else { c := d(); if c { e } }") |
| 54 |
|
| 55 |
simplifyAndCompareStmts(t, "l: switch a { case b, c: d()() }", "l: switch { default: _1 := a; if _1 == (b) || _1 == (c) { _2 := d(); _2() } }") |
| 56 |
simplifyAndCompareStmts(t, "switch a() { case b: c }", "switch { default: _1 := a(); if _1 == (b) { c } }") |
| 57 |
simplifyAndCompareStmts(t, "switch x := a(); x { case b, c: d }", "switch { default: x := a(); _1 := x; if _1 == (b) || _1 == (c) { d } }") |
| 58 |
simplifyAndCompareStmts(t, "switch a() { case b: c; default: e; case c: d }", "switch { default: _1 := a(); if _1 == (b) { c } else if _1 == (c) { d } else { e } }") |
| 59 |
simplifyAndCompareStmts(t, "switch a { case b(): c }", "switch { default: _1 := a; _2 := b(); if _1 == (_2) { c } }") |
| 60 |
simplifyAndCompareStmts(t, "switch a { default: d; fallthrough; case b: c }", "switch { default: _1 := a; if _1 == (b) { c } else { d; c } }") |
| 61 |
simplifyAndCompareStmts(t, "switch a := 0; a {}", "switch { default: a := 0; _ = a }") |
| 62 |
simplifyAndCompareStmts(t, "switch a := 0; a { default: }", "switch { default: a := 0; _ = a }") |
| 63 |
|
| 64 |
simplifyAndCompareStmts(t, "switch a().(type) { case b, c: d }", "_1 := a(); switch _1.(type) { case b, c: d }") |
| 65 |
simplifyAndCompareStmts(t, "switch x := a(); x.(type) { case b: c }", "{ x := a(); switch x.(type) { case b: c } }") |
| 66 |
simplifyAndCompareStmts(t, "switch a := b().(type) { case c: d }", "_1 := b(); switch a := _1.(type) { case c: d }") |
| 67 |
simplifyAndCompareStmts(t, "switch a.(type) { case b, c: d()() }", "switch a.(type) { case b, c: _1 := d(); _1() }") |
| 68 |
|
| 69 |
simplifyAndCompareStmts(t, "for a { b()() }", "for a { _1 := b(); _1() }") |
| 70 |
// simplifyAndCompareStmts(t, "for a() { b() }", "for { _1 := a(); if !_1 { break }; b() }") |
| 71 |
|
| 72 |
simplifyAndCompareStmts(t, "select { case <-a: b()(); default: c()() }", "select { case <-a: _1 := b(); _1(); default: _2 := c(); _2() }") |
| 73 |
simplifyAndCompareStmts(t, "select { case <-a(): b; case <-c(): d }", "_1 := a(); _2 := c(); select { case <-_1: b; case <-_2: d }") |
| 74 |
simplifyAndCompareStmts(t, "var d int; select { case a().f, a().g = <-b(): c; case d = <-e(): f }", "var d int; _5 := b(); _6 := e(); select { case _1, _3 := <-_5: _2 := a(); _2.f = _1; _4 := a(); _4.g = _3; c; case d = <-_6: f }") |
| 75 |
simplifyAndCompareStmts(t, "select { case a() <- b(): c; case d() <- e(): f }", "_1 := a(); _2 := b(); _3 := d(); _4 := e(); select { case _1 <- _2: c; case _3 <- _4: f }") |
| 76 |
|
| 77 |
simplifyAndCompareStmts(t, "a().f++", "_1 := a(); _1.f++") |
| 78 |
simplifyAndCompareStmts(t, "return a()", "_1 := a(); return _1") |
| 79 |
simplifyAndCompareStmts(t, "go a()()", "_1 := a(); go _1()") |
| 80 |
simplifyAndCompareStmts(t, "defer a()()", "_1 := a(); defer _1()") |
| 81 |
simplifyAndCompareStmts(t, "a() <- b", "_1 := a(); _1 <- b") |
| 82 |
simplifyAndCompareStmts(t, "a <- b()", "_1 := b(); a <- _1") |
| 83 |
|
| 84 |
for _, name := range []string{"var", "tuple", "range"} { |
| 85 |
fset := token.NewFileSet() |
| 86 |
inFile, err := parser.ParseFile(fset, fmt.Sprintf("testdata/%s.go", name), nil, 0) |
| 87 |
if err != nil { |
| 88 |
t.Fatal(err) |
| 89 |
} |
| 90 |
|
| 91 |
typesInfo := &types.Info{ |
| 92 |
Types: make(map[ast.Expr]types.TypeAndValue), |
| 93 |
Defs: make(map[*ast.Ident]types.Object), |
| 94 |
Uses: make(map[*ast.Ident]types.Object), |
| 95 |
Scopes: make(map[ast.Node]*types.Scope), |
| 96 |
} |
| 97 |
config := &types.Config{ |
| 98 |
Importer: importer.Default(), |
| 99 |
} |
| 100 |
if _, err := config.Check("main", fset, []*ast.File{inFile}, typesInfo); err != nil { |
| 101 |
t.Fatal(err) |
| 102 |
} |
| 103 |
|
| 104 |
outFile := Simplify(inFile, typesInfo, true) |
| 105 |
got := fprint(t, fset, outFile) |
| 106 |
expected, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s.expected.go", name)) |
| 107 |
if err != nil { |
| 108 |
t.Fatal(err) |
| 109 |
} |
| 110 |
if got != string(expected) { |
| 111 |
t.Errorf("expected:\n%s\n--- got:\n%s\n", string(expected), got) |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
func simplifyAndCompareStmts(t *testing.T, in, out string) { |
| 117 |
inFile := "package main; func main() { " + in + " }" |
| 118 |
outFile := "package main; func main() { " + out + " }" |
| 119 |
simplifyAndCompare(t, inFile, outFile) |
| 120 |
simplifyAndCompare(t, outFile, outFile) |
| 121 |
} |
| 122 |
|
| 123 |
func simplifyAndCompare(t *testing.T, in, out string) { |
| 124 |
fset := token.NewFileSet() |
| 125 |
|
| 126 |
expected := fprint(t, fset, parse(t, fset, out)) |
| 127 |
|
| 128 |
inFile := parse(t, fset, in) |
| 129 |
typesInfo := &types.Info{ |
| 130 |
Types: make(map[ast.Expr]types.TypeAndValue), |
| 131 |
Defs: make(map[*ast.Ident]types.Object), |
| 132 |
Uses: make(map[*ast.Ident]types.Object), |
| 133 |
Scopes: make(map[ast.Node]*types.Scope), |
| 134 |
} |
| 135 |
outFile := Simplify(inFile, typesInfo, true) |
| 136 |
got := fprint(t, fset, outFile) |
| 137 |
|
| 138 |
if got != expected { |
| 139 |
t.Errorf("\n--- input:\n%s\n--- expected output:\n%s\n--- got:\n%s\n", in, expected, got) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func parse(t *testing.T, fset *token.FileSet, body string) *ast.File { |
| 144 |
file, err := parser.ParseFile(fset, "", body, 0) |
| 145 |
if err != nil { |
| 146 |
t.Fatal(err) |
| 147 |
} |
| 148 |
return file |
| 149 |
} |
| 150 |
|
| 151 |
func fprint(t *testing.T, fset *token.FileSet, file *ast.File) string { |
| 152 |
var buf bytes.Buffer |
| 153 |
if err := printer.Fprint(&buf, fset, file); err != nil { |
| 154 |
t.Fatal(err) |
| 155 |
} |
| 156 |
return buf.String() |
| 157 |
} |
| 158 |
|
| 159 |
func TestContainsCall(t *testing.T) { |
| 160 |
testContainsCall(t, "a", false) |
| 161 |
testContainsCall(t, "a()", true) |
| 162 |
testContainsCall(t, "T{a, b}", false) |
| 163 |
testContainsCall(t, "T{a, b()}", true) |
| 164 |
testContainsCall(t, "T{a: a, b: b()}", true) |
| 165 |
testContainsCall(t, "(a())", true) |
| 166 |
testContainsCall(t, "a().f", true) |
| 167 |
testContainsCall(t, "a()[b]", true) |
| 168 |
testContainsCall(t, "a[b()]", true) |
| 169 |
testContainsCall(t, "a()[:]", true) |
| 170 |
testContainsCall(t, "a[b():]", true) |
| 171 |
testContainsCall(t, "a[:b()]", true) |
| 172 |
testContainsCall(t, "a[:b:c()]", true) |
| 173 |
testContainsCall(t, "a().(T)", true) |
| 174 |
testContainsCall(t, "*a()", true) |
| 175 |
testContainsCall(t, "-a()", true) |
| 176 |
testContainsCall(t, "&a()", true) |
| 177 |
testContainsCall(t, "&a()", true) |
| 178 |
testContainsCall(t, "a() + b", true) |
| 179 |
testContainsCall(t, "a + b()", true) |
| 180 |
} |
| 181 |
|
| 182 |
func testContainsCall(t *testing.T, in string, expected bool) { |
| 183 |
x, err := parser.ParseExpr(in) |
| 184 |
if err != nil { |
| 185 |
t.Fatal(err) |
| 186 |
} |
| 187 |
if got := ContainsCall(x); got != expected { |
| 188 |
t.Errorf("ContainsCall(%s): expected %t, got %t", in, expected, got) |
| 189 |
} |
| 190 |
} |