statistics.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package reporting
  2. import "fmt"
  3. func (self *statistics) BeginStory(story *StoryReport) {}
  4. func (self *statistics) Enter(scope *ScopeReport) {}
  5. func (self *statistics) Report(report *AssertionResult) {
  6. if !self.failing && report.Failure != "" {
  7. self.failing = true
  8. }
  9. if !self.erroring && report.Error != nil {
  10. self.erroring = true
  11. }
  12. if report.Skipped {
  13. self.skipped += 1
  14. } else {
  15. self.total++
  16. }
  17. }
  18. func (self *statistics) Exit() {}
  19. func (self *statistics) EndStory() {
  20. self.reportAssertions()
  21. self.reportSkippedSections()
  22. self.completeReport()
  23. }
  24. func (self *statistics) reportAssertions() {
  25. self.decideColor()
  26. self.out.Print("\n%d %s thus far", self.total, plural("assertion", self.total))
  27. }
  28. func (self *statistics) decideColor() {
  29. if self.failing && !self.erroring {
  30. fmt.Print(yellowColor)
  31. } else if self.erroring {
  32. fmt.Print(redColor)
  33. } else {
  34. fmt.Print(greenColor)
  35. }
  36. }
  37. func (self *statistics) reportSkippedSections() {
  38. if self.skipped > 0 {
  39. fmt.Print(yellowColor)
  40. self.out.Print(" (one or more sections skipped)")
  41. self.skipped = 0
  42. }
  43. }
  44. func (self *statistics) completeReport() {
  45. fmt.Print(resetColor)
  46. self.out.Print("\n")
  47. self.out.Print("\n")
  48. }
  49. func (self *statistics) Write(content []byte) (written int, err error) {
  50. return len(content), nil // no-op
  51. }
  52. func NewStatisticsReporter(out *Printer) *statistics {
  53. self := statistics{}
  54. self.out = out
  55. return &self
  56. }
  57. type statistics struct {
  58. out *Printer
  59. total int
  60. failing bool
  61. erroring bool
  62. skipped int
  63. }
  64. func plural(word string, count int) string {
  65. if count == 1 {
  66. return word
  67. }
  68. return word + "s"
  69. }