| 1 |
package tblfmt |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
"reflect" |
| 8 |
"strconv" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/xo/tblfmt/internal" |
| 13 |
) |
| 14 |
|
| 15 |
func TestFromMap(t *testing.T) { |
| 16 |
tests := map[string]struct { |
| 17 |
name string |
| 18 |
opts map[string]string |
| 19 |
exp Builder |
| 20 |
}{ |
| 21 |
"empty": { |
| 22 |
exp: newErrEncoder, |
| 23 |
}, |
| 24 |
"aligned": { |
| 25 |
opts: map[string]string{ |
| 26 |
"format": "aligned", |
| 27 |
}, |
| 28 |
exp: NewTableEncoder, |
| 29 |
}, |
| 30 |
"unaligned": { |
| 31 |
opts: map[string]string{ |
| 32 |
"format": "unaligned", |
| 33 |
}, |
| 34 |
exp: NewUnalignedEncoder, |
| 35 |
}, |
| 36 |
"csv": { |
| 37 |
opts: map[string]string{ |
| 38 |
"format": "csv", |
| 39 |
}, |
| 40 |
exp: NewCSVEncoder, |
| 41 |
}, |
| 42 |
"any": { |
| 43 |
opts: map[string]string{ |
| 44 |
"format": "aligned", |
| 45 |
"border": "2", |
| 46 |
"title": "some title", |
| 47 |
"expanded": "on", |
| 48 |
}, |
| 49 |
exp: NewExpandedEncoder, |
| 50 |
}, |
| 51 |
} |
| 52 |
for n, test := range tests { |
| 53 |
t.Run(n, func(t *testing.T) { |
| 54 |
builder, _ := FromMap(test.opts) |
| 55 |
if reflect.ValueOf(builder).Pointer() != reflect.ValueOf(test.exp).Pointer() { |
| 56 |
t.Errorf("invalid builder, expected %T, got %T", test.exp, builder) |
| 57 |
} |
| 58 |
}) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestFromMapFormats(t *testing.T) { |
| 63 |
t.Parallel() |
| 64 |
for _, typ := range []string{ |
| 65 |
"big", |
| 66 |
"multi", |
| 67 |
"tiny", |
| 68 |
"wide", |
| 69 |
} { |
| 70 |
t.Run(typ, func(t *testing.T) { |
| 71 |
t.Parallel() |
| 72 |
tests := loadTests(t, typ) |
| 73 |
for _, test := range tests { |
| 74 |
t.Run(test.idstr, func(t *testing.T) { |
| 75 |
f, opts := FromMap(test.opts) |
| 76 |
var rs ResultSet |
| 77 |
switch typ { |
| 78 |
case "big": |
| 79 |
rs = internal.Big(1549508725559526476) |
| 80 |
case "multi": |
| 81 |
rs = internal.Multi() |
| 82 |
case "tiny": |
| 83 |
rs = internal.Tiny() |
| 84 |
case "wide": |
| 85 |
rs = internal.Wide() |
| 86 |
} |
| 87 |
enc, err := f(rs, opts...) |
| 88 |
if err != nil { |
| 89 |
t.Fatalf("expected no error, got: %v", err) |
| 90 |
} |
| 91 |
var buf bytes.Buffer |
| 92 |
if err := enc.EncodeAll(&buf); err != nil { |
| 93 |
t.Fatalf("expected no error, got: %v", err) |
| 94 |
} |
| 95 |
if err := os.WriteFile(test.out, buf.Bytes(), 0o644); err != nil { |
| 96 |
t.Fatalf("expected no error, got: %v", err) |
| 97 |
} |
| 98 |
if s, exp := buf.String(), string(test.exp); s != exp { |
| 99 |
t.Errorf("expected:\n%s\ngot:\n%s", exp, s) |
| 100 |
} |
| 101 |
}) |
| 102 |
} |
| 103 |
}) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func TestFromMapTuplesOnly(t *testing.T) { |
| 108 |
tests := []struct { |
| 109 |
format string |
| 110 |
tuplesOnly string |
| 111 |
exp bool |
| 112 |
}{ |
| 113 |
{"aligned", "on", false}, |
| 114 |
{"aligned", "off", true}, |
| 115 |
{"unaligned", "on", false}, |
| 116 |
{"unaligned", "off", true}, |
| 117 |
} |
| 118 |
for i, test := range tests { |
| 119 |
t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 120 |
t.Logf("format: %s tuples_only: %s has footer: %t", test.format, test.tuplesOnly, test.exp) |
| 121 |
rs := internal.New([]string{"id"}, [][]any{{"1"}}) |
| 122 |
params := map[string]string{ |
| 123 |
"format": test.format, |
| 124 |
"tuples_only": test.tuplesOnly, |
| 125 |
"border": "1", |
| 126 |
"linestyle": "ascii", |
| 127 |
"fieldsep": "|", |
| 128 |
"footer": "on", |
| 129 |
} |
| 130 |
var buf bytes.Buffer |
| 131 |
if err := EncodeAll(&buf, rs, params); err != nil { |
| 132 |
t.Fatalf("expected no error, got: %v", err) |
| 133 |
} |
| 134 |
out := buf.String() |
| 135 |
t.Logf("output:\n%s", out) |
| 136 |
if b := strings.Contains(out, "(1 row)"); b != test.exp { |
| 137 |
t.Errorf("expected %t, got: %t", test.exp, b) |
| 138 |
} |
| 139 |
}) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
type goldTest struct { |
| 144 |
id int |
| 145 |
idstr string |
| 146 |
in string |
| 147 |
gld string |
| 148 |
out string |
| 149 |
opts map[string]string |
| 150 |
exp []byte |
| 151 |
} |
| 152 |
|
| 153 |
func loadTests(t *testing.T, typ string) []goldTest { |
| 154 |
t.Helper() |
| 155 |
base := "testdata/" + typ |
| 156 |
var tests []goldTest |
| 157 |
err := filepath.Walk(base, func(name string, fi os.FileInfo, err error) error { |
| 158 |
switch { |
| 159 |
case err != nil: |
| 160 |
return err |
| 161 |
case fi.IsDir(), !strings.HasSuffix(name, ".in"): |
| 162 |
return nil |
| 163 |
} |
| 164 |
n := strings.TrimSuffix(name, ".in") |
| 165 |
id, err := strconv.ParseInt(strings.TrimPrefix(n, base+"/"), 10, 64) |
| 166 |
if err != nil { |
| 167 |
t.Fatalf("unable to parse %q: %v", name, err) |
| 168 |
} |
| 169 |
opts := loadOpts(t, name) |
| 170 |
gld := n + ".gld" |
| 171 |
exp, err := os.ReadFile(gld) |
| 172 |
if err != nil { |
| 173 |
t.Fatalf("unable to open %s: %v", gld, err) |
| 174 |
} |
| 175 |
tests = append(tests, goldTest{ |
| 176 |
id: int(id), |
| 177 |
idstr: strconv.Itoa(int(id)), |
| 178 |
in: name, |
| 179 |
gld: gld, |
| 180 |
out: n + ".out", |
| 181 |
opts: opts, |
| 182 |
exp: exp, |
| 183 |
}) |
| 184 |
return nil |
| 185 |
}) |
| 186 |
if err != nil { |
| 187 |
t.Fatalf("expected no error, got: %v", err) |
| 188 |
} |
| 189 |
return tests |
| 190 |
} |
| 191 |
|
| 192 |
func loadOpts(t *testing.T, in string) map[string]string { |
| 193 |
t.Helper() |
| 194 |
opts := make(map[string]string) |
| 195 |
buf, err := os.ReadFile(in) |
| 196 |
if err != nil { |
| 197 |
t.Fatalf("unable to open %q", in) |
| 198 |
} |
| 199 |
loop: |
| 200 |
for { |
| 201 |
i := bytes.Index(buf, []byte{'\n'}) |
| 202 |
if i == -1 { |
| 203 |
break loop |
| 204 |
} |
| 205 |
line := string(bytes.TrimSpace(buf[:i])) |
| 206 |
if line == "" { |
| 207 |
break loop |
| 208 |
} |
| 209 |
v := strings.Split(line, ":") |
| 210 |
if len(v) != 2 { |
| 211 |
t.Fatalf("missing : in line") |
| 212 |
} |
| 213 |
opts[strings.TrimSpace(v[0])] = strings.TrimSpace(v[1]) |
| 214 |
buf = buf[i+1:] |
| 215 |
} |
| 216 |
if opts["format"] == "" { |
| 217 |
t.Fatalf("format was not defined") |
| 218 |
} |
| 219 |
if s := opts["format"]; s == "expanded" { |
| 220 |
opts["format"] = "aligned" |
| 221 |
opts["expanded"] = "on" |
| 222 |
} |
| 223 |
if s := opts["linestyle"]; strings.HasPrefix(s, "unicode") { |
| 224 |
opts["linestyle"] = "unicode" |
| 225 |
opts["unicode_border_linestyle"] = "single" |
| 226 |
if strings.Contains(s, "double") { |
| 227 |
opts["unicode_border_linestyle"] = "double" |
| 228 |
} |
| 229 |
} |
| 230 |
return opts |
| 231 |
} |