github.com/richardwilkes/toolbox@v1.121.0/xio/linewriter_test.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package xio_test 11 12 import ( 13 "testing" 14 15 "github.com/richardwilkes/toolbox/check" 16 "github.com/richardwilkes/toolbox/xio" 17 ) 18 19 func TestLineWriter(t *testing.T) { 20 lines := make([]string, 0) 21 w := xio.NewLineWriter(func(line []byte) { 22 lines = append(lines, string(line)) 23 }) 24 n, err := w.Write([]byte{}) 25 check.Equal(t, 0, n) 26 check.NoError(t, err) 27 check.Equal(t, 0, len(lines)) 28 n, err = w.Write([]byte{'\n'}) 29 check.Equal(t, 1, n) 30 check.NoError(t, err) 31 check.NoError(t, w.Close()) 32 check.Equal(t, 1, len(lines)) 33 check.Equal(t, "", lines[0]) 34 35 lines = make([]string, 0) 36 w = xio.NewLineWriter(func(line []byte) { 37 lines = append(lines, string(line)) 38 }) 39 n, err = w.Write([]byte{'\n', '\n'}) 40 check.Equal(t, 2, n) 41 check.NoError(t, err) 42 check.NoError(t, w.Close()) 43 check.Equal(t, 2, len(lines)) 44 check.Equal(t, "", lines[0]) 45 check.Equal(t, "", lines[1]) 46 47 lines = make([]string, 0) 48 w = xio.NewLineWriter(func(line []byte) { 49 lines = append(lines, string(line)) 50 }) 51 n, err = w.Write([]byte{'\n', 'a', '\n'}) 52 check.Equal(t, 3, n) 53 check.NoError(t, err) 54 check.NoError(t, w.Close()) 55 check.Equal(t, 2, len(lines)) 56 check.Equal(t, "", lines[0]) 57 check.Equal(t, "a", lines[1]) 58 59 lines = make([]string, 0) 60 w = xio.NewLineWriter(func(line []byte) { 61 lines = append(lines, string(line)) 62 }) 63 n, err = w.Write([]byte{'\n', 'a', '\n'}) 64 check.Equal(t, 3, n) 65 check.NoError(t, err) 66 check.NoError(t, w.Close()) 67 check.Equal(t, 2, len(lines)) 68 check.Equal(t, "", lines[0]) 69 check.Equal(t, "a", lines[1]) 70 71 lines = make([]string, 0) 72 w = xio.NewLineWriter(func(line []byte) { 73 lines = append(lines, string(line)) 74 }) 75 n, err = w.Write([]byte{'a', '\n', '\n'}) 76 check.Equal(t, 3, n) 77 check.NoError(t, err) 78 check.NoError(t, w.Close()) 79 check.Equal(t, 2, len(lines)) 80 check.Equal(t, "a", lines[0]) 81 check.Equal(t, "", lines[1]) 82 83 lines = make([]string, 0) 84 w = xio.NewLineWriter(func(line []byte) { 85 lines = append(lines, string(line)) 86 }) 87 n, err = w.Write([]byte{'a', '\n', '\n', 'b'}) 88 check.Equal(t, 4, n) 89 check.NoError(t, err) 90 check.NoError(t, w.Close()) 91 check.Equal(t, 3, len(lines)) 92 check.Equal(t, "a", lines[0]) 93 check.Equal(t, "", lines[1]) 94 check.Equal(t, "b", lines[2]) 95 96 lines = make([]string, 0) 97 w = xio.NewLineWriter(func(line []byte) { 98 lines = append(lines, string(line)) 99 }) 100 n, err = w.Write([]byte{'a', '\n'}) 101 check.Equal(t, 2, n) 102 check.NoError(t, err) 103 check.Equal(t, 1, len(lines)) 104 check.Equal(t, "a", lines[0]) 105 n, err = w.Write([]byte{'\n'}) 106 check.Equal(t, 1, n) 107 check.NoError(t, err) 108 check.Equal(t, 2, len(lines)) 109 check.Equal(t, "a", lines[0]) 110 check.Equal(t, "", lines[1]) 111 n, err = w.Write([]byte{'b'}) 112 check.Equal(t, 1, n) 113 check.NoError(t, err) 114 check.Equal(t, 2, len(lines)) 115 check.Equal(t, "a", lines[0]) 116 check.Equal(t, "", lines[1]) 117 check.NoError(t, w.Close()) 118 check.Equal(t, 3, len(lines)) 119 check.Equal(t, "a", lines[0]) 120 check.Equal(t, "", lines[1]) 121 check.Equal(t, "b", lines[2]) 122 }