|
| 1 | +package csvdecoder |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestParserIgnoreUnmatchingFields(t *testing.T) { |
| 10 | + var strVal string |
| 11 | + var intVal int |
| 12 | + var anotherIntVal int |
| 13 | + |
| 14 | + for _, tc := range []struct { |
| 15 | + name string |
| 16 | + config ParserConfig |
| 17 | + data string |
| 18 | + scanTargets []interface{} |
| 19 | + expectedError error |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "should work when numbers match and flag is false", |
| 23 | + config: ParserConfig{ |
| 24 | + IgnoreUnmatchingFields: false, |
| 25 | + }, |
| 26 | + data: "rec,2\n", |
| 27 | + scanTargets: []interface{}{&strVal, &intVal}, |
| 28 | + expectedError: nil, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "should work when numbers match and flag is true", |
| 32 | + config: ParserConfig{ |
| 33 | + IgnoreUnmatchingFields: true, |
| 34 | + }, |
| 35 | + data: "rec,2\n", |
| 36 | + scanTargets: []interface{}{&strVal, &intVal}, |
| 37 | + expectedError: nil, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "should work when numbers match with default config", |
| 41 | + config: ParserConfig{}, |
| 42 | + data: "rec,2\n", |
| 43 | + scanTargets: []interface{}{&strVal, &intVal}, |
| 44 | + expectedError: nil, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "should work with more targets when the flag is true", |
| 48 | + config: ParserConfig{ |
| 49 | + IgnoreUnmatchingFields: true, |
| 50 | + }, |
| 51 | + data: "rec,2\n", |
| 52 | + scanTargets: []interface{}{&strVal, &intVal, &anotherIntVal}, |
| 53 | + expectedError: nil, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "should work with more records when the flag is true", |
| 57 | + config: ParserConfig{ |
| 58 | + IgnoreUnmatchingFields: true, |
| 59 | + }, |
| 60 | + data: "rec,2\n", |
| 61 | + scanTargets: []interface{}{&strVal}, |
| 62 | + expectedError: nil, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "should fail with more targets when the flag is false", |
| 66 | + config: ParserConfig{ |
| 67 | + IgnoreUnmatchingFields: false, |
| 68 | + }, |
| 69 | + data: "rec,2\n", |
| 70 | + scanTargets: []interface{}{&strVal, &intVal, &anotherIntVal}, |
| 71 | + expectedError: ErrScanTargetsNotMatch, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "should fail with more records when the flag is false", |
| 75 | + config: ParserConfig{ |
| 76 | + IgnoreUnmatchingFields: false, |
| 77 | + }, |
| 78 | + data: "rec,2\n", |
| 79 | + scanTargets: []interface{}{&strVal}, |
| 80 | + expectedError: ErrScanTargetsNotMatch, |
| 81 | + }, |
| 82 | + } { |
| 83 | + tc := tc |
| 84 | + t.Run(tc.name, func(t *testing.T) { |
| 85 | + parser, err := NewParserWithConfig(strings.NewReader(tc.data), tc.config) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("could not create parser: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + for parser.Next() { |
| 91 | + if err := parser.Scan(tc.scanTargets...); !errors.Is(err, tc.expectedError) { |
| 92 | + t.Errorf("expected '%s', got '%v'", tc.expectedError, err) |
| 93 | + } |
| 94 | + } |
| 95 | + if parser.Err() != nil { |
| 96 | + t.Error(err) |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
0 commit comments