mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
added bargraph unit test
This commit is contained in:
parent
02c07aa96f
commit
ae5ed9ee86
@ -11,9 +11,10 @@ import (
|
|||||||
|
|
||||||
//BarGraph lets make graphs
|
//BarGraph lets make graphs
|
||||||
type BarGraph struct {
|
type BarGraph struct {
|
||||||
enabled bool
|
enabled bool
|
||||||
focusable bool
|
focusable bool
|
||||||
|
starChar string
|
||||||
|
maxStars int
|
||||||
Name string
|
Name string
|
||||||
RefreshedAt time.Time
|
RefreshedAt time.Time
|
||||||
RefreshInt int
|
RefreshInt int
|
||||||
@ -27,9 +28,10 @@ type BarGraph struct {
|
|||||||
// NewBarGraph initialize your fancy new graph
|
// NewBarGraph initialize your fancy new graph
|
||||||
func NewBarGraph(name string, configKey string, focusable bool) BarGraph {
|
func NewBarGraph(name string, configKey string, focusable bool) BarGraph {
|
||||||
widget := BarGraph{
|
widget := BarGraph{
|
||||||
enabled: Config.UBool(fmt.Sprintf("wtf.mods.%s.enabled", configKey), false),
|
enabled: Config.UBool(fmt.Sprintf("wtf.mods.%s.enabled", configKey), false),
|
||||||
focusable: focusable,
|
focusable: focusable,
|
||||||
|
starChar: Config.UString(fmt.Sprintf("wtf.mods.%s.graphIcon", configKey), name),
|
||||||
|
maxStars: Config.UInt(fmt.Sprintf("wtf.mods.%s.graphStars", configKey), 20),
|
||||||
Name: Config.UString(fmt.Sprintf("wtf.mods.%s.title", configKey), name),
|
Name: Config.UString(fmt.Sprintf("wtf.mods.%s.title", configKey), name),
|
||||||
RefreshInt: Config.UInt(fmt.Sprintf("wtf.mods.%s.refreshInterval", configKey)),
|
RefreshInt: Config.UInt(fmt.Sprintf("wtf.mods.%s.refreshInterval", configKey)),
|
||||||
}
|
}
|
||||||
@ -99,8 +101,14 @@ func (widget *BarGraph) addView() {
|
|||||||
|
|
||||||
// BuildBars will build a string of * to represent your data of [time][value]
|
// BuildBars will build a string of * to represent your data of [time][value]
|
||||||
// time should be passed as a int64
|
// time should be passed as a int64
|
||||||
func (widget *BarGraph) BuildBars(maxStars int, starChar string, data [][2]int64) {
|
func (widget *BarGraph) BuildBars(data [][2]int64) {
|
||||||
|
|
||||||
|
widget.View.SetText(BuildStars(data, widget.maxStars, widget.starChar))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//BuildStars build the string to display
|
||||||
|
func BuildStars(data [][2]int64, maxStars int, starChar string) string {
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
|
|
||||||
//counter to inintialize min value
|
//counter to inintialize min value
|
||||||
@ -158,8 +166,7 @@ func (widget *BarGraph) BuildBars(maxStars int, starChar string, data [][2]int64
|
|||||||
buffer.WriteString(fmt.Sprintf("%s -\t [red]%s[white] - (%d)\n", t.Format("Jan 02, 2006"), stars, val))
|
buffer.WriteString(fmt.Sprintf("%s -\t [red]%s[white] - (%d)\n", t.Format("Jan 02, 2006"), stars, val))
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.View.SetText(buffer.String())
|
return buffer.String()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------------------- Exported Functions -------------------- */
|
/* -------------------- Exported Functions -------------------- */
|
||||||
|
33
wtf_tests/bargraph/bargraph_test.go
Normal file
33
wtf_tests/bargraph/bargraph_test.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package bargraphtests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/senorprogrammer/wtf/wtf"
|
||||||
|
. "github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MakeData - Create sample data
|
||||||
|
func makeData() [][2]int64 {
|
||||||
|
|
||||||
|
//this could come from config
|
||||||
|
const lineCount = 2
|
||||||
|
var stats [lineCount][2]int64
|
||||||
|
|
||||||
|
stats[0][1] = 1530122942
|
||||||
|
stats[0][0] = 100
|
||||||
|
|
||||||
|
stats[1][1] = 1530132942
|
||||||
|
stats[1][0] = 210
|
||||||
|
|
||||||
|
return stats[:]
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//TestOutput of the bargraph make string (BuildStars) function
|
||||||
|
func TestOutput(t *testing.T) {
|
||||||
|
|
||||||
|
result := BuildStars(makeData(), 20, "*")
|
||||||
|
|
||||||
|
Equal(t, result, "Jan 18, 1970 -\t [red]*[white] - (100)\nJan 18, 1970 -\t [red]********************[white] - (210)\n")
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user