File size: 8,865 Bytes
e36aeda | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | // Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package syntax
import (
"fmt"
"strings"
"testing"
)
// A test is a source code snippet of a particular node type.
// In the snippet, a '@' indicates the position recorded by
// the parser when creating the respective node.
type test struct {
nodetyp string
snippet string
}
var decls = []test{
// The position of declarations is always the
// position of the first token of an individual
// declaration, independent of grouping.
{"ImportDecl", `import @"math"`},
{"ImportDecl", `import @mymath "math"`},
{"ImportDecl", `import @. "math"`},
{"ImportDecl", `import (@"math")`},
{"ImportDecl", `import (@mymath "math")`},
{"ImportDecl", `import (@. "math")`},
{"ConstDecl", `const @x`},
{"ConstDecl", `const @x = 0`},
{"ConstDecl", `const @x, y, z = 0, 1, 2`},
{"ConstDecl", `const (@x)`},
{"ConstDecl", `const (@x = 0)`},
{"ConstDecl", `const (@x, y, z = 0, 1, 2)`},
{"TypeDecl", `type @T int`},
{"TypeDecl", `type @T = int`},
{"TypeDecl", `type (@T int)`},
{"TypeDecl", `type (@T = int)`},
{"VarDecl", `var @x int`},
{"VarDecl", `var @x, y, z int`},
{"VarDecl", `var @x int = 0`},
{"VarDecl", `var @x, y, z int = 1, 2, 3`},
{"VarDecl", `var @x = 0`},
{"VarDecl", `var @x, y, z = 1, 2, 3`},
{"VarDecl", `var (@x int)`},
{"VarDecl", `var (@x, y, z int)`},
{"VarDecl", `var (@x int = 0)`},
{"VarDecl", `var (@x, y, z int = 1, 2, 3)`},
{"VarDecl", `var (@x = 0)`},
{"VarDecl", `var (@x, y, z = 1, 2, 3)`},
{"FuncDecl", `func @f() {}`},
{"FuncDecl", `func @(T) f() {}`},
{"FuncDecl", `func @(x T) f() {}`},
}
var exprs = []test{
// The position of an expression is the position
// of the left-most token that identifies the
// kind of expression.
{"Name", `@x`},
{"BasicLit", `@0`},
{"BasicLit", `@0x123`},
{"BasicLit", `@3.1415`},
{"BasicLit", `@.2718`},
{"BasicLit", `@1i`},
{"BasicLit", `@'a'`},
{"BasicLit", `@"abc"`},
{"BasicLit", "@`abc`"},
{"CompositeLit", `@{}`},
{"CompositeLit", `T@{}`},
{"CompositeLit", `struct{x, y int}@{}`},
{"KeyValueExpr", `"foo"@: true`},
{"KeyValueExpr", `"a"@: b`},
{"FuncLit", `@func (){}`},
{"ParenExpr", `@(x)`},
{"SelectorExpr", `a@.b`},
{"IndexExpr", `a@[i]`},
{"SliceExpr", `a@[:]`},
{"SliceExpr", `a@[i:]`},
{"SliceExpr", `a@[:j]`},
{"SliceExpr", `a@[i:j]`},
{"SliceExpr", `a@[i:j:k]`},
{"AssertExpr", `x@.(T)`},
{"Operation", `@*b`},
{"Operation", `@+b`},
{"Operation", `@-b`},
{"Operation", `@!b`},
{"Operation", `@^b`},
{"Operation", `@&b`},
{"Operation", `@<-b`},
{"Operation", `a @|| b`},
{"Operation", `a @&& b`},
{"Operation", `a @== b`},
{"Operation", `a @+ b`},
{"Operation", `a @* b`},
{"CallExpr", `f@()`},
{"CallExpr", `f@(x, y, z)`},
{"CallExpr", `obj.f@(1, 2, 3)`},
{"CallExpr", `func(x int) int { return x + 1 }@(y)`},
// ListExpr: tested via multi-value const/var declarations
}
var types = []test{
{"Operation", `@*T`},
{"Operation", `@*struct{}`},
{"ArrayType", `@[10]T`},
{"ArrayType", `@[...]T`},
{"SliceType", `@[]T`},
{"DotsType", `@...T`},
{"StructType", `@struct{}`},
{"InterfaceType", `@interface{}`},
{"FuncType", `func@()`},
{"MapType", `@map[T]T`},
{"ChanType", `@chan T`},
{"ChanType", `@chan<- T`},
{"ChanType", `@<-chan T`},
}
var fields = []test{
{"Field", `@T`},
{"Field", `@(T)`},
{"Field", `@x T`},
{"Field", `@x *(T)`},
{"Field", `@x, y, z T`},
{"Field", `@x, y, z (*T)`},
}
var stmts = []test{
{"EmptyStmt", `@`},
{"LabeledStmt", `L@:`},
{"LabeledStmt", `L@: ;`},
{"LabeledStmt", `L@: f()`},
{"BlockStmt", `@{}`},
// The position of an ExprStmt is the position of the expression.
{"ExprStmt", `@<-ch`},
{"ExprStmt", `f@()`},
{"ExprStmt", `append@(s, 1, 2, 3)`},
{"SendStmt", `ch @<- x`},
{"DeclStmt", `@const x = 0`},
{"DeclStmt", `@const (x = 0)`},
{"DeclStmt", `@type T int`},
{"DeclStmt", `@type T = int`},
{"DeclStmt", `@type (T1 = int; T2 = float32)`},
{"DeclStmt", `@var x = 0`},
{"DeclStmt", `@var x, y, z int`},
{"DeclStmt", `@var (a, b = 1, 2)`},
{"AssignStmt", `x @= y`},
{"AssignStmt", `a, b, x @= 1, 2, 3`},
{"AssignStmt", `x @+= y`},
{"AssignStmt", `x @:= y`},
{"AssignStmt", `x, ok @:= f()`},
{"AssignStmt", `x@++`},
{"AssignStmt", `a[i]@--`},
{"BranchStmt", `@break`},
{"BranchStmt", `@break L`},
{"BranchStmt", `@continue`},
{"BranchStmt", `@continue L`},
{"BranchStmt", `@fallthrough`},
{"BranchStmt", `@goto L`},
{"CallStmt", `@defer f()`},
{"CallStmt", `@go f()`},
{"ReturnStmt", `@return`},
{"ReturnStmt", `@return x`},
{"ReturnStmt", `@return a, b, a + b*f(1, 2, 3)`},
{"IfStmt", `@if cond {}`},
{"IfStmt", `@if cond { f() } else {}`},
{"IfStmt", `@if cond { f() } else { g(); h() }`},
{"ForStmt", `@for {}`},
{"ForStmt", `@for { f() }`},
{"SwitchStmt", `@switch {}`},
{"SwitchStmt", `@switch { default: }`},
{"SwitchStmt", `@switch { default: x++ }`},
{"SelectStmt", `@select {}`},
{"SelectStmt", `@select { default: }`},
{"SelectStmt", `@select { default: ch <- false }`},
}
var ranges = []test{
{"RangeClause", `@range s`},
{"RangeClause", `i = @range s`},
{"RangeClause", `i := @range s`},
{"RangeClause", `_, x = @range s`},
{"RangeClause", `i, x = @range s`},
{"RangeClause", `_, x := @range s.f`},
{"RangeClause", `i, x := @range f(i)`},
}
var guards = []test{
{"TypeSwitchGuard", `x@.(type)`},
{"TypeSwitchGuard", `x := x@.(type)`},
}
var cases = []test{
{"CaseClause", `@case x:`},
{"CaseClause", `@case x, y, z:`},
{"CaseClause", `@case x == 1, y == 2:`},
{"CaseClause", `@default:`},
}
var comms = []test{
{"CommClause", `@case <-ch:`},
{"CommClause", `@case x <- ch:`},
{"CommClause", `@case x = <-ch:`},
{"CommClause", `@case x := <-ch:`},
{"CommClause", `@case x, ok = <-ch: f(1, 2, 3)`},
{"CommClause", `@case x, ok := <-ch: x++`},
{"CommClause", `@default:`},
{"CommClause", `@default: ch <- true`},
}
func TestPos(t *testing.T) {
// TODO(gri) Once we have a general tree walker, we can use that to find
// the first occurrence of the respective node and we don't need to hand-
// extract the node for each specific kind of construct.
testPos(t, decls, "package p; ", "",
func(f *File) Node { return f.DeclList[0] },
)
// embed expressions in a composite literal so we can test key:value and naked composite literals
testPos(t, exprs, "package p; var _ = T{ ", " }",
func(f *File) Node { return f.DeclList[0].(*VarDecl).Values.(*CompositeLit).ElemList[0] },
)
// embed types in a function signature so we can test ... types
testPos(t, types, "package p; func f(", ")",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Type.ParamList[0].Type },
)
testPos(t, fields, "package p; func f(", ")",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Type.ParamList[0] },
)
testPos(t, stmts, "package p; func _() { ", "; }",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Body.List[0] },
)
testPos(t, ranges, "package p; func _() { for ", " {} }",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Body.List[0].(*ForStmt).Init.(*RangeClause) },
)
testPos(t, guards, "package p; func _() { switch ", " {} }",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Body.List[0].(*SwitchStmt).Tag.(*TypeSwitchGuard) },
)
testPos(t, cases, "package p; func _() { switch { ", " } }",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Body.List[0].(*SwitchStmt).Body[0] },
)
testPos(t, comms, "package p; func _() { select { ", " } }",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Body.List[0].(*SelectStmt).Body[0] },
)
}
func testPos(t *testing.T, list []test, prefix, suffix string, extract func(*File) Node) {
for _, test := range list {
// complete source, compute @ position, and strip @ from source
src, index := stripAt(prefix + test.snippet + suffix)
if index < 0 {
t.Errorf("missing @: %s (%s)", src, test.nodetyp)
continue
}
// build syntax tree
file, err := Parse(nil, strings.NewReader(src), nil, nil, 0)
if err != nil {
t.Errorf("parse error: %s: %v (%s)", src, err, test.nodetyp)
continue
}
// extract desired node
node := extract(file)
if typ := typeOf(node); typ != test.nodetyp {
t.Errorf("type error: %s: type = %s, want %s", src, typ, test.nodetyp)
continue
}
// verify node position with expected position as indicated by @
if pos := int(node.Pos().Col()); pos != index+colbase {
t.Errorf("pos error: %s: pos = %d, want %d (%s)", src, pos, index+colbase, test.nodetyp)
continue
}
}
}
func stripAt(s string) (string, int) {
if i := strings.Index(s, "@"); i >= 0 {
return s[:i] + s[i+1:], i
}
return s, -1
}
func typeOf(n Node) string {
const prefix = "*syntax."
k := fmt.Sprintf("%T", n)
return strings.TrimPrefix(k, prefix)
}
|