problems.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package reporting
  2. import "fmt"
  3. type problem struct {
  4. silent bool
  5. out *Printer
  6. errors []*AssertionResult
  7. failures []*AssertionResult
  8. }
  9. func (self *problem) BeginStory(story *StoryReport) {}
  10. func (self *problem) Enter(scope *ScopeReport) {}
  11. func (self *problem) Report(report *AssertionResult) {
  12. if report.Error != nil {
  13. self.errors = append(self.errors, report)
  14. } else if report.Failure != "" {
  15. self.failures = append(self.failures, report)
  16. }
  17. }
  18. func (self *problem) Exit() {}
  19. func (self *problem) EndStory() {
  20. self.show(self.showErrors, redColor)
  21. self.show(self.showFailures, yellowColor)
  22. self.prepareForNextStory()
  23. }
  24. func (self *problem) show(display func(), color string) {
  25. if !self.silent {
  26. fmt.Print(color)
  27. }
  28. display()
  29. if !self.silent {
  30. fmt.Print(resetColor)
  31. }
  32. self.out.Dedent()
  33. }
  34. func (self *problem) showErrors() {
  35. for i, e := range self.errors {
  36. if i == 0 {
  37. self.out.Println("\nErrors:\n")
  38. self.out.Indent()
  39. }
  40. self.out.Println(errorTemplate, e.File, e.Line, e.Error, e.StackTrace)
  41. }
  42. }
  43. func (self *problem) showFailures() {
  44. for i, f := range self.failures {
  45. if i == 0 {
  46. self.out.Println("\nFailures:\n")
  47. self.out.Indent()
  48. }
  49. self.out.Println(failureTemplate, f.File, f.Line, f.Failure)
  50. }
  51. }
  52. func (self *problem) Write(content []byte) (written int, err error) {
  53. return len(content), nil // no-op
  54. }
  55. func NewProblemReporter(out *Printer) *problem {
  56. self := new(problem)
  57. self.out = out
  58. self.prepareForNextStory()
  59. return self
  60. }
  61. func NewSilentProblemReporter(out *Printer) *problem {
  62. self := NewProblemReporter(out)
  63. self.silent = true
  64. return self
  65. }
  66. func (self *problem) prepareForNextStory() {
  67. self.errors = []*AssertionResult{}
  68. self.failures = []*AssertionResult{}
  69. }