reporting.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package goblin
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. type Reporter interface {
  9. beginDescribe(string)
  10. endDescribe()
  11. begin()
  12. end()
  13. failure(*Failure)
  14. itTook(time.Duration)
  15. itFailed(string)
  16. itPassed(string)
  17. itIsPending(string)
  18. itIsExcluded(string)
  19. }
  20. type TextFancier interface {
  21. Red(text string) string
  22. Gray(text string) string
  23. Cyan(text string) string
  24. Green(text string) string
  25. Yellow(text string) string
  26. WithCheck(text string) string
  27. }
  28. type DetailedReporter struct {
  29. level, failed, passed, pending, excluded int
  30. failures []*Failure
  31. executionTime, totalExecutionTime time.Duration
  32. fancy TextFancier
  33. }
  34. func (r *DetailedReporter) SetTextFancier(f TextFancier) {
  35. r.fancy = f
  36. }
  37. type TerminalFancier struct {
  38. }
  39. func (self *TerminalFancier) Red(text string) string {
  40. return "\033[31m" + text + "\033[0m"
  41. }
  42. func (self *TerminalFancier) Gray(text string) string {
  43. return "\033[90m" + text + "\033[0m"
  44. }
  45. func (self *TerminalFancier) Cyan(text string) string {
  46. return "\033[36m" + text + "\033[0m"
  47. }
  48. func (self *TerminalFancier) Green(text string) string {
  49. return "\033[32m" + text + "\033[0m"
  50. }
  51. func (self *TerminalFancier) Yellow(text string) string {
  52. return "\033[33m" + text + "\033[0m"
  53. }
  54. func (self *TerminalFancier) WithCheck(text string) string {
  55. return "\033[32m\u2713\033[0m " + text
  56. }
  57. func (r *DetailedReporter) getSpace() string {
  58. return strings.Repeat(" ", (r.level+1)*2)
  59. }
  60. func (r *DetailedReporter) failure(failure *Failure) {
  61. r.failures = append(r.failures, failure)
  62. }
  63. func (r *DetailedReporter) print(text string) {
  64. fmt.Printf("%v%v\n", r.getSpace(), text)
  65. }
  66. func (r *DetailedReporter) printWithCheck(text string) {
  67. fmt.Printf("%v%v\n", r.getSpace(), r.fancy.WithCheck(text))
  68. }
  69. func (r *DetailedReporter) beginDescribe(name string) {
  70. fmt.Println("")
  71. r.print(name)
  72. r.level++
  73. }
  74. func (r *DetailedReporter) endDescribe() {
  75. r.level--
  76. }
  77. func (r *DetailedReporter) itTook(duration time.Duration) {
  78. r.executionTime = duration
  79. r.totalExecutionTime += duration
  80. }
  81. func (r *DetailedReporter) itFailed(name string) {
  82. r.failed++
  83. r.print(r.fancy.Red(strconv.Itoa(r.failed) + ") " + name))
  84. }
  85. func (r *DetailedReporter) itPassed(name string) {
  86. r.passed++
  87. r.printWithCheck(r.fancy.Gray(name))
  88. }
  89. func (r *DetailedReporter) itIsPending(name string) {
  90. r.pending++
  91. r.print(r.fancy.Cyan("- " + name))
  92. }
  93. func (r *DetailedReporter) itIsExcluded(name string) {
  94. r.excluded++
  95. r.print(r.fancy.Yellow("- " + name))
  96. }
  97. func (r *DetailedReporter) begin() {
  98. }
  99. func (r *DetailedReporter) end() {
  100. comp := fmt.Sprintf("%d tests complete", r.passed)
  101. t := fmt.Sprintf("(%d ms)", r.totalExecutionTime/time.Millisecond)
  102. //fmt.Printf("\n\n \033[32m%d tests complete\033[0m \033[90m(%d ms)\033[0m\n", r.passed, r.totalExecutionTime/time.Millisecond)
  103. fmt.Printf("\n\n %v %v\n", r.fancy.Green(comp), r.fancy.Gray(t))
  104. if r.pending > 0 {
  105. pend := fmt.Sprintf("%d test(s) pending", r.pending)
  106. fmt.Printf(" %v\n\n", r.fancy.Cyan(pend))
  107. }
  108. if r.excluded > 0 {
  109. excl := fmt.Sprintf("%d test(s) excluded", r.excluded)
  110. fmt.Printf(" %v\n\n", r.fancy.Yellow(excl))
  111. }
  112. if len(r.failures) > 0 {
  113. fmt.Printf("%s \n\n", r.fancy.Red(fmt.Sprintf(" %d tests failed:", len(r.failures))))
  114. }
  115. for i, failure := range r.failures {
  116. fmt.Printf(" %d) %s:\n\n", i+1, failure.testName)
  117. fmt.Printf(" %s\n", r.fancy.Red(failure.message))
  118. for _, stackItem := range failure.stack {
  119. fmt.Printf(" %s\n", r.fancy.Gray(stackItem))
  120. }
  121. }
  122. }