-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchartgo_test.go
More file actions
141 lines (125 loc) · 3.47 KB
/
Copy pathchartgo_test.go
File metadata and controls
141 lines (125 loc) · 3.47 KB
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
139
140
141
package main
import (
"strings"
"testing"
"chartgo/canvas"
"chartgo/components"
"chartgo/utils"
)
func TestCanvas(t *testing.T) {
c := canvas.New(10, 5)
if c.Width() != 10 {
t.Errorf("Expected width 10, got %d", c.Width())
}
if c.Height() != 5 {
t.Errorf("Expected height 5, got %d", c.Height())
}
if c.PixelWidth() != 20 {
t.Errorf("Expected pixel width 20, got %d", c.PixelWidth())
}
if c.PixelHeight() != 20 {
t.Errorf("Expected pixel height 20, got %d", c.PixelHeight())
}
c.Set(0, 0)
if !c.Get(0, 0) {
t.Error("Expected pixel (0,0) to be set")
}
c.Unset(0, 0)
if c.Get(0, 0) {
t.Error("Expected pixel (0,0) to be unset")
}
c.Set(-1, -1)
c.Set(100, 100)
c.DrawLine(0, 0, 19, 19)
output := c.Render()
if len(output) == 0 {
t.Error("Expected non-empty render output")
}
}
func TestUtils(t *testing.T) {
data := []float64{1, 5, 3, 9, 2}
if utils.Min(data) != 1 {
t.Errorf("Expected min 1, got %f", utils.Min(data))
}
if utils.Max(data) != 9 {
t.Errorf("Expected max 9, got %f", utils.Max(data))
}
if utils.Sum(data) != 20 {
t.Errorf("Expected sum 20, got %f", utils.Sum(data))
}
scaled := utils.Scale(5, 0, 10, 0, 100)
if scaled != 50 {
t.Errorf("Expected scaled value 50, got %f", scaled)
}
}
func TestKPICard(t *testing.T) {
kpi := components.NewKPICard("CPU", "85%").SetWidth(20)
output := kpi.View()
if !strings.Contains(output, "CPU") {
t.Error("Expected KPI card to contain title")
}
if !strings.Contains(output, "85%") {
t.Error("Expected KPI card to contain value")
}
}
func TestLineChart(t *testing.T) {
data := []float64{10, 20, 15, 30, 25}
chart := components.NewLineChart(data).SetWidth(20).SetHeight(5).SetTitle("Test")
output := chart.View()
if len(output) == 0 {
t.Error("Expected non-empty line chart output")
}
if !strings.Contains(output, "Test") {
t.Error("Expected line chart to contain title")
}
}
func TestLineChartEmpty(t *testing.T) {
chart := components.NewLineChart([]float64{})
output := chart.View()
if !strings.Contains(output, "No data") {
t.Error("Expected 'No data' message for empty line chart")
}
}
func TestBarChart(t *testing.T) {
data := []float64{80, 60, 40}
labels := []string{"A", "B", "C"}
chart := components.NewBarChart(data, labels).SetWidth(40).SetTitle("Test")
output := chart.View()
if !strings.Contains(output, "A") {
t.Error("Expected bar chart to contain label A")
}
if !strings.Contains(output, "Test") {
t.Error("Expected bar chart to contain title")
}
}
func TestFunnelChart(t *testing.T) {
data := []float64{100, 75, 50}
labels := []string{"Step1", "Step2", "Step3"}
chart := components.NewFunnelChart(data, labels).SetWidth(40).SetTitle("Funnel")
output := chart.View()
if !strings.Contains(output, "Step1") {
t.Error("Expected funnel chart to contain label")
}
if !strings.Contains(output, "Funnel") {
t.Error("Expected funnel chart to contain title")
}
}
func TestPieChart(t *testing.T) {
data := []float64{50, 30, 20}
labels := []string{"X", "Y", "Z"}
chart := components.NewPieChart(data, labels).SetWidth(40).SetTitle("Pie")
output := chart.View()
if !strings.Contains(output, "X") {
t.Error("Expected pie chart to contain label")
}
if !strings.Contains(output, "Pie") {
t.Error("Expected pie chart to contain title")
}
}
func TestPieChartEmpty(t *testing.T) {
chart := components.NewPieChart([]float64{}, []string{})
output := chart.View()
if !strings.Contains(output, "No data") {
t.Error("Expected 'No data' message for empty pie chart")
}
}