Skip to content

Commit

Permalink
Add test for custom separator
Browse files Browse the repository at this point in the history
Signed-off-by: Aswin Karthik <[email protected]>
  • Loading branch information
aswinkarthik committed Feb 21, 2020
1 parent bf510ac commit 731e773
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 39 deletions.
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 44 additions & 37 deletions pkg/digest/diff_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit 731e773

Please sign in to comment.