refactor: move filters out of parser
Filters are now separate structures to include a product or not based on their own set of properties. For now, include and exclude filters are supported. They take a regex as an argument and include a product if the regex matches (or doesn't match) the product name. This commit will allow us to create new filters on product like on a price range. Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
parent
b6feb2d656
commit
244c9f68e7
10 changed files with 212 additions and 206 deletions
41
filter_exclude_test.go
Normal file
41
filter_exclude_test.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExcludeFilter(t *testing.T) {
|
||||
tests := []struct {
|
||||
regex string // exclusive regex
|
||||
name string // product name
|
||||
included bool // should be included or not
|
||||
}{
|
||||
{"(?i)(rtx|rx)(.*)(3060|3070|3080|3090|5700|6800|6900)( )?(xt|ti)?", "MSI GeForce RTX 3060 GAMING X", false}, // 3060 in the exclude regex
|
||||
{"(?i)(rtx|rx)(.*)(3060|3070|3080|3090|5700|6800|6900)( )?(xt|ti)?", "ASUS AMD Radeon RX 5600 XT TUF Gaming X3", true}, // 5600 not in the exclude regex
|
||||
{"", "MSI GeForce RTX 3060 GAMING X", true}, // do nothing when the exclude regex is empty
|
||||
}
|
||||
|
||||
for i, tc := range tests {
|
||||
t.Run(fmt.Sprintf("TestExcludeFilter#%d", i), func(t *testing.T) {
|
||||
product := &Product{Name: tc.name}
|
||||
filter, err := NewExcludeFilter(tc.regex)
|
||||
if err != nil {
|
||||
t.Errorf("cannot create filter with regex '%s': %s", tc.regex, err)
|
||||
}
|
||||
|
||||
included := filter.Include(product)
|
||||
|
||||
if included != tc.included {
|
||||
t.Errorf("regex '%s' for product '%s': got included=%t, want included=%t", tc.regex, tc.name, included, tc.included)
|
||||
} else {
|
||||
if included {
|
||||
t.Logf("regex '%s' includes product '%s'", tc.regex, tc.name)
|
||||
} else {
|
||||
t.Logf("regex '%s' excludes product '%s'", tc.regex, tc.name)
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
Reference in a new issue