github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/go/util/writers/writers_test.go (about) 1 // Copyright 2016 Attic Labs, Inc. All rights reserved. 2 // Licensed under the Apache License, version 2.0: 3 // http://www.apache.org/licenses/LICENSE-2.0 4 5 package writers 6 7 import ( 8 "bytes" 9 "io" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 type maxLineTestCase struct { 16 data string 17 maxLines uint32 18 expected string 19 errorExpected bool 20 } 21 22 func TestMaxLineWriter(t *testing.T) { 23 assert := assert.New(t) 24 25 tcs := []maxLineTestCase{ 26 {"hey there\nthis text contains\n3 lines\n", 1, "hey there\n", true}, 27 {"hey there\nthis text contains\n3 lines\n", 2, "hey there\nthis text contains\n", true}, 28 {"hey there\nthis text contains\n3 lines\n", 3, "hey there\nthis text contains\n3 lines\n", false}, 29 {"hey there\nthis text contains\n3 lines\nand more\n", 3, "hey there\nthis text contains\n3 lines\n", true}, 30 {"hey there\nthis text contains\n3 lines\n", 4, "hey there\nthis text contains\n3 lines\n", false}, 31 {"hey there\nthis text contains\n3 lines\n", 0, "hey there\nthis text contains\n3 lines\n", false}, 32 {"\n\n\n\n", 2, "\n\n", true}, 33 } 34 35 for i, tc := range tcs { 36 buf := bytes.NewBuffer(nil) 37 mlw := MaxLineWriter{Dest: buf, MaxLines: tc.maxLines} 38 l, err := mlw.Write([]byte(tc.data)) 39 assert.Equal(len(tc.expected), l, "test #%d case failed", i) 40 if tc.errorExpected { 41 assert.Error(err, "test #%d case failed", i) 42 assert.IsType(MaxLinesError{}, err, "test #%d case failed", i) 43 } else { 44 assert.NoError(err, "test #%d case failed", i) 45 } 46 assert.Equal(tc.expected, buf.String(), "test #%d case failed", i) 47 } 48 } 49 50 type prefixTestCase struct { 51 data string 52 prefix string 53 expected string 54 needsPrefix bool 55 } 56 57 func TestPrefixWriter(t *testing.T) { 58 assert := assert.New(t) 59 60 tcs := []prefixTestCase{ 61 {"\n", "yo:", "yo:\n", true}, 62 {"\n", "yo:", "\n", false}, 63 {"\n\n", "yo:", "yo:\nyo:\n", true}, 64 {"\n\n", "yo:", "\nyo:\n", false}, 65 {"hey there\nthis text contains\n3 lines\n", "yo:", "yo:hey there\nyo:this text contains\nyo:3 lines\n", true}, 66 {"hey there\nthis text contains\n3 lines\n", "yo:", "hey there\nyo:this text contains\nyo:3 lines\n", false}, 67 {"hey there\nthis text contains\n3 lines\n", "", "hey there\nthis text contains\n3 lines\n", true}, 68 {"hey there\nthis text contains\n3 lines\n", "", "hey there\nthis text contains\n3 lines\n", false}, 69 } 70 71 for _, tc := range tcs { 72 getPrefix := func(w *PrefixWriter) []byte { 73 return []byte(tc.prefix) 74 } 75 buf := bytes.NewBuffer(nil) 76 pw := PrefixWriter{Dest: buf, PrefixFunc: getPrefix, NeedsPrefix: tc.needsPrefix} 77 l, err := pw.Write([]byte(tc.data)) 78 assert.NoError(err) 79 assert.Equal(len(tc.expected), l) 80 assert.Equal(tc.expected, buf.String()) 81 } 82 } 83 84 type prefixMaxLineTestCase struct { 85 data string 86 prefix string 87 expected string 88 needsPrefix bool 89 maxLines uint32 90 errorExpected bool 91 } 92 93 func TestPrefixMaxLineWriter(t *testing.T) { 94 assert := assert.New(t) 95 96 tcs := []prefixMaxLineTestCase{ 97 {"hey there\nthis text contains\n3 lines\n", "yo:", "yo:hey there\nyo:this text contains\nyo:3 lines\n", true, 0, false}, 98 {"hey there\nthis text contains\n3 lines\n", "yo:", "yo:hey there\n", true, 1, true}, 99 {"hey there\nthis text contains\n3 lines\n", "yo:", "hey there\nyo:this text contains\nyo:3 lines\n", false, 0, false}, 100 {"hey there\nthis text contains\n3 lines\n", "yo:", "hey there\nyo:this text contains\n", false, 2, true}, 101 {"hey there\nthis text contains\n3 lines\n", "", "hey there\nthis text contains\n3 lines\n", true, 0, false}, 102 {"hey there\nthis text contains\n3 lines\n", "", "hey there\nthis text contains\n", false, 2, true}, 103 } 104 105 doTest := func(tc prefixMaxLineTestCase, tcNum int, buf *bytes.Buffer, tw io.Writer) { 106 l, err := tw.Write([]byte(tc.data)) 107 if tc.errorExpected { 108 assert.Error(err, "test #%d case failed", tcNum) 109 assert.IsType(MaxLinesError{}, err, "test #%d case failed", tcNum) 110 } else { 111 assert.NoError(err, "test #%d case failed", tcNum) 112 } 113 assert.Equal(len(tc.expected), l, "test #%d case failed", tcNum) 114 assert.Equal(tc.expected, buf.String(), "test #%d case failed", tcNum) 115 } 116 117 for i, tc := range tcs { 118 getPrefix := func(w *PrefixWriter) []byte { 119 return []byte(tc.prefix) 120 } 121 buf := &bytes.Buffer{} 122 mlw := &MaxLineWriter{Dest: buf, MaxLines: tc.maxLines} 123 pw := &PrefixWriter{Dest: mlw, PrefixFunc: getPrefix, NeedsPrefix: tc.needsPrefix} 124 doTest(tc, i, buf, pw) 125 126 buf = &bytes.Buffer{} 127 pw = &PrefixWriter{Dest: buf, PrefixFunc: getPrefix, NeedsPrefix: tc.needsPrefix} 128 mlw = &MaxLineWriter{Dest: pw, MaxLines: tc.maxLines} 129 doTest(tc, i, buf, mlw) 130 } 131 }