func TestReader(t *testing.T) { s := []byte("abcde") r := NewReader(s) test.Bytes(t, r.Bytes(), s, "reader must return bytes stored") buf := make([]byte, 3) n, err := r.Read(buf) test.Error(t, err, nil, "error must be nil") test.That(t, n == 3, "first read must read 3 characters") test.Bytes(t, buf, []byte("abc"), "first read must match 'abc'") n, err = r.Read(buf) test.Error(t, err, nil, "error must be nil") test.That(t, n == 2, "second read must read 2 characters") test.Bytes(t, buf[:n], []byte("de"), "second read must match 'de'") n, err = r.Read(buf) test.Error(t, err, io.EOF, "error must be io.EOF") test.That(t, n == 0, "third read must read 0 characters") n, err = r.Read(nil) test.Error(t, err, nil, "error must be nil") test.That(t, n == 0, "read to nil buffer must return 0 characters read") r.Reset() n, err = r.Read(buf) test.Error(t, err, nil, "error must be nil") test.That(t, n == 3, "read after reset must read 3 characters") test.Bytes(t, buf, []byte("abc"), "read after reset must match 'abc'") }
func TestLexerSmall(t *testing.T) { s := `abcdefghijklm` z := NewLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 4) test.That(t, z.Peek(8) == 'i', "first character must be 'i' at position 8") z = NewLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 4) test.That(t, z.Peek(12) == 'm', "first character must be 'm' at position 12") z = NewLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 0) test.That(t, z.Peek(4) == 'e', "first character must be 'e' at position 4") z = NewLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 13) test.That(t, z.Peek(13) == 0, "thirteenth character must yield error") }
func TestAttributes(t *testing.T) { r := bytes.NewBufferString(`<rect x="0" y="1" width="2" height="3" rx="4" ry="5"/>`) l := xml.NewLexer(r) tb := NewTokenBuffer(l) tb.Shift() for k := 0; k < 2; k++ { // run twice to ensure similar results attrs, _ := tb.Attributes(svg.X, svg.Y, svg.Width, svg.Height, svg.Rx, svg.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 TestLenInt(t *testing.T) { lenIntTests := []struct { number int64 expected int }{ {0, 1}, {1, 1}, {10, 2}, {99, 2}, // coverage {100, 3}, {1000, 4}, {10000, 5}, {100000, 6}, {1000000, 7}, {10000000, 8}, {100000000, 9}, {1000000000, 10}, {10000000000, 11}, {100000000000, 12}, {1000000000000, 13}, {10000000000000, 14}, {100000000000000, 15}, {1000000000000000, 16}, {10000000000000000, 17}, {100000000000000000, 18}, {1000000000000000000, 19}, } for _, tt := range lenIntTests { test.That(t, LenInt(tt.number) == tt.expected, "return", tt.expected, "for", tt.number) } }
func TestLexerShift(t *testing.T) { s := `Lorem ipsum dolor sit amet, consectetur adipiscing elit.` z := NewLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 5) z.Move(len("Lorem ")) test.Bytes(t, z.Shift(), []byte("Lorem "), "shift must return the buffered string") test.That(t, z.ShiftLen() == len("Lorem "), "shifted length must equal last shift") }
func TestParseDimension(t *testing.T) { var dimensionTests = []struct { dimension string expectedNum int expectedUnit int }{ {"5px", 1, 2}, {"5px ", 1, 2}, {"5%", 1, 1}, {"5em", 1, 2}, {"px", 0, 0}, {"1", 1, 0}, {"1~", 1, 0}, } for _, tt := range dimensionTests { num, unit := Dimension([]byte(tt.dimension)) test.That(t, num == tt.expectedNum, "number") test.That(t, unit == tt.expectedUnit, "unit") } }
func TestParseQuoteEntity(t *testing.T) { var quoteEntityTests = []struct { quoteEntity string expectedQuote byte expectedN int }{ {""", '"', 5}, {"'", '\'', 6}, {""", '"', 8}, {"'", '\'', 6}, {""", '"', 6}, {"'", '\'', 6}, {">", 0x00, 0}, {"&", 0x00, 0}, } for _, tt := range quoteEntityTests { quote, n := QuoteEntity([]byte(tt.quoteEntity)) test.That(t, quote == tt.expectedQuote, "quote", quote, "must equal", tt.expectedQuote) test.That(t, n == tt.expectedN, "quote length", n, "must equal", tt.expectedN) } }
func TestWriter(t *testing.T) { w := NewWriter(make([]byte, 0, 3)) test.That(t, w.Len() == 0, "buffer must initially have zero length") n, _ := w.Write([]byte("abc")) test.That(t, n == 3, "first write must write 3 characters") test.Bytes(t, w.Bytes(), []byte("abc"), "first write must match 'abc'") test.That(t, w.Len() == 3, "buffer must have length 3 after first write") n, _ = w.Write([]byte("def")) test.That(t, n == 3, "second write must write 3 characters") test.Bytes(t, w.Bytes(), []byte("abcdef"), "second write must match 'abcdef'") w.Reset() test.Bytes(t, w.Bytes(), []byte(""), "reset must match ''") n, _ = w.Write([]byte("ghijkl")) test.That(t, n == 6, "third write must write 6 characters") test.Bytes(t, w.Bytes(), []byte("ghijkl"), "third write must match 'ghijkl'") }
func TestShifterRunes(t *testing.T) { z := NewShifter(bytes.NewBufferString("aæ†\U00100000")) r, n := z.PeekRune(0) test.That(t, n == 1, "first character must be length 1") test.That(t, r == 'a', "first character must be rune 'a'") r, n = z.PeekRune(1) test.That(t, n == 2, "second character must be length 2") test.That(t, r == 'æ', "second character must be rune 'æ'") r, n = z.PeekRune(3) test.That(t, n == 3, "fourth character must be length 3") test.That(t, r == '†', "fourth character must be rune '†'") r, n = z.PeekRune(6) test.That(t, n == 4, "seventh character must be length 4") test.That(t, r == '\U00100000', "seventh character must be rune '\U00100000'") }
func TestParseFloat(t *testing.T) { floatTests := []struct { f string expected float64 }{ {"5", 5}, {"5.1", 5.1}, {"-5.1", -5.1}, {"5.1e-2", 5.1e-2}, {"5.1e+2", 5.1e+2}, {"0.0e1", 0.0e1}, {"18446744073709551620", 18446744073709551620.0}, {"1e23", 1e23}, // TODO: hard to test due to float imprecision // {"1.7976931348623e+308", 1.7976931348623e+308) // {"4.9406564584124e-308", 4.9406564584124e-308) } for _, tt := range floatTests { f, n := ParseFloat([]byte(tt.f)) test.That(t, n == len(tt.f), "parsed", n, "characters instead for", tt.f) test.That(t, f == tt.expected, "return", tt.expected, "for", tt.f) } }
func TestEqual(t *testing.T) { test.That(t, Equal([]byte("abc"), []byte("abc"))) test.That(t, !Equal([]byte("abcd"), []byte("abc"))) test.That(t, !Equal([]byte("bbc"), []byte("abc"))) test.That(t, EqualFold([]byte("Abc"), []byte("abc"))) test.That(t, !EqualFold([]byte("Abcd"), []byte("abc"))) test.That(t, !EqualFold([]byte("Bbc"), []byte("abc"))) }
func TestBufferPool(t *testing.T) { z := &bufferPool{} lorem := []byte("Lorem ipsum") dolor := []byte("dolor sit amet") consectetur := []byte("consectetur adipiscing elit") // set lorem as first buffer and get new dolor buffer b := z.swap(lorem, len(dolor)) test.That(t, len(b) == 0) test.That(t, cap(b) == len(dolor)) b = append(b, dolor...) // free first buffer so it will be reused z.free(len(lorem)) b = z.swap(b, len(lorem)) b = b[:len(lorem)] test.Bytes(t, b, lorem) b = z.swap(b, len(consectetur)) b = append(b, consectetur...) // free in advance to reuse the same buffer z.free(len(dolor) + len(lorem) + len(consectetur)) test.That(t, z.head == 0) b = z.swap(b, len(consectetur)) b = b[:len(consectetur)] test.Bytes(t, b, consectetur) // free in advance but request larger buffer z.free(len(consectetur)) b = z.swap(b, len(consectetur)+1) b = append(b, consectetur...) b = append(b, '.') test.That(t, cap(b) == len(consectetur)+1) }
func TestParseNumber(t *testing.T) { var numberTests = []struct { number string expected int }{ {"5", 1}, {"0.51", 4}, {"0.5e-99", 7}, {"0.5e-", 3}, {"+50.0", 5}, {".0", 2}, {"0.", 1}, {"", 0}, {"+", 0}, {".", 0}, {"a", 0}, } for _, tt := range numberTests { n := Number([]byte(tt.number)) test.That(t, n == tt.expected) } }
func TestParseInt(t *testing.T) { intTests := []struct { i string expected int64 }{ {"5", 5}, {"99", 99}, {"999", 999}, {"-5", -5}, {"+5", 5}, {"9223372036854775807", 9223372036854775807}, {"9223372036854775808", 0}, {"-9223372036854775807", -9223372036854775807}, {"-9223372036854775808", -9223372036854775808}, {"-9223372036854775809", 0}, {"18446744073709551620", 0}, {"a", 0}, } for _, tt := range intTests { i, _ := ParseInt([]byte(tt.i)) test.That(t, i == tt.expected, "return", tt.expected, "for", tt.i) } }
func TestShifterSmall(t *testing.T) { s := `abcdefghi` z := NewShifterSize(test.NewPlainReader(bytes.NewBufferString(s)), 4) test.That(t, z.Peek(8) == 'i', "first character must be 'i' at position 8") }
func TestWhitespace(t *testing.T) { test.That(t, IsAllWhitespace([]byte("\t \r\n\f"))) test.That(t, !IsAllWhitespace([]byte("\t \r\n\fx"))) }
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>` z := NewTokenBuffer(xml.NewLexer(bytes.NewBufferString(s))) tok := z.Shift() test.That(t, tok.Hash == svg.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 == svg.D, "third token is d") test.That(t, z.pos == 0, "don't change positon after peeking") test.That(t, len(z.buf) == 3, "mtwo tokens after peeking") test.That(t, z.Peek(8).Hash == svg.Svg, "nineth token is <svg>") test.That(t, z.pos == 0, "don't change positon 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") tok = z.Shift() tok = z.Shift() test.That(t, tok.Hash == svg.Path, "third token is <path>") test.That(t, z.pos == 2, "don't change positon after peeking") }
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(html.NewLexer(bytes.NewBufferString(s))) tok := z.Shift() test.That(t, tok.Hash == html.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, z.Peek(2).Hash == html.Href, "third token is href") test.That(t, z.pos == 0, "don't change positon after peeking") test.That(t, len(z.buf) == 3, "two tokens after peeking") test.That(t, z.Peek(8).Hash == html.P, "nineth token is <p>") test.That(t, z.pos == 0, "don't change positon after peeking") test.That(t, len(z.buf) == 9, "nine tokens after peeking") test.That(t, z.Peek(9).TokenType == html.ErrorToken, "tenth token is an error") test.That(t, z.Peek(9) == z.Peek(10), "tenth and eleventh tokens are EOF") test.That(t, len(z.buf) == 10, "ten tokens after peeking") tok = z.Shift() tok = z.Shift() test.That(t, tok.Hash == html.A, "third token is <a>") test.That(t, z.pos == 2, "don't change positon after peeking") }
func TestLexerEmptyReader(t *testing.T) { z := NewLexer(test.NewEmptyReader()) test.That(t, z.Peek(0) == 0, "first character must yield error") test.Error(t, z.Err(), io.EOF, "error must be EOF") test.That(t, z.Peek(0) == 0, "second peek must also yield error") }
func TestShifter(t *testing.T) { s := `Lorem ipsum dolor sit amet, consectetur adipiscing elit.` var z = NewShifter(bytes.NewBufferString(s)) test.That(t, z.IsEOF(), "buffer must be fully in memory") test.Error(t, z.Err(), nil, "buffer is at EOF but must not return EOF until we reach that") test.That(t, z.Pos() == 0, "buffer must start at position 0") test.That(t, z.Peek(0) == 'L', "first character must be 'L'") test.That(t, z.Peek(1) == 'o', "second character must be 'o'") z.Move(1) test.That(t, z.Peek(0) == 'o', "must be 'o' at position 1") test.That(t, z.Peek(1) == 'r', "must be 'r' at position 1") z.MoveTo(6) test.That(t, z.Peek(0) == 'i', "must be 'i' at position 6") test.That(t, z.Peek(1) == 'p', "must be 'p' at position 7") test.Bytes(t, z.Bytes(), []byte("Lorem "), "buffered string must now read 'Lorem ' when at position 6") test.Bytes(t, z.Shift(), []byte("Lorem "), "shift must return the buffered string") test.That(t, z.Pos() == 0, "after shifting position must be 0") test.That(t, z.Peek(0) == 'i', "must be 'i' at position 0 after shifting") test.That(t, z.Peek(1) == 'p', "must be 'p' at position 1 after shifting") test.Error(t, z.Err(), nil, "error must be nil at this point") z.Move(len(s) - len("Lorem ") - 1) test.Error(t, z.Err(), nil, "error must be nil just before the end of the buffer") z.Skip() test.That(t, z.Pos() == 0, "after skipping position must be 0") z.Move(1) test.Error(t, z.Err(), io.EOF, "error must be EOF when past the buffer") z.Move(-1) test.Error(t, z.Err(), nil, "error must be nil just before the end of the buffer, even when it has been past the buffer") }
func TestShifterZeroLen(t *testing.T) { var z = NewShifter(test.NewPlainReader(bytes.NewBufferString(""))) test.That(t, z.Peek(0) == 0, "first character must yield error") }
func TestShifterEmptyReader(t *testing.T) { var z = NewShifter(test.NewEmptyReader()) test.That(t, z.Peek(0) == 0, "first character must yield error") test.That(t, z.IsEOF(), "empty reader must return EOF") }