File: cell_test.go

package info (click to toggle)
golang-github-scylladb-termtables 0.0~git20191203.c4c0b6d-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 184 kB
  • sloc: makefile: 2
file content (113 lines) | stat: -rw-r--r-- 2,682 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
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
// Copyright 2012-2015 Apcera Inc. All rights reserved.

package termtables

import (
	"testing"
)

func TestCellRenderString(t *testing.T) {
	style := &renderStyle{TableStyle: TableStyle{}, cellWidths: map[int]int{}}
	cell := createCell(0, "foobar", nil)

	output := cell.Render(style)
	if output != "foobar" {
		t.Fatal("Unexpected output:", output)
	}
}

func TestCellRenderBool(t *testing.T) {
	style := &renderStyle{TableStyle: TableStyle{}, cellWidths: map[int]int{}}
	cell := createCell(0, true, nil)

	output := cell.Render(style)
	if output != "true" {
		t.Fatal("Unexpected output:", output)
	}
}

func TestCellRenderInteger(t *testing.T) {
	style := &renderStyle{TableStyle: TableStyle{}, cellWidths: map[int]int{}}
	cell := createCell(0, 12345, nil)

	output := cell.Render(style)
	if output != "12345" {
		t.Fatal("Unexpected output:", output)
	}
}

func TestCellRenderFloat(t *testing.T) {
	style := &renderStyle{TableStyle: TableStyle{}, cellWidths: map[int]int{}}
	cell := createCell(0, 12.345, nil)

	output := cell.Render(style)
	if output != "12.35" {
		t.Fatal("Unexpected output:", output)
	}
}

func TestCellRenderPadding(t *testing.T) {
	style := &renderStyle{TableStyle: TableStyle{PaddingLeft: 3, PaddingRight: 4}, cellWidths: map[int]int{}}

	cell := createCell(0, "foobar", nil)

	output := cell.Render(style)
	if output != "   foobar    " {
		t.Fatal("Unexpected output:", output)
	}
}

type foo struct {
	v string
}

func (f *foo) String() string {
	return f.v
}

func TestCellRenderStringerStruct(t *testing.T) {
	style := &renderStyle{TableStyle: TableStyle{}, cellWidths: map[int]int{}}
	cell := createCell(0, &foo{v: "bar"}, nil)

	output := cell.Render(style)
	if output != "bar" {
		t.Fatal("Unexpected output:", output)
	}
}

type fooString string

func TestCellRenderGeneric(t *testing.T) {
	style := &renderStyle{TableStyle: TableStyle{}, cellWidths: map[int]int{}}
	cell := createCell(0, fooString("baz"), nil)

	output := cell.Render(style)
	if output != "baz" {
		t.Fatal("Unexpected output:", output)
	}
}

func TestFilterColorCodes(t *testing.T) {
	tests := []struct {
		in  string
		out string
	}{
		{"abc", "abc"},
		{"", ""},
		{"\033[31m\033[0m", ""},
		{"a\033[31mb\033[0mc", "abc"},
		{"\033[31mabc\033[0m", "abc"},
		{"\033[31mfoo\033[0mbar", "foobar"},
		{"\033[31mfoo\033[mbar", "foobar"},
		{"\033[31mfoo\033[0;0mbar", "foobar"},
		{"\033[31;4mfoo\033[0mbar", "foobar"},
		{"\033[31;4;43mfoo\033[0mbar", "foobar"},
	}
	for _, test := range tests {
		got := filterColorCodes(test.in)
		if got != test.out {
			t.Errorf("Invalid color-code filter result; expected %q but got %q from input %q",
				test.out, got, test.in)
		}
	}
}