1package columnize 2 3import "testing" 4 5func TestListOfStringsInput(t *testing.T) { 6 input := []string{ 7 "Column A | Column B | Column C", 8 "x | y | z", 9 } 10 11 config := DefaultConfig() 12 output := Format(input, config) 13 14 expected := "Column A Column B Column C\n" 15 expected += "x y z" 16 17 if output != expected { 18 t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) 19 } 20} 21 22func TestEmptyLinesOutput(t *testing.T) { 23 input := []string{ 24 "Column A | Column B | Column C", 25 "", 26 "x | y | z", 27 } 28 29 config := DefaultConfig() 30 output := Format(input, config) 31 32 expected := "Column A Column B Column C\n" 33 expected += "\n" 34 expected += "x y z" 35 36 if output != expected { 37 t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) 38 } 39} 40 41func TestLeadingSpacePreserved(t *testing.T) { 42 input := []string{ 43 "| Column B | Column C", 44 "x | y | z", 45 } 46 47 config := DefaultConfig() 48 output := Format(input, config) 49 50 expected := " Column B Column C\n" 51 expected += "x y z" 52 53 if output != expected { 54 t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) 55 } 56} 57 58func TestColumnWidthCalculator(t *testing.T) { 59 input := []string{ 60 "Column A | Column B | Column C", 61 "Longer than A | Longer than B | Longer than C", 62 "short | short | short", 63 } 64 65 config := DefaultConfig() 66 output := Format(input, config) 67 68 expected := "Column A Column B Column C\n" 69 expected += "Longer than A Longer than B Longer than C\n" 70 expected += "short short short" 71 72 if output != expected { 73 t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) 74 } 75} 76 77func TestVariedInputSpacing(t *testing.T) { 78 input := []string{ 79 "Column A |Column B| Column C", 80 "x|y| z", 81 } 82 83 config := DefaultConfig() 84 output := Format(input, config) 85 86 expected := "Column A Column B Column C\n" 87 expected += "x y z" 88 89 if output != expected { 90 t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) 91 } 92} 93 94func TestUnmatchedColumnCounts(t *testing.T) { 95 input := []string{ 96 "Column A | Column B | Column C", 97 "Value A | Value B", 98 "Value A | Value B | Value C | Value D", 99 } 100 101 config := DefaultConfig() 102 output := Format(input, config) 103 104 expected := "Column A Column B Column C\n" 105 expected += "Value A Value B\n" 106 expected += "Value A Value B Value C Value D" 107 108 if output != expected { 109 t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) 110 } 111} 112 113func TestAlternateDelimiter(t *testing.T) { 114 input := []string{ 115 "Column | A % Column | B % Column | C", 116 "Value A % Value B % Value C", 117 } 118 119 config := DefaultConfig() 120 config.Delim = "%" 121 output := Format(input, config) 122 123 expected := "Column | A Column | B Column | C\n" 124 expected += "Value A Value B Value C" 125 126 if output != expected { 127 t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) 128 } 129} 130 131func TestAlternateSpacingString(t *testing.T) { 132 input := []string{ 133 "Column A | Column B | Column C", 134 "x | y | z", 135 } 136 137 config := DefaultConfig() 138 config.Glue = " " 139 output := Format(input, config) 140 141 expected := "Column A Column B Column C\n" 142 expected += "x y z" 143 144 if output != expected { 145 t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) 146 } 147} 148 149func TestSimpleFormat(t *testing.T) { 150 input := []string{ 151 "Column A | Column B | Column C", 152 "x | y | z", 153 } 154 155 output := SimpleFormat(input) 156 157 expected := "Column A Column B Column C\n" 158 expected += "x y z" 159 160 if output != expected { 161 t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) 162 } 163} 164 165func TestAlternatePrefixString(t *testing.T) { 166 input := []string{ 167 "Column A | Column B | Column C", 168 "x | y | z", 169 } 170 171 config := DefaultConfig() 172 config.Prefix = " " 173 output := Format(input, config) 174 175 expected := " Column A Column B Column C\n" 176 expected += " x y z" 177 178 if output != expected { 179 t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) 180 } 181} 182 183func TestEmptyFieldReplacement(t *testing.T) { 184 input := []string{ 185 "Column A | Column B | Column C", 186 "x | | z", 187 } 188 189 config := DefaultConfig() 190 config.Empty = "<none>" 191 output := Format(input, config) 192 193 expected := "Column A Column B Column C\n" 194 expected += "x <none> z" 195 196 if output != expected { 197 t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) 198 } 199} 200 201func TestEmptyConfigValues(t *testing.T) { 202 input := []string{ 203 "Column A | Column B | Column C", 204 "x | y | z", 205 } 206 207 config := Config{} 208 output := Format(input, &config) 209 210 expected := "Column A Column B Column C\n" 211 expected += "x y z" 212 213 if output != expected { 214 t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) 215 } 216} 217 218func TestMergeConfig(t *testing.T) { 219 conf1 := &Config{Delim: "a", Glue: "a", Prefix: "a", Empty: "a"} 220 conf2 := &Config{Delim: "b", Glue: "b", Prefix: "b", Empty: "b"} 221 conf3 := &Config{Delim: "c", Prefix: "c"} 222 223 m := MergeConfig(conf1, conf2) 224 if m.Delim != "b" || m.Glue != "b" || m.Prefix != "b" || m.Empty != "b" { 225 t.Fatalf("bad: %#v", m) 226 } 227 228 m = MergeConfig(conf1, conf3) 229 if m.Delim != "c" || m.Glue != "a" || m.Prefix != "c" || m.Empty != "a" { 230 t.Fatalf("bad: %#v", m) 231 } 232 233 m = MergeConfig(conf1, nil) 234 if m.Delim != "a" || m.Glue != "a" || m.Prefix != "a" || m.Empty != "a" { 235 t.Fatalf("bad: %#v", m) 236 } 237 238 m = MergeConfig(conf1, &Config{}) 239 if m.Delim != "a" || m.Glue != "a" || m.Prefix != "a" || m.Empty != "a" { 240 t.Fatalf("bad: %#v", m) 241 } 242} 243