blob: 1ba4496cc13b7d62cc3dfc5d2db8aa82f5c822a2 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Serge Bazanskie0c06172023-09-19 12:28:16 +00004package clitable
Serge Bazanski504ea312023-03-22 17:47:48 +01005
6import (
7 "bytes"
8 "strings"
9 "testing"
10)
11
12// TestTableLayout performs a smoke test of the table layout functionality.
13func TestTableLayout(t *testing.T) {
Tim Windelschmidt3b5a9172024-05-23 13:33:52 +020014 var tab Table
Serge Bazanski504ea312023-03-22 17:47:48 +010015
Tim Windelschmidt3b5a9172024-05-23 13:33:52 +020016 var e Entry
Serge Bazanskie0c06172023-09-19 12:28:16 +000017 e.Add("id", "short")
18 e.Add("labels", "")
19 tab.Add(e)
Serge Bazanski504ea312023-03-22 17:47:48 +010020
Serge Bazanskie0c06172023-09-19 12:28:16 +000021 e = Entry{}
22 e.Add("whoops", "only in second")
23 e.Add("labels", "bar")
24 e.Add("id", "this one is a very long one")
25 tab.Add(e)
Serge Bazanski504ea312023-03-22 17:47:48 +010026
Serge Bazanskie0c06172023-09-19 12:28:16 +000027 e = Entry{}
28 e.Add("id", "normal length")
29 e.Add("labels", "foo")
30 tab.Add(e)
Serge Bazanski504ea312023-03-22 17:47:48 +010031
32 buf := bytes.NewBuffer(nil)
Serge Bazanskie0c06172023-09-19 12:28:16 +000033 tab.Print(buf, nil)
Serge Bazanski504ea312023-03-22 17:47:48 +010034
35 golden := `
36ID LABELS WHOOPS
37short
38this one is a very long one bar only in second
39normal length foo
40`
41 golden = strings.TrimSpace(golden)
42 got := strings.TrimSpace(buf.String())
43 if got != golden {
Lorenz Brun0ec0c532024-08-29 12:39:47 +000044 t.Logf("wanted: \n%s", golden)
45 t.Logf("got: \n%s", got)
Serge Bazanski504ea312023-03-22 17:47:48 +010046 t.Errorf("mismatch")
47 }
48}