go/clitable: factor out from metroctl
We need the same functionality in bmcli, so factor it out from metroctl
into a generic library.
Change-Id: I3fb3dfaae44a64d204e9220f117f379c382c5c4f
Reviewed-on: https://review.monogon.dev/c/monogon/+/2172
Tested-by: Jenkins CI
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
diff --git a/go/clitable/table_test.go b/go/clitable/table_test.go
new file mode 100644
index 0000000..5fca84c
--- /dev/null
+++ b/go/clitable/table_test.go
@@ -0,0 +1,45 @@
+package clitable
+
+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")
+ }
+}