reporter.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package reporting
  2. import "io"
  3. type Reporter interface {
  4. BeginStory(story *StoryReport)
  5. Enter(scope *ScopeReport)
  6. Report(r *AssertionResult)
  7. Exit()
  8. EndStory()
  9. io.Writer
  10. }
  11. type reporters struct{ collection []Reporter }
  12. func (self *reporters) BeginStory(s *StoryReport) { self.foreach(func(r Reporter) { r.BeginStory(s) }) }
  13. func (self *reporters) Enter(s *ScopeReport) { self.foreach(func(r Reporter) { r.Enter(s) }) }
  14. func (self *reporters) Report(a *AssertionResult) { self.foreach(func(r Reporter) { r.Report(a) }) }
  15. func (self *reporters) Exit() { self.foreach(func(r Reporter) { r.Exit() }) }
  16. func (self *reporters) EndStory() { self.foreach(func(r Reporter) { r.EndStory() }) }
  17. func (self *reporters) Write(contents []byte) (written int, err error) {
  18. self.foreach(func(r Reporter) {
  19. written, err = r.Write(contents)
  20. })
  21. return written, err
  22. }
  23. func (self *reporters) foreach(action func(Reporter)) {
  24. for _, r := range self.collection {
  25. action(r)
  26. }
  27. }
  28. func NewReporters(collection ...Reporter) *reporters {
  29. self := new(reporters)
  30. self.collection = collection
  31. return self
  32. }