...
Run Format

Source file src/encoding/csv/writer_test.go

Documentation: encoding/csv

     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  	Error   error
    17  	UseCRLF bool
    18  	Comma   rune
    19  }{
    20  	{Input: [][]string{{"abc"}}, Output: "abc\n"},
    21  	{Input: [][]string{{"abc"}}, Output: "abc\r\n", UseCRLF: true},
    22  	{Input: [][]string{{`"abc"`}}, Output: `"""abc"""` + "\n"},
    23  	{Input: [][]string{{`a"b`}}, Output: `"a""b"` + "\n"},
    24  	{Input: [][]string{{`"a"b"`}}, Output: `"""a""b"""` + "\n"},
    25  	{Input: [][]string{{" abc"}}, Output: `" abc"` + "\n"},
    26  	{Input: [][]string{{"abc,def"}}, Output: `"abc,def"` + "\n"},
    27  	{Input: [][]string{{"abc", "def"}}, Output: "abc,def\n"},
    28  	{Input: [][]string{{"abc"}, {"def"}}, Output: "abc\ndef\n"},
    29  	{Input: [][]string{{"abc\ndef"}}, Output: "\"abc\ndef\"\n"},
    30  	{Input: [][]string{{"abc\ndef"}}, Output: "\"abc\r\ndef\"\r\n", UseCRLF: true},
    31  	{Input: [][]string{{"abc\rdef"}}, Output: "\"abcdef\"\r\n", UseCRLF: true},
    32  	{Input: [][]string{{"abc\rdef"}}, Output: "\"abc\rdef\"\n", UseCRLF: false},
    33  	{Input: [][]string{{""}}, Output: "\n"},
    34  	{Input: [][]string{{"", ""}}, Output: ",\n"},
    35  	{Input: [][]string{{"", "", ""}}, Output: ",,\n"},
    36  	{Input: [][]string{{"", "", "a"}}, Output: ",,a\n"},
    37  	{Input: [][]string{{"", "a", ""}}, Output: ",a,\n"},
    38  	{Input: [][]string{{"", "a", "a"}}, Output: ",a,a\n"},
    39  	{Input: [][]string{{"a", "", ""}}, Output: "a,,\n"},
    40  	{Input: [][]string{{"a", "", "a"}}, Output: "a,,a\n"},
    41  	{Input: [][]string{{"a", "a", ""}}, Output: "a,a,\n"},
    42  	{Input: [][]string{{"a", "a", "a"}}, Output: "a,a,a\n"},
    43  	{Input: [][]string{{`\.`}}, Output: "\"\\.\"\n"},
    44  	{Input: [][]string{{"x09\x41\xb4\x1c", "aktau"}}, Output: "x09\x41\xb4\x1c,aktau\n"},
    45  	{Input: [][]string{{",x09\x41\xb4\x1c", "aktau"}}, Output: "\",x09\x41\xb4\x1c\",aktau\n"},
    46  	{Input: [][]string{{"a", "a", ""}}, Output: "a|a|\n", Comma: '|'},
    47  	{Input: [][]string{{",", ",", ""}}, Output: ",|,|\n", Comma: '|'},
    48  	{Input: [][]string{{"foo"}}, Comma: '"', Error: errInvalidDelim},
    49  }
    50  
    51  func TestWrite(t *testing.T) {
    52  	for n, tt := range writeTests {
    53  		b := &bytes.Buffer{}
    54  		f := NewWriter(b)
    55  		f.UseCRLF = tt.UseCRLF
    56  		if tt.Comma != 0 {
    57  			f.Comma = tt.Comma
    58  		}
    59  		err := f.WriteAll(tt.Input)
    60  		if err != tt.Error {
    61  			t.Errorf("Unexpected error:\ngot  %v\nwant %v", err, tt.Error)
    62  		}
    63  		out := b.String()
    64  		if out != tt.Output {
    65  			t.Errorf("#%d: out=%q want %q", n, out, tt.Output)
    66  		}
    67  	}
    68  }
    69  
    70  type errorWriter struct{}
    71  
    72  func (e errorWriter) Write(b []byte) (int, error) {
    73  	return 0, errors.New("Test")
    74  }
    75  
    76  func TestError(t *testing.T) {
    77  	b := &bytes.Buffer{}
    78  	f := NewWriter(b)
    79  	f.Write([]string{"abc"})
    80  	f.Flush()
    81  	err := f.Error()
    82  
    83  	if err != nil {
    84  		t.Errorf("Unexpected error: %s\n", err)
    85  	}
    86  
    87  	f = NewWriter(errorWriter{})
    88  	f.Write([]string{"abc"})
    89  	f.Flush()
    90  	err = f.Error()
    91  
    92  	if err == nil {
    93  		t.Error("Error should not be nil")
    94  	}
    95  }
    96  

View as plain text