gitee.com/quant1x/pkg@v0.2.8/gocsv/types_test.go (about) 1 package gocsv 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 type sampleTypeUnmarshaller struct { 9 val string 10 } 11 12 func (s *sampleTypeUnmarshaller) UnmarshalCSV(val string) error { 13 s.val = val 14 return nil 15 } 16 17 func (s sampleTypeUnmarshaller) MarshalCSV() (string, error) { 18 return s.val, nil 19 } 20 21 type sampleTextUnmarshaller struct { 22 val []byte 23 } 24 25 func (s *sampleTextUnmarshaller) UnmarshalText(text []byte) error { 26 s.val = text 27 return nil 28 } 29 30 func (s sampleTextUnmarshaller) MarshalText() ([]byte, error) { 31 return s.val, nil 32 } 33 34 type sampleStringer string 35 36 func (s sampleStringer) String() string { 37 return string(s) 38 } 39 40 type stringAlias string 41 type customStringAlias string 42 43 func (s customStringAlias) MarshalCSV() (string, error) { 44 return `"` + string(s) + `"`, nil 45 } 46 47 func Test_getFieldAsString_CustomStringAlias(t *testing.T) { 48 s, err := getFieldAsString(reflect.ValueOf(customStringAlias("foo"))) 49 if err != nil { 50 t.Fatalf("getFieldAsString failure: %s", err) 51 } 52 53 if string(s) != `"foo"` { 54 t.Fatalf(`expected "foo" got %s`, s) 55 } 56 57 s, err = getFieldAsString(reflect.ValueOf(stringAlias("foo"))) 58 if err != nil { 59 t.Fatalf("getFieldAsString failure: %s", err) 60 } 61 62 if string(s) != `foo` { 63 t.Fatalf(`expected foo got %s`, s) 64 } 65 } 66 67 func Benchmark_unmarshall_TypeUnmarshaller(b *testing.B) { 68 sample := sampleTypeUnmarshaller{} 69 val := reflect.ValueOf(&sample) 70 for n := 0; n < b.N; n++ { 71 if err := unmarshall(val, "foo"); err != nil { 72 b.Fatalf("unmarshall error: %s", err.Error()) 73 } 74 } 75 } 76 77 func Benchmark_unmarshall_TextUnmarshaller(b *testing.B) { 78 sample := sampleTextUnmarshaller{} 79 val := reflect.ValueOf(&sample) 80 for n := 0; n < b.N; n++ { 81 if err := unmarshall(val, "foo"); err != nil { 82 b.Fatalf("unmarshall error: %s", err.Error()) 83 } 84 } 85 } 86 87 func Benchmark_marshall_TypeMarshaller(b *testing.B) { 88 sample := sampleTypeUnmarshaller{"foo"} 89 val := reflect.ValueOf(&sample) 90 for n := 0; n < b.N; n++ { 91 _, err := marshall(val) 92 if err != nil { 93 b.Fatalf("marshall error: %s", err.Error()) 94 } 95 } 96 } 97 98 func Benchmark_marshall_TextMarshaller(b *testing.B) { 99 sample := sampleTextUnmarshaller{[]byte("foo")} 100 val := reflect.ValueOf(&sample) 101 for n := 0; n < b.N; n++ { 102 _, err := marshall(val) 103 if err != nil { 104 b.Fatalf("marshall error: %s", err.Error()) 105 } 106 } 107 } 108 109 func Benchmark_marshall_Stringer(b *testing.B) { 110 sample := sampleStringer("foo") 111 val := reflect.ValueOf(&sample) 112 for n := 0; n < b.N; n++ { 113 _, err := marshall(val) 114 if err != nil { 115 b.Fatalf("marshall error: %s", err.Error()) 116 } 117 } 118 } 119 120 func TestToInt(t *testing.T) { 121 TestCase := []struct { 122 field string 123 result int 124 err error 125 }{ 126 {"123.2", 123, nil}, 127 {"123", 123, nil}, 128 {"1.2.3", 1, nil}, 129 {"0.123", 0, nil}, 130 {"5.25051E+07", 52505100, nil}, 131 } 132 //v := 5.25051e+07 133 134 for idx, item := range TestCase { 135 out, err := toInt(item.field) 136 if err != nil { 137 t.Fatal(err) 138 } 139 if int(out) != item.result { 140 t.Fatal(idx, "result not equal") 141 } 142 } 143 }