File: table_test.go

package info (click to toggle)
golang-code.rocketnine-tslocum-cview 1.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,396 kB
  • sloc: makefile: 40
file content (106 lines) | stat: -rw-r--r-- 2,147 bytes parent folder | download
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
package cview

import (
	"fmt"
	"testing"
)

var tableTestCases = generateTableTestCases()

type tableTestCase struct {
	rows         int
	columns      int
	fixedRows    int
	fixedColumns int
}

func (c *tableTestCase) String() string {
	return fmt.Sprintf("Rows=%d/Cols=%d/FixedRows=%d/FixedCols=%d", c.rows, c.columns, c.fixedRows, c.fixedColumns)
}

func TestTable(t *testing.T) {
	t.Parallel()

	for _, c := range tableTestCases {
		c := c // Capture

		t.Run(c.String(), func(t *testing.T) {
			t.Parallel()

			table := tc(c)

			app, err := newTestApp(table)
			if err != nil {
				t.Errorf("failed to initialize Application: %s", err)
			}

			for row := 0; row < c.rows; row++ {
				for column := 0; column < c.columns; column++ {
					contents := table.GetCell(row, column).GetText()
					expected := fmt.Sprintf("%d,%d", column, row)
					if contents != expected {
						t.Errorf("failed to either get or set TableCell text: expected %s, got %s", expected, contents)
					}
				}
			}

			table.Draw(app.screen)

			table.Clear()
		})
	}
}

func BenchmarkTableDraw(b *testing.B) {
	for _, c := range tableTestCases {
		c := c // Capture

		b.Run(c.String(), func(b *testing.B) {
			table := tc(c)

			app, err := newTestApp(table)
			if err != nil {
				b.Errorf("failed to initialize Application: %s", err)
			}

			table.Draw(app.screen)

			b.ReportAllocs()
			b.ResetTimer()

			for i := 0; i < b.N; i++ {
				table.Draw(app.screen)
			}
		})
	}
}

func generateTableTestCases() []*tableTestCase {
	var cases []*tableTestCase
	for i := 1; i < 3; i++ {
		rows := i * 5
		for i := 1; i < 3; i++ {
			columns := i * 7
			for fixedRows := 0; fixedRows < 3; fixedRows++ {
				for fixedColumns := 0; fixedColumns < 3; fixedColumns++ {
					cases = append(cases, &tableTestCase{rows, columns, fixedRows, fixedColumns})
				}
			}
		}
	}
	return cases
}

func tc(c *tableTestCase) *Table {
	table := NewTable()

	for row := 0; row < c.rows; row++ {
		for column := 0; column < c.columns; column++ {
			table.SetCellSimple(row, column, fmt.Sprintf("%d,%d", column, row))
		}
	}

	table.SetFixed(c.fixedRows, c.fixedColumns)

	return table
}