...

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

View as plain text