Repository: goplus/gop
Pull request #1176 (2022-05-17 02:49:49)

test gop/ast/togo


Patch:
diff --git a/ast/fromgo/gopast.go b/ast/fromgo/gopast.go
index 658ce996..aa1618e9 100644
--- a/ast/fromgo/gopast.go
+++ b/ast/fromgo/gopast.go
@@ -153,7 +153,7 @@ func gopExpr(val ast.Expr) gopast.Expr {
 			Elt:      gopExpr(v.Elt),
 		}
 	}
-	log.Panicln("goExpr: unknown expr -", reflect.TypeOf(val))
+	log.Panicln("gopExpr: unknown expr -", reflect.TypeOf(val))
 	return nil
 }
 
@@ -279,7 +279,7 @@ func gopGenDecl(v *ast.GenDecl) *gopast.GenDecl {
 		case token.VAR, token.CONST:
 			specs[i] = gopValueSpec(spec.(*ast.ValueSpec))
 		default:
-			log.Panicln("goGenDecl: unknown spec -", v.Tok)
+			log.Panicln("gopGenDecl: unknown spec -", v.Tok)
 		}
 	}
 	return &gopast.GenDecl{
@@ -300,7 +300,7 @@ func gopDecl(decl ast.Decl) gopast.Decl {
 	case *ast.FuncDecl:
 		return gopFuncDecl(v)
 	}
-	log.Panicln("goDecl: unkown decl -", reflect.TypeOf(decl))
+	log.Panicln("gopDecl: unkown decl -", reflect.TypeOf(decl))
 	return nil
 }
 


Test patch:
diff --git a/ast/fromgo/gopast_test.go b/ast/fromgo/gopast_test.go
index 4d0e0bdd..0539331e 100644
--- a/ast/fromgo/gopast_test.go
+++ b/ast/fromgo/gopast_test.go
@@ -71,10 +71,10 @@ func TestErrASTFile(t *testing.T) {
 }
 
 func TestErrDecl(t *testing.T) {
-	testPanic(t, "goDecl: unkown decl - <nil>\n", func() {
+	testPanic(t, "gopDecl: unkown decl - <nil>\n", func() {
 		gopDecl(nil)
 	})
-	testPanic(t, "goGenDecl: unknown spec - ILLEGAL\n", func() {
+	testPanic(t, "gopGenDecl: unknown spec - ILLEGAL\n", func() {
 		gopGenDecl(&ast.GenDecl{
 			Specs: []ast.Spec{nil},
 		})
@@ -82,7 +82,7 @@ func TestErrDecl(t *testing.T) {
 }
 
 func TestErrExpr(t *testing.T) {
-	testPanic(t, "goExpr: unknown expr - *ast.BadExpr\n", func() {
+	testPanic(t, "gopExpr: unknown expr - *ast.BadExpr\n", func() {
 		gopExpr(&ast.BadExpr{})
 	})
 }
diff --git a/ast/togo/goast_test.go b/ast/togo/goast_test.go
new file mode 100644
index 00000000..ff95c59a
--- /dev/null
+++ b/ast/togo/goast_test.go
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package togo
+
+import (
+	"bytes"
+	"go/format"
+	"go/token"
+	"testing"
+
+	gopast "github.com/goplus/gop/ast"
+	"github.com/goplus/gop/parser"
+)
+
+func testAST(t *testing.T, from, to string) {
+	fset := token.NewFileSet()
+	f, err := parser.ParseFile(fset, "foo.go", from, 0)
+	if err != nil {
+		t.Fatal("parser.ParseFile:", err)
+	}
+	gopf := ASTFile(f, 0)
+	var b bytes.Buffer
+	err = format.Node(&b, fset, gopf)
+	if err != nil {
+		t.Fatal("format.Node:", err)
+	}
+	result := b.String()
+	if to != result {
+		t.Fatalf("\nResult:\n%s\nExpected:\n%s\n", result, to)
+	}
+}
+
+func test(t *testing.T, src string) {
+	testAST(t, src, src)
+}
+
+func testPanic(t *testing.T, panicMsg string, doPanic func()) {
+	t.Run(panicMsg, func(t *testing.T) {
+		defer func() {
+			if e := recover(); e == nil {
+				t.Fatal("testPanic: no error?")
+			} else if msg := e.(string); msg != panicMsg {
+				t.Fatalf("\nResult:\n%s\nExpected Panic:\n%s\n", msg, panicMsg)
+			}
+		}()
+		doPanic()
+	})
+}
+
+func TestErrASTFile(t *testing.T) {
+	testPanic(t, "ASTFile: doesn't support keeping func body now\n", func() {
+		ASTFile(nil, KeepFuncBody)
+	})
+}
+
+func TestErrDecl(t *testing.T) {
+	testPanic(t, "goDecl: unkown decl - <nil>\n", func() {
+		goDecl(nil)
+	})
+	testPanic(t, "goGenDecl: unknown spec - ILLEGAL\n", func() {
+		goGenDecl(&gopast.GenDecl{
+			Specs: []gopast.Spec{nil},
+		})
+	})
+}
+
+func TestErrExpr(t *testing.T) {
+	testPanic(t, "goExpr: unknown expr - *ast.BadExpr\n", func() {
+		goExpr(&gopast.BadExpr{})
+	})
+}
+
+func TestBasic(t *testing.T) {
+	test(t, `package main
+
+import "fmt"
+
+type a struct {
+	v   map[int]chan int
+	arr *[2]func()
+	i   interface{}
+}
+
+var b = &a{
+	arr: &[2]func(){
+		nil,
+		func() {},
+	},
+}
+
+const c = (10 + 20) * 2
+
+var d = b.arr[1]
+
+var e = b.arr[:1]
+
+var f = a.i.(func() (int))()
+
+func foo(v ...interface{}) {}
+`)
+}