github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/encoding/csv/writer_test.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 // Copyright 2011 The Go Authors. All rights reserved. 12 // Use of this source code is governed by a BSD-style 13 // license that can be found in licenses/BSD-golang.txt. 14 15 package csv 16 17 import ( 18 "bytes" 19 "testing" 20 21 "github.com/cockroachdb/errors" 22 ) 23 24 var writeTests = []struct { 25 Input [][]string 26 Output string 27 UseCRLF bool 28 }{ 29 {Input: [][]string{{"abc"}}, Output: "abc\n"}, 30 {Input: [][]string{{"abc"}}, Output: "abc\r\n", UseCRLF: true}, 31 {Input: [][]string{{`"abc"`}}, Output: `"""abc"""` + "\n"}, 32 {Input: [][]string{{`a"b`}}, Output: `"a""b"` + "\n"}, 33 {Input: [][]string{{`"a"b"`}}, Output: `"""a""b"""` + "\n"}, 34 {Input: [][]string{{" abc"}}, Output: `" abc"` + "\n"}, 35 {Input: [][]string{{"abc,def"}}, Output: `"abc,def"` + "\n"}, 36 {Input: [][]string{{"abc", "def"}}, Output: "abc,def\n"}, 37 {Input: [][]string{{"abc"}, {"def"}}, Output: "abc\ndef\n"}, 38 {Input: [][]string{{"abc\ndef"}}, Output: "\"abc\ndef\"\n"}, 39 {Input: [][]string{{"abc\ndef"}}, Output: "\"abc\r\ndef\"\r\n", UseCRLF: true}, 40 {Input: [][]string{{"abc\rdef"}}, Output: "\"abcdef\"\r\n", UseCRLF: true}, 41 {Input: [][]string{{"abc\rdef"}}, Output: "\"abc\rdef\"\n", UseCRLF: false}, 42 {Input: [][]string{{""}}, Output: "\n"}, 43 {Input: [][]string{{"", ""}}, Output: ",\n"}, 44 {Input: [][]string{{"", "", ""}}, Output: ",,\n"}, 45 {Input: [][]string{{"", "", "a"}}, Output: ",,a\n"}, 46 {Input: [][]string{{"", "a", ""}}, Output: ",a,\n"}, 47 {Input: [][]string{{"", "a", "a"}}, Output: ",a,a\n"}, 48 {Input: [][]string{{"a", "", ""}}, Output: "a,,\n"}, 49 {Input: [][]string{{"a", "", "a"}}, Output: "a,,a\n"}, 50 {Input: [][]string{{"a", "a", ""}}, Output: "a,a,\n"}, 51 {Input: [][]string{{"a", "a", "a"}}, Output: "a,a,a\n"}, 52 {Input: [][]string{{`\.`}}, Output: "\"\\.\"\n"}, 53 } 54 55 func TestWrite(t *testing.T) { 56 for n, tt := range writeTests { 57 b := &bytes.Buffer{} 58 f := NewWriter(b) 59 f.UseCRLF = tt.UseCRLF 60 err := f.WriteAll(tt.Input) 61 if err != nil { 62 t.Errorf("Unexpected error: %s\n", err) 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 if err := f.Write([]string{"abc"}); err != nil { 81 t.Errorf("Unexpected error: %s\n", err) 82 } 83 f.Flush() 84 err := f.Error() 85 86 if err != nil { 87 t.Errorf("Unexpected error: %s\n", err) 88 } 89 90 f = NewWriter(errorWriter{}) 91 if err := f.Write([]string{"abc"}); err != nil { 92 t.Errorf("Unexpected error: %s\n", err) 93 } 94 f.Flush() 95 err = f.Error() 96 97 if err == nil { 98 t.Error("Error should not be nil") 99 } 100 }