Repository: uber-go/zap
File: writer_test.go (Go)

Code before:
func TestAddSyncWriteSyncer(t *testing.T) {
	buf := &bytes.Buffer{}
	concrete := &spy.WriteSyncer{Writer: buf}
	ws := AddSync(concrete)
	requireWriteWorks(t, ws)

	require.NoError(t, ws.Sync(), "Unexpected error syncing a WriteSyncer.")
	require.True(t, concrete.SyncCalled, "Expected to dispatch to concrete type's Sync method.")

	concrete.Err = errors.New("fail")
	assert.Error(t, ws.Sync(), "Expected to propagate errors from concrete type's Sync method.")
}

func TestAddSyncWriteFlusher(t *testing.T) {
	buf := &bytes.Buffer{}
	concrete := &spy.WriteFlusher{Writer: buf}
	ws := AddSync(concrete)
	requireWriteWorks(t, ws)

	require.NoError(t, ws.Sync(), "Unexpected error syncing a WriteSyncer.")
	require.True(t, concrete.FlushCalled, "Expected to dispatch to concrete type's Flush method.")

	concrete.Err = errors.New("fail")
	assert.Error(t, ws.Sync(), "Expected to propagate errors from concrete type's Flush method.")
}

func TestAddSyncWriter(t *testing.T) {
	// If we pass a plain io.Writer, make sure that we still get a WriteSyncer
	// with a no-op Sync.
	buf := &bytes.Buffer{}
	ws := AddSync(buf)
	requireWriteWorks(t, ws)
	assert.NoError(t, ws.Sync(), "Unexpected error calling a no-op Sync method.")
}


Review comment (suggestion):
let's put a test for `AddSync` where there's a `Sync` and a `Flush` method -- only the `Sync` should be called, but just want to have it tested.


Code after:
func TestAddSyncWriteSyncer(t *testing.T) {
	buf := &bytes.Buffer{}
	concrete := &spy.WriteSyncer{Writer: buf}
	ws := AddSync(concrete)
	requireWriteWorks(t, ws)

	require.NoError(t, ws.Sync(), "Unexpected error syncing a WriteSyncer.")
	require.True(t, concrete.Called(), "Expected to dispatch to concrete type's Sync method.")

	concrete.SetError(errors.New("fail"))
	assert.Error(t, ws.Sync(), "Expected to propagate errors from concrete type's Sync method.")
}

func TestAddSyncWriteFlusher(t *testing.T) {
	buf := &bytes.Buffer{}
	concrete := &spy.WriteFlusher{Writer: buf}
	ws := AddSync(concrete)
	requireWriteWorks(t, ws)

	require.NoError(t, ws.Sync(), "Unexpected error syncing a WriteSyncer.")
	require.True(t, concrete.Called(), "Expected to dispatch to concrete type's Flush method.")

	concrete.SetError(errors.New("fail"))
	assert.Error(t, ws.Sync(), "Expected to propagate errors from concrete type's Flush method.")
}

func TestAddSyncWriteFlushSyncer(t *testing.T) {
	buf := &bytes.Buffer{}
	concrete := &spy.WriteFlushSyncer{Writer: buf}
	ws := AddSync(concrete)
	requireWriteWorks(t, ws)

	require.NoError(t, ws.Sync(), "Unexpected error syncing a WriteSyncer.")
	require.True(t, concrete.Syncer.Called(), "Expected to delegate to concrete type's Sync method.")
	require.False(t, concrete.Flusher.Called(), "Unexpected use of concrete type's Flush method.")

	concrete.Syncer.SetError(errors.New("fail sync"))
	concrete.Flusher.SetError(nil)
	assert.Error(t, ws.Sync(), "Expected to propagate errors from concrete type's Sync method.")

	concrete.Syncer.SetError(nil)
	concrete.Flusher.SetError(errors.New("flush sync"))
	assert.NoError(t, ws.Sync(), "Errors in concrete type's Flush method shouldn't affect wrapper's Sync.")
}

func TestAddSyncWriter(t *testing.T) {
	// If we pass a plain io.Writer, make sure that we still get a WriteSyncer
	// with a no-op Sync.
	buf := &bytes.Buffer{}
	ws := AddSync(buf)
	requireWriteWorks(t, ws)