11 func decodeToString(src
string) (string, error
) {
12 dec
, err
:= NewDecoder(strings
.NewReader(src
))
16 p
, err
:= ioutil
.ReadAll(dec
)
20 func TestDecoder(t
*testing
.T
) {
21 for _
, test
:= range []struct {
42 // bad version indicator
51 // text outside <pre> elements
82 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\n",
85 // no <pre> elements, hence no version indicator
96 // empty <pre> elements, hence no version indicator
102 aGVsbG8gd29ybGQK<pre></pre>
108 // other elements inside <pre>
110 "blah <pre>0aGVsb<p>G8gd29</p>ybGQK</pre>",
116 "blah <!-- <pre>aGVsbG8gd29ybGQK</pre> -->",
120 // all kinds of ASCII whitespace
122 "blah <pre>\x200\x09aG\x0aV\x0csb\x0dG8\x20gd29ybGQK</pre>",
148 // test disabled because Go stdlib handles this incorrectly:
149 // https://github.com/golang/go/issues/31626
151 "<pre>QQ==</pre><pre>Qg==</pre>",
158 "blah <pre></pre><pre>0aGVsbG8gd29ybGQK",
164 "blah <pre>0aGVsb<pre>G8gd29</pre>ybGQK</pre>",
169 output
, err
:= decodeToString(test
.input
)
170 if test
.expectedErr
&& err
== nil {
171 t
.Errorf("%+q → (%+q, %v), expected error", test
.input
, output
, err
)
174 if !test
.expectedErr
&& err
!= nil {
175 t
.Errorf("%+q → (%+q, %v), expected no error", test
.input
, output
, err
)
178 if !test
.expectedErr
&& output
!= test
.expectedOutput
{
179 t
.Errorf("%+q → (%+q, %v), expected (%+q, %v)",
180 test
.input
, output
, err
, test
.expectedOutput
, nil)
186 func roundTrip(s
string) (string, error
) {
187 var encoded strings
.Builder
188 enc
, err
:= NewEncoder(&encoded
)
192 _
, err
= io
.Copy(enc
, strings
.NewReader(s
))
200 return decodeToString(encoded
.String())
203 func TestRoundTrip(t
*testing
.T
) {
204 lengths
:= make([]int, 0)
205 // Test short strings and lengths around elementSizeLimit thresholds.
206 for i
:= 0; i
< bytesPerChunk
*2; i
++ {
207 lengths
= append(lengths
, i
)
209 for i
:= -10; i
< +10; i
++ {
210 lengths
= append(lengths
, elementSizeLimit
+i
)
211 lengths
= append(lengths
, 2*elementSizeLimit
+i
)
213 for _
, n
:= range lengths
{
214 buf
:= make([]byte, n
)
217 output
, err
:= roundTrip(input
)
219 t
.Errorf("length %d → error %v", n
, err
)
223 t
.Errorf("length %d → %+q", n
, output
)