File: buffer_test.go

package info (click to toggle)
golang-github-tdewolff-minify 2.24.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,416 kB
  • sloc: javascript: 394,644; xml: 25,649; ansic: 253; makefile: 111; python: 109; sh: 74
file content (37 lines) | stat: -rw-r--r-- 1,326 bytes parent folder | download | duplicates (3)
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
package xml

import (
	"testing"

	"github.com/tdewolff/parse/v2"
	"github.com/tdewolff/parse/v2/xml"
	"github.com/tdewolff/test"
)

func TestBuffer(t *testing.T) {
	//    0 12  3           45   6   7   8             9   0
	s := `<p><a href="//url">text</a>text<!--comment--></p>`
	z := NewTokenBuffer(xml.NewLexer(parse.NewInputString(s)))

	tok := z.Shift()
	test.That(t, string(tok.Text) == "p", "first token is <p>")
	test.That(t, z.pos == 0, "shift first token and restore position")
	test.That(t, len(z.buf) == 0, "shift first token and restore length")

	test.That(t, string(z.Peek(2).Text) == "href", "third token is href")
	test.That(t, z.pos == 0, "don't change position after peeking")
	test.That(t, len(z.buf) == 3, "two tokens after peeking")

	test.That(t, string(z.Peek(8).Text) == "p", "ninth token is <p>")
	test.That(t, z.pos == 0, "don't change position after peeking")
	test.That(t, len(z.buf) == 9, "nine tokens after peeking")

	test.That(t, z.Peek(9).TokenType == xml.ErrorToken, "tenth token is an error")
	test.That(t, z.Peek(9) == z.Peek(10), "tenth and eleventh token are EOF")
	test.That(t, len(z.buf) == 10, "ten tokens after peeking")

	_ = z.Shift()
	tok = z.Shift()
	test.That(t, string(tok.Text) == "a", "third token is <a>")
	test.That(t, z.pos == 2, "don't change position after peeking")
}