gitlab.com/thomasboni/go-enry/v2@v2.8.3-0.20220418031202-30b0d7a3de98/data/rule/rule_test.go (about) 1 package rule 2 3 import ( 4 "regexp" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 const lang = "ActionScript" 11 12 var fixtures = []struct { 13 name string 14 rule Heuristic 15 numLangs int 16 matching string 17 noMatch string 18 }{ 19 {"Always", Always(MatchingLanguages(lang)), 1, "a", ""}, 20 {"Not", Not(MatchingLanguages(lang), regexp.MustCompile(`a`)), 1, "b", "a"}, 21 {"And", And(MatchingLanguages(lang), regexp.MustCompile(`a`), regexp.MustCompile(`b`)), 1, "ab", "a"}, 22 {"Or", Or(MatchingLanguages(lang), regexp.MustCompile(`a|b`)), 1, "ab", "c"}, 23 } 24 25 func TestRules(t *testing.T) { 26 for _, f := range fixtures { 27 t.Run(f.name, func(t *testing.T) { 28 assert.NotNil(t, f.rule) 29 assert.NotNil(t, f.rule.Languages()) 30 assert.Equal(t, f.numLangs, len(f.rule.Languages())) 31 assert.Truef(t, f.rule.Match([]byte(f.matching)), 32 "'%s' is expected to .Match() by rule %s%v", f.matching, f.name, f.rule) 33 if f.noMatch != "" { 34 assert.Falsef(t, f.rule.Match([]byte(f.noMatch)), 35 "'%s' is expected NOT to .Match() by rule %s%v", f.noMatch, f.name, f.rule) 36 } 37 }) 38 } 39 }