Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use `var _ = Describe("")` in lieu of `func init() { }`

  • Loading branch information...
commit c21be2cbb459e32bda1d39dd947879df809b85c5 1 parent c977a58
Tim Jarratt tjarratt authored
23 ginkgo/convert/ginkgo_ast_nodes.go
View
@@ -10,19 +10,18 @@ import (
/*
* Creates a func init() node
*/
-func createInitBlock() *ast.FuncDecl {
- blockStatement := &ast.BlockStmt{List: []ast.Stmt{}}
- fieldList := &ast.FieldList{}
- funcType := &ast.FuncType{Params: fieldList}
- ident := &ast.Ident{Name: "init"}
-
- return &ast.FuncDecl{Name: ident, Type: funcType, Body: blockStatement}
+ func createVarUnderscoreBlock() *ast.ValueSpec {
+ valueSpec := &ast.ValueSpec{}
+ object := &ast.Object{Kind: 4, Name: "_", Decl:valueSpec, Data: 0}
+ ident := &ast.Ident{Name: "_", Obj: object}
+ valueSpec.Names = append(valueSpec.Names, ident)
+ return valueSpec
}
/*
* Creates a Describe("Testing with ginkgo", func() { }) node
*/
-func createDescribeBlock() *ast.ExprStmt {
+func createDescribeBlock() *ast.CallExpr {
blockStatement := &ast.BlockStmt{List: []ast.Stmt{}}
fieldList := &ast.FieldList{}
@@ -30,9 +29,7 @@ func createDescribeBlock() *ast.ExprStmt {
funcLit := &ast.FuncLit{Type: funcType, Body: blockStatement}
basicLit := &ast.BasicLit{Kind: 9, Value: "\"Testing with Ginkgo\""}
describeIdent := &ast.Ident{Name: "Describe"}
- callExpr := &ast.CallExpr{Fun: describeIdent, Args: []ast.Expr{basicLit, funcLit}}
-
- return &ast.ExprStmt{X: callExpr}
+ return &ast.CallExpr{Fun: describeIdent, Args: []ast.Expr{basicLit, funcLit}}
}
/*
@@ -48,11 +45,11 @@ func namedTestingTArg(node *ast.FuncDecl) string {
/*
* Convenience function to return the block statement node for a Describe statement
*/
-func blockStatementFromDescribe(desc *ast.ExprStmt) *ast.BlockStmt {
+func blockStatementFromDescribe(desc *ast.CallExpr) *ast.BlockStmt {
var funcLit *ast.FuncLit
var found = false
- for _, node := range desc.X.(*ast.CallExpr).Args {
+ for _, node := range desc.Args {
switch node := node.(type) {
case *ast.FuncLit:
found = true
16 ginkgo/convert/testfile_rewriter.go
View
@@ -30,15 +30,23 @@ func rewriteTestsInFile(pathToFile string) {
addGinkgoImports(rootNode)
removeTestingImport(rootNode)
- topLevelInitFunc := createInitBlock()
+ varUnderscoreBlock := createVarUnderscoreBlock()
describeBlock := createDescribeBlock()
- topLevelInitFunc.Body.List = append(topLevelInitFunc.Body.List, describeBlock)
+ varUnderscoreBlock.Values = []ast.Expr{describeBlock}
for _, testFunc := range findTestFuncs(rootNode) {
rewriteTestFuncAsItStatement(testFunc, rootNode, describeBlock)
}
- rootNode.Decls = append(rootNode.Decls[:1], append([]ast.Decl{topLevelInitFunc}, rootNode.Decls[1:]...)...)
+ underscoreDecl := &ast.GenDecl{
+ Tok: 85, // gah, magick numbers are needed to make this work
+ TokPos: 14, // this tricks Go into writing "var _ = Describe"
+ Specs: []ast.Spec{varUnderscoreBlock},
+ }
+
+ imports := rootNode.Decls[0]
+ tail := rootNode.Decls[1:]
+ rootNode.Decls = append(append([]ast.Decl{imports}, underscoreDecl), tail...)
rewriteOtherFuncsToUseGinkgoT(rootNode.Decls)
walkNodesInRootNodeReplacingTestingT(rootNode)
@@ -61,7 +69,7 @@ func rewriteTestsInFile(pathToFile string) {
* It("does something neat", func() { __test_body_here__ }) and adds it
* to the Describe's list of statements
*/
-func rewriteTestFuncAsItStatement(testFunc *ast.FuncDecl, rootNode *ast.File, describe *ast.ExprStmt) {
+func rewriteTestFuncAsItStatement(testFunc *ast.FuncDecl, rootNode *ast.File, describe *ast.CallExpr) {
var funcIndex int = -1
for index, child := range rootNode.Decls {
if child == testFunc {
12 ginkgo/integration/_fixtures/convert_goldmasters/extra_functions_test.go
View
@@ -4,15 +4,13 @@ import (
. "github.com/onsi/ginkgo"
)
-func init() {
- Describe("Testing with Ginkgo", func() {
- It("something less important", func() {
+var _ = Describe("Testing with Ginkgo", func() {
+ It("something less important", func() {
- strp := "hello!"
- somethingImportant(GinkgoT(), &strp)
- })
+ strp := "hello!"
+ somethingImportant(GinkgoT(), &strp)
})
-}
+})
func somethingImportant(t GinkgoTestingT, message *string) {
t.Log("Something important happened in a test: " + *message)
10 ginkgo/integration/_fixtures/convert_goldmasters/nested_subpackage_test.go
View
@@ -4,10 +4,8 @@ import (
. "github.com/onsi/ginkgo"
)
-func init() {
- Describe("Testing with Ginkgo", func() {
- It("nested sub packages", func() {
- GinkgoT().Fail(true)
- })
+var _ = Describe("Testing with Ginkgo", func() {
+ It("nested sub packages", func() {
+ GinkgoT().Fail(true)
})
-}
+})
12 ginkgo/integration/_fixtures/convert_goldmasters/nested_test.go
View
@@ -4,12 +4,10 @@ import (
. "github.com/onsi/ginkgo"
)
-func init() {
- Describe("Testing with Ginkgo", func() {
- It("something less important", func() {
+var _ = Describe("Testing with Ginkgo", func() {
+ It("something less important", func() {
- whatever := &UselessStruct{}
- GinkgoT().Fail(whatever.ImportantField != "SECRET_PASSWORD")
- })
+ whatever := &UselessStruct{}
+ GinkgoT().Fail(whatever.ImportantField != "SECRET_PASSWORD")
})
-}
+})
16 ginkgo/integration/_fixtures/convert_goldmasters/outside_package_test.go
View
@@ -4,17 +4,15 @@ import (
. "github.com/onsi/ginkgo"
)
-func init() {
- Describe("Testing with Ginkgo", func() {
- It("something important", func() {
+var _ = Describe("Testing with Ginkgo", func() {
+ It("something important", func() {
- whatever := &UselessStruct{}
- if whatever.ImportantField != "SECRET_PASSWORD" {
- GinkgoT().Fail()
- }
- })
+ whatever := &UselessStruct{}
+ if whatever.ImportantField != "SECRET_PASSWORD" {
+ GinkgoT().Fail()
+ }
})
-}
+})
type UselessStruct struct {
ImportantField string
38 ginkgo/integration/_fixtures/convert_goldmasters/xunit_test.go
View
@@ -4,27 +4,25 @@ import (
. "github.com/onsi/ginkgo"
)
-func init() {
- Describe("Testing with Ginkgo", func() {
- It("something important", func() {
-
- whatever := &UselessStruct{
- T: GinkgoT(),
- ImportantField: "SECRET_PASSWORD",
- }
- something := &UselessStruct{ImportantField: "string value"}
- assertEqual(GinkgoT(), whatever.ImportantField, "SECRET_PASSWORD")
- assertEqual(GinkgoT(), something.ImportantField, "string value")
-
- var foo = func(t GinkgoTestingT) {}
- foo(GinkgoT())
-
- strp := "something"
- testFunc(GinkgoT(), &strp)
- GinkgoT().Fail()
- })
+var _ = Describe("Testing with Ginkgo", func() {
+ It("something important", func() {
+
+ whatever := &UselessStruct{
+ T: GinkgoT(),
+ ImportantField: "SECRET_PASSWORD",
+ }
+ something := &UselessStruct{ImportantField: "string value"}
+ assertEqual(GinkgoT(), whatever.ImportantField, "SECRET_PASSWORD")
+ assertEqual(GinkgoT(), something.ImportantField, "string value")
+
+ var foo = func(t GinkgoTestingT) {}
+ foo(GinkgoT())
+
+ strp := "something"
+ testFunc(GinkgoT(), &strp)
+ GinkgoT().Fail()
})
-}
+})
type UselessStruct struct {
ImportantField string
Please sign in to comment.
Something went wrong with that request. Please try again.