| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- package convey
- import (
- "fmt"
- "net/http"
- "net/http/httptest"
- "path"
- "runtime"
- "strconv"
- "strings"
- "testing"
- "github.com/smartystreets/goconvey/convey/reporting"
- )
- func TestSingleScopeReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- So(1, ShouldEqual, 1)
- })
- expectEqual(t, "Begin|A|Success|Exit|End", myReporter.wholeStory())
- }
- func TestNestedScopeReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- Convey("B", func() {
- So(1, ShouldEqual, 1)
- })
- })
- expectEqual(t, "Begin|A|B|Success|Exit|Exit|End", myReporter.wholeStory())
- }
- func TestFailureReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- So(1, ShouldBeNil)
- })
- expectEqual(t, "Begin|A|Failure|Exit|End", myReporter.wholeStory())
- }
- func TestFirstFailureEndsScopeExecution(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- So(1, ShouldBeNil)
- So(nil, ShouldBeNil)
- })
- expectEqual(t, "Begin|A|Failure|Exit|End", myReporter.wholeStory())
- }
- func TestComparisonFailureDeserializedAndReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- So("hi", ShouldEqual, "bye")
- })
- expectEqual(t, "Begin|A|Failure(bye/hi)|Exit|End", myReporter.wholeStory())
- }
- func TestNestedFailureReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- Convey("B", func() {
- So(2, ShouldBeNil)
- })
- })
- expectEqual(t, "Begin|A|B|Failure|Exit|Exit|End", myReporter.wholeStory())
- }
- func TestSuccessAndFailureReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- So(nil, ShouldBeNil)
- So(1, ShouldBeNil)
- })
- expectEqual(t, "Begin|A|Success|Failure|Exit|End", myReporter.wholeStory())
- }
- func TestIncompleteActionReportedAsSkipped(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- Convey("B", nil)
- })
- expectEqual(t, "Begin|A|B|Skipped|Exit|Exit|End", myReporter.wholeStory())
- }
- func TestSkippedConveyReportedAsSkipped(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- SkipConvey("B", func() {
- So(1, ShouldEqual, 1)
- })
- })
- expectEqual(t, "Begin|A|B|Skipped|Exit|Exit|End", myReporter.wholeStory())
- }
- func TestMultipleSkipsAreReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- Convey("0", func() {
- So(nil, ShouldBeNil)
- })
- SkipConvey("1", func() {})
- SkipConvey("2", func() {})
- Convey("3", nil)
- Convey("4", nil)
- Convey("5", func() {
- So(nil, ShouldBeNil)
- })
- })
- expected := "Begin" +
- "|A|0|Success|Exit|Exit" +
- "|A|1|Skipped|Exit|Exit" +
- "|A|2|Skipped|Exit|Exit" +
- "|A|3|Skipped|Exit|Exit" +
- "|A|4|Skipped|Exit|Exit" +
- "|A|5|Success|Exit|Exit" +
- "|End"
- expectEqual(t, expected, myReporter.wholeStory())
- }
- func TestSkippedAssertionIsNotReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- SkipSo(1, ShouldEqual, 1)
- })
- expectEqual(t, "Begin|A|Skipped|Exit|End", myReporter.wholeStory())
- }
- func TestMultipleSkippedAssertionsAreNotReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- SkipSo(1, ShouldEqual, 1)
- So(1, ShouldEqual, 1)
- SkipSo(1, ShouldEqual, 1)
- })
- expectEqual(t, "Begin|A|Skipped|Success|Skipped|Exit|End", myReporter.wholeStory())
- }
- func TestErrorByManualPanicReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- panic("Gopher alert!")
- })
- expectEqual(t, "Begin|A|Error|Exit|End", myReporter.wholeStory())
- }
- func TestIterativeConveysReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- for x := 0; x < 3; x++ {
- Convey(strconv.Itoa(x), func() {
- So(x, ShouldEqual, x)
- })
- }
- })
- expectEqual(t, "Begin|A|0|Success|Exit|Exit|A|1|Success|Exit|Exit|A|2|Success|Exit|Exit|End", myReporter.wholeStory())
- }
- func TestNestedIterativeConveysReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func() {
- for x := 0; x < 3; x++ {
- Convey(strconv.Itoa(x), func() {
- for y := 0; y < 3; y++ {
- Convey("< "+strconv.Itoa(y), func() {
- So(x, ShouldBeLessThan, y)
- })
- }
- })
- }
- })
- expectEqual(t, ("Begin|" +
- "A|0|< 0|Failure|Exit|Exit|Exit|" +
- "A|0|< 1|Success|Exit|Exit|Exit|" +
- "A|0|< 2|Success|Exit|Exit|Exit|" +
- "A|1|< 0|Failure|Exit|Exit|Exit|" +
- "A|1|< 1|Failure|Exit|Exit|Exit|" +
- "A|1|< 2|Success|Exit|Exit|Exit|" +
- "A|2|< 0|Failure|Exit|Exit|Exit|" +
- "A|2|< 1|Failure|Exit|Exit|Exit|" +
- "A|2|< 2|Failure|Exit|Exit|Exit|" +
- "End"), myReporter.wholeStory())
- }
- func TestEmbeddedAssertionReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- Convey("A", test, func(c C) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- c.So(r.FormValue("msg"), ShouldEqual, "ping")
- }))
- http.DefaultClient.Get(ts.URL + "?msg=ping")
- })
- expectEqual(t, "Begin|A|Success|Exit|End", myReporter.wholeStory())
- }
- func TestEmbeddedContextHelperReported(t *testing.T) {
- myReporter, test := setupFakeReporter()
- helper := func(c C) http.HandlerFunc {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- c.Convey("Embedded", func() {
- So(r.FormValue("msg"), ShouldEqual, "ping")
- })
- })
- }
- Convey("A", test, func(c C) {
- ts := httptest.NewServer(helper(c))
- http.DefaultClient.Get(ts.URL + "?msg=ping")
- })
- expectEqual(t, "Begin|A|Embedded|Success|Exit|Exit|End", myReporter.wholeStory())
- }
- func expectEqual(t *testing.T, expected interface{}, actual interface{}) {
- if expected != actual {
- _, file, line, _ := runtime.Caller(1)
- t.Errorf("Expected '%v' to be '%v' but it wasn't. See '%s' at line %d.",
- actual, expected, path.Base(file), line)
- }
- }
- func setupFakeReporter() (*fakeReporter, *fakeGoTest) {
- myReporter := new(fakeReporter)
- myReporter.calls = []string{}
- testReporter = myReporter
- return myReporter, new(fakeGoTest)
- }
- type fakeReporter struct {
- calls []string
- }
- func (self *fakeReporter) BeginStory(story *reporting.StoryReport) {
- self.calls = append(self.calls, "Begin")
- }
- func (self *fakeReporter) Enter(scope *reporting.ScopeReport) {
- self.calls = append(self.calls, scope.Title)
- }
- func (self *fakeReporter) Report(report *reporting.AssertionResult) {
- if report.Error != nil {
- self.calls = append(self.calls, "Error")
- } else if report.Failure != "" {
- message := "Failure"
- if report.Expected != "" || report.Actual != "" {
- message += fmt.Sprintf("(%s/%s)", report.Expected, report.Actual)
- }
- self.calls = append(self.calls, message)
- } else if report.Skipped {
- self.calls = append(self.calls, "Skipped")
- } else {
- self.calls = append(self.calls, "Success")
- }
- }
- func (self *fakeReporter) Exit() {
- self.calls = append(self.calls, "Exit")
- }
- func (self *fakeReporter) EndStory() {
- self.calls = append(self.calls, "End")
- }
- func (self *fakeReporter) Write(content []byte) (int, error) {
- return len(content), nil // no-op
- }
- func (self *fakeReporter) wholeStory() string {
- return strings.Join(self.calls, "|")
- }
- ////////////////////////////////
- type fakeGoTest struct{}
- func (self *fakeGoTest) Fail() {}
- func (self *fakeGoTest) Fatalf(format string, args ...interface{}) {}
- var test t = new(fakeGoTest)
|