github.com/agilebits/godog@v0.7.9/examples/godogs/godogs_test.go (about)

     1  /* file: $GOPATH/src/godogs/godogs_test.go */
     2  package main
     3  
     4  import (
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/DATA-DOG/godog"
    11  	"github.com/DATA-DOG/godog/colors"
    12  )
    13  
    14  var opt = godog.Options{Output: colors.Colored(os.Stdout)}
    15  
    16  func init() {
    17  	godog.BindFlags("godog.", flag.CommandLine, &opt)
    18  }
    19  
    20  func TestMain(m *testing.M) {
    21  	flag.Parse()
    22  	opt.Paths = flag.Args()
    23  
    24  	status := godog.RunWithOptions("godogs", func(s *godog.Suite) {
    25  		FeatureContext(s)
    26  	}, opt)
    27  
    28  	if st := m.Run(); st > status {
    29  		status = st
    30  	}
    31  	os.Exit(status)
    32  }
    33  
    34  func thereAreGodogs(available int) error {
    35  	Godogs = available
    36  	return nil
    37  }
    38  
    39  func iEat(num int) error {
    40  	if Godogs < num {
    41  		return fmt.Errorf("you cannot eat %d godogs, there are %d available", num, Godogs)
    42  	}
    43  	Godogs -= num
    44  	return nil
    45  }
    46  
    47  func thereShouldBeRemaining(remaining int) error {
    48  	if Godogs != remaining {
    49  		return fmt.Errorf("expected %d godogs to be remaining, but there is %d", remaining, Godogs)
    50  	}
    51  	return nil
    52  }
    53  
    54  func FeatureContext(s *godog.Suite) {
    55  	s.Step(`^there are (\d+) godogs$`, thereAreGodogs)
    56  	s.Step(`^I eat (\d+)$`, iEat)
    57  	s.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
    58  
    59  	s.BeforeScenario(func(interface{}) {
    60  		Godogs = 0 // clean the state before every scenario
    61  	})
    62  }