2017-02-18 22:46:11 +00:00
|
|
|
package tools
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type QuotedFieldsTestCase struct {
|
|
|
|
Given string
|
|
|
|
Expected []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *QuotedFieldsTestCase) Assert(t *testing.T) {
|
|
|
|
actual := QuotedFields(c.Given)
|
|
|
|
|
|
|
|
assert.Equal(t, c.Expected, actual,
|
|
|
|
"tools: expected QuotedFields(%q) to equal %#v (was %#v)",
|
|
|
|
c.Given, c.Expected, actual,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotedFields(t *testing.T) {
|
2017-02-19 20:02:39 +00:00
|
|
|
for desc, c := range map[string]QuotedFieldsTestCase{
|
2017-02-21 18:50:55 +00:00
|
|
|
"simple": {`foo bar`, []string{"foo", "bar"}},
|
|
|
|
"simple trailing": {`foo bar `, []string{"foo", "bar"}},
|
|
|
|
"simple leading": {` foo bar`, []string{"foo", "bar"}},
|
2017-02-19 20:02:39 +00:00
|
|
|
|
2017-02-21 18:50:55 +00:00
|
|
|
"single quotes": {`foo 'bar baz'`, []string{"foo", "bar baz"}},
|
|
|
|
"single quotes trailing": {`foo 'bar baz' `, []string{"foo", "bar baz"}},
|
|
|
|
"single quotes leading": {` foo 'bar baz'`, []string{"foo", "bar baz"}},
|
2017-02-19 20:02:39 +00:00
|
|
|
|
2017-02-21 18:50:55 +00:00
|
|
|
"single quotes empty": {`foo ''`, []string{"foo", ""}},
|
|
|
|
"single quotes trailing empty": {`foo '' `, []string{"foo", ""}},
|
|
|
|
"single quotes leading empty": {` foo ''`, []string{"foo", ""}},
|
2017-02-21 18:45:52 +00:00
|
|
|
|
2017-02-21 18:50:55 +00:00
|
|
|
"double quotes": {`foo "bar baz"`, []string{"foo", "bar baz"}},
|
|
|
|
"double quotes trailing": {`foo "bar baz" `, []string{"foo", "bar baz"}},
|
|
|
|
"double quotes leading": {` foo "bar baz"`, []string{"foo", "bar baz"}},
|
2017-02-19 20:02:39 +00:00
|
|
|
|
2017-02-21 18:50:55 +00:00
|
|
|
"double quotes empty": {`foo ""`, []string{"foo", ""}},
|
|
|
|
"double quotes trailing empty": {`foo "" `, []string{"foo", ""}},
|
|
|
|
"double quotes leading empty": {` foo ""`, []string{"foo", ""}},
|
2017-02-21 18:45:52 +00:00
|
|
|
|
2017-02-21 18:50:55 +00:00
|
|
|
"nested single quotes": {`foo 'bar 'baz''`, []string{"foo", "bar 'baz'"}},
|
|
|
|
"nested single quotes trailing": {`foo 'bar 'baz'' `, []string{"foo", "bar 'baz'"}},
|
|
|
|
"nested single quotes leading": {` foo 'bar 'baz''`, []string{"foo", "bar 'baz'"}},
|
2017-02-19 20:02:39 +00:00
|
|
|
|
2017-02-21 18:50:55 +00:00
|
|
|
"nested single quotes empty": {`foo 'bar '''`, []string{"foo", "bar ''"}},
|
|
|
|
"nested single quotes trailing empty": {`foo 'bar ''' `, []string{"foo", "bar ''"}},
|
|
|
|
"nested single quotes leading empty": {` foo 'bar '''`, []string{"foo", "bar ''"}},
|
2017-02-21 18:45:52 +00:00
|
|
|
|
2017-02-21 18:50:55 +00:00
|
|
|
"nested double quotes": {`foo "bar "baz""`, []string{"foo", `bar "baz"`}},
|
|
|
|
"nested double quotes trailing": {`foo "bar "baz"" `, []string{"foo", `bar "baz"`}},
|
|
|
|
"nested double quotes leading": {` foo "bar "baz""`, []string{"foo", `bar "baz"`}},
|
2017-02-19 20:02:39 +00:00
|
|
|
|
2017-02-21 18:50:55 +00:00
|
|
|
"nested double quotes empty": {`foo "bar """`, []string{"foo", `bar ""`}},
|
|
|
|
"nested double quotes trailing empty": {`foo "bar """ `, []string{"foo", `bar ""`}},
|
|
|
|
"nested double quotes leading empty": {` foo "bar """`, []string{"foo", `bar ""`}},
|
2017-02-21 18:45:52 +00:00
|
|
|
|
2017-02-21 18:50:55 +00:00
|
|
|
"mixed quotes": {`foo 'bar "baz"'`, []string{"foo", `bar "baz"`}},
|
|
|
|
"mixed quotes trailing": {`foo 'bar "baz"' `, []string{"foo", `bar "baz"`}},
|
|
|
|
"mixed quotes leading": {` foo 'bar "baz"'`, []string{"foo", `bar "baz"`}},
|
2017-02-21 18:45:52 +00:00
|
|
|
|
2017-02-21 18:50:55 +00:00
|
|
|
"mixed quotes empty": {`foo 'bar ""'`, []string{"foo", `bar ""`}},
|
|
|
|
"mixed quotes trailing empty": {`foo 'bar ""' `, []string{"foo", `bar ""`}},
|
|
|
|
"mixed quotes leading empty": {` foo 'bar ""'`, []string{"foo", `bar ""`}},
|
2017-02-18 22:46:11 +00:00
|
|
|
} {
|
2017-02-21 16:53:20 +00:00
|
|
|
t.Log(desc)
|
|
|
|
c.Assert(t)
|
2017-02-18 22:46:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-13 20:52:08 +00:00
|
|
|
|
|
|
|
func TestLongestReturnsEmptyStringGivenEmptySet(t *testing.T) {
|
|
|
|
assert.Equal(t, "", Longest(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLongestReturnsLongestString(t *testing.T) {
|
|
|
|
assert.Equal(t, "longest", []string{"short", "longer", "longest"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLongestReturnsLastStringGivenSameLength(t *testing.T) {
|
|
|
|
assert.Equal(t, "baz", []string{"foo", "bar", "baz"})
|
|
|
|
}
|
2017-06-13 20:52:21 +00:00
|
|
|
|
|
|
|
func TestRjustRightJustifiesString(t *testing.T) {
|
|
|
|
unjust := []string{
|
|
|
|
"short",
|
|
|
|
"longer",
|
|
|
|
"longest",
|
|
|
|
}
|
|
|
|
expected := []string{
|
|
|
|
" short",
|
|
|
|
" longer",
|
|
|
|
"longest",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, Rjust(unjust))
|
|
|
|
}
|