github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/util/writers/writers_test.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // This file incorporates work covered by the following copyright and 16 // permission notice: 17 // 18 // Copyright 2016 Attic Labs, Inc. All rights reserved. 19 // Licensed under the Apache License, version 2.0: 20 // http://www.apache.org/licenses/LICENSE-2.0 21 22 package writers 23 24 import ( 25 "bytes" 26 "io" 27 "testing" 28 29 "github.com/stretchr/testify/assert" 30 ) 31 32 type maxLineTestCase struct { 33 data string 34 maxLines uint32 35 expected string 36 errorExpected bool 37 } 38 39 func TestMaxLineWriter(t *testing.T) { 40 assert := assert.New(t) 41 42 tcs := []maxLineTestCase{ 43 {"hey there\nthis text contains\n3 lines\n", 1, "hey there\n", true}, 44 {"hey there\nthis text contains\n3 lines\n", 2, "hey there\nthis text contains\n", true}, 45 {"hey there\nthis text contains\n3 lines\n", 3, "hey there\nthis text contains\n3 lines\n", false}, 46 {"hey there\nthis text contains\n3 lines\nand more\n", 3, "hey there\nthis text contains\n3 lines\n", true}, 47 {"hey there\nthis text contains\n3 lines\n", 4, "hey there\nthis text contains\n3 lines\n", false}, 48 {"hey there\nthis text contains\n3 lines\n", 0, "hey there\nthis text contains\n3 lines\n", false}, 49 {"\n\n\n\n", 2, "\n\n", true}, 50 } 51 52 for i, tc := range tcs { 53 buf := bytes.NewBuffer(nil) 54 mlw := MaxLineWriter{Dest: buf, MaxLines: tc.maxLines} 55 l, err := mlw.Write([]byte(tc.data)) 56 assert.Equal(len(tc.expected), l, "test #%d case failed", i) 57 if tc.errorExpected { 58 assert.Error(err, "test #%d case failed", i) 59 assert.IsType(MaxLinesError{}, err, "test #%d case failed", i) 60 } else { 61 assert.NoError(err, "test #%d case failed", i) 62 } 63 assert.Equal(tc.expected, buf.String(), "test #%d case failed", i) 64 } 65 } 66 67 type prefixTestCase struct { 68 data string 69 prefix string 70 expected string 71 needsPrefix bool 72 } 73 74 func TestPrefixWriter(t *testing.T) { 75 assert := assert.New(t) 76 77 tcs := []prefixTestCase{ 78 {"\n", "yo:", "yo:\n", true}, 79 {"\n", "yo:", "\n", false}, 80 {"\n\n", "yo:", "yo:\nyo:\n", true}, 81 {"\n\n", "yo:", "\nyo:\n", false}, 82 {"hey there\nthis text contains\n3 lines\n", "yo:", "yo:hey there\nyo:this text contains\nyo:3 lines\n", true}, 83 {"hey there\nthis text contains\n3 lines\n", "yo:", "hey there\nyo:this text contains\nyo:3 lines\n", false}, 84 {"hey there\nthis text contains\n3 lines\n", "", "hey there\nthis text contains\n3 lines\n", true}, 85 {"hey there\nthis text contains\n3 lines\n", "", "hey there\nthis text contains\n3 lines\n", false}, 86 } 87 88 for _, tc := range tcs { 89 getPrefix := func(w *PrefixWriter) []byte { 90 return []byte(tc.prefix) 91 } 92 buf := bytes.NewBuffer(nil) 93 pw := PrefixWriter{Dest: buf, PrefixFunc: getPrefix, NeedsPrefix: tc.needsPrefix} 94 l, err := pw.Write([]byte(tc.data)) 95 assert.NoError(err) 96 assert.Equal(len(tc.expected), l) 97 assert.Equal(tc.expected, buf.String()) 98 } 99 } 100 101 type prefixMaxLineTestCase struct { 102 data string 103 prefix string 104 expected string 105 needsPrefix bool 106 maxLines uint32 107 errorExpected bool 108 } 109 110 func TestPrefixMaxLineWriter(t *testing.T) { 111 assert := assert.New(t) 112 113 tcs := []prefixMaxLineTestCase{ 114 {"hey there\nthis text contains\n3 lines\n", "yo:", "yo:hey there\nyo:this text contains\nyo:3 lines\n", true, 0, false}, 115 {"hey there\nthis text contains\n3 lines\n", "yo:", "yo:hey there\n", true, 1, true}, 116 {"hey there\nthis text contains\n3 lines\n", "yo:", "hey there\nyo:this text contains\nyo:3 lines\n", false, 0, false}, 117 {"hey there\nthis text contains\n3 lines\n", "yo:", "hey there\nyo:this text contains\n", false, 2, true}, 118 {"hey there\nthis text contains\n3 lines\n", "", "hey there\nthis text contains\n3 lines\n", true, 0, false}, 119 {"hey there\nthis text contains\n3 lines\n", "", "hey there\nthis text contains\n", false, 2, true}, 120 } 121 122 doTest := func(tc prefixMaxLineTestCase, tcNum int, buf *bytes.Buffer, tw io.Writer) { 123 l, err := tw.Write([]byte(tc.data)) 124 if tc.errorExpected { 125 assert.Error(err, "test #%d case failed", tcNum) 126 assert.IsType(MaxLinesError{}, err, "test #%d case failed", tcNum) 127 } else { 128 assert.NoError(err, "test #%d case failed", tcNum) 129 } 130 assert.Equal(len(tc.expected), l, "test #%d case failed", tcNum) 131 assert.Equal(tc.expected, buf.String(), "test #%d case failed", tcNum) 132 } 133 134 for i, tc := range tcs { 135 getPrefix := func(w *PrefixWriter) []byte { 136 return []byte(tc.prefix) 137 } 138 buf := &bytes.Buffer{} 139 mlw := &MaxLineWriter{Dest: buf, MaxLines: tc.maxLines} 140 pw := &PrefixWriter{Dest: mlw, PrefixFunc: getPrefix, NeedsPrefix: tc.needsPrefix} 141 doTest(tc, i, buf, pw) 142 143 buf = &bytes.Buffer{} 144 pw = &PrefixWriter{Dest: buf, PrefixFunc: getPrefix, NeedsPrefix: tc.needsPrefix} 145 mlw = &MaxLineWriter{Dest: pw, MaxLines: tc.maxLines} 146 doTest(tc, i, buf, mlw) 147 } 148 }