1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

WTF-657 Add spec coverage for cfg/common_settings.go (#728)

* WTF-657 Add spec coverage for cfg/common_settings.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for cfg/position_validation.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for cfg/validations.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for checklist/checklist.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for checklist/checklist_item.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for utils/conversions.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Get rid of utils.Home() function

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for utils/homedir.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for utils/text.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Clean up utils/utils.go

Signed-off-by: Chris Cummer <chriscummer@me.com>
This commit is contained in:
Chris Cummer
2019-10-30 17:35:00 -07:00
committed by GitHub
parent 837dc92cfc
commit 1bfca29d17
31 changed files with 857 additions and 69 deletions

View File

@@ -14,8 +14,8 @@ type Colors struct {
BorderNormal string
Checked string
Foreground string
HighlightFore string
HighlightBack string
HighlightFore string
Text string
Title string
@@ -83,11 +83,11 @@ func NewCommonSettingsFromModule(name, defaultTitle string, defaultFocusable boo
PositionSettings: NewPositionSettingsFromYAML(name, moduleConfig),
Bordered: moduleConfig.UBool("border", true),
Config: moduleConfig,
Enabled: moduleConfig.UBool("enabled", false),
Focusable: moduleConfig.UBool("focusable", defaultFocusable),
RefreshInterval: moduleConfig.UInt("refreshInterval", 300),
Title: moduleConfig.UString("title", defaultTitle),
Config: moduleConfig,
focusChar: moduleConfig.UInt("focusChar", -1),
}
@@ -135,7 +135,7 @@ func (common *Common) RightAlignFormat(width int) string {
return fmt.Sprintf("%%%ds", width-borderOffset)
}
func (common *Common) SigilStr(len, pos int, width int) string {
func (common *Common) SigilStr(len, pos, width int) string {
sigils := ""
if len > 1 {

187
cfg/common_settings_test.go Normal file
View File

@@ -0,0 +1,187 @@
package cfg
import (
"testing"
"github.com/olebedev/config"
"github.com/stretchr/testify/assert"
)
var (
testYaml = `
wtf:
colors:
`
moduleConfig, _ = config.ParseYaml(testYaml)
globalSettings, _ = config.ParseYaml(testYaml)
testCfg = NewCommonSettingsFromModule(
"test",
"Test Config",
true,
moduleConfig,
globalSettings,
)
)
func Test_NewCommonSettingsFromModule(t *testing.T) {
assert.Equal(t, true, testCfg.Bordered)
assert.Equal(t, false, testCfg.Enabled)
assert.Equal(t, true, testCfg.Focusable)
assert.Equal(t, "test", testCfg.Module.Name)
assert.Equal(t, "test", testCfg.Module.Type)
assert.Equal(t, "", testCfg.FocusChar())
assert.Equal(t, 300, testCfg.RefreshInterval)
assert.Equal(t, "Test Config", testCfg.Title)
}
func Test_DefaultFocusedRowColor(t *testing.T) {
assert.Equal(t, "black:green", testCfg.DefaultFocusedRowColor())
}
func Test_DefaultRowColor(t *testing.T) {
assert.Equal(t, "white:transparent", testCfg.DefaultRowColor())
}
func Test_FocusChar(t *testing.T) {
tests := []struct {
name string
expectedChar string
before func(testCfg *Common)
}{
{
name: "with no focus char specified",
expectedChar: "",
before: func(testCfg *Common) {
testCfg.focusChar = -1
},
},
{
name: "with explicit focus char specified",
expectedChar: "3",
before: func(testCfg *Common) {
testCfg.focusChar = 3
},
},
{
name: "with ridiculous focus char specified",
expectedChar: "Q",
before: func(testCfg *Common) {
testCfg.focusChar = 33
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.before(testCfg)
assert.Equal(t, tt.expectedChar, testCfg.FocusChar())
})
}
}
func Test_RowColor(t *testing.T) {
tests := []struct {
name string
idx int
expectedColor string
}{
{
name: "odd rows, default",
idx: 3,
expectedColor: "lightblue",
},
{
name: "even rows, default",
idx: 8,
expectedColor: "white",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expectedColor, testCfg.RowColor(tt.idx))
})
}
}
func Test_RightAlignFormat(t *testing.T) {
tests := []struct {
name string
width int
expected string
}{
{
name: "with zero",
width: 0,
expected: "%-2s",
},
{
name: "with positive integer",
width: 3,
expected: "%1s",
},
{
name: "with negative integer",
width: -3,
expected: "%-5s",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, testCfg.RightAlignFormat(tt.width))
})
}
}
func Test_SigilStr(t *testing.T) {
tests := []struct {
name string
len int
pos int
width int
expected string
}{
{
name: "with zero pages",
len: 0,
pos: 1,
width: 5,
expected: "",
},
{
name: "with one page",
len: 1,
pos: 1,
width: 5,
expected: "",
},
{
name: "with multiple pages",
len: 3,
pos: 1,
width: 5,
expected: "[lightblue]*_*[white]",
},
{
name: "with negative pages",
len: -3,
pos: 1,
width: 5,
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, testCfg.SigilStr(tt.len, tt.pos, tt.width))
})
}
}
func Test_Validations(t *testing.T) {
assert.Equal(t, 4, len(testCfg.Validations()))
}

View File

@@ -0,0 +1,14 @@
package cfg
import (
"errors"
)
var (
errTest = errors.New("Busted")
)
func ExampleDisplayError() {
displayError(errTest)
// Output: Error: Busted
}

View File

@@ -41,10 +41,10 @@ func NewPositionSettingsFromYAML(moduleName string, moduleConfig *config.Config)
pos := PositionSettings{
Validations: validations,
Top: validations.valueFor("top"),
Left: validations.valueFor("left"),
Width: validations.valueFor("width"),
Height: validations.valueFor("height"),
Top: validations.intValueFor("top"),
Left: validations.intValueFor("left"),
Width: validations.intValueFor("width"),
Height: validations.intValueFor("height"),
}
return pos

View File

@@ -54,6 +54,8 @@ func (posVal *positionValidation) String() string {
return fmt.Sprintf("Invalid value for %s:\t%d", aurora.Yellow(posVal.name), posVal.intVal)
}
/* -------------------- Unexported Functions -------------------- */
func newPositionValidation(name string, intVal int, err error) *positionValidation {
posVal := &positionValidation{
err: err,

View File

@@ -0,0 +1,26 @@
package cfg
import (
"errors"
"testing"
"github.com/alecthomas/assert"
)
var (
posVal = &positionValidation{
err: errors.New("Busted"),
name: "top",
intVal: -3,
}
)
func Test_Attributes(t *testing.T) {
assert.EqualError(t, posVal.Error(), "Busted")
assert.Equal(t, true, posVal.HasError())
assert.Equal(t, -3, posVal.IntValue())
assert.Contains(t, posVal.String(), "Invalid")
assert.Contains(t, posVal.String(), "top")
assert.Contains(t, posVal.String(), "-3")
}

View File

@@ -18,7 +18,7 @@ func (vals *Validations) append(key string, posVal Validatable) {
vals.validations[key] = posVal
}
func (vals *Validations) valueFor(key string) int {
func (vals *Validations) intValueFor(key string) int {
val := vals.validations[key]
if val != nil {
return val.IntValue()

38
cfg/validations_test.go Normal file
View File

@@ -0,0 +1,38 @@
package cfg
import (
"testing"
"github.com/stretchr/testify/assert"
)
var (
vals = NewValidations()
)
func Test_intValueFor(t *testing.T) {
vals.append("left", newPositionValidation("left", 3, nil))
tests := []struct {
name string
key string
expected int
}{
{
name: "with valid key",
key: "left",
expected: 3,
},
{
name: "with invalid key",
key: "cat",
expected: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, vals.intValueFor(tt.key))
})
}
}