story.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // TODO: in order for this reporter to be completely honest
  2. // we need to retrofit to be more like the json reporter such that:
  3. // 1. it maintains ScopeResult collections, which count assertions
  4. // 2. it reports only after EndStory(), so that all tick marks
  5. // are placed near the appropriate title.
  6. // 3. Under unit test
  7. package reporting
  8. import (
  9. "fmt"
  10. "strings"
  11. )
  12. type story struct {
  13. out *Printer
  14. titlesById map[string]string
  15. currentKey []string
  16. }
  17. func (self *story) BeginStory(story *StoryReport) {}
  18. func (self *story) Enter(scope *ScopeReport) {
  19. self.out.Indent()
  20. self.currentKey = append(self.currentKey, scope.Title)
  21. ID := strings.Join(self.currentKey, "|")
  22. if _, found := self.titlesById[ID]; !found {
  23. self.out.Println("")
  24. self.out.Print(scope.Title)
  25. self.out.Insert(" ")
  26. self.titlesById[ID] = scope.Title
  27. }
  28. }
  29. func (self *story) Report(report *AssertionResult) {
  30. if report.Error != nil {
  31. fmt.Print(redColor)
  32. self.out.Insert(error_)
  33. } else if report.Failure != "" {
  34. fmt.Print(yellowColor)
  35. self.out.Insert(failure)
  36. } else if report.Skipped {
  37. fmt.Print(yellowColor)
  38. self.out.Insert(skip)
  39. } else {
  40. fmt.Print(greenColor)
  41. self.out.Insert(success)
  42. }
  43. fmt.Print(resetColor)
  44. }
  45. func (self *story) Exit() {
  46. self.out.Dedent()
  47. self.currentKey = self.currentKey[:len(self.currentKey)-1]
  48. }
  49. func (self *story) EndStory() {
  50. self.titlesById = make(map[string]string)
  51. self.out.Println("\n")
  52. }
  53. func (self *story) Write(content []byte) (written int, err error) {
  54. return len(content), nil // no-op
  55. }
  56. func NewStoryReporter(out *Printer) *story {
  57. self := new(story)
  58. self.out = out
  59. self.titlesById = make(map[string]string)
  60. return self
  61. }