File: table_test.go

package info (click to toggle)
golang-github-gosuri-uitable 0.0~git20170830.36ee7e94-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 116 kB
  • sloc: makefile: 8
file content (81 lines) | stat: -rw-r--r-- 1,394 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package uitable

import (
	"sync"
	"testing"
)

func TestCell(t *testing.T) {
	c := &Cell{
		Data:  "foo bar",
		Width: 5,
	}

	got := c.String()
	if got != "fo..." {
		t.Fatal("need", "fo...", "got", got)
	}
	if c.LineWidth() != 5 {
		t.Fatal("need", 5, "got", c.LineWidth())
	}

	c.Wrap = true
	got = c.String()
	if got != "foo\nbar" {
		t.Fatal("need", "foo\nbar", "got", got)
	}
	if c.LineWidth() != 3 {
		t.Fatal("need", 3, "got", c.LineWidth())
	}
}

func TestRow(t *testing.T) {
	row := &Row{
		Separator: "\t",
		Cells: []*Cell{
			{Data: "foo", Width: 3, Wrap: true},
			{Data: "bar baz", Width: 3, Wrap: true},
		},
	}
	got := row.String()
	need := "foo\tbar\n   \tbaz"

	if got != need {
		t.Fatalf("need: %q | got: %q ", need, got)
	}
}

func TestAlign(t *testing.T) {
	table := New()
	table.AddRow("foo", "bar baz")
	table.Rows = []*Row{{
		Separator: "\t",
		Cells: []*Cell{
			{Data: "foo", Width: 5, Wrap: true},
			{Data: "bar baz", Width: 10, Wrap: true},
		},
	}}
	table.RightAlign(1)
	got := table.String()
	need := "foo  \t   bar baz"

	if got != need {
		t.Fatalf("need: %q | got: %q ", need, got)
	}
}

func TestAddRow(t *testing.T) {
	var wg sync.WaitGroup
	table := New()
	for i := 0; i < 100; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			table.AddRow("foo")
		}()
	}
	wg.Wait()
	if len(table.Rows) != 100 {
		t.Fatal("want", 100, "got", len(table.Rows))
	}
}