|
|
@@ -0,0 +1,49 @@
|
|
|
+// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
+// SPDX-License-Identifier: Apache-2.0
|
|
|
+
|
|
|
+package filter // import "miniflux.app/v2/internal/reader/filter"
|
|
|
+
|
|
|
+import (
|
|
|
+ "slices"
|
|
|
+ "testing"
|
|
|
+)
|
|
|
+
|
|
|
+func TestValidateRules(t *testing.T) {
|
|
|
+ tests := []struct {
|
|
|
+ name string
|
|
|
+ rules string
|
|
|
+ want []RuleError
|
|
|
+ }{
|
|
|
+ {name: "empty rules", rules: ""},
|
|
|
+ {name: "every field with a regex", rules: "EntryTitle=(?i)golang\nEntryURL=example\\.org\nEntryCommentsURL=news\nEntryContent=sponsored\nEntryAuthor=^Alice$\nEntryTag=go"},
|
|
|
+ {name: "every date pattern", rules: "EntryDate=future\nEntryDate=before:2024-01-01\nEntryDate=after:2024-01-01\nEntryDate=between:2024-01-01,2024-02-01\nEntryDate=max-age:30d\nEntryDate=max-age:12h"},
|
|
|
+ {name: "trailing newline", rules: "EntryTitle=foo\n"},
|
|
|
+ {name: "CRLF line breaks", rules: "EntryTitle=foo\r\nEntryAuthor=bar\r\n"},
|
|
|
+ {name: "blank lines", rules: "\nEntryTitle=foo\n\n \nEntryAuthor=bar"},
|
|
|
+ {name: "spaces around the separator", rules: " EntryTitle = foo "},
|
|
|
+ {name: "unknown field", rules: "Title=foo", want: []RuleError{{Line: 1, Kind: RuleErrUnknownField}}},
|
|
|
+ {name: "field name with a suffix", rules: "EntryTitleX=foo", want: []RuleError{{Line: 1, Kind: RuleErrUnknownField}}},
|
|
|
+ {name: "no field and no separator", rules: "foo", want: []RuleError{{Line: 1, Kind: RuleErrUnknownField}}},
|
|
|
+ {name: "wrong separator", rules: "EntryTitle:foo", want: []RuleError{{Line: 1, Kind: RuleErrMissingSeparator}}},
|
|
|
+ {name: "field name only", rules: "EntryTitle", want: []RuleError{{Line: 1, Kind: RuleErrMissingSeparator}}},
|
|
|
+ {name: "empty pattern", rules: "EntryTitle=", want: []RuleError{{Line: 1, Kind: RuleErrEmptyPattern}}},
|
|
|
+ {name: "blank pattern", rules: "EntryDate= ", want: []RuleError{{Line: 1, Kind: RuleErrEmptyPattern}}},
|
|
|
+ {name: "invalid regex", rules: "EntryTitle=[", want: []RuleError{{Line: 1, Kind: RuleErrInvalidRegex}}},
|
|
|
+ {name: "invalid max-age duration", rules: "EntryDate=max-age:30days", want: []RuleError{{Line: 1, Kind: RuleErrInvalidDate}}},
|
|
|
+ {name: "invalid calendar date", rules: "EntryDate=before:2024-13-45", want: []RuleError{{Line: 1, Kind: RuleErrInvalidDate}}},
|
|
|
+ {name: "unknown date pattern", rules: "EntryDate=last-week", want: []RuleError{{Line: 1, Kind: RuleErrInvalidDate}}},
|
|
|
+ {name: "date keyword is case-sensitive", rules: "EntryDate=Future", want: []RuleError{{Line: 1, Kind: RuleErrInvalidDate}}},
|
|
|
+ {name: "between with one date", rules: "EntryDate=between:2024-01-01", want: []RuleError{{Line: 1, Kind: RuleErrInvalidDate}}},
|
|
|
+ {name: "between with a space after the comma", rules: "EntryDate=between:2024-01-01, 2024-02-01", want: []RuleError{{Line: 1, Kind: RuleErrInvalidDate}}},
|
|
|
+ {name: "line numbers count blank lines", rules: "EntryTitle=a\n\nEntryDate=bad", want: []RuleError{{Line: 3, Kind: RuleErrInvalidDate}}},
|
|
|
+ {name: "every invalid line is reported", rules: "Title=a\nEntryTitle=[", want: []RuleError{{Line: 1, Kind: RuleErrUnknownField}, {Line: 2, Kind: RuleErrInvalidRegex}}},
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, tc := range tests {
|
|
|
+ t.Run(tc.name, func(t *testing.T) {
|
|
|
+ if got := ValidateRules(tc.rules); !slices.Equal(got, tc.want) {
|
|
|
+ t.Fatalf("ValidateRules(%q) = %+v, want %+v", tc.rules, got, tc.want)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|