Permalink
Browse files

Merge pull request #36 from tjarratt/from-the-desc-of-mr-underscore

Use `var _ = Describe()` instead of `func init() { }`
  • Loading branch information...
2 parents 3bafcec + c21be2c commit 6eba1b1d1f32657bcef0d4d53d7603f23c6772a3 @onsi committed Feb 13, 2014
@@ -10,29 +10,26 @@ 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{}
funcType := &ast.FuncType{Params: fieldList}
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
@@ -112,3 +109,15 @@ func rewriteTestName(testName string) string {
return strings.Join(append(nameComponents, currentString), " ")
}
+
+func newGinkgoTFromIdent(ident *ast.Ident) *ast.CallExpr {
+ return &ast.CallExpr{
+ Lparen: ident.NamePos + 1,
+ Rparen: ident.NamePos + 2,
+ Fun: &ast.Ident{Name: "GinkgoT"},
+ }
+}
+
+func newGinkgoTestingT() *ast.Ident {
+ return &ast.Ident{Name: "GinkgoTestingT"}
+}
@@ -1,17 +0,0 @@
-package convert
-
-import (
- "go/ast"
-)
-
-func newGinkgoTFromIdent(ident *ast.Ident) *ast.CallExpr {
- return &ast.CallExpr{
- Lparen: ident.NamePos + 1,
- Rparen: ident.NamePos + 2,
- Fun: &ast.Ident{Name: "GinkgoT"},
- }
-}
-
-func newGinkgoTestingT() *ast.Ident {
- return &ast.Ident{Name: "GinkgoTestingT"}
-}
@@ -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, topLevelInitFunc)
+ 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 {
@@ -5,7 +5,8 @@ import (
)
func TestSomethingLessImportant(t *testing.T) {
- somethingImportant(t, &"hello!")
+ strp := "hello!"
+ somethingImportant(t, &strp)
}
func somethingImportant(t *testing.T, message *string) {
@@ -1,20 +0,0 @@
-package tmp
-
-import (
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
-)
-
-type UselessStruct struct {
- ImportantField string
-}
-
-func init() {
- Describe("Testing with ginkgo", func() {
- It("TestSomethingImportant", func() {
-
- whatever := &UselessStruct{}
- t.Fail(whatever.ImportantField != "SECRET_PASSWORD")
- })
- })
-}
@@ -10,5 +10,7 @@ type UselessStruct struct {
func TestSomethingImportant(t *testing.T) {
whatever := &UselessStruct{}
- t.Fail(whatever.ImportantField != "SECRET_PASSWORD")
+ if whatever.ImportantField != "SECRET_PASSWORD" {
+ t.Fail()
+ }
}
@@ -11,17 +11,25 @@ type UselessStruct struct {
var testFunc = func(t *testing.T, arg *string) {}
+func assertEqual(t *testing.T, arg1, arg2 interface{}) {
+ if arg1 != arg2 {
+ t.Fail()
+ }
+}
+
func TestSomethingImportant(t *testing.T) {
whatever := &UselessStruct{
T: t,
- ImportantField: "twisty maze of passages",
+ ImportantField: "SECRET_PASSWORD",
}
- app := "string value"
- something := &UselessStruct{ImportantField: app}
+ something := &UselessStruct{ImportantField: "string value"}
+ assertEqual(t, whatever.ImportantField, "SECRET_PASSWORD")
+ assertEqual(t, something.ImportantField, "string value")
- t.Fail(whatever.ImportantField != "SECRET_PASSWORD")
- assert.Equal(t, whatever.ImportantField, "SECRET_PASSWORD")
var foo = func(t *testing.T) {}
- foo()
- testFunc(t, "something")
+ foo(t)
+
+ strp := "something"
+ testFunc(t, &strp)
+ t.Fail()
}
@@ -4,13 +4,14 @@ import (
. "github.com/onsi/ginkgo"
)
+var _ = Describe("Testing with Ginkgo", func() {
+ It("something less important", func() {
+
+ strp := "hello!"
+ somethingImportant(GinkgoT(), &strp)
+ })
+})
+
func somethingImportant(t GinkgoTestingT, message *string) {
t.Log("Something important happened in a test: " + *message)
}
-func init() {
- Describe("Testing with Ginkgo", func() {
- It("something less important", func() {
- somethingImportant(GinkgoT(), &"hello!")
- })
- })
-}
@@ -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)
})
-}
+})
@@ -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")
})
-}
+})
@@ -4,16 +4,16 @@ import (
. "github.com/onsi/ginkgo"
)
-type UselessStruct struct {
- ImportantField string
-}
-
-func init() {
- Describe("Testing with Ginkgo", func() {
- It("something important", func() {
+var _ = Describe("Testing with Ginkgo", func() {
+ It("something important", func() {
- whatever := &UselessStruct{}
- GinkgoT().Fail(whatever.ImportantField != "SECRET_PASSWORD")
- })
+ whatever := &UselessStruct{}
+ if whatever.ImportantField != "SECRET_PASSWORD" {
+ GinkgoT().Fail()
+ }
})
+})
+
+type UselessStruct struct {
+ ImportantField string
}
@@ -4,28 +4,35 @@ import (
. "github.com/onsi/ginkgo"
)
+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
T GinkgoTestingT
}
var testFunc = func(t GinkgoTestingT, arg *string) {}
-func init() {
- Describe("Testing with Ginkgo", func() {
- It("something important", func() {
-
- whatever := &UselessStruct{
- T: GinkgoT(),
- ImportantField: "twisty maze of passages",
- }
- app := "string value"
- something := &UselessStruct{ImportantField: app}
- GinkgoT().Fail(whatever.ImportantField != "SECRET_PASSWORD")
- assert.Equal(GinkgoT(), whatever.ImportantField, "SECRET_PASSWORD")
- var foo = func(t GinkgoTestingT) {}
- foo()
- testFunc(GinkgoT(), "something")
- })
- })
+func assertEqual(t GinkgoTestingT, arg1, arg2 interface{}) {
+ if arg1 != arg2 {
+ t.Fail()
+ }
}

0 comments on commit 6eba1b1

Please sign in to comment.