github/linguist — pull request #1784 by bkeepers (2014-11-28 20:54:48+00:00)
Title: Refactor heuristics

Now that we're depending more on heuristics, I think we should make defining and testing them more explicit.

This adds `disambiguate` to define a new heuristic, passing in the name of the languages to disambiguate, and a block that returns either a definitive language or `nil`.

```ruby
disambiguate "Perl", "Prolog" do |data|
if data.include?("use strict")
Language["Perl"]
elsif data.include?(":-")
Language["Prolog"]
end
end
```

The heuristics are tested by calling `assert_heuristics` and passing in a hash of language names and files, which will assert that the given files a detected properly.

```ruby
def test_pl_prolog_perl_by_heuristics
assert_heuristics({
"Prolog" => "Prolog/turing.pl",
"Perl" => "Perl/perl-test.t",
})
end
```

Thoughts? @arfon @pchaigno

Note that the side-by-side diff is probably more helpful on this PR.