Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Commit

Permalink
Add match package (#864)
Browse files Browse the repository at this point in the history
* 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
arberiii and coderworld10 authored Jun 15, 2020
1 parent ad42a52 commit 284d1d6
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
13 changes: 13 additions & 0 deletions backend/app/usecase/matcher/contains_all.go
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
}
69 changes: 69 additions & 0 deletions backend/app/usecase/matcher/contains_all_test.go
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))
})
}
}
13 changes: 13 additions & 0 deletions backend/app/usecase/matcher/contains_any.go
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
}
69 changes: 69 additions & 0 deletions backend/app/usecase/matcher/contains_any_test.go
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))
})
}
}

0 comments on commit 284d1d6

Please sign in to comment.