github.com/biogo/biogo@v1.0.4/util/wrapper_test.go (about) 1 // Copyright ©2011-2012 The bíogo Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package util 6 7 import ( 8 "bytes" 9 10 "gopkg.in/check.v1" 11 ) 12 13 const lorem = `Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.` 14 15 // Tests 16 func (s *S) TestWrapper(c *check.C) { 17 for i, t := range []struct { 18 w *Wrapper 19 in string 20 out string 21 err error 22 }{ 23 { 24 w: &Wrapper{limit: 0, width: 0}, 25 in: lorem, 26 out: ``, 27 err: nil, 28 }, 29 { 30 w: &Wrapper{limit: -1, width: 0}, 31 in: lorem, 32 out: lorem, 33 err: nil, 34 }, 35 { 36 w: &Wrapper{limit: -1, width: 20}, 37 in: lorem, 38 out: "" + 39 "Lorem ipsum dolor si\n" + 40 "t amet, consectetur \n" + 41 "adipisicing elit, se\n" + 42 "d do eiusmod tempor \n" + 43 "incididunt ut labore\n" + 44 " et dolore magna ali\n" + 45 "qua.", 46 err: nil, 47 }, 48 { 49 w: &Wrapper{limit: 33, width: 20}, 50 in: lorem, 51 out: "" + 52 "Lorem ipsum dolor si\n" + 53 "t amet, conse", 54 err: nil, 55 }, 56 { 57 w: &Wrapper{n: 2, limit: 33, width: 20}, 58 in: lorem, 59 out: "" + 60 "Lorem ipsum dolor \n" + 61 "sit amet, con", 62 err: nil, 63 }, 64 } { 65 b := &bytes.Buffer{} 66 t.w.w = b 67 n, err := t.w.Write([]byte(t.in)) 68 s := b.String() 69 c.Check(err, check.Equals, t.err, check.Commentf("Test %d", i)) 70 c.Check(s, check.Equals, t.out, check.Commentf("Test %d", i)) 71 if t.w.limit >= 0 { 72 c.Check(t.w.n, check.Equals, min(len(t.in), t.w.limit), check.Commentf("Test %d", i)) 73 } 74 c.Check(n, check.Equals, len(s), check.Commentf("Test %d", i)) 75 } 76 } 77 78 func (s *S) TestWrapperSingleBytes(c *check.C) { 79 for i, t := range []struct { 80 w *Wrapper 81 in string 82 out string 83 err error 84 }{ 85 { 86 w: &Wrapper{limit: 0, width: 0}, 87 in: lorem, 88 out: ``, 89 err: nil, 90 }, 91 { 92 w: &Wrapper{limit: -1, width: 0}, 93 in: lorem, 94 out: lorem, 95 err: nil, 96 }, 97 { 98 w: &Wrapper{limit: -1, width: 20}, 99 in: lorem, 100 out: "" + 101 "Lorem ipsum dolor si\n" + 102 "t amet, consectetur \n" + 103 "adipisicing elit, se\n" + 104 "d do eiusmod tempor \n" + 105 "incididunt ut labore\n" + 106 " et dolore magna ali\n" + 107 "qua.", 108 err: nil, 109 }, 110 { 111 w: &Wrapper{limit: 33, width: 20}, 112 in: lorem, 113 out: "" + 114 "Lorem ipsum dolor si\n" + 115 "t amet, conse", 116 err: nil, 117 }, 118 { 119 w: &Wrapper{n: 2, limit: 33, width: 20}, 120 in: lorem, 121 out: "" + 122 "Lorem ipsum dolor \n" + 123 "sit amet, con", 124 err: nil, 125 }, 126 } { 127 b := &bytes.Buffer{} 128 t.w.w = b 129 var n int 130 for _, l := range t.in { 131 _n, err := t.w.Write([]byte{byte(l)}) 132 c.Check(err, check.Equals, t.err, check.Commentf("Test %d", i)) 133 n += _n 134 } 135 s := b.String() 136 c.Check(s, check.Equals, t.out, check.Commentf("Test %d", i)) 137 if t.w.limit >= 0 { 138 c.Check(t.w.n, check.Equals, min(len(t.in), t.w.limit), check.Commentf("Test %d", i)) 139 } 140 c.Check(n, check.Equals, len(s), check.Commentf("Test %d", i)) 141 } 142 } 143 func (s *S) TestWrapperSegments(c *check.C) { 144 for i, t := range []struct { 145 w *Wrapper 146 in []string 147 out string 148 err error 149 }{ 150 { 151 w: &Wrapper{limit: 0, width: 0}, 152 in: []string{`Lorem ipsum`, ` dolor sit a`, `met, consectetur adi`, `pisicing elit, sed do eiu`, `s`, `mod tempor in`, `cididunt ut labore et dolor`, `e magna aliqua.`}, 153 out: ``, 154 err: nil, 155 }, 156 { 157 w: &Wrapper{limit: -1, width: 0}, 158 in: []string{`Lorem ipsum`, ` dolor sit a`, `met, consectetur adi`, `pisicing elit, sed do eiu`, `s`, `mod tempor in`, `cididunt ut labore et dolor`, `e magna aliqua.`}, 159 out: lorem, 160 err: nil, 161 }, 162 { 163 w: &Wrapper{limit: -1, width: 20}, 164 in: []string{`Lorem ipsum`, ` dolor sit a`, `met, consectetur adi`, `pisicing elit, sed do eiu`, `s`, `mod tempor in`, `cididunt ut labore et dolor`, `e magna aliqua.`}, 165 out: "" + 166 "Lorem ipsum dolor si\n" + 167 "t amet, consectetur \n" + 168 "adipisicing elit, se\n" + 169 "d do eiusmod tempor \n" + 170 "incididunt ut labore\n" + 171 " et dolore magna ali\n" + 172 "qua.", 173 err: nil, 174 }, 175 { 176 w: &Wrapper{limit: 33, width: 20}, 177 in: []string{`Lorem ipsum`, ` dolor sit a`, `met, consectetur adi`, `pisicing elit, sed do eiu`, `s`, `mod tempor in`, `cididunt ut labore et dolor`, `e magna aliqua.`}, 178 out: "" + 179 "Lorem ipsum dolor si\n" + 180 "t amet, conse", 181 err: nil, 182 }, 183 { 184 w: &Wrapper{n: 2, limit: 33, width: 20}, 185 in: []string{`Lorem ipsum dolor si`, `t a`, `met, consectetur adi`, `pisicing elit, sed do eiu`, `s`, `mod tempor in`, `cididunt ut labore et dolor`, `e magna aliqua.`}, 186 out: "" + 187 "Lorem ipsum dolor \n" + 188 "sit amet, con", 189 err: nil, 190 }, 191 } { 192 b := &bytes.Buffer{} 193 t.w.w = b 194 var n int 195 for _, l := range t.in { 196 _n, err := t.w.Write([]byte(l)) 197 c.Check(err, check.Equals, t.err, check.Commentf("Test %d", i)) 198 n += _n 199 } 200 s := b.String() 201 c.Check(s, check.Equals, t.out, check.Commentf("Test %d", i)) 202 c.Check(n, check.Equals, len(s), check.Commentf("Test %d", i)) 203 } 204 }