File: cached_reader_test.go

package info (click to toggle)
golang-github-antchfx-xmlquery 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, bullseye-proposed-updates, sid
  • size: 156 kB
  • sloc: xml: 120; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 813 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package xmlquery

import (
	"bufio"
	"bytes"
	"strings"
	"testing"
)

func TestCaching(t *testing.T) {
	buf := strings.NewReader(`ABCDEF`)
	bufReader := bufio.NewReader(buf)
	cachedReader := newCachedReader(bufReader)

	b, err := cachedReader.ReadByte()
	if err != nil {
		t.Fatal(err.Error())
	}

	if b != 'A' {
		t.Fatalf("Expected read byte to be A, got %c instead.", b)
	}

	cachedReader.StartCaching()
	tmpBuf := make([]byte, 10)
	n, err := cachedReader.Read(tmpBuf)
	if err != nil {
		t.Fatal(err.Error())
	}

	if n != 5 {
		t.Fatalf("Expected 5 bytes to be read. Got %d instead.", n)
	}
	if !bytes.Equal(tmpBuf[:n], []byte("BCDEF")) {
		t.Fatalf("Incorrect read buffer value")
	}

	cached := cachedReader.Cache()
	if !bytes.Equal(cached, []byte("BCDEF")) {
		t.Fatalf("Incorrect cached buffer value")
	}
}