2018-05-15 18:52:11 +00:00
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2018-11-29 16:31:48 +00:00
|
|
|
"github.com/dimchansky/utfbom"
|
2018-05-15 18:52:11 +00:00
|
|
|
"io/ioutil"
|
2018-11-29 16:31:48 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-05-15 18:52:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCRFilter(t *testing.T) {
|
|
|
|
sr := strings.NewReader("line 1\r\nline 2")
|
2018-11-29 16:31:48 +00:00
|
|
|
cr := carriageReturnFilteringReader{r: sr}
|
2018-05-15 18:52:11 +00:00
|
|
|
b, err := ioutil.ReadAll(cr)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(b) != "line 1\nline 2" {
|
|
|
|
t.Errorf("Unexpected output %q", b)
|
|
|
|
}
|
2018-11-29 16:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckBOM(t *testing.T) {
|
|
|
|
testdata := []struct {
|
|
|
|
encoding utfbom.Encoding
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{utfbom.Unknown, ""},
|
|
|
|
{utfbom.UTF8, ""},
|
|
|
|
{utfbom.UTF16BigEndian, "UTF16BigEndian"},
|
|
|
|
{utfbom.UTF16LittleEndian, "UTF16LittleEndian"},
|
|
|
|
{utfbom.UTF32BigEndian, "UTF32BigEndian"},
|
|
|
|
{utfbom.UTF32LittleEndian, "UTF32LittleEndian"},
|
|
|
|
}
|
|
|
|
for _, d := range testdata {
|
|
|
|
err := checkBOM(d.encoding)
|
|
|
|
if d.err == "" && err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if d.err != "" && err == nil {
|
|
|
|
t.Errorf("Missing expected error %s", d.err)
|
|
|
|
}
|
|
|
|
if err != nil && !strings.Contains(err.Error(), d.err) {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|