mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Start on basic heuristics tests
This commit is contained in:
@@ -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
23
test/test_heuristics.rb
Normal 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
|
||||
Reference in New Issue
Block a user