buffer_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package xml // import "github.com/tdewolff/minify/xml"
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/tdewolff/parse/xml"
  6. "github.com/tdewolff/test"
  7. )
  8. func TestBuffer(t *testing.T) {
  9. // 0 12 3 45 6 7 8 9 0
  10. s := `<p><a href="//url">text</a>text<!--comment--></p>`
  11. z := NewTokenBuffer(xml.NewLexer(bytes.NewBufferString(s)))
  12. tok := z.Shift()
  13. test.That(t, string(tok.Text) == "p", "first token is <p>")
  14. test.That(t, z.pos == 0, "shift first token and restore position")
  15. test.That(t, len(z.buf) == 0, "shift first token and restore length")
  16. test.That(t, string(z.Peek(2).Text) == "href", "third token is href")
  17. test.That(t, z.pos == 0, "don't change position after peeking")
  18. test.That(t, len(z.buf) == 3, "two tokens after peeking")
  19. test.That(t, string(z.Peek(8).Text) == "p", "ninth token is <p>")
  20. test.That(t, z.pos == 0, "don't change position after peeking")
  21. test.That(t, len(z.buf) == 9, "nine tokens after peeking")
  22. test.That(t, z.Peek(9).TokenType == xml.ErrorToken, "tenth token is an error")
  23. test.That(t, z.Peek(9) == z.Peek(10), "tenth and eleventh token are EOF")
  24. test.That(t, len(z.buf) == 10, "ten tokens after peeking")
  25. _ = z.Shift()
  26. tok = z.Shift()
  27. test.That(t, string(tok.Text) == "a", "third token is <a>")
  28. test.That(t, z.pos == 2, "don't change position after peeking")
  29. }