diff --git a/pkg/itemfilter/itemfilter_test.go b/pkg/itemfilter/itemfilter_test.go new file mode 100644 index 0000000..dfb9633 --- /dev/null +++ b/pkg/itemfilter/itemfilter_test.go @@ -0,0 +1,45 @@ +package itemfilter + +import ( + "testing" + + "github.com/hectorgimenez/d2go/pkg/data" + "github.com/hectorgimenez/d2go/pkg/data/item" + "github.com/hectorgimenez/d2go/pkg/nip" + "github.com/stretchr/testify/require" +) + +func TestEvaluate(t *testing.T) { + type args struct { + i data.Item + nipRule string // not the best test but too lazy to write rules manually + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "", + args: args{ + i: data.Item{ + Name: item.Name("lightplatedboots"), + Quality: item.QualityUnique, + Ethereal: false, + }, + nipRule: "[name] == lightplatedboots && [quality] == unique && [flag] != ethereal # [enhanceddefense] == 60 // goblin toe", + }, + want: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rule, err := nip.ParseLine(tt.args.nipRule) + require.NoError(t, err) + + if got := Evaluate(tt.args.i, []nip.Rule{rule}); got != tt.want { + t.Errorf("Evaluate() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/nip/file_reader.go b/pkg/nip/file_reader.go index 24198f0..d16f196 100644 --- a/pkg/nip/file_reader.go +++ b/pkg/nip/file_reader.go @@ -43,7 +43,7 @@ func ParseNIPFile(filePath string) ([]Rule, error) { rules := make([]Rule, 0) for fileScanner.Scan() { - rule, err := parseLine(fileScanner.Text()) + rule, err := ParseLine(fileScanner.Text()) if errors.Is(err, errEmptyLine) { continue } diff --git a/pkg/nip/nip_parser.go b/pkg/nip/nip_parser.go index 1e6eaeb..970ba43 100644 --- a/pkg/nip/nip_parser.go +++ b/pkg/nip/nip_parser.go @@ -14,7 +14,7 @@ var ( propertyNameRegex = regexp.MustCompile(`\[(.*)\]`) ) -func parseLine(line string) (Rule, error) { +func ParseLine(line string) (Rule, error) { line = lineCleanup(line) if line == "" { return Rule{}, errEmptyLine diff --git a/pkg/nip/nip_parser_test.go b/pkg/nip/nip_parser_test.go index 0cca470..76ec831 100644 --- a/pkg/nip/nip_parser_test.go +++ b/pkg/nip/nip_parser_test.go @@ -10,7 +10,7 @@ import ( const nipLine = "[type] == boots && [quality] == rare # [frw] >= 10 && [fireresist] >= 10 && ([lightresist]+[coldresist] >= 10 && [dexterity] >= 1 && [fireresist]+[poisonresist] >= 10) // this is a comment" func Test_parseLine(t *testing.T) { - rules, err := parseLine(nipLine) + rules, err := ParseLine(nipLine) require.NoError(t, err) expected := Rule{