github.com/Laisky/zap@v1.27.0/zaptest/writer_test.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package zaptest 22 23 import ( 24 "errors" 25 "testing" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestSyncer(t *testing.T) { 31 err := errors.New("sentinel") 32 s := &Syncer{} 33 s.SetError(err) 34 assert.Equal(t, err, s.Sync(), "Expected Sync to fail with provided error.") 35 assert.True(t, s.Called(), "Expected to record that Sync was called.") 36 } 37 38 func TestDiscarder(t *testing.T) { 39 d := &Discarder{} 40 payload := []byte("foo") 41 n, err := d.Write(payload) 42 assert.NoError(t, err, "Unexpected error writing to Discarder.") 43 assert.Equal(t, len(payload), n, "Wrong number of bytes written.") 44 } 45 46 func TestFailWriter(t *testing.T) { 47 w := &FailWriter{} 48 payload := []byte("foo") 49 n, err := w.Write(payload) 50 assert.Error(t, err, "Expected an error writing to FailWriter.") 51 assert.Equal(t, len(payload), n, "Wrong number of bytes written.") 52 } 53 54 func TestShortWriter(t *testing.T) { 55 w := &ShortWriter{} 56 payload := []byte("foo") 57 n, err := w.Write(payload) 58 assert.NoError(t, err, "Unexpected error writing to ShortWriter.") 59 assert.Equal(t, len(payload)-1, n, "Wrong number of bytes written.") 60 } 61 62 func TestBuffer(t *testing.T) { 63 buf := &Buffer{} 64 buf.WriteString("foo\n") 65 buf.WriteString("bar\n") 66 assert.Equal(t, []string{"foo", "bar"}, buf.Lines(), "Unexpected output from Lines.") 67 assert.Equal(t, "foo\nbar", buf.Stripped(), "Unexpected output from Stripped.") 68 }