statistics.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. if !self.suppressed {
  21. self.PrintSummary()
  22. }
  23. }
  24. func (self *statistics) Suppress() {
  25. self.suppressed = true
  26. }
  27. func (self *statistics) PrintSummary() {
  28. self.reportAssertions()
  29. self.reportSkippedSections()
  30. self.completeReport()
  31. }
  32. func (self *statistics) reportAssertions() {
  33. self.decideColor()
  34. self.out.Print("\n%d total %s", self.total, plural("assertion", self.total))
  35. }
  36. func (self *statistics) decideColor() {
  37. if self.failing && !self.erroring {
  38. fmt.Print(yellowColor)
  39. } else if self.erroring {
  40. fmt.Print(redColor)
  41. } else {
  42. fmt.Print(greenColor)
  43. }
  44. }
  45. func (self *statistics) reportSkippedSections() {
  46. if self.skipped > 0 {
  47. fmt.Print(yellowColor)
  48. self.out.Print(" (one or more sections skipped)")
  49. }
  50. }
  51. func (self *statistics) completeReport() {
  52. fmt.Print(resetColor)
  53. self.out.Print("\n")
  54. self.out.Print("\n")
  55. }
  56. func (self *statistics) Write(content []byte) (written int, err error) {
  57. return len(content), nil // no-op
  58. }
  59. func NewStatisticsReporter(out *Printer) *statistics {
  60. self := statistics{}
  61. self.out = out
  62. return &self
  63. }
  64. type statistics struct {
  65. out *Printer
  66. total int
  67. failing bool
  68. erroring bool
  69. skipped int
  70. suppressed bool
  71. }
  72. func plural(word string, count int) string {
  73. if count == 1 {
  74. return word
  75. }
  76. return word + "s"
  77. }