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