예제 #1
0
func TestCopy(t *testing.T) {
	foo := []byte("abc")
	bar := Copy(foo)
	foo[0] = 'b'
	test.Bytes(t, foo, []byte("bbc"))
	test.Bytes(t, bar, []byte("abc"))
}
예제 #2
0
func TestToLower(t *testing.T) {
	foo := []byte("Abc")
	bar := ToLower(foo)
	bar[1] = 'B'
	test.Bytes(t, foo, []byte("aBc"))
	test.Bytes(t, bar, []byte("aBc"))
}
예제 #3
0
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")
}
예제 #4
0
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'")
}
예제 #5
0
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")
}
예제 #6
0
func TestReplaceMultipleWhitespace(t *testing.T) {
	wsRegexp := regexp.MustCompile("[ \t\f]+")
	wsNewlinesRegexp := regexp.MustCompile("[ ]*[\r\n][ \r\n]*")
	for _, e := range wsSlices {
		reference := wsRegexp.ReplaceAll(e, []byte(" "))
		reference = wsNewlinesRegexp.ReplaceAll(reference, []byte("\n"))
		test.Bytes(t, ReplaceMultipleWhitespace(e), reference, "must remove all multiple whitespace but keep newlines")
	}
}
예제 #7
0
func TestMinify(t *testing.T) {
	test.Error(t, m.Minify("?", nil, nil), ErrNotExist, "minifier doesn't exist")
	test.Error(t, m.Minify("dummy/nil", nil, nil), nil)
	test.Error(t, m.Minify("dummy/err", nil, nil), errDummy)

	b := []byte("test")
	out, err := m.Bytes("dummy/nil", b)
	test.Error(t, err, nil)
	test.Bytes(t, out, []byte{}, "dummy/nil returns empty byte slice")
	out, err = m.Bytes("?", b)
	test.Error(t, err, ErrNotExist, "minifier doesn't exist")
	test.Bytes(t, out, b, "return input when minifier doesn't exist")

	s := "test"
	out2, err := m.String("dummy/nil", s)
	test.Error(t, err, nil)
	test.String(t, out2, "", "dummy/nil returns empty string")
	out2, err = m.String("?", s)
	test.Error(t, err, ErrNotExist, "minifier doesn't exist")
	test.String(t, out2, s, "return input when minifier doesn't exist")
}
예제 #8
0
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'")
}
예제 #9
0
func TestParseDataURI(t *testing.T) {
	var dataURITests = []struct {
		dataURI          string
		expectedMimetype string
		expectedData     string
		expectedErr      error
	}{
		{"www.domain.com", "", "", ErrBadDataURI},
		{"data:,", "text/plain", "", nil},
		{"data:text/xml,", "text/xml", "", nil},
		{"data:,text", "text/plain", "text", nil},
		{"data:;base64,dGV4dA==", "text/plain", "text", nil},
		{"data:image/svg+xml,", "image/svg+xml", "", nil},
		{"data:;base64,()", "", "", base64.CorruptInputError(0)},
	}
	for _, tt := range dataURITests {
		mimetype, data, err := DataURI([]byte(tt.dataURI))
		test.Bytes(t, mimetype, []byte(tt.expectedMimetype), "mimetype")
		test.Bytes(t, data, []byte(tt.expectedData), "data")
		test.Error(t, err, tt.expectedErr)
	}
}
예제 #10
0
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)
}
예제 #11
0
func TestMediatype(t *testing.T) {
	var mediatypeTests = []struct {
		mediatype        string
		expectedMimetype string
		expectedParams   map[string]string
	}{
		{"text/plain", "text/plain", nil},
		{"text/plain;charset=US-ASCII", "text/plain", map[string]string{"charset": "US-ASCII"}},
		{" text/plain  ; charset = US-ASCII ", "text/plain", map[string]string{"charset": "US-ASCII"}},
		{" text/plain  a", "text/plain", nil},
		{"text/plain;base64", "text/plain", map[string]string{"base64": ""}},
		{"text/plain;inline=;base64", "text/plain", map[string]string{"inline": "", "base64": ""}},
	}
	for _, tt := range mediatypeTests {
		mimetype, _ := Mediatype([]byte(tt.mediatype))
		test.Bytes(t, mimetype, []byte(tt.expectedMimetype), "mimetype")
		//test.That(t, params == tt.expectedParams, "parameters") // TODO
	}
}
예제 #12
0
func TestTrim(t *testing.T) {
	test.Bytes(t, TrimWhitespace([]byte("a")), []byte("a"))
	test.Bytes(t, TrimWhitespace([]byte(" a")), []byte("a"))
	test.Bytes(t, TrimWhitespace([]byte("a ")), []byte("a"))
	test.Bytes(t, TrimWhitespace([]byte(" ")), []byte(""))
}