diff --git a/cmd/root.go b/cmd/root.go index 3f11844..25110d2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -149,14 +149,13 @@ func timeTrack(start time.Time, name string) { } func parseSeparator(sep string) (rune, error) { - if strings.HasPrefix(sep, "\\t") { return '\t', nil } runesep, _ := utf8.DecodeRuneInString(sep) if runesep == utf8.RuneError { - return ' ', fmt.Errorf("Unable to use %v (%q) as a separator", separator, separator) + return ' ', fmt.Errorf("unable to use %v (%q) as a separator", separator, separator) } return runesep, nil diff --git a/pkg/digest/diff_test.go b/pkg/digest/diff_test.go index 65ed88f..c0d31a3 100644 --- a/pkg/digest/diff_test.go +++ b/pkg/digest/diff_test.go @@ -1,11 +1,12 @@ package digest_test import ( - "strings" - "testing" - + "fmt" "github.com/aswinkarthik/csvdiff/pkg/digest" "github.com/stretchr/testify/assert" + "strings" + "testing" + "unicode/utf8" ) func TestDiff(t *testing.T) { @@ -23,43 +24,49 @@ func TestDiff(t *testing.T) { ` t.Run("default config", func(t *testing.T) { - baseConfig := &digest.Config{ - Reader: strings.NewReader(base), - Key: []int{0}, - Separator: ',', - LazyQuotes: false, - } + separators := []string{",", "\t", "|"} + for _, sep := range separators { + t.Run(fmt.Sprintf("should support \"%s\" as separator", sep), func(t *testing.T) { + sepRune, _ := utf8.DecodeRuneInString(sep) + baseConfig := &digest.Config{ + Reader: strings.NewReader(strings.ReplaceAll(base, ",", sep)), + Key: []int{0}, + Separator: sepRune, + LazyQuotes: false, + } - deltaConfig := &digest.Config{ - Reader: strings.NewReader(delta), - Key: []int{0}, - Separator: ',', - LazyQuotes: false, - } + deltaConfig := &digest.Config{ + Reader: strings.NewReader(strings.ReplaceAll(delta,",", sep)), + Key: []int{0}, + Separator: sepRune, + LazyQuotes: false, + } - expected := digest.Differences{ - Additions: []digest.Addition{ - strings.Split("4,col-1,col-2,col-3,four-value-added", ","), - strings.Split("5,col-1,col-2,col-3,five-value-added", ","), - }, - Modifications: []digest.Modification{ - { - Current: strings.Split("2,col-1,col-2,col-3,two-value-modified", ","), - Original: strings.Split("2,col-1,col-2,col-3,two-value", ","), - }, - { - Current: strings.Split("100,col-1-modified,col-2,col-3,hundred-value-modified", ","), - Original: strings.Split("100,col-1,col-2,col-3,hundred-value", ","), - }, - }, - Deletions: []digest.Deletion{ - strings.Split("3,col-1,col-2,col-3,three-value", ","), - }, - } + expected := digest.Differences{ + Additions: []digest.Addition{ + strings.Split("4,col-1,col-2,col-3,four-value-added", ","), + strings.Split("5,col-1,col-2,col-3,five-value-added", ","), + }, + Modifications: []digest.Modification{ + { + Current: strings.Split("2,col-1,col-2,col-3,two-value-modified", ","), + Original: strings.Split("2,col-1,col-2,col-3,two-value", ","), + }, + { + Current: strings.Split("100,col-1-modified,col-2,col-3,hundred-value-modified", ","), + Original: strings.Split("100,col-1,col-2,col-3,hundred-value", ","), + }, + }, + Deletions: []digest.Deletion{ + strings.Split("3,col-1,col-2,col-3,three-value", ","), + }, + } - actual, err := digest.Diff(*baseConfig, *deltaConfig) - assert.NoError(t, err) - assert.Equal(t, expected, actual) + actual, err := digest.Diff(*baseConfig, *deltaConfig) + assert.NoError(t, err) + assert.Equal(t, expected, actual) + }) + } }) deltaLazyQuotes := `1,col-1,col-2,col-3,one-value