Files
dolt/samples/go/csv/csv_reader_test.go
Ben Kalman 03b7221c36 Use stretchr/testify not attic-labs/testify (#3677)
stretchr has fixed a bug with the -count flag. I could merge these
changes into attic-labs, but it's easier to just use strechr.

We forked stretchr a long time ago so that we didn't link in the HTTP
testing libraries into the noms binaries (because we were using d.Chk in
production code). The HTTP issue doesn't seem to happen anymore, even
though we're still using d.Chk.
2017-09-07 15:01:03 -07:00

84 lines
2.2 KiB
Go

// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package csv
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCR(t *testing.T) {
testFile := []byte("a,b,c\r1,2,3\r")
delimiter, err := StringToRune(",")
r := NewCSVReader(bytes.NewReader(testFile), delimiter)
lines, err := r.ReadAll()
assert.NoError(t, err, "An error occurred while reading the data: %v", err)
if len(lines) != 2 {
t.Errorf("Wrong number of lines. Expected 2, got %d", len(lines))
}
}
func TestLF(t *testing.T) {
testFile := []byte("a,b,c\n1,2,3\n")
delimiter, err := StringToRune(",")
r := NewCSVReader(bytes.NewReader(testFile), delimiter)
lines, err := r.ReadAll()
assert.NoError(t, err, "An error occurred while reading the data: %v", err)
if len(lines) != 2 {
t.Errorf("Wrong number of lines. Expected 2, got %d", len(lines))
}
}
func TestCRLF(t *testing.T) {
testFile := []byte("a,b,c\r\n1,2,3\r\n")
delimiter, err := StringToRune(",")
r := NewCSVReader(bytes.NewReader(testFile), delimiter)
lines, err := r.ReadAll()
assert.NoError(t, err, "An error occurred while reading the data: %v", err)
if len(lines) != 2 {
t.Errorf("Wrong number of lines. Expected 2, got %d", len(lines))
}
}
func TestCRInQuote(t *testing.T) {
testFile := []byte("a,\"foo,\rbar\",c\r1,\"2\r\n2\",3\r")
delimiter, err := StringToRune(",")
r := NewCSVReader(bytes.NewReader(testFile), delimiter)
lines, err := r.ReadAll()
assert.NoError(t, err, "An error occurred while reading the data: %v", err)
if len(lines) != 2 {
t.Errorf("Wrong number of lines. Expected 2, got %d", len(lines))
}
if strings.Contains(lines[1][1], "\n\n") {
t.Error("The CRLF was converted to a LFLF")
}
}
func TestCRLFEndOfBufferLength(t *testing.T) {
testFile := make([]byte, 4096*2, 4096*2)
testFile[4095] = 13 // \r byte
testFile[4096] = 10 // \n byte
delimiter, err := StringToRune(",")
r := NewCSVReader(bytes.NewReader(testFile), delimiter)
lines, err := r.ReadAll()
assert.NoError(t, err, "An error occurred while reading the data: %v", err)
if len(lines) != 2 {
t.Errorf("Wrong number of lines. Expected 2, got %d", len(lines))
}
}