mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-12-08 12:28:47 +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 []
|
# Returns an array of language name Strings, or []
|
||||||
def self.find_by_heuristics(data, languages)
|
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)
|
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)
|
disambiguate_h(data, languages)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -27,7 +27,7 @@ module Linguist
|
|||||||
# Returns an array of still-possible languages, or nil
|
# Returns an array of still-possible languages, or nil
|
||||||
def self.disambiguate_pod(data, languages)
|
def self.disambiguate_pod(data, languages)
|
||||||
matches = []
|
matches = []
|
||||||
matches << Language["Perl"] if data.includes?("my $")
|
matches << Language["Perl"] if data.include?("my $")
|
||||||
matches
|
matches
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ module Linguist
|
|||||||
# We want to look for Objective-C.
|
# We want to look for Objective-C.
|
||||||
def self.disambiguate_h(data, languages)
|
def self.disambiguate_h(data, languages)
|
||||||
matches = []
|
matches = []
|
||||||
matches << Language["Objective-C"] if data.includes?("NSData *") && data.includes?("@interface")
|
matches << Language["Objective-C"] if data.include?("@interface")
|
||||||
matches
|
matches
|
||||||
end
|
end
|
||||||
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