Archived
1
0
Fork 0

Bugfix include and exclude regexes

Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
Julien Riou 2021-03-01 09:06:00 +01:00
parent 14576cde0e
commit 6f002f007d
No known key found for this signature in database
GPG key ID: FF42D23B580C89F7
2 changed files with 103 additions and 10 deletions

View file

@ -23,17 +23,23 @@ type Parser struct {
// NewParser to create a new Parser instance
func NewParser(includeRegex string, excludeRegex string) (*Parser, error) {
var err error
var includeRegexCompiled, excludeRegexCompiled *regexp.Regexp
log.Debugf("compiling include name regex")
includeRegexCompiled, err := regexp.Compile(includeRegex)
if err != nil {
return nil, err
if includeRegex != "" {
includeRegexCompiled, err = regexp.Compile(includeRegex)
if err != nil {
return nil, err
}
}
log.Debugf("compiling exclude name regex")
excludeRegexCompiled, err := regexp.Compile(excludeRegex)
if err != nil {
return nil, err
if excludeRegex != "" {
excludeRegexCompiled, err = regexp.Compile(excludeRegex)
if err != nil {
return nil, err
}
}
log.Debugf("creating context with headless browser drivers")
@ -105,13 +111,14 @@ func (p *Parser) filterExclusive(products []*Product) []*Product {
var filtered []*Product
if p.excludeRegex != nil {
for _, product := range products {
if !p.excludeRegex.MatchString(product.Name) {
log.Debugf("product %s included because it matches does not match the exclude regex", product.Name)
filtered = append(filtered, product)
} else {
if p.excludeRegex.MatchString(product.Name) {
log.Debugf("product %s excluded because it matches the exclude regex", product.Name)
} else {
log.Debugf("product %s included because it does not match the exclude regex", product.Name)
filtered = append(filtered, product)
}
}
return filtered
}
return products
}

86
parser_test.go Normal file
View file

@ -0,0 +1,86 @@
package main
import (
"fmt"
"testing"
)
func TestFilterInclusive(t *testing.T) {
tests := []struct {
regex string // inclusive 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", true}, // 3060 in the include regex
{"(?i)(rtx|rx)(.*)(3060|3070|3080|3090|5700|6800|6900)( )?(xt|ti)?", "ASUS AMD Radeon RX 5600 XT TUF Gaming X3", false}, // 5600 not in the include regex
{"", "MSI GeForce RTX 3060 GAMING X", true}, // do nothing when the include regex is empty
}
for i, tc := range tests {
t.Run(fmt.Sprintf("TestFilterInclusive#%d", i), func(t *testing.T) {
p, err := NewParser(tc.regex, "")
if err != nil {
t.Errorf("failed to initialize parser: %s", err)
} else {
products := []*Product{{Name: tc.name}}
filtered := p.filterInclusive(products)
included := false
for _, product := range filtered {
if product.Name == tc.name && !included {
included = true
}
}
if tc.included != 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)
}
}
}
})
}
}
func TestFilterExclusive(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("TestFilterExclusive#%d", i), func(t *testing.T) {
p, err := NewParser("", tc.regex)
if err != nil {
t.Errorf("failed to initialize parser: %s", err)
} else {
products := []*Product{{Name: tc.name}}
filtered := p.filterExclusive(products)
included := false
for _, product := range filtered {
if product.Name == tc.name && !included {
included = true
}
}
if tc.included != 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)
}
}
}
})
}
}