Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An example comparison for docs #38

Open
wants to merge 2 commits into
base: add-docs-on-how-highlighting-works
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/linguist/heuristics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def self.call(blob, languages)
[] # No heuristics matched
end

# Public: Do we have a heuristic for these candidate languages?
#
# languages - Array of Language objects
#
# Returns true if there are matching heuristics or nil
def self.defined_for?(languages)
@heuristics.select { |h| h.matches?(languages) }.any?
end

# Internal: Define a new heuristic.
#
# languages - String names of languages to disambiguate.
Expand Down
38 changes: 27 additions & 11 deletions test/test_heuristics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ def test_no_match
assert_equal [], results
end

def assert_heuristics(hash)
candidates = hash.keys.map { |l| Language[l] }

hash.each do |language, blobs|
Array(blobs).each do |blob|
result = Heuristics.call(file_blob(blob), candidates)
assert_equal [Language[language]], result, "Failed for #{blob}"
end
end
end

# Candidate languages = ["C++", "Objective-C"]
def test_obj_c_by_heuristics
# Only calling out '.h' filenames as these are the ones causing issues
Expand Down Expand Up @@ -148,17 +159,6 @@ def test_cs_by_heuristics
})
end

def assert_heuristics(hash)
candidates = hash.keys.map { |l| Language[l] }

hash.each do |language, blobs|
Array(blobs).each do |blob|
result = Heuristics.call(file_blob(blob), candidates)
assert_equal [Language[language]], result, "Failed for #{blob}"
end
end
end

def test_ls_by_heuristics
assert_heuristics({
"LiveScript" => "LiveScript/hello.ls",
Expand All @@ -172,4 +172,20 @@ def test_ts_by_heuristics
"XML" => all_fixtures("XML", "*.ts")
})
end

def output_languages(languages)
languages.map { |l| l.name }.join(", ")
end

# If a language extension isn't globally unique then check if we have a heuristic
Linguist::Language.all.each do |language|
define_method "test_#{language.name}_has_adequate_heuristics" do
language.extensions.each do |extension|
languages = Language.find_by_extension(extension)
next if extension == ".script!" || languages.size == 1

assert Heuristics.defined_for?(languages), "No heuristic for #{extension} and #{output_languages(languages)}"
end
end
end
end