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

Add utils.Includes() helper function

This commit is contained in:
Chris Cummer
2019-08-20 16:39:15 -07:00
parent faf365baa1
commit c832db9ddb
3 changed files with 85 additions and 17 deletions

View File

@@ -33,12 +33,7 @@ const (
// > true
//
func DoesNotInclude(strs []string, val string) bool {
for _, str := range strs {
if val == str {
return false
}
}
return true
return !Includes(strs, val)
}
// ExecuteCommand executes an external command on the local machine as the current user
@@ -76,6 +71,26 @@ func FindMatch(pattern string, data string) [][]string {
return r.FindAllStringSubmatch(data, -1)
}
// Includes takes a slice of strings and a target string and returns
// TRUE if the slice includes the target, FALSE if it does not
//
// Example:
//
// x := Includes([]string{"cat", "dog", "rat"}, "dog")
// > true
//
// x := Includes([]string{"cat", "dog", "rat"}, "pig")
// > false
//
func Includes(strs []string, val string) bool {
for _, str := range strs {
if val == str {
return true
}
}
return false
}
// OpenFile opens the file defined in `path` via the operating system
func OpenFile(path string) {
if (strings.HasPrefix(path, "http://")) || (strings.HasPrefix(path, "https://")) {

View File

@@ -9,8 +9,35 @@ import (
)
func Test_DoesNotInclude(t *testing.T) {
Equal(t, true, DoesNotInclude([]string{"cat", "dog", "rat"}, "bat"))
Equal(t, false, DoesNotInclude([]string{"cat", "dog", "rat"}, "dog"))
tests := []struct {
name string
strs []string
val string
expected bool
}{
{
name: "when included",
strs: []string{"a", "b", "c"},
val: "b",
expected: false,
},
{
name: "when not included",
strs: []string{"a", "b", "c"},
val: "f",
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := DoesNotInclude(tt.strs, tt.val)
if tt.expected != actual {
t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual)
}
})
}
}
func Test_ExecuteCommand(t *testing.T) {
@@ -50,6 +77,38 @@ func Test_FindMatch(t *testing.T) {
Equal(t, expected, result)
}
func Test_Includes(t *testing.T) {
tests := []struct {
name string
strs []string
val string
expected bool
}{
{
name: "when included",
strs: []string{"a", "b", "c"},
val: "b",
expected: true,
},
{
name: "when not included",
strs: []string{"a", "b", "c"},
val: "f",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := Includes(tt.strs, tt.val)
if tt.expected != actual {
t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual)
}
})
}
}
func Test_ReadFileBytes(t *testing.T) {
tests := []struct {
name string