github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/encoding/csv/writer_test.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package csv
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"testing"
    11  )
    12  
    13  var writeTests = []struct {
    14  	Input   [][]string
    15  	Output  string
    16  	UseCRLF bool
    17  }{
    18  	{Input: [][]string{{"abc"}}, Output: "abc\n"},
    19  	{Input: [][]string{{"abc"}}, Output: "abc\r\n", UseCRLF: true},
    20  	{Input: [][]string{{`"abc"`}}, Output: `"""abc"""` + "\n"},
    21  	{Input: [][]string{{`a"b`}}, Output: `"a""b"` + "\n"},
    22  	{Input: [][]string{{`"a"b"`}}, Output: `"""a""b"""` + "\n"},
    23  	{Input: [][]string{{" abc"}}, Output: `" abc"` + "\n"},
    24  	{Input: [][]string{{"abc,def"}}, Output: `"abc,def"` + "\n"},
    25  	{Input: [][]string{{"abc", "def"}}, Output: "abc,def\n"},
    26  	{Input: [][]string{{"abc"}, {"def"}}, Output: "abc\ndef\n"},
    27  	{Input: [][]string{{"abc\ndef"}}, Output: "\"abc\ndef\"\n"},
    28  	{Input: [][]string{{"abc\ndef"}}, Output: "\"abc\r\ndef\"\r\n", UseCRLF: true},
    29  	{Input: [][]string{{"abc\rdef"}}, Output: "\"abcdef\"\r\n", UseCRLF: true},
    30  	{Input: [][]string{{"abc\rdef"}}, Output: "\"abc\rdef\"\n", UseCRLF: false},
    31  	{Input: [][]string{{""}}, Output: "\n"},
    32  	{Input: [][]string{{"", ""}}, Output: ",\n"},
    33  	{Input: [][]string{{"", "", ""}}, Output: ",,\n"},
    34  	{Input: [][]string{{"", "", "a"}}, Output: ",,a\n"},
    35  	{Input: [][]string{{"", "a", ""}}, Output: ",a,\n"},
    36  	{Input: [][]string{{"", "a", "a"}}, Output: ",a,a\n"},
    37  	{Input: [][]string{{"a", "", ""}}, Output: "a,,\n"},
    38  	{Input: [][]string{{"a", "", "a"}}, Output: "a,,a\n"},
    39  	{Input: [][]string{{"a", "a", ""}}, Output: "a,a,\n"},
    40  	{Input: [][]string{{"a", "a", "a"}}, Output: "a,a,a\n"},
    41  	{Input: [][]string{{`\.`}}, Output: "\"\\.\"\n"},
    42  }
    43  
    44  func TestWrite(t *testing.T) {
    45  	for n, tt := range writeTests {
    46  		b := &bytes.Buffer{}
    47  		f := NewWriter(b)
    48  		f.UseCRLF = tt.UseCRLF
    49  		err := f.WriteAll(tt.Input)
    50  		if err != nil {
    51  			t.Errorf("Unexpected error: %s\n", err)
    52  		}
    53  		out := b.String()
    54  		if out != tt.Output {
    55  			t.Errorf("#%d: out=%q want %q", n, out, tt.Output)
    56  		}
    57  	}
    58  }
    59  
    60  type errorWriter struct{}
    61  
    62  func (e errorWriter) Write(b []byte) (int, error) {
    63  	return 0, errors.New("Test")
    64  }
    65  
    66  func TestError(t *testing.T) {
    67  	b := &bytes.Buffer{}
    68  	f := NewWriter(b)
    69  	f.Write([]string{"abc"})
    70  	f.Flush()
    71  	err := f.Error()
    72  
    73  	if err != nil {
    74  		t.Errorf("Unexpected error: %s\n", err)
    75  	}
    76  
    77  	f = NewWriter(errorWriter{})
    78  	f.Write([]string{"abc"})
    79  	f.Flush()
    80  	err = f.Error()
    81  
    82  	if err == nil {
    83  		t.Error("Error should not be nil")
    84  	}
    85  }