-
Notifications
You must be signed in to change notification settings - Fork 51
/
heuristics_test.go
61 lines (54 loc) · 1.61 KB
/
heuristics_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package data
import (
"regexp"
"testing"
"github.com/src-d/enry/v2/data/rule"
"github.com/stretchr/testify/assert"
)
var testContentHeuristics = map[string]*Heuristics{
".md": &Heuristics{ // final pattern for parsed YAML rule
rule.Or(
rule.MatchingLanguages("Markdown"),
regexp.MustCompile(`(^[-A-Za-z0-9=#!\*\[|>])|<\/ | \A\z`),
),
rule.Or(
rule.MatchingLanguages("GCC Machine Description"),
regexp.MustCompile(`^(;;|\(define_)`),
),
rule.Always(
rule.MatchingLanguages("Markdown"),
),
},
".ms": &Heuristics{
// Order defines precedence: And, Or, Not, Named, Always
rule.And(
rule.MatchingLanguages("Unix Assembly"),
rule.Not(rule.MatchingLanguages(""), regexp.MustCompile(`/\*`)),
rule.Or(
rule.MatchingLanguages(""),
regexp.MustCompile(`^\s*\.(?:include\s|globa?l\s|[A-Za-z][_A-Za-z0-9]*:)`),
),
),
rule.Or(
rule.MatchingLanguages("Roff"),
regexp.MustCompile(`^[.''][A-Za-z]{2}(\s|$)`),
),
rule.Always(
rule.MatchingLanguages("MAXScript"),
),
},
}
func TestContentHeuristic_MatchingAlways(t *testing.T) {
lang := testContentHeuristics[".md"].matchString("")
assert.Equal(t, []string{"Markdown"}, lang)
lang = testContentHeuristics[".ms"].matchString("")
assert.Equal(t, []string{"MAXScript"}, lang)
}
func TestContentHeuristic_MatchingAnd(t *testing.T) {
lang := testContentHeuristics[".md"].matchString(";;")
assert.Equal(t, []string{"GCC Machine Description"}, lang)
}
func TestContentHeuristic_MatchingOr(t *testing.T) {
lang := testContentHeuristics[".ms"].matchString(" .include \"math.s\"")
assert.Equal(t, []string{"Unix Assembly"}, lang)
}