| 1 | package tools |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/stretchr/testify/assert" |
| 7 | ) |
| 8 | |
| 9 | type QuotedFieldsTestCase struct { |
| 10 | Given string |
| 11 | Expected []string |
| 12 | } |
| 13 | |
| 14 | func (c *QuotedFieldsTestCase) Assert(t *testing.T) { |
| 15 | actual := QuotedFields(c.Given) |
| 16 | |
| 17 | assert.Equal(t, c.Expected, actual, |
| 18 | "tools: expected QuotedFields(%q) to equal %#v (was %#v)", |
| 19 | c.Given, c.Expected, actual, |
| 20 | ) |
| 21 | } |
| 22 | |
| 23 | func TestQuotedFields(t *testing.T) { |
| 24 | for desc, c := range map[string]QuotedFieldsTestCase{ |
| 25 | "simple": {`foo bar`, []string{"foo", "bar"}}, |
| 26 | "simple trailing": {`foo bar `, []string{"foo", "bar"}}, |
| 27 | "simple leading": {` foo bar`, []string{"foo", "bar"}}, |
| 28 | |
| 29 | "single quotes": {`foo 'bar baz'`, []string{"foo", "bar baz"}}, |
| 30 | "single quotes trailing": {`foo 'bar baz' `, []string{"foo", "bar baz"}}, |
| 31 | "single quotes leading": {` foo 'bar baz'`, []string{"foo", "bar baz"}}, |
| 32 | |
| 33 | "single quotes empty": {`foo ''`, []string{"foo", ""}}, |
| 34 | "single quotes trailing empty": {`foo '' `, []string{"foo", ""}}, |
| 35 | "single quotes leading empty": {` foo ''`, []string{"foo", ""}}, |
| 36 | |
| 37 | "double quotes": {`foo "bar baz"`, []string{"foo", "bar baz"}}, |
| 38 | "double quotes trailing": {`foo "bar baz" `, []string{"foo", "bar baz"}}, |
| 39 | "double quotes leading": {` foo "bar baz"`, []string{"foo", "bar baz"}}, |
| 40 | |
| 41 | "double quotes empty": {`foo ""`, []string{"foo", ""}}, |
| 42 | "double quotes trailing empty": {`foo "" `, []string{"foo", ""}}, |
| 43 | "double quotes leading empty": {` foo ""`, []string{"foo", ""}}, |
| 44 | |
| 45 | "nested single quotes": {`foo 'bar 'baz''`, []string{"foo", "bar 'baz'"}}, |
| 46 | "nested single quotes trailing": {`foo 'bar 'baz'' `, []string{"foo", "bar 'baz'"}}, |
| 47 | "nested single quotes leading": {` foo 'bar 'baz''`, []string{"foo", "bar 'baz'"}}, |
| 48 | |
| 49 | "nested single quotes empty": {`foo 'bar '''`, []string{"foo", "bar ''"}}, |
| 50 | "nested single quotes trailing empty": {`foo 'bar ''' `, []string{"foo", "bar ''"}}, |
| 51 | "nested single quotes leading empty": {` foo 'bar '''`, []string{"foo", "bar ''"}}, |
| 52 | |
| 53 | "nested double quotes": {`foo "bar "baz""`, []string{"foo", `bar "baz"`}}, |
| 54 | "nested double quotes trailing": {`foo "bar "baz"" `, []string{"foo", `bar "baz"`}}, |
| 55 | "nested double quotes leading": {` foo "bar "baz""`, []string{"foo", `bar "baz"`}}, |
| 56 | |
| 57 | "nested double quotes empty": {`foo "bar """`, []string{"foo", `bar ""`}}, |
| 58 | "nested double quotes trailing empty": {`foo "bar """ `, []string{"foo", `bar ""`}}, |
| 59 | "nested double quotes leading empty": {` foo "bar """`, []string{"foo", `bar ""`}}, |
| 60 | |
| 61 | "mixed quotes": {`foo 'bar "baz"'`, []string{"foo", `bar "baz"`}}, |
| 62 | "mixed quotes trailing": {`foo 'bar "baz"' `, []string{"foo", `bar "baz"`}}, |
| 63 | "mixed quotes leading": {` foo 'bar "baz"'`, []string{"foo", `bar "baz"`}}, |
| 64 | |
| 65 | "mixed quotes empty": {`foo 'bar ""'`, []string{"foo", `bar ""`}}, |
| 66 | "mixed quotes trailing empty": {`foo 'bar ""' `, []string{"foo", `bar ""`}}, |
| 67 | "mixed quotes leading empty": {` foo 'bar ""'`, []string{"foo", `bar ""`}}, |
| 68 | } { |
| 69 | t.Log(desc) |
| 70 | c.Assert(t) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestLongestReturnsEmptyStringGivenEmptySet(t *testing.T) { |
| 75 | assert.Equal(t, "", Longest(nil)) |
| 76 | } |
| 77 | |
| 78 | func TestLongestReturnsLongestString(t *testing.T) { |
| 79 | assert.Equal(t, "longest", Longest([]string{"short", "longer", "longest"})) |
| 80 | } |
| 81 | |
| 82 | func TestLongestReturnsLastStringGivenSameLength(t *testing.T) { |
| 83 | assert.Equal(t, "baz", Longest([]string{"foo", "bar", "baz"})) |
| 84 | } |
| 85 | |
| 86 | func TestRjustRightJustifiesString(t *testing.T) { |
| 87 | unjust := []string{ |
| 88 | "short", |
| 89 | "longer", |
| 90 | "longest", |
| 91 | } |
| 92 | expected := []string{ |
| 93 | " short", |
| 94 | " longer", |
| 95 | "longest", |
| 96 | } |
| 97 | |
| 98 | assert.Equal(t, expected, Rjust(unjust)) |
| 99 | } |
| 100 | |
| 101 | func TestLjustLeftJustifiesString(t *testing.T) { |
| 102 | unjust := []string{ |
| 103 | "short", |
| 104 | "longer", |
| 105 | "longest", |
| 106 | } |
| 107 | expected := []string{ |
| 108 | "short ", |
| 109 | "longer ", |
| 110 | "longest", |
| 111 | } |
| 112 | |
| 113 | assert.Equal(t, expected, Ljust(unjust)) |
| 114 | } |
| 115 | |
| 116 | func TestIndentIndentsStrings(t *testing.T) { |
| 117 | assert.Equal(t, "\tfoo\n\tbar", Indent("foo\nbar")) |
| 118 | } |
| 119 | |
| 120 | func TestIndentIndentsSingleLineStrings(t *testing.T) { |
| 121 | assert.Equal(t, "\tfoo", Indent("foo")) |
| 122 | } |
| 123 | |
| 124 | func TestIndentReturnsEmptyStrings(t *testing.T) { |
| 125 | assert.Equal(t, "", Indent("")) |
| 126 | } |
| 127 | |
| 128 | func TestUndentRemovesLeadingWhitespace(t *testing.T) { |
| 129 | assert.Equal(t, "foo", Undent("\t\t\tfoo")) |
| 130 | assert.Equal(t, "foo", Undent("foo")) |
| 131 | assert.Equal(t, "foo", Undent(" foo")) |
| 132 | } |
| 133 | |
| 134 | func TestUndentRemovesPreservesLinebreaks(t *testing.T) { |
| 135 | // No leading space |
| 136 | assert.Equal(t, "\r\nfoo", Undent("\r\nfoo")) |
| 137 | assert.Equal(t, "foo\r\n", Undent("foo\r\n")) |
| 138 | assert.Equal(t, "\r\nfoo\r\n", Undent("\r\nfoo\r\n")) |
| 139 | assert.Equal(t, "\nfoo", Undent("\nfoo")) |
| 140 | assert.Equal(t, "foo\n", Undent("foo\n")) |
| 141 | assert.Equal(t, "\nfoo\n", Undent("\nfoo\n")) |
| 142 | |
| 143 | // Trim leading space |
| 144 | assert.Equal(t, "\r\nfoo", Undent("\r\n foo")) |
| 145 | assert.Equal(t, "foo\r\n", Undent(" foo\r\n")) |
| 146 | assert.Equal(t, "\r\nfoo\r\n", Undent("\r\n foo\r\n")) |
| 147 | assert.Equal(t, "\nfoo", Undent("\n foo")) |
| 148 | assert.Equal(t, "foo\n", Undent(" foo\n")) |
| 149 | assert.Equal(t, "\nfoo\n", Undent("\n foo\n")) |
| 150 | |
| 151 | // Preserve trailing space |
| 152 | assert.Equal(t, "\r\nfoo ", Undent("\r\nfoo ")) |
| 153 | assert.Equal(t, "foo \r\n", Undent("foo \r\n")) |
| 154 | assert.Equal(t, "\r\nfoo \r\n", Undent("\r\nfoo \r\n")) |
| 155 | assert.Equal(t, "\nfoo ", Undent("\nfoo ")) |
| 156 | assert.Equal(t, "foo \n", Undent("foo \n")) |
| 157 | assert.Equal(t, "\nfoo \n", Undent("\nfoo \n")) |
| 158 | |
| 159 | // Trim leading space, preserve trailing space |
| 160 | assert.Equal(t, "\r\nfoo ", Undent("\r\n foo ")) |
| 161 | assert.Equal(t, "foo \r\n", Undent(" foo \r\n")) |
| 162 | assert.Equal(t, "\r\nfoo \r\n", Undent("\r\n foo \r\n")) |
| 163 | assert.Equal(t, "\nfoo ", Undent("\n foo ")) |
| 164 | assert.Equal(t, "foo \n", Undent(" foo \n")) |
| 165 | assert.Equal(t, "\nfoo \n", Undent("\n foo \n")) |
| 166 | } |
| 167 | |
| 168 | func TestFormatFraction(t *testing.T) { |
| 169 | assert.Equal(t, "1/1", FormatFraction(1, 1)) |
| 170 | assert.Equal(t, "33/100", FormatFraction(33, 100)) |
| 171 | assert.Equal(t, "9876543/1234567890", FormatFraction(9876543, 1234567890)) |
| 172 | |
| 173 | // Allow numerator to exceed denominator |
| 174 | assert.Equal(t, "5000/200", FormatFraction(5000, 200)) |
| 175 | |
| 176 | // Allow zeroes in denominator |
| 177 | assert.Equal(t, "0/0", FormatFraction(0, 0)) |
| 178 | assert.Equal(t, "99/0", FormatFraction(99, 0)) |
| 179 | |
| 180 | // Adjust unknown denominator to match length of numerator |
| 181 | assert.Equal(t, "0/?", FormatFraction(0, -1)) |
| 182 | assert.Equal(t, "1/?", FormatFraction(1, -1)) |
| 183 | assert.Equal(t, "88/??", FormatFraction(88, -88)) |
| 184 | assert.Equal(t, "1138/????", FormatFraction(1138, -1)) |
| 185 | assert.Equal(t, "6345789/???????", FormatFraction(6345789, -1)) |
| 186 | } |