1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
package goblin
import (
"flag"
"fmt"
"regexp"
"runtime"
"sync"
"testing"
"time"
)
type Done func(error ...interface{})
type Runnable interface {
run(*G) bool
}
type Itable interface {
run(*G) bool
failed(string, []string)
}
func (g *G) Describe(name string, h func()) {
d := &Describe{name: name, h: h, parent: g.parent}
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 (g *G) Timeout(time time.Duration) {
g.timeout = time
g.timer.Reset(time)
}
type Describe struct {
name string
h func()
children []Runnable
befores []func()
afters []func()
afterEach []func()
beforeEach []func()
justBeforeEach []func()
hasTests bool
parent *Describe
}
func (d *Describe) runBeforeEach() {
if d.parent != nil {
d.parent.runBeforeEach()
}
for _, b := range d.beforeEach {
b()
}
}
func (d *Describe) runJustBeforeEach() {
if d.parent != nil {
d.parent.runJustBeforeEach()
}
for _, b := range d.justBeforeEach {
b()
}
}
func (d *Describe) runAfterEach() {
for _, a := range d.afterEach {
a()
}
if d.parent != nil {
d.parent.runAfterEach()
}
}
func (d *Describe) run(g *G) bool {
failed := false
if d.hasTests {
g.reporter.BeginDescribe(d.name)
for _, b := range d.befores {
b()
}
for _, r := range d.children {
if r.run(g) {
failed = true
}
}
for _, a := range d.afters {
a()
}
g.reporter.EndDescribe()
}
return failed
}
type Failure struct {
Stack []string
TestName string
Message string
}
type It struct {
h interface{}
name string
parent *Describe
failure *Failure
failureMu sync.RWMutex
reporter Reporter
isAsync bool
}
func (it *It) run(g *G) bool {
g.currentIt = it
if it.h == nil {
g.reporter.ItIsPending(it.name)
return false
}
runIt(g, it)
failed := false
it.failureMu.RLock()
if it.failure != nil {
failed = true
}
it.failureMu.RUnlock()
if failed {
g.reporter.ItFailed(it.name)
g.reporter.Failure(it.failure)
} else {
g.reporter.ItPassed(it.name)
}
return failed
}
func (it *It) failed(msg string, stack []string) {
it.failureMu.Lock()
defer it.failureMu.Unlock()
it.failure = &Failure{Stack: stack, Message: msg, TestName: it.parent.name + " " + it.name}
}
type Xit struct {
h interface{}
name string
parent *Describe
failure *Failure
reporter Reporter
isAsync bool
}
func (xit *Xit) run(g *G) bool {
g.currentIt = xit
g.reporter.ItIsExcluded(xit.name)
return false
}
func (xit *Xit) failed(msg string, stack []string) {
xit.failure = nil
}
func parseFlags() {
//Flag parsing
flag.Parse()
if *regexParam != "" {
runRegex = regexp.MustCompile(*regexParam)
} else {
runRegex = nil
}
}
var doParseOnce sync.Once
var timeout = flag.Duration("goblin.timeout", 5*time.Second, "Sets default timeouts for all tests")
var isTty = flag.Bool("goblin.tty", true, "Sets the default output format (color / monochrome)")
var regexParam = flag.String("goblin.run", "", "Runs only tests which match the supplied regex")
var runRegex *regexp.Regexp
func Goblin(t *testing.T, arguments ...string) *G {
doParseOnce.Do(func() {
parseFlags()
})
g := &G{t: t, timeout: *timeout}
var fancy TextFancier
if *isTty {
fancy = &TerminalFancier{}
} else {
fancy = &Monochrome{}
}
g.reporter = Reporter(&DetailedReporter{fancy: fancy})
return g
}
func runIt(g *G, it *It) {
g.mutex.Lock()
g.timedOut = false
g.mutex.Unlock()
g.timer = time.NewTimer(g.timeout)
g.shouldContinue = make(chan bool)
if call, ok := it.h.(func()); ok {
// the test is synchronous
go func(c chan bool) {
it.parent.runBeforeEach()
it.parent.runJustBeforeEach()
timeTrack(g, func() { call() })
it.parent.runAfterEach()
c <- true
}(g.shouldContinue)
} else if call, ok := it.h.(func(Done)); ok {
doneCalled := 0
go func(c chan bool) {
it.parent.runBeforeEach()
it.parent.runJustBeforeEach()
timeTrack(g, func() {
call(func(msg ...interface{}) {
if len(msg) > 0 {
g.Fail(msg)
} else {
doneCalled++
if doneCalled > 1 {
g.Fail("Done called multiple times")
}
it.parent.runAfterEach()
c <- true
}
})
})
}(g.shouldContinue)
} else {
panic("Not implemented.")
}
select {
case <-g.shouldContinue:
case <-g.timer.C:
//Set to nil as it shouldn't continue
g.shouldContinue = nil
g.timedOut = true
g.Fail("Test exceeded " + fmt.Sprintf("%s", g.timeout))
}
// Reset timeout value
g.timeout = *timeout
}
type G struct {
t *testing.T
parent *Describe
currentIt Itable
timeout time.Duration
reporter Reporter
timedOut bool
shouldContinue chan bool
mutex sync.Mutex
timer *time.Timer
}
func (g *G) SetReporter(r Reporter) {
g.reporter = r
}
func (g *G) It(name string, h ...interface{}) {
if matchesRegex(name) {
it := &It{name: name, parent: g.parent, reporter: g.reporter}
if g.parent == nil {
panic(fmt.Sprintf("It(\"%s\") block should be written inside Describe() block.", name))
}
notifyParents(g.parent)
if len(h) > 0 {
it.h = h[0]
}
g.parent.children = append(g.parent.children, Runnable(it))
}
}
func (g *G) Xit(name string, h ...interface{}) {
if matchesRegex(name) {
xit := &Xit{name: name, parent: g.parent, reporter: g.reporter}
notifyParents(g.parent)
if len(h) > 0 {
xit.h = h[0]
}
g.parent.children = append(g.parent.children, Runnable(xit))
}
}
func matchesRegex(value string) bool {
if runRegex != nil {
return runRegex.MatchString(value)
}
return true
}
func notifyParents(d *Describe) {
d.hasTests = true
if d.parent != nil {
notifyParents(d.parent)
}
}
func (g *G) Before(h func()) {
g.parent.befores = append(g.parent.befores, h)
}
func (g *G) BeforeEach(h func()) {
g.parent.beforeEach = append(g.parent.beforeEach, h)
}
func (g *G) JustBeforeEach(h func()) {
g.parent.justBeforeEach = append(g.parent.justBeforeEach, h)
}
func (g *G) After(h func()) {
g.parent.afters = append(g.parent.afters, h)
}
func (g *G) AfterEach(h func()) {
g.parent.afterEach = append(g.parent.afterEach, h)
}
func (g *G) Assert(src interface{}) *Assertion {
return &Assertion{src: src, fail: g.Fail}
}
func timeTrack(g *G, call func()) {
t := time.Now()
defer func() {
g.reporter.ItTook(time.Since(t))
}()
call()
}
func (g *G) errorCommon(msg string, fatal bool) {
if g.currentIt == nil {
panic("Asserts should be written inside an It() block.")
}
g.currentIt.failed(msg, ResolveStack(9))
if g.shouldContinue != nil {
g.shouldContinue <- true
}
if fatal {
g.mutex.Lock()
defer g.mutex.Unlock()
if !g.timedOut {
//Stop test function execution
runtime.Goexit()
}
}
}
func (g *G) Fail(error interface{}) {
message := fmt.Sprintf("%v", error)
g.errorCommon(message, true)
}
func (g *G) FailNow() {
g.t.FailNow()
}
func (g *G) Failf(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...)
g.errorCommon(message, true)
}
func (g *G) Fatalf(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...)
g.errorCommon(message, true)
}
func (g *G) Errorf(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...)
g.errorCommon(message, false)
}
func (g *G) Helper() {
g.t.Helper()
}
|