Start on basic heuristics tests

This commit is contained in:
Ted Nyman
2013-12-15 20:06:59 -08:00
parent 0c668ee179
commit b3c6c85387
2 changed files with 27 additions and 4 deletions

View File

@@ -11,9 +11,9 @@ module Linguist
# Returns an array of language name Strings, or []
def self.find_by_heuristics(data, languages)
if languages.all? { |l| ["pod", "perl"].include?(l) }
if languages.all? { |l| ["pod", "perl"].include?(l.downcase) }
disambiguate_pod(data, languages)
elsif languages.all? { |l| ["objective-c", "c++"].include?(l) }
elsif languages.all? { |l| ["objective-c", "c++"].include?(l.downcase) }
disambiguate_h(data, languages)
end
end
@@ -27,7 +27,7 @@ module Linguist
# Returns an array of still-possible languages, or nil
def self.disambiguate_pod(data, languages)
matches = []
matches << Language["Perl"] if data.includes?("my $")
matches << Language["Perl"] if data.include?("my $")
matches
end
@@ -35,7 +35,7 @@ module Linguist
# We want to look for Objective-C.
def self.disambiguate_h(data, languages)
matches = []
matches << Language["Objective-C"] if data.includes?("NSData *") && data.includes?("@interface")
matches << Language["Objective-C"] if data.include?("@interface")
matches
end
end

23
test/test_heuristics.rb Normal file
View File

@@ -0,0 +1,23 @@
require 'linguist/heuristics'
require 'linguist/language'
require 'linguist/samples'
require 'test/unit'
class TestHeuristcs < Test::Unit::TestCase
include Linguist
def samples_path
File.expand_path("../../samples", __FILE__)
end
def fixture(name)
File.read(File.join(samples_path, name))
end
def test_find_by_heuristics
languages = ["C++", "Objective-C"]
results = Heuristics.find_by_heuristics(fixture("Objective-C/StyleViewController.h"), languages)
assert_equal Language["Objective-C"], results.first
end
end