+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions merrors/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
//
// Example 3:
//
// func CloseAll(closers []io.Closer) error {
// errs := merrors.New()
// for _ , c := range closers {
// errs.Add(c.Close())
// }
// return errs.Err()
// }
//
// func CloseAll(closers []io.Closer) error {
// errs := merrors.New()
// for _ , c := range closers {
// errs.Add(c.Close())
// }
// return errs.Err()
// }
package merrors
46 changes: 46 additions & 0 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,52 @@ func FaultOrPanicToErr(f func()) (err error) {
return err
}

// ContainsStringSlice fails the test if needle is not contained within haystack, if haystack or needle is
// an empty slice, or if needle is longer than haystack.
func ContainsStringSlice(tb testing.TB, haystack, needle []string) {
_, file, line, _ := runtime.Caller(1)

if !contains(haystack, needle) {
tb.Fatalf(sprintfWithLimit("\033[31m%s:%d: %#v does not contain %#v\033[39m\n\n", filepath.Base(file), line, haystack, needle))
}
}

func contains(haystack, needle []string) bool {
if len(haystack) == 0 || len(needle) == 0 {
return false
}

if len(haystack) < len(needle) {
return false
}

for i := 0; i < len(haystack); i++ {
outer := i

for j := 0; j < len(needle); j++ {
// End of the haystack but not the end of the needle, end
if outer == len(haystack) {
return false
}

// No match, try the next index of the haystack
if haystack[outer] != needle[j] {
break
}

// End of the needle and it still matches, end
if j == len(needle)-1 {
return true
}

// This element matches between the two slices, try the next one
outer++
}
}

return false
}

func sprintfWithLimit(act string, v ...interface{}) string {
s := fmt.Sprintf(act, v...)
if len(s) > 10000 {
Expand Down
70 changes: 70 additions & 0 deletions testutil/testutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) The EfficientGo Authors.
// Licensed under the Apache License 2.0.

package testutil

import "testing"

func TestContains(t *testing.T) {
tests := map[string]struct {
haystack []string
needle []string
shouldMatch bool
}{
"empty haystack": {
haystack: []string{},
needle: []string{"key1"},
shouldMatch: false,
},

"empty needle": {
haystack: []string{"key1", "key2", "key3"},
needle: []string{},
shouldMatch: false,
},

"single value needle": {
haystack: []string{"key1", "key2", "key3"},
needle: []string{"key1"},
shouldMatch: true,
},

"multiple value needle": {
haystack: []string{"key1", "key2", "key3"},
needle: []string{"key1", "key2"},
shouldMatch: true,
},

"same size needle as haystack": {
haystack: []string{"key1", "key2", "key3"},
needle: []string{"key1", "key2", "key3"},
shouldMatch: true,
},

"larger needle than haystack": {
haystack: []string{"key1", "key2", "key3"},
needle: []string{"key1", "key2", "key3", "key4"},
shouldMatch: false,
},

"needle not contained": {
haystack: []string{"key1", "key2", "key3"},
needle: []string{"key4"},
shouldMatch: false,
},

"haystack ends before needle": {
haystack: []string{"key1", "key2", "key3"},
needle: []string{"key3", "key4"},
shouldMatch: false,
},
}

for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
if testData.shouldMatch != contains(testData.haystack, testData.needle) {
t.Fatalf("unexpected result testing contains() with %#v", testData)
}
})
}
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载