summaryrefslogtreecommitdiffstats
path: root/layout/layout_test.go
blob: 568bbee196c175d07c52ef4e3608420a111e1c91 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package layout

// NOT MY FAULT.  Some dependency already pulled in testify -- 13kLOC
import (
	"github.com/stretchr/testify/assert"
	"strings"
	"testing"
)

func TestParsing(t *testing.T) {
	tests := []struct {
		i string
		f func(l layout)
	}{
		{"cpu", func(l layout) {
			assert.Equal(t, 1, len(l.Rows))
			assert.Equal(t, 1, len(l.Rows[0]))
		}},
		{"   cpu   \ndisk/1     mem/3\ntemp   \nnet    procs", func(l layout) {
			assert.Equal(t, 4, len(l.Rows))
			assert.Equal(t, 1, len(l.Rows[0]))
			assert.Equal(t, 2, len(l.Rows[1]))
			assert.Equal(t, 1, len(l.Rows[2]))
			assert.Equal(t, 2, len(l.Rows[3]))
		}},
		{"cpu\ndisk/1 mem/3\ntemp\nnet procs", func(l layout) {
			assert.Equal(t, 4, len(l.Rows))
			// 1
			assert.Equal(t, 1, len(l.Rows[0]))
			assert.Equal(t, 1.0, l.Rows[0][0].Weight)
			assert.Equal(t, 1, l.Rows[0][0].Height)
			// 2
			assert.Equal(t, 2, len(l.Rows[1]))
			assert.Equal(t, 1.0/4, l.Rows[1][0].Weight)
			assert.Equal(t, 1, l.Rows[1][0].Height)
			assert.Equal(t, 3.0/4, l.Rows[1][1].Weight)
			assert.Equal(t, 1, l.Rows[1][1].Height)
			// 3
			assert.Equal(t, 1, len(l.Rows[2]))
			assert.Equal(t, 1.0, l.Rows[2][0].Weight)
			assert.Equal(t, 1, l.Rows[2][0].Height)
			// 4
			assert.Equal(t, 2, len(l.Rows[3]))
			assert.Equal(t, 0.5, l.Rows[3][0].Weight)
			assert.Equal(t, 1, l.Rows[3][0].Height)
			assert.Equal(t, 0.5, l.Rows[3][1].Weight)
			assert.Equal(t, 1, l.Rows[3][1].Height)
		}},
		{"2:cpu\ndisk\nmem", func(l layout) {
			assert.Equal(t, 3, len(l.Rows))
			assert.Equal(t, 1, len(l.Rows[0]))
			assert.Equal(t, 2, l.Rows[0][0].Height)
			assert.Equal(t, 1, len(l.Rows[1]))
			assert.Equal(t, 1, l.Rows[1][0].Height)
			assert.Equal(t, 1, len(l.Rows[2]))
			assert.Equal(t, 1, l.Rows[2][0].Height)
		}},
		{"2:cpu disk\nmem", func(l layout) {
			assert.Equal(t, 2, len(l.Rows))
			assert.Equal(t, 2, len(l.Rows[0]))
			assert.Equal(t, 2, l.Rows[0][0].Height)
			assert.Equal(t, 1, l.Rows[0][1].Height)
			assert.Equal(t, 1, len(l.Rows[1]))
			assert.Equal(t, 1, l.Rows[1][0].Height)
		}},
		{"cpu 2:disk\nmem", func(l layout) {
			assert.Equal(t, 2, len(l.Rows))
			assert.Equal(t, 2, len(l.Rows[0]))
			assert.Equal(t, 1, l.Rows[0][0].Height)
			assert.Equal(t, 2, l.Rows[0][1].Height)
			assert.Equal(t, 1, len(l.Rows[1]))
			assert.Equal(t, 1, l.Rows[1][0].Height)
		}},
		{"cpu disk\n2:mem", func(l layout) {
			assert.Equal(t, 2, len(l.Rows))
			assert.Equal(t, 2, len(l.Rows[0]))
			assert.Equal(t, 1, l.Rows[0][0].Height)
			assert.Equal(t, 1, l.Rows[0][1].Height)
			assert.Equal(t, 1, len(l.Rows[1]))
			assert.Equal(t, 2, l.Rows[1][0].Height)
		}},
		{"cpu 2:disk/3\nmem", func(l layout) {
			assert.Equal(t, 2, len(l.Rows))
			assert.Equal(t, 2, len(l.Rows[0]))
			assert.Equal(t, 1, l.Rows[0][0].Height)
			assert.Equal(t, 1.0/4, l.Rows[0][0].Weight)
			assert.Equal(t, 2, l.Rows[0][1].Height)
			assert.Equal(t, 3.0/4, l.Rows[0][1].Weight)
			assert.Equal(t, 1, len(l.Rows[1]))
			assert.Equal(t, 1, l.Rows[1][0].Height)
			assert.Equal(t, 1.0, l.Rows[1][0].Weight)
		}},
		{"2:cpu disk\nmem/3", func(l layout) {
			assert.Equal(t, 2, len(l.Rows))
			assert.Equal(t, 2, len(l.Rows[0]))
			assert.Equal(t, 2, l.Rows[0][0].Height)
			assert.Equal(t, 0.5, l.Rows[0][0].Weight)
			assert.Equal(t, 1, l.Rows[0][1].Height)
			assert.Equal(t, 0.5, l.Rows[0][1].Weight)
			assert.Equal(t, 1, len(l.Rows[1]))
			assert.Equal(t, 1, l.Rows[1][0].Height)
			assert.Equal(t, 1.0, l.Rows[1][0].Weight)
		}},
		{"cpu/2 mem/1 6:procs\n3:temp/1 2:disk/2\npower\nnet procs", func(l layout) {
			assert.Equal(t, 4, len(l.Rows))
			// First row
			assert.Equal(t, 3, len(l.Rows[0]))
			assert.Equal(t, 1, l.Rows[0][0].Height)
			assert.Equal(t, 0.5, l.Rows[0][0].Weight)
			assert.Equal(t, 1, l.Rows[0][1].Height)
			assert.Equal(t, 0.25, l.Rows[0][1].Weight)
			assert.Equal(t, 6, l.Rows[0][2].Height)
			assert.Equal(t, 0.25, l.Rows[0][2].Weight)
			// Second row
			assert.Equal(t, 2, len(l.Rows[1]))
			assert.Equal(t, 3, l.Rows[1][0].Height)
			assert.Equal(t, 1/3.0, l.Rows[1][0].Weight)
			assert.Equal(t, 2, l.Rows[1][1].Height)
			assert.Equal(t, 2/3.0, l.Rows[1][1].Weight)
			// Third row
			assert.Equal(t, 1, len(l.Rows[2]))
			assert.Equal(t, 1, l.Rows[2][0].Height)
			assert.Equal(t, 1.0, l.Rows[2][0].Weight)
			// Fourth row
			assert.Equal(t, 2, len(l.Rows[3]))
			assert.Equal(t, 1, l.Rows[3][0].Height)
			assert.Equal(t, 0.5, l.Rows[3][0].Weight)
			assert.Equal(t, 1, l.Rows[3][1].Height)
			assert.Equal(t, 0.5, l.Rows[3][1].Weight)
		}},
	}

	for _, tc := range tests {
		in := strings.NewReader(tc.i)
		l := ParseLayout(in)
		tc.f(l)