Permalink
Browse files

Add CurrentGinkgoTestDescription

  • Loading branch information...
1 parent 5d1b03e commit 56d6f65e2d015b245bdb07b8c39781ecb10b671d Onsi Fakhouri committed Jan 23, 2014
Showing with 78 additions and 0 deletions.
  1. +16 −0 example.go
  2. +9 −0 example_collection.go
  3. +15 −0 example_test.go
  4. +23 −0 ginkgo.go
  5. +4 −0 suite.go
  6. +11 −0 suite_test.go
View
@@ -2,6 +2,7 @@ package ginkgo
import (
"github.com/onsi/ginkgo/types"
+ "strings"
"time"
)
@@ -200,6 +201,21 @@ func (ex *example) summary(suiteID string) *types.ExampleSummary {
}
}
+func (ex *example) ginkgoTestDescription() GinkgoTestDescription {
+ summary := ex.summary("")
+
+ leafCodeLocation := summary.ComponentCodeLocations[len(summary.ComponentCodeLocations)-1]
+
+ return GinkgoTestDescription{
+ ComponentTexts: summary.ComponentTexts[1:],
+ FullTestText: strings.Join(summary.ComponentTexts[1:], " "),
+ TestText: summary.ComponentTexts[len(summary.ComponentTexts)-1],
+ IsMeasurement: summary.IsMeasurement,
+ FileName: leafCodeLocation.FileName,
+ LineNumber: leafCodeLocation.LineNumber,
+ }
+}
+
func (ex *example) measurementsReport() (measurements map[string]*types.ExampleMeasurement) {
measurements = map[string]*types.ExampleMeasurement{}
if ex.subjectComponentType() != types.ExampleComponentTypeMeasure {
@@ -167,6 +167,15 @@ func (collection *exampleCollection) fail(failure failureData) {
}
}
+func (collection *exampleCollection) currentGinkgoTestDescription() GinkgoTestDescription {
+ currentExample := collection.runningExample
+ if currentExample == nil {
+ return GinkgoTestDescription{}
+ }
+
+ return currentExample.ginkgoTestDescription()
+}
+
func (collection *exampleCollection) reportSuiteWillBegin() {
collection.startTime = time.Now()
summary := collection.summary()
View
@@ -238,6 +238,21 @@ func init() {
})
})
+ Describe("the GinkgoTestDescription", func() {
+ It("should have the GinkgoTestDescription", func() {
+ ginkgoTestDescription := ex.ginkgoTestDescription()
+ Ω(ginkgoTestDescription.ComponentTexts).Should(Equal([]string{
+ "inner", "it",
+ }))
+
+ Ω(ginkgoTestDescription.FullTestText).Should(Equal("inner it"))
+ Ω(ginkgoTestDescription.TestText).Should(Equal("it"))
+ Ω(ginkgoTestDescription.IsMeasurement).Should(BeFalse())
+ Ω(ginkgoTestDescription.FileName).Should(Equal(it.codeLocation.FileName))
+ Ω(ginkgoTestDescription.LineNumber).Should(Equal(it.codeLocation.LineNumber))
+ })
+ })
+
Context("when none of the runnable nodes fail", func() {
It("has a summary reporting no failure", func() {
ex.run()
View
@@ -48,6 +48,29 @@ type Reporter reporters.Reporter
//to tell Ginkgo that your async test is done.
type Done chan<- interface{}
+//GinkgoTestDescription represents the information about the current running test returned by CurrentGinkgoTest
+// ComponentTexts: a list of all the texts for the containers (Describes and Contexts) leading up to the current test.
+// FullTestText: a concatenation of ComponentTexts
+// TestText: the text in the actual It or Measure node
+// IsMeasurement: true if the current test is a measurement
+// FileName: the name of the file containing the current test
+// LineNumber: the line number for the current test
+type GinkgoTestDescription struct {
+ ComponentTexts []string
+ FullTestText string
+ TestText string
+
+ IsMeasurement bool
+
+ FileName string
+ LineNumber int
+}
+
+//CurrentGinkgoTestDescripton returns information about the current running test.
+func CurrentGinkgoTestDescription() GinkgoTestDescription {
+ return globalSuite.currentGinkgoTestDescription()
+}
+
//Measurement tests receive a Benchmarker.
//
//You use the Time() function to time how long the passed in body function takes to run
View
@@ -55,6 +55,10 @@ func (suite *suite) fail(message string, callerSkip int) {
}
}
+func (suite *suite) currentGinkgoTestDescription() GinkgoTestDescription {
+ return suite.exampleCollection.currentGinkgoTestDescription()
+}
+
func (suite *suite) pushContainerNode(text string, body func(), flag flagType, codeLocation types.CodeLocation) {
container := newContainerNode(text, flag, codeLocation)
suite.currentContainer.pushContainerNode(container)
View
@@ -87,6 +87,17 @@ func init() {
Ω(fakeR.BeginSummary.SuiteDescription).Should(Equal("suite description"))
})
+ It("provides information about the current test", func() {
+ description := CurrentGinkgoTestDescription()
+ Ω(description.ComponentTexts).Should(Equal([]string{"Suite", "running a suite", "provides information about the current test"}))
+ Ω(description.FullTestText).Should(Equal("Suite running a suite provides information about the current test"))
+ Ω(description.TestText).Should(Equal("provides information about the current test"))
+ Ω(description.IsMeasurement).Should(BeFalse())
+ Ω(description.FileName).Should(ContainSubstring("suite_test.go"))
+ Ω(description.LineNumber).Should(BeNumerically(">", 50))
+ Ω(description.LineNumber).Should(BeNumerically("<", 150))
+ })
+
Measure("should run measurements", func(b Benchmarker) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))

0 comments on commit 56d6f65

Please sign in to comment.