m/cli/metroctl: implement tabular print
Change-Id: I0511d48218bcc7e2e56af66839392bf11643733c
Reviewed-on: https://review.monogon.dev/c/monogon/+/1391
Tested-by: Jenkins CI
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
diff --git a/metropolis/cli/metroctl/table_test.go b/metropolis/cli/metroctl/table_test.go
new file mode 100644
index 0000000..0ea1448
--- /dev/null
+++ b/metropolis/cli/metroctl/table_test.go
@@ -0,0 +1,45 @@
+package main
+
+import (
+ "bytes"
+ "strings"
+ "testing"
+)
+
+// TestTableLayout performs a smoke test of the table layout functionality.
+func TestTableLayout(t *testing.T) {
+ tab := table{}
+
+ e := entry{}
+ e.add("id", "short")
+ e.add("labels", "")
+ tab.add(e)
+
+ e = entry{}
+ e.add("whoops", "only in second")
+ e.add("labels", "bar")
+ e.add("id", "this one is a very long one")
+ tab.add(e)
+
+ e = entry{}
+ e.add("id", "normal length")
+ e.add("labels", "foo")
+ tab.add(e)
+
+ buf := bytes.NewBuffer(nil)
+ tab.print(buf, nil)
+
+ golden := `
+ID LABELS WHOOPS
+short
+this one is a very long one bar only in second
+normal length foo
+`
+ golden = strings.TrimSpace(golden)
+ got := strings.TrimSpace(buf.String())
+ if got != golden {
+ t.Logf("wanted: \n" + golden)
+ t.Logf("got: \n" + got)
+ t.Errorf("mismatch")
+ }
+}