source: add some test cases for File

This commit is contained in:
bep 2015-03-12 20:50:44 +01:00
parent f8a840a14c
commit b190ad0ff9
2 changed files with 31 additions and 0 deletions

View file

@ -75,6 +75,9 @@ func GuessType(in string) string {
// ReaderToBytes takes an io.Reader argument, reads from it // ReaderToBytes takes an io.Reader argument, reads from it
// and returns bytes. // and returns bytes.
func ReaderToBytes(lines io.Reader) []byte { func ReaderToBytes(lines io.Reader) []byte {
if lines == nil {
return []byte{}
}
b := bp.GetBuffer() b := bp.GetBuffer()
defer bp.PutBuffer(b) defer bp.PutBuffer(b)
@ -87,6 +90,9 @@ func ReaderToBytes(lines io.Reader) []byte {
// ReaderToString is the same as ReaderToBytes, but returns a string. // ReaderToString is the same as ReaderToBytes, but returns a string.
func ReaderToString(lines io.Reader) string { func ReaderToString(lines io.Reader) string {
if lines == nil {
return ""
}
b := bp.GetBuffer() b := bp.GetBuffer()
defer bp.PutBuffer(b) defer bp.PutBuffer(b)
b.ReadFrom(lines) b.ReadFrom(lines)

25
source/file_test.go Normal file
View file

@ -0,0 +1,25 @@
package source
import (
"github.com/spf13/hugo/helpers"
"github.com/stretchr/testify/assert"
"testing"
)
func TestFileUniqueID(t *testing.T) {
f1 := File{uniqueID: "123"}
f2 := NewFile("a")
assert.Equal(t, "123", f1.UniqueID())
assert.Equal(t, "0cc175b9c0f1b6a831c399e269772661", f2.UniqueID())
}
func TestFileString(t *testing.T) {
assert.Equal(t, "abc", NewFileWithContents("a", helpers.StringToReader("abc")).String())
assert.Equal(t, "", NewFile("a").String())
}
func TestFileBytes(t *testing.T) {
assert.Equal(t, []byte("abc"), NewFileWithContents("a", helpers.StringToReader("abc")).Bytes())
assert.Equal(t, []byte(""), NewFile("a").Bytes())
}