Permalink
Browse files

Improve converted It("converts tests to ginkgo")

Given that unit test names always start with Test and are camel cased, it
seemed to me that we could do better to write a much more natural test name
for `ginkgo convert`.
  • Loading branch information...
1 parent c8efbb9 commit ab055965facfab00befb2451391cf1a423db2d24 @tjarratt tjarratt committed Feb 7, 2014
@@ -3,6 +3,8 @@ package convert
import (
"fmt"
"go/ast"
+ "strings"
+ "unicode"
)
/*
@@ -75,8 +77,38 @@ func createItStatementForTestFunc(testFunc *ast.FuncDecl) *ast.ExprStmt {
fieldList := &ast.FieldList{}
funcType := &ast.FuncType{Params: fieldList}
funcLit := &ast.FuncLit{Type: funcType, Body: blockStatement}
- basicLit := &ast.BasicLit{Kind: 9, Value: fmt.Sprintf("\"%s\"", testFunc.Name.Name)}
+
+ testName := rewriteTestName(testFunc.Name.Name)
+ basicLit := &ast.BasicLit{Kind: 9, Value: fmt.Sprintf("\"%s\"", testName)}
itBlockIdent := &ast.Ident{Name: "It"}
callExpr := &ast.CallExpr{Fun: itBlockIdent, Args: []ast.Expr{basicLit, funcLit}}
return &ast.ExprStmt{X: callExpr}
}
+
+/*
+* rewrite test names to be human readable
+* eg: rewrites "TestSomethingAmazing" as "something amazing"
+*/
+func rewriteTestName(testName string) string {
+ nameComponents := []string{}
+ currentString := ""
+ indexOfTest := strings.Index(testName, "Test")
+ if indexOfTest != 0 {
+ return testName
+ }
+
+ testName = strings.Replace(testName, "Test", "", 1)
+ first, rest := testName[0], testName[1:]
+ testName = string(unicode.ToLower(rune(first))) + rest
+
+ for _, rune := range testName {
+ if unicode.IsUpper(rune) {
+ nameComponents = append(nameComponents, currentString)
+ currentString = string(unicode.ToLower(rune))
+ } else {
+ currentString += string(rune)
+ }
+ }
+
+ return strings.Join(append(nameComponents, currentString), " ")
+}
@@ -9,7 +9,7 @@ func somethingImportant(t GinkgoTestingT, message *string) {
}
func init() {
Describe("Testing with Ginkgo", func() {
- It("TestSomethingLessImportant", func() {
+ It("something less important", func() {
somethingImportant(GinkgoT(), &"hello!")
})
})
@@ -6,7 +6,7 @@ import (
func init() {
Describe("Testing with Ginkgo", func() {
- It("TestNestedSubPackages", func() {
+ It("nested sub packages", func() {
GinkgoT().Fail(true)
})
})
@@ -6,7 +6,7 @@ import (
func init() {
Describe("Testing with Ginkgo", func() {
- It("TestSomethingLessImportant", func() {
+ It("something less important", func() {
whatever := &UselessStruct{}
GinkgoT().Fail(whatever.ImportantField != "SECRET_PASSWORD")
@@ -10,7 +10,7 @@ type UselessStruct struct {
func init() {
Describe("Testing with Ginkgo", func() {
- It("TestSomethingImportant", func() {
+ It("something important", func() {
whatever := &UselessStruct{}
GinkgoT().Fail(whatever.ImportantField != "SECRET_PASSWORD")
@@ -13,7 +13,7 @@ var testFunc = func(t GinkgoTestingT, arg *string) {}
func init() {
Describe("Testing with Ginkgo", func() {
- It("TestSomethingImportant", func() {
+ It("something important", func() {
whatever := &UselessStruct{
T: GinkgoT(),

0 comments on commit ab05596

Please sign in to comment.