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

@@ -3,7 +3,7 @@ package utils
import (
"testing"
. "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)
func Test_MapToStrs(t *testing.T) {
@@ -18,7 +18,7 @@ func Test_MapToStrs(t *testing.T) {
source[val] = val
}
Equal(t, expected, MapToStrs(source))
assert.Equal(t, expected, MapToStrs(source))
}
func Test_ToInts(t *testing.T) {
@@ -29,16 +29,23 @@ func Test_ToInts(t *testing.T) {
source[idx] = val
}
Equal(t, expected, ToInts(source))
assert.Equal(t, expected, ToInts(source))
}
func Test_ToStrs(t *testing.T) {
expected := []string{"cat", "dog", "rat"}
expectedInts := []int{1, 2, 3}
expectedStrs := []string{"1", "2", "3"}
source := make([]interface{}, len(expected))
for idx, val := range expected {
source[idx] = val
fromInts := make([]interface{}, 3)
for idx, val := range expectedInts {
fromInts[idx] = val
}
Equal(t, expected, ToStrs(source))
fromStrs := make([]interface{}, 3)
for idx, val := range expectedStrs {
fromStrs[idx] = val
}
assert.Equal(t, expectedStrs, ToStrs(fromInts))
assert.Equal(t, expectedStrs, ToStrs(fromStrs))
}

View File

@@ -26,16 +26,10 @@ func ExpandHomeDir(path string) (string, error) {
return "", errors.New("cannot expand user-specific home dir")
}
dir, err := Home()
dir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(dir, path[1:]), nil
}
// Home returns the home directory for the executing user.
// An error is returned if a home directory cannot be detected.
func Home() (string, error) {
return os.UserHomeDir()
}

52
utils/homedir_test.go Normal file
View File

@@ -0,0 +1,52 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_ExpandHomeDir(t *testing.T) {
tests := []struct {
name string
path string
expectedStart string
expectedContains string
expectedError error
}{
{
name: "with empty path",
path: "",
expectedStart: "",
expectedContains: "",
expectedError: nil,
},
{
name: "with relative path",
path: "~/test",
expectedStart: "/",
expectedContains: "/test",
expectedError: nil,
},
{
name: "with absolute path",
path: "/Users/test",
expectedStart: "/",
expectedContains: "/test",
expectedError: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, err := ExpandHomeDir(tt.path)
if len(tt.path) > 0 {
assert.Equal(t, tt.expectedStart, string(actual[0]))
}
assert.Contains(t, actual, tt.expectedContains)
assert.Equal(t, tt.expectedError, err)
})
}
}

View File

@@ -3,18 +3,26 @@ package utils
import (
"testing"
. "github.com/stretchr/testify/assert"
"github.com/rivo/tview"
"github.com/stretchr/testify/assert"
)
func Test_CenterText(t *testing.T) {
Equal(t, "cat", CenterText("cat", -9))
Equal(t, "cat", CenterText("cat", 0))
Equal(t, " cat ", CenterText("cat", 9))
assert.Equal(t, "cat", CenterText("cat", -9))
assert.Equal(t, "cat", CenterText("cat", 0))
assert.Equal(t, " cat ", CenterText("cat", 9))
}
func Test_HighlightableHelper(t *testing.T) {
view := tview.NewTextView()
actual := HighlightableHelper(view, "cats", 0, 5)
assert.Equal(t, "[\"0\"][\"\"]cats [\"\"]\n", actual)
}
func Test_RowPadding(t *testing.T) {
Equal(t, "", RowPadding(0, 0))
Equal(t, "", RowPadding(5, 2))
Equal(t, " ", RowPadding(1, 2))
Equal(t, " ", RowPadding(0, 5))
assert.Equal(t, "", RowPadding(0, 0))
assert.Equal(t, "", RowPadding(5, 2))
assert.Equal(t, " ", RowPadding(1, 2))
assert.Equal(t, " ", RowPadding(0, 5))
}

View File

@@ -121,8 +121,8 @@ func ReadFileBytes(filePath string) ([]byte, error) {
return fileData, nil
}
// ParseJson is a standard JSON reader from text
func ParseJson(obj interface{}, text io.Reader) error {
// ParseJSON is a standard JSON reader from text
func ParseJSON(obj interface{}, text io.Reader) error {
d := json.NewDecoder(text)
return d.Decode(obj)
}
@@ -139,10 +139,10 @@ func CalculateDimensions(moduleConfig, globalConfig *config.Config) (int, int) {
rows := ToInts(globalConfig.UList("wtf.grid.rows"))
// Make sure the values are in bounds
left = clamp(left, 0, len(cols)-1)
top = clamp(top, 0, len(rows)-1)
width = clamp(width, 0, len(cols)-left)
height = clamp(height, 0, len(rows)-top)
left = Clamp(left, 0, len(cols)-1)
top = Clamp(top, 0, len(rows)-1)
width = Clamp(width, 0, len(cols)-left)
height = Clamp(height, 0, len(rows)-top)
// Start with the border subtracted and add all the spanned rows and cols
w, h := -2, -2
@@ -154,20 +154,35 @@ func CalculateDimensions(moduleConfig, globalConfig *config.Config) (int, int) {
}
// The usable space may be empty
w = max(w, 0)
h = max(h, 0)
w = MaxInt(w, 0)
h = MaxInt(h, 0)
return w, h
}
func max(a, b int) int {
if a > b {
return a
// MaxInt returns the larger of x or y
//
// Examples:
//
// MaxInt(3, 2) => 3
// MaxInt(2, 3) => 3
//
func MaxInt(x, y int) int {
if x > y {
return x
}
return b
return y
}
func clamp(x, a, b int) int {
// Clamp restricts values to a minimum and maximum value
//
// Examples:
//
// clamp(6, 3, 8) => 4
// clamp(1, 3, 8) => 3
// clamp(9, 3, 8) => 8
//
func Clamp(x, a, b int) int {
if a > x {
return a
}

View File

@@ -5,7 +5,7 @@ import (
"reflect"
"testing"
. "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)
func Test_DoesNotInclude(t *testing.T) {
@@ -74,7 +74,8 @@ func Test_FindMatch(t *testing.T) {
expected := [][]string([][]string{[]string{"SSID: 7E5B5C", "7E5B5C"}})
result = FindMatch(`s*SSID: (.+)s*`, "SSID: 7E5B5C")
Equal(t, expected, result)
assert.Equal(t, expected, result)
}
func Test_Includes(t *testing.T) {