This repository has been archived by the owner on Oct 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* draft matcher * factor keyword * document exported types * replace error format * comment constants * rename matcher to match * fix logic of contains * rename match type * fix tests * remove the interface Co-authored-by: Harry Liu <byliuyang11@gmail.com>
- Loading branch information
1 parent
ad42a52
commit 284d1d6
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package matcher | ||
|
||
import "strings" | ||
|
||
// ContainsAll checks whether the input contains all the elements in the list. | ||
func ContainsAll(words []string, input string) bool { | ||
for _, word := range words { | ||
if !strings.Contains(input, word) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package matcher | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/short-d/app/fw/assert" | ||
) | ||
|
||
func TestContainsAll_IsMatch(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
name string | ||
words []string | ||
input string | ||
expected bool | ||
}{ | ||
{ | ||
name: "empty words and empty input", | ||
words: nil, | ||
input: "", | ||
expected: true, | ||
}, | ||
{ | ||
name: "empty words", | ||
words: nil, | ||
input: "a", | ||
expected: true, | ||
}, | ||
{ | ||
name: "empty input", | ||
words: []string{"a"}, | ||
input: "", | ||
expected: false, | ||
}, | ||
{ | ||
name: "complete match", | ||
words: []string{"a"}, | ||
input: "a", | ||
expected: true, | ||
}, | ||
{ | ||
name: "all match", | ||
words: []string{"a", "ab", "aa"}, | ||
input: "aaaba", | ||
expected: true, | ||
}, | ||
{ | ||
name: "no match", | ||
words: []string{"a", "b", "c"}, | ||
input: "xyz", | ||
expected: false, | ||
}, | ||
{ | ||
name: "at least one mismatch", | ||
words: []string{"a", "b", "c"}, | ||
input: "xcz", | ||
expected: false, | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(testCase.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.Equal(t, testCase.expected, ContainsAll(testCase.words, testCase.input)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package matcher | ||
|
||
import "strings" | ||
|
||
// ContainsAny checks whether the input contains any element of the list. | ||
func ContainsAny(words []string, input string) bool { | ||
for _, word := range words { | ||
if strings.Contains(input, word) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package matcher | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/short-d/app/fw/assert" | ||
) | ||
|
||
func TestContainsAny_IsMatch(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
name string | ||
words []string | ||
input string | ||
expected bool | ||
}{ | ||
{ | ||
name: "empty words and empty input", | ||
words: nil, | ||
input: "", | ||
expected: false, | ||
}, | ||
{ | ||
name: "empty words", | ||
words: nil, | ||
input: "a", | ||
expected: false, | ||
}, | ||
{ | ||
name: "empty input", | ||
words: []string{"a"}, | ||
input: "", | ||
expected: false, | ||
}, | ||
{ | ||
name: "complete match", | ||
words: []string{"a"}, | ||
input: "a", | ||
expected: true, | ||
}, | ||
{ | ||
name: "all match", | ||
words: []string{"a", "ab", "aa"}, | ||
input: "aaaba", | ||
expected: true, | ||
}, | ||
{ | ||
name: "no match", | ||
words: []string{"a", "b", "c"}, | ||
input: "xyz", | ||
expected: false, | ||
}, | ||
{ | ||
name: "any one match", | ||
words: []string{"a", "b", "c"}, | ||
input: "xcz", | ||
expected: true, | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(testCase.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.Equal(t, testCase.expected, ContainsAny(testCase.words, testCase.input)) | ||
}) | ||
} | ||
} |