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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
package svg
import (
"strconv"
"testing"
"github.com/tdewolff/parse/v2"
"github.com/tdewolff/parse/v2/xml"
"github.com/tdewolff/test"
)
func TestBuffer(t *testing.T) {
// 0 12 3 4 5 6 7 8 9 01
s := `<svg><path d="M0 0L1 1z"/>text<tag/>text</svg>`
r := parse.NewInputString(s)
z := NewTokenBuffer(r, xml.NewLexer(r))
tok := z.Shift()
test.That(t, tok.Hash == Svg, "first token is <svg>")
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, z.Peek(2).Hash == D, "third token is d")
test.That(t, z.pos == 0, "don't change position after peeking")
test.That(t, len(z.buf) == 3, "mtwo tokens after peeking")
test.That(t, z.Peek(8).Hash == Svg, "ninth token is <svg>")
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, tok.Hash == Path, "third token is <path>")
test.That(t, z.pos == 2, "don't change position after peeking")
}
func TestAttributes(t *testing.T) {
r := parse.NewInputString(`<rect x="0" y="1" width="2" height="3" rx="4" ry="5"/>`)
l := xml.NewLexer(r)
tb := NewTokenBuffer(r, l)
tb.Shift()
for k := 0; k < 2; k++ { // run twice to ensure similar results
attrs := tb.Attributes(X, Y, Width, Height, Rx, Ry)
for i := 0; i < 6; i++ {
test.That(t, attrs[i] != nil, "attr must not be nil")
val := string(attrs[i].AttrVal)
j, _ := strconv.ParseInt(val, 10, 32)
test.That(t, int(j) == i, "attr data is bad at position", i)
}
}
}
////////////////////////////////////////////////////////////////
func BenchmarkAttributes(b *testing.B) {
r := parse.NewInputString(`<rect x="0" y="1" width="2" height="3" rx="4" ry="5"/>`)
l := xml.NewLexer(r)
tb := NewTokenBuffer(r, l)
tb.Shift()
tb.Peek(6)
for i := 0; i < b.N; i++ {
tb.Attributes(X, Y, Width, Height, Rx, Ry)
}
}
|