| 1 |
//go:build ignore |
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"log" |
| 8 |
"os" |
| 9 |
"regexp" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"github.com/robertkrimen/otto/ast" |
| 13 |
"github.com/robertkrimen/otto/parser" |
| 14 |
) |
| 15 |
|
| 16 |
type walker struct { |
| 17 |
nodes []ast.Node |
| 18 |
} |
| 19 |
|
| 20 |
func (w *walker) Enter(node ast.Node) ast.Visitor { |
| 21 |
w.nodes = append(w.nodes, node) |
| 22 |
return w |
| 23 |
} |
| 24 |
|
| 25 |
func (w *walker) Exit(node ast.Node) { |
| 26 |
} |
| 27 |
|
| 28 |
func getAllNodes(node ast.Node) []ast.Node { |
| 29 |
w := &walker{} |
| 30 |
ast.Walk(w, node) |
| 31 |
return w.nodes |
| 32 |
} |
| 33 |
|
| 34 |
func getCalls(node ast.Node, name string) []*ast.CallExpression { |
| 35 |
nodes := []*ast.CallExpression{} |
| 36 |
for _, n := range getAllNodes(node) { |
| 37 |
if ce, ok := n.(*ast.CallExpression); ok { |
| 38 |
var calleeName string |
| 39 |
switch callee := ce.Callee.(type) { |
| 40 |
case *ast.Identifier: |
| 41 |
calleeName = callee.Name |
| 42 |
case *ast.DotExpression: |
| 43 |
calleeName = callee.Identifier.Name |
| 44 |
default: |
| 45 |
continue |
| 46 |
} |
| 47 |
if calleeName == name { |
| 48 |
nodes = append(nodes, ce) |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
return nodes |
| 53 |
} |
| 54 |
|
| 55 |
func getPropertyValue(node ast.Node, key string) ast.Expression { |
| 56 |
for _, p := range node.(*ast.ObjectLiteral).Value { |
| 57 |
if p.Key == key { |
| 58 |
return p.Value |
| 59 |
} |
| 60 |
} |
| 61 |
return nil |
| 62 |
} |
| 63 |
|
| 64 |
type operation struct { |
| 65 |
startLine int |
| 66 |
startColumn int |
| 67 |
endLine int |
| 68 |
endColumn int |
| 69 |
text []string |
| 70 |
} |
| 71 |
|
| 72 |
type check struct { |
| 73 |
before []string |
| 74 |
operations []operation |
| 75 |
after []string |
| 76 |
} |
| 77 |
|
| 78 |
type test struct { |
| 79 |
description string |
| 80 |
checks []check |
| 81 |
} |
| 82 |
|
| 83 |
func stringSliceToGoSource(slice []string) string { |
| 84 |
var b strings.Builder |
| 85 |
b.WriteString("[]string{\n") |
| 86 |
for _, s := range slice { |
| 87 |
b.WriteString(fmt.Sprintf("%#v,\n", s)) |
| 88 |
} |
| 89 |
b.WriteString("}") |
| 90 |
return b.String() |
| 91 |
} |
| 92 |
|
| 93 |
func testToGoTest(test test, name string) string { |
| 94 |
var b strings.Builder |
| 95 |
|
| 96 |
b.WriteString("func Test") |
| 97 |
b.WriteString(name) |
| 98 |
b.WriteString("(t *testing.T) {\n") |
| 99 |
|
| 100 |
for _, c := range test.checks { |
| 101 |
b.WriteString("check(\n") |
| 102 |
b.WriteString("t,\n") |
| 103 |
b.WriteString(fmt.Sprintf("%v,\n", stringSliceToGoSource(c.before))) |
| 104 |
b.WriteString("[]operation{\n") |
| 105 |
for _, op := range c.operations { |
| 106 |
b.WriteString("operation{\n") |
| 107 |
b.WriteString(fmt.Sprintf("start: Loc{%v, %v},\n", op.startColumn, op.startLine)) |
| 108 |
b.WriteString(fmt.Sprintf("end: Loc{%v, %v},\n", op.endColumn, op.endLine)) |
| 109 |
b.WriteString(fmt.Sprintf("text: %v,\n", stringSliceToGoSource(op.text))) |
| 110 |
b.WriteString("},\n") |
| 111 |
} |
| 112 |
b.WriteString("},\n") |
| 113 |
b.WriteString(fmt.Sprintf("%v,\n", stringSliceToGoSource(c.after))) |
| 114 |
b.WriteString(")\n") |
| 115 |
} |
| 116 |
|
| 117 |
b.WriteString("}\n") |
| 118 |
|
| 119 |
return b.String() |
| 120 |
} |
| 121 |
|
| 122 |
func nodeToStringSlice(node ast.Node) []string { |
| 123 |
var result []string |
| 124 |
for _, s := range node.(*ast.ArrayLiteral).Value { |
| 125 |
result = append(result, s.(*ast.StringLiteral).Value) |
| 126 |
} |
| 127 |
return result |
| 128 |
} |
| 129 |
|
| 130 |
func nodeToStringSlice2(node ast.Node) []string { |
| 131 |
var result []string |
| 132 |
for _, o := range node.(*ast.ArrayLiteral).Value { |
| 133 |
result = append(result, getPropertyValue(o, "text").(*ast.StringLiteral).Value) |
| 134 |
} |
| 135 |
return result |
| 136 |
} |
| 137 |
|
| 138 |
func nodeToInt(node ast.Node) int { |
| 139 |
return int(node.(*ast.NumberLiteral).Value.(int64)) |
| 140 |
} |
| 141 |
|
| 142 |
func getChecks(node ast.Node) []check { |
| 143 |
checks := []check{} |
| 144 |
|
| 145 |
for _, ce := range getCalls(node, "testApplyEdits") { |
| 146 |
if len(ce.ArgumentList) != 3 { |
| 147 |
// Wrong function |
| 148 |
continue |
| 149 |
} |
| 150 |
|
| 151 |
before := nodeToStringSlice2(ce.ArgumentList[0]) |
| 152 |
after := nodeToStringSlice2(ce.ArgumentList[2]) |
| 153 |
|
| 154 |
var operations []operation |
| 155 |
for _, op := range ce.ArgumentList[1].(*ast.ArrayLiteral).Value { |
| 156 |
args := getPropertyValue(op, "range").(*ast.NewExpression).ArgumentList |
| 157 |
operations = append(operations, operation{ |
| 158 |
startLine: nodeToInt(args[0]) - 1, |
| 159 |
startColumn: nodeToInt(args[1]) - 1, |
| 160 |
endLine: nodeToInt(args[2]) - 1, |
| 161 |
endColumn: nodeToInt(args[3]) - 1, |
| 162 |
text: []string{getPropertyValue(op, "text").(*ast.StringLiteral).Value}, |
| 163 |
}) |
| 164 |
} |
| 165 |
|
| 166 |
checks = append(checks, check{before, operations, after}) |
| 167 |
} |
| 168 |
|
| 169 |
for _, ce := range getCalls(node, "testApplyEditsWithSyncedModels") { |
| 170 |
if len(ce.ArgumentList) > 3 && ce.ArgumentList[3].(*ast.BooleanLiteral).Value { |
| 171 |
// inputEditsAreInvalid == true |
| 172 |
continue |
| 173 |
} |
| 174 |
|
| 175 |
before := nodeToStringSlice(ce.ArgumentList[0]) |
| 176 |
after := nodeToStringSlice(ce.ArgumentList[2]) |
| 177 |
|
| 178 |
var operations []operation |
| 179 |
for _, op := range getCalls(ce.ArgumentList[1], "editOp") { |
| 180 |
operations = append(operations, operation{ |
| 181 |
startLine: nodeToInt(op.ArgumentList[0]) - 1, |
| 182 |
startColumn: nodeToInt(op.ArgumentList[1]) - 1, |
| 183 |
endLine: nodeToInt(op.ArgumentList[2]) - 1, |
| 184 |
endColumn: nodeToInt(op.ArgumentList[3]) - 1, |
| 185 |
text: nodeToStringSlice(op.ArgumentList[4]), |
| 186 |
}) |
| 187 |
} |
| 188 |
|
| 189 |
checks = append(checks, check{before, operations, after}) |
| 190 |
} |
| 191 |
|
| 192 |
return checks |
| 193 |
} |
| 194 |
|
| 195 |
func getTests(node ast.Node) []test { |
| 196 |
tests := []test{} |
| 197 |
for _, ce := range getCalls(node, "test") { |
| 198 |
description := ce.ArgumentList[0].(*ast.StringLiteral).Value |
| 199 |
body := ce.ArgumentList[1].(*ast.FunctionLiteral).Body |
| 200 |
checks := getChecks(body) |
| 201 |
if len(checks) > 0 { |
| 202 |
tests = append(tests, test{description, checks}) |
| 203 |
} |
| 204 |
} |
| 205 |
return tests |
| 206 |
} |
| 207 |
|
| 208 |
func main() { |
| 209 |
var tests []test |
| 210 |
|
| 211 |
for _, filename := range os.Args[1:] { |
| 212 |
source, err := os.ReadFile(filename) |
| 213 |
if err != nil { |
| 214 |
log.Fatalln(err) |
| 215 |
} |
| 216 |
|
| 217 |
program, err := parser.ParseFile(nil, "", source, parser.IgnoreRegExpErrors) |
| 218 |
if err != nil { |
| 219 |
log.Fatalln(err) |
| 220 |
} |
| 221 |
|
| 222 |
tests = append(tests, getTests(program)...) |
| 223 |
} |
| 224 |
|
| 225 |
if len(tests) == 0 { |
| 226 |
log.Fatalln("no tests found!") |
| 227 |
} |
| 228 |
|
| 229 |
fmt.Println("// This file is generated from VSCode model tests by the testgen tool.") |
| 230 |
fmt.Println("// DO NOT EDIT THIS FILE BY HAND; your changes will be overwritten!\n") |
| 231 |
fmt.Println("package buffer") |
| 232 |
fmt.Println(`import "testing"`) |
| 233 |
|
| 234 |
re := regexp.MustCompile(`[^\w]`) |
| 235 |
usedNames := map[string]bool{} |
| 236 |
|
| 237 |
for _, test := range tests { |
| 238 |
name := strings.Title(strings.ToLower(test.description)) |
| 239 |
name = re.ReplaceAllLiteralString(name, "") |
| 240 |
if name == "" { |
| 241 |
name = "Unnamed" |
| 242 |
} |
| 243 |
if usedNames[name] { |
| 244 |
for i := 2; ; i++ { |
| 245 |
newName := fmt.Sprintf("%v_%v", name, i) |
| 246 |
if !usedNames[newName] { |
| 247 |
name = newName |
| 248 |
break |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
usedNames[name] = true |
| 253 |
|
| 254 |
fmt.Println(testToGoTest(test, name)) |
| 255 |
} |
| 256 |
} |