Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/skip describe standalone #84

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ func Test(t *testing.T) {
g.Assert(3+1).Equal(4)
})
})

g.Skip.Describe("Skip all tests in this block", func(){
// Skipped
g.It("Should not run", func() {
g.Assert(2).Equal(4)
})
})
}
```

Expand Down
66 changes: 66 additions & 0 deletions describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,69 @@ func TestNestedAfter(t *testing.T) {
t.Fatal("Failed")
}
}

func TestSkipDescribe(t *testing.T) {
g := Goblin(t)

g.Describe("Describe will run", func() {

g.It("This will run", func() {
g.Assert(4).Equal(4)
})
})

g.Skip.Describe("Describe will not run", func() {

g.Before(func() {
t.Fatal("This Before() should not run")
})
g.After(func() {
t.Fatal("This After() should not run")
})

g.BeforeEach(func() {
t.Fatal("This BeforeEach() should not run")
})
g.AfterEach(func() {
t.Fatal("This AfterEach() should not run")
})

g.JustBeforeEach(func() {
t.Fatal("This JustBeforeEach() should not run")
})

g.It("This will not run", func() {
t.Fatal("Failed")
})

g.Describe("Describe will not run also", func() {
g.Before(func() {
t.Fatal("This Before() should not run")
})
g.After(func() {
t.Fatal("This After() should not run")
})
g.It("This will not run also", func() {
t.Fatal("Failed")
})
})
})

g.Describe("Last describe will run", func() {

counter := 0

g.Before(func() {
counter++
})
g.BeforeEach(func() {
counter++
})
g.JustBeforeEach(func() {
counter++
})
g.It("This will run", func() {
g.Assert(counter).Equal(3)
})
})
}
62 changes: 61 additions & 1 deletion goblin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (g *G) Describe(name string, h func()) {

if d.parent != nil {
d.parent.children = append(d.parent.children, Runnable(d))
d.isSkipped = d.parent.isSkipped
}

g.parent = d
Expand Down Expand Up @@ -59,6 +60,7 @@ type Describe struct {
justBeforeEach []func()
hasTests bool
parent *Describe
isSkipped bool
}

func (d *Describe) runBeforeEach() {
Expand Down Expand Up @@ -95,7 +97,12 @@ func (d *Describe) runAfterEach() {
func (d *Describe) run(g *G) bool {
failed := false
if d.hasTests {
g.reporter.BeginDescribe(d.name)

if d.isSkipped {
g.reporter.BeginXdescribe(d.name)
} else {
g.reporter.BeginDescribe(d.name)
}

for _, b := range d.befores {
b()
Expand Down Expand Up @@ -135,6 +142,11 @@ type It struct {
func (it *It) run(g *G) bool {
g.currentIt = it

if it.parent.isSkipped {
g.reporter.ItIsExcluded(it.name)
return false
}

if it.h == nil {
g.reporter.ItIsPending(it.name)
return false
Expand Down Expand Up @@ -166,6 +178,14 @@ func (it *It) failed(msg string, stack []string) {
it.failure = &Failure{Stack: stack, Message: msg, TestName: it.parent.name + " " + it.name}
}

type Skip struct {
g *G
}

func (s *Skip) Describe(name string, h func()) {
s.g.xdescribe(name, h)
}

type Xit struct {
h interface{}
name string
Expand Down Expand Up @@ -206,6 +226,8 @@ func Goblin(t *testing.T, arguments ...string) *G {
parseFlags()
}
g := &G{t: t, timeout: *timeout}
g.Skip = &Skip{g: g}

var fancy TextFancier
if *isTty {
fancy = &TerminalFancier{}
Expand Down Expand Up @@ -267,6 +289,7 @@ type G struct {
shouldContinue chan bool
mutex sync.Mutex
timer *time.Timer
Skip *Skip
}

func (g *G) SetReporter(r Reporter) {
Expand Down Expand Up @@ -295,6 +318,28 @@ func (g *G) Xit(name string, h ...interface{}) {
}
}

func (g *G) xdescribe(name string, h func()) {
d := &Describe{name: name, h: h, parent: g.parent, isSkipped: true}

if d.parent != nil {
d.parent.children = append(d.parent.children, Runnable(d))
}

g.parent = d

h()

g.parent = d.parent

if g.parent == nil && d.hasTests {
g.reporter.Begin()
if d.run(g) {
g.t.Fail()
}
g.reporter.End()
}
}

func matchesRegex(value string) bool {
if runRegex != nil {
return runRegex.MatchString(value)
Expand All @@ -310,22 +355,37 @@ func notifyParents(d *Describe) {
}

func (g *G) Before(h func()) {
if g.parent.isSkipped {
return
}
g.parent.befores = append(g.parent.befores, h)
}

func (g *G) BeforeEach(h func()) {
if g.parent.isSkipped {
return
}
g.parent.beforeEach = append(g.parent.beforeEach, h)
}

func (g *G) JustBeforeEach(h func()) {
if g.parent.isSkipped {
return
}
g.parent.justBeforeEach = append(g.parent.justBeforeEach, h)
}

func (g *G) After(h func()) {
if g.parent.isSkipped {
return
}
g.parent.afters = append(g.parent.afters, h)
}

func (g *G) AfterEach(h func()) {
if g.parent.isSkipped {
return
}
g.parent.afterEach = append(g.parent.afterEach, h)
}

Expand Down
7 changes: 7 additions & 0 deletions reporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type Reporter interface {
BeginDescribe(string)
BeginXdescribe(string)
EndDescribe()
Begin()
End()
Expand Down Expand Up @@ -89,6 +90,12 @@ func (r *DetailedReporter) BeginDescribe(name string) {
r.level++
}

func (r *DetailedReporter) BeginXdescribe(name string) {
fmt.Println("")
r.print(r.fancy.Yellow(name))
r.level++
}

func (r *DetailedReporter) EndDescribe() {
r.level--
}
Expand Down
4 changes: 4 additions & 0 deletions reporting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (r *FakeReporter) BeginDescribe(name string) {
r.describes = append(r.describes, name)
}

func (r *FakeReporter) BeginXdescribe(name string) {
r.BeginDescribe(name)
}

func (r *FakeReporter) EndDescribe() {
r.ends++
}
Expand Down