aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2021-01-07 17:20:55 -0800
committerRobert Griesemer <gri@golang.org>2021-01-08 17:32:18 +0000
commit7903214fcc52a53a7749b4634eb9e940c27ffe75 (patch)
tree0e09bd0e3ad27ca6d7f98f64e8279236234713ab
parent0aede1205bdac5d3b938476a6e682190e835bb26 (diff)
downloadgo-7903214fcc52a53a7749b4634eb9e940c27ffe75.tar.gz
go-7903214fcc52a53a7749b4634eb9e940c27ffe75.zip
[dev.typeparams] cmd/compile/internal/syntax: add ShortString tests
This CL moves the exprstring_test.go from the types2 package into the syntax package (which contains the actual ShortString function). The code is mostly un- changed but for the updated TestShortString function. Change-Id: Ib39e3181e643fc0ac96ddf144a3114893a50c2fc Reviewed-on: https://go-review.googlesource.com/c/go/+/282554 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
-rw-r--r--src/cmd/compile/internal/syntax/printer_test.go87
-rw-r--r--src/cmd/compile/internal/types2/exprstring_test.go98
2 files changed, 87 insertions, 98 deletions
diff --git a/src/cmd/compile/internal/syntax/printer_test.go b/src/cmd/compile/internal/syntax/printer_test.go
index 6c07fe0a26..e83e9c1b2c 100644
--- a/src/cmd/compile/internal/syntax/printer_test.go
+++ b/src/cmd/compile/internal/syntax/printer_test.go
@@ -96,3 +96,90 @@ func testOut() io.Writer {
}
return ioutil.Discard
}
+
+func dup(s string) [2]string { return [2]string{s, s} }
+
+var exprTests = [][2]string{
+ // basic type literals
+ dup("x"),
+ dup("true"),
+ dup("42"),
+ dup("3.1415"),
+ dup("2.71828i"),
+ dup(`'a'`),
+ dup(`"foo"`),
+ dup("`bar`"),
+
+ // func and composite literals
+ dup("func() {}"),
+ dup("[]int{}"),
+ {"func(x int) complex128 { return 0 }", "func(x int) complex128 {…}"},
+ {"[]int{1, 2, 3}", "[]int{…}"},
+
+ // non-type expressions
+ dup("(x)"),
+ dup("x.f"),
+ dup("a[i]"),
+
+ dup("s[:]"),
+ dup("s[i:]"),
+ dup("s[:j]"),
+ dup("s[i:j]"),
+ dup("s[:j:k]"),
+ dup("s[i:j:k]"),
+
+ dup("x.(T)"),
+
+ dup("x.([10]int)"),
+ dup("x.([...]int)"),
+
+ dup("x.(struct{})"),
+ dup("x.(struct{x int; y, z float32; E})"),
+
+ dup("x.(func())"),
+ dup("x.(func(x int))"),
+ dup("x.(func() int)"),
+ dup("x.(func(x, y int, z float32) (r int))"),
+ dup("x.(func(a, b, c int))"),
+ dup("x.(func(x ...T))"),
+
+ dup("x.(interface{})"),
+ dup("x.(interface{m(); n(x int); E})"),
+ dup("x.(interface{m(); n(x int) T; E; F})"),
+
+ dup("x.(map[K]V)"),
+
+ dup("x.(chan E)"),
+ dup("x.(<-chan E)"),
+ dup("x.(chan<- chan int)"),
+ dup("x.(chan<- <-chan int)"),
+ dup("x.(<-chan chan int)"),
+ dup("x.(chan (<-chan int))"),
+
+ dup("f()"),
+ dup("f(x)"),
+ dup("int(x)"),
+ dup("f(x, x + y)"),
+ dup("f(s...)"),
+ dup("f(a, s...)"),
+
+ dup("*x"),
+ dup("&x"),
+ dup("x + y"),
+ dup("x + y << (2 * s)"),
+}
+
+func TestShortString(t *testing.T) {
+ for _, test := range exprTests {
+ src := "package p; var _ = " + test[0]
+ ast, err := Parse(nil, strings.NewReader(src), nil, nil, 0)
+ if err != nil {
+ t.Errorf("%s: %s", test[0], err)
+ continue
+ }
+ x := ast.DeclList[0].(*VarDecl).Values
+ if got := ShortString(x); got != test[1] {
+ t.Errorf("%s: got %s, want %s", test[0], got, test[1])
+ }
+ }
+}
diff --git a/src/cmd/compile/internal/types2/exprstring_test.go b/src/cmd/compile/internal/types2/exprstring_test.go
deleted file mode 100644
index 39e1354eac..0000000000
--- a/src/cmd/compile/internal/types2/exprstring_test.go
+++ /dev/null
@@ -1,98 +0,0 @@
-// UNREVIEWED
-// Copyright 2013 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 types2_test
-
-import (
- "testing"
-
- "cmd/compile/internal/syntax"
-)
-
-// TODO(gri) move these tests into syntax package
-var testExprs = []testEntry{
- // basic type literals
- dup("x"),
- dup("true"),
- dup("42"),
- dup("3.1415"),
- dup("2.71828i"),
- dup(`'a'`),
- dup(`"foo"`),
- dup("`bar`"),
-
- // func and composite literals
- dup("func() {}"),
- dup("[]int{}"),
- {"func(x int) complex128 { return 0 }", "func(x int) complex128 {…}"},
- {"[]int{1, 2, 3}", "[]int{…}"},
-
- // non-type expressions
- dup("(x)"),
- dup("x.f"),
- dup("a[i]"),
-
- dup("s[:]"),
- dup("s[i:]"),
- dup("s[:j]"),
- dup("s[i:j]"),
- dup("s[:j:k]"),
- dup("s[i:j:k]"),
-
- dup("x.(T)"),
-
- dup("x.([10]int)"),
- dup("x.([...]int)"),
-
- dup("x.(struct{})"),
- dup("x.(struct{x int; y, z float32; E})"),
-
- dup("x.(func())"),
- dup("x.(func(x int))"),
- dup("x.(func() int)"),
- dup("x.(func(x, y int, z float32) (r int))"),
- dup("x.(func(a, b, c int))"),
- dup("x.(func(x ...T))"),
-
- dup("x.(interface{})"),
- dup("x.(interface{m(); n(x int); E})"),
- dup("x.(interface{m(); n(x int) T; E; F})"),
-
- dup("x.(map[K]V)"),
-
- dup("x.(chan E)"),
- dup("x.(<-chan E)"),
- dup("x.(chan<- chan int)"),
- dup("x.(chan<- <-chan int)"),
- dup("x.(<-chan chan int)"),
- dup("x.(chan (<-chan int))"),
-
- dup("f()"),
- dup("f(x)"),
- dup("int(x)"),
- dup("f(x, x + y)"),
- dup("f(s...)"),
- dup("f(a, s...)"),
-
- dup("*x"),
- dup("&x"),
- dup("x + y"),
- dup("x + y << (2 * s)"),
-}
-
-func TestExprString(t *testing.T) {
- for _, test := range testExprs {
- src := "package p; var _ = " + test.src
- f, err := parseSrc("expr", src)
- if err != nil {
- t.Errorf("%s: %s", test.src, err)
- continue
- }
- x := f.DeclList[0].(*syntax.VarDecl).Values
- if got := syntax.ShortString(x); got != test.str {
- t.Errorf("%s: got %s, want %s", test.src, got, test.str)
- }
- }
-}