Skip to content

Commit

Permalink
Rework inheritance test
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Oct 3, 2023
1 parent f99fd63 commit 50355b8
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 65 deletions.
142 changes: 80 additions & 62 deletions expect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,86 @@ func TestExpect_Propagation(t *testing.T) {
})
}

func TestExpect_Inheritance(t *testing.T) {
t.Run("reporter", func(t *testing.T) {
rootReporter := newMockReporter(t)
req2Reporter := newMockReporter(t)

e := WithConfig(Config{
BaseURL: "http://example.com",
Client: &mockClient{},
Reporter: rootReporter,
})

req1 := e.GET("/")
req2 := e.GET("/")

req2.WithReporter(req2Reporter)

// So far OK
req1.chain.assert(t, success)
req2.chain.assert(t, success)

resp1 := req1.Expect()
resp2 := req2.Expect()

// So far OK
resp1.chain.assert(t, success)
resp2.chain.assert(t, success)

// Failure on resp1 should be reported to rootReporter,
// which was inherited from config
resp1.JSON().Object().Value("foo").chain.assert(t, failure)
assert.Equal(t, 1, rootReporter.reportCalled)
assert.Equal(t, 0, req2Reporter.reportCalled)

// Failure on resp2 should be reported to req2Reporter,
// which was inherited from req2
resp2.JSON().Object().Value("foo").chain.assert(t, failure)
assert.Equal(t, 1, rootReporter.reportCalled)
assert.Equal(t, 1, req2Reporter.reportCalled)
})

t.Run("assertion handler", func(t *testing.T) {
rootHandler := &mockAssertionHandler{}
req2Handler := &mockAssertionHandler{}

e := WithConfig(Config{
BaseURL: "http://example.com",
Client: &mockClient{},
AssertionHandler: rootHandler,
})

req1 := e.GET("/")
req2 := e.GET("/")

req2.WithAssertionHandler(req2Handler)

// So far OK
req1.chain.assert(t, success)
req2.chain.assert(t, success)

resp1 := req1.Expect()
resp2 := req2.Expect()

// So far OK
resp1.chain.assert(t, success)
resp2.chain.assert(t, success)

// Failure on resp1 should be reported to rootReporter,
// which was inherited from config
resp1.JSON().Object().Value("foo").chain.assert(t, failure)
assert.Equal(t, 1, rootHandler.failureCalled)
assert.Equal(t, 0, req2Handler.failureCalled)

// Failure on resp2 should be reported to req2Reporter,
// which was inherited from req2
resp2.JSON().Object().Value("foo").chain.assert(t, failure)
assert.Equal(t, 1, rootHandler.failureCalled)
assert.Equal(t, 1, req2Handler.failureCalled)
})
}

func TestExpect_RequestFactory(t *testing.T) {
t.Run("default factory", func(t *testing.T) {
e := WithConfig(Config{
Expand Down Expand Up @@ -763,65 +843,3 @@ func TestExpect_Adapters(t *testing.T) {
assert.Contains(t, message, "test logger called")
})
}

func TestReporterAndAssertionHandler(t *testing.T) {
mainRep := newMockReporter(t)
mainAssertionHandler := &mockAssertionHandler{}

config := Config{
BaseURL: "http://example.com",
Reporter: mainRep,
}

assertionConfig := Config{
BaseURL: "http://example.com",
AssertionHandler: mainAssertionHandler,
}

cases := []struct {
name string
config Config
hasReporter bool
wantEqual chainResult
}{
{
name: "Response reported failure with reporter",
config: config,
hasReporter: true,
wantEqual: failure,
},
{
name: "Response reported failure with assertion handler",
config: assertionConfig,
hasReporter: true,
wantEqual: failure,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
subRep := newMockReporter(t)
subAssertionHandler := &mockAssertionHandler{}
var req2 *Request

req1 := NewRequest(config, "GET", "/")

if tc.hasReporter {
req2 = NewRequest(config, "GET", "/2").WithReporter(subRep)
} else {
req2 = NewRequest(config, "GET", "/2").WithAssertionHandler(subAssertionHandler)
}

resp1 := req1.Expect().Status(200)
resp2 := req2.Expect().Status(200)

// Make some assertion that fails on both responses
resp1.JSON().Object().Value("foo").chain.assert(t, failure)
resp2.JSON().Object().Value("foo").chain.assert(t, failure)

resp1.chain.assert(t, tc.wantEqual)
resp2.chain.assert(t, tc.wantEqual)
})
}

}
8 changes: 5 additions & 3 deletions mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ func (ml *mockLogger) Logf(message string, args ...interface{}) {

// mock reporter
type mockReporter struct {
testing *testing.T
reported bool
reportCb func()
testing *testing.T
reported bool
reportCalled int
reportCb func()
}

func newMockReporter(t *testing.T) *mockReporter {
Expand All @@ -53,6 +54,7 @@ func newMockReporter(t *testing.T) *mockReporter {
func (mr *mockReporter) Errorf(message string, args ...interface{}) {
mr.testing.Logf("Fail: "+message, args...)
mr.reported = true
mr.reportCalled++

if mr.reportCb != nil {
mr.reportCb()
Expand Down

0 comments on commit 50355b8

Please sign in to comment.