utesting_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package utesting
  17. import (
  18. "bytes"
  19. "regexp"
  20. "strings"
  21. "testing"
  22. )
  23. func TestTest(t *testing.T) {
  24. tests := []Test{
  25. {
  26. Name: "successful test",
  27. Fn: func(t *T) {},
  28. },
  29. {
  30. Name: "failing test",
  31. Fn: func(t *T) {
  32. t.Log("output")
  33. t.Error("failed")
  34. },
  35. },
  36. {
  37. Name: "panicking test",
  38. Fn: func(t *T) {
  39. panic("oh no")
  40. },
  41. },
  42. }
  43. results := RunTests(tests, nil)
  44. if results[0].Failed || results[0].Output != "" {
  45. t.Fatalf("wrong result for successful test: %#v", results[0])
  46. }
  47. if !results[1].Failed || results[1].Output != "output\nfailed\n" {
  48. t.Fatalf("wrong result for failing test: %#v", results[1])
  49. }
  50. if !results[2].Failed || !strings.HasPrefix(results[2].Output, "panic: oh no\n") {
  51. t.Fatalf("wrong result for panicking test: %#v", results[2])
  52. }
  53. }
  54. var outputTests = []Test{
  55. {
  56. Name: "TestWithLogs",
  57. Fn: func(t *T) {
  58. t.Log("output line 1")
  59. t.Log("output line 2\noutput line 3")
  60. },
  61. },
  62. {
  63. Name: "TestNoLogs",
  64. Fn: func(t *T) {},
  65. },
  66. {
  67. Name: "FailWithLogs",
  68. Fn: func(t *T) {
  69. t.Log("output line 1")
  70. t.Error("failed 1")
  71. },
  72. },
  73. {
  74. Name: "FailMessage",
  75. Fn: func(t *T) {
  76. t.Error("failed 2")
  77. },
  78. },
  79. {
  80. Name: "FailNoOutput",
  81. Fn: func(t *T) {
  82. t.Fail()
  83. },
  84. },
  85. }
  86. func TestOutput(t *testing.T) {
  87. var buf bytes.Buffer
  88. RunTests(outputTests, &buf)
  89. want := regexp.MustCompile(`
  90. ^-- RUN TestWithLogs
  91. output line 1
  92. output line 2
  93. output line 3
  94. -- OK TestWithLogs \([^)]+\)
  95. -- OK TestNoLogs \([^)]+\)
  96. -- RUN FailWithLogs
  97. output line 1
  98. failed 1
  99. -- FAIL FailWithLogs \([^)]+\)
  100. -- RUN FailMessage
  101. failed 2
  102. -- FAIL FailMessage \([^)]+\)
  103. -- FAIL FailNoOutput \([^)]+\)
  104. 2/5 tests passed.
  105. $`[1:])
  106. if !want.MatchString(buf.String()) {
  107. t.Fatalf("output does not match: %q", buf.String())
  108. }
  109. }
  110. func TestOutputTAP(t *testing.T) {
  111. var buf bytes.Buffer
  112. RunTAP(outputTests, &buf)
  113. want := `
  114. 1..5
  115. ok 1 TestWithLogs
  116. # output line 1
  117. # output line 2
  118. # output line 3
  119. ok 2 TestNoLogs
  120. not ok 3 FailWithLogs
  121. # output line 1
  122. # failed 1
  123. not ok 4 FailMessage
  124. # failed 2
  125. not ok 5 FailNoOutput
  126. `
  127. if buf.String() != want[1:] {
  128. t.Fatalf("output does not match: %q", buf.String())
  129. }
  130. }