gopkg.in/src-d/enry.v1@v1.7.3/data/heuristics_test.go (about)

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