use generic functions for dealing with lists.

This commit is contained in:
fiatjaf
2022-11-08 07:15:08 -03:00
parent c4d52e516f
commit f98f54d3be
7 changed files with 50 additions and 82 deletions

View File

@@ -4,6 +4,8 @@ import (
"encoding/json"
"testing"
"time"
"golang.org/x/exp/slices"
)
func TestFilterUnmarshal(t *testing.T) {
@@ -16,7 +18,7 @@ func TestFilterUnmarshal(t *testing.T) {
if f.Since == nil || f.Since.Format("2006-01-02") != "2022-02-07" ||
f.Until != nil ||
f.Tags == nil || len(f.Tags) != 2 || !f.Tags["something"].Contains("bab") {
f.Tags == nil || len(f.Tags) != 2 || !slices.Contains(f.Tags["something"], "bab") {
t.Error("failed to parse filter correctly")
}
}
@@ -25,7 +27,7 @@ func TestFilterMarshal(t *testing.T) {
tm := time.Unix(12345678, 0)
filterj, err := json.Marshal(Filter{
Kinds: IntList{1, 2, 4},
Kinds: []int{1, 2, 4},
Tags: TagMap{"fruit": {"banana", "mango"}},
Until: &tm,
})
@@ -40,20 +42,20 @@ func TestFilterMarshal(t *testing.T) {
}
func TestFilterMatching(t *testing.T) {
if (Filter{Kinds: IntList{4, 5}}).Matches(&Event{Kind: 6}) {
if (Filter{Kinds: []int{4, 5}}).Matches(&Event{Kind: 6}) {
t.Error("matched event that shouldn't have matched")
}
if !(Filter{Kinds: IntList{4, 5}}).Matches(&Event{Kind: 4}) {
if !(Filter{Kinds: []int{4, 5}}).Matches(&Event{Kind: 4}) {
t.Error("failed to match event by kind")
}
if !(Filter{
Kinds: IntList{4, 5},
Kinds: []int{4, 5},
Tags: TagMap{
"p": {"ooo"},
},
IDs: StringList{"prefix"},
IDs: []string{"prefix"},
}).Matches(&Event{
Kind: 4,
Tags: Tags{{"p", "ooo", ",x,x,"}, {"m", "yywyw", "xxx"}},
@@ -65,15 +67,15 @@ func TestFilterMatching(t *testing.T) {
func TestFilterEquality(t *testing.T) {
if !FilterEqual(
Filter{Kinds: IntList{4, 5}},
Filter{Kinds: IntList{4, 5}},
Filter{Kinds: []int{4, 5}},
Filter{Kinds: []int{4, 5}},
) {
t.Error("kinds filters should be equal")
}
if !FilterEqual(
Filter{Kinds: IntList{4, 5}, Tags: TagMap{"letter": {"a", "b"}}},
Filter{Kinds: IntList{4, 5}, Tags: TagMap{"letter": {"b", "a"}}},
Filter{Kinds: []int{4, 5}, Tags: TagMap{"letter": {"a", "b"}}},
Filter{Kinds: []int{4, 5}, Tags: TagMap{"letter": {"b", "a"}}},
) {
t.Error("kind+tags filters should be equal")
}
@@ -81,24 +83,24 @@ func TestFilterEquality(t *testing.T) {
tm := time.Now()
if !FilterEqual(
Filter{
Kinds: IntList{4, 5},
Kinds: []int{4, 5},
Tags: TagMap{"letter": {"a", "b"}, "fruit": {"banana"}},
Since: &tm,
IDs: StringList{"aaaa", "bbbb"},
IDs: []string{"aaaa", "bbbb"},
},
Filter{
Kinds: IntList{5, 4},
Kinds: []int{5, 4},
Tags: TagMap{"letter": {"a", "b"}, "fruit": {"banana"}},
Since: &tm,
IDs: StringList{"aaaa", "bbbb"},
IDs: []string{"aaaa", "bbbb"},
},
) {
t.Error("kind+2tags+since+ids filters should be equal")
}
if FilterEqual(
Filter{Kinds: IntList{1, 4, 5}},
Filter{Kinds: IntList{4, 5, 6}},
Filter{Kinds: []int{1, 4, 5}},
Filter{Kinds: []int{4, 5, 6}},
) {
t.Error("kinds filters shouldn't be equal")
}