Debian Codegraph @

common/hugio/readers_test.go

raw
1 // Copyright 2026 The Hugo Authors. All rights reserved.
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 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package hugio
15
16 import (
17 "io"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 )
22
23 func TestNewSkipBOMReader(t *testing.T) {
24 c := qt.New(t)
25
26 newReader := func(s string) ReadSeekCloser {
27 r, err := NewSkipBOMReader(NewReadSeekerNoOpCloserFromString(s))
28 c.Assert(err, qt.IsNil)
29 return r
30 }
31
32 readAll := func(r io.Reader) string {
33 b, err := io.ReadAll(r)
34 c.Assert(err, qt.IsNil)
35 return string(b)
36 }
37
38 r := newReader(BOM + "abc")
39 c.Assert(readAll(r), qt.Equals, "abc")
40
41 pos, err := r.Seek(0, io.SeekStart)
42 c.Assert(err, qt.IsNil)
43 c.Assert(pos, qt.Equals, int64(0))
44 c.Assert(readAll(r), qt.Equals, "abc")
45
46 pos, err = r.Seek(1, io.SeekStart)
47 c.Assert(err, qt.IsNil)
48 c.Assert(pos, qt.Equals, int64(1))
49 c.Assert(readAll(r), qt.Equals, "bc")
50
51 pos, err = r.Seek(-1, io.SeekEnd)
52 c.Assert(err, qt.IsNil)
53 c.Assert(pos, qt.Equals, int64(2))
54 c.Assert(readAll(r), qt.Equals, "c")
55
56 pos, err = r.Seek(0, io.SeekCurrent)
57 c.Assert(err, qt.IsNil)
58 c.Assert(pos, qt.Equals, int64(3))
59
60 _, err = r.Seek(-1, io.SeekStart)
61 c.Assert(err, qt.ErrorMatches, ".*negative position")
62 _, err = r.Seek(-4, io.SeekEnd)
63 c.Assert(err, qt.ErrorMatches, ".*negative position")
64 _, err = r.Seek(-4, io.SeekCurrent)
65 c.Assert(err, qt.ErrorMatches, ".*negative position")
66
67 pos, err = r.Seek(-3, io.SeekEnd)
68 c.Assert(err, qt.IsNil)
69 c.Assert(pos, qt.Equals, int64(0))
70 c.Assert(readAll(r), qt.Equals, "abc")
71
72 c.Assert(r.Close(), qt.IsNil)
73
74 for _, s := range []string{"abc", "ab", "a", "", BOM[:2], BOM} {
75 got := readAll(newReader(s))
76 if s == BOM {
77 c.Assert(got, qt.Equals, "")
78 } else {
79 c.Assert(got, qt.Equals, s)
80 }
81 }
82 }