mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-12-29 13:21:01 +00:00
Test all lexers can by found by name
This commit is contained in:
@@ -4,9 +4,17 @@ require 'yaml'
|
|||||||
module Linguist
|
module Linguist
|
||||||
# Mirror of Pygments Lexer structure.
|
# Mirror of Pygments Lexer structure.
|
||||||
class Lexer < Struct.new(:name, :aliases, :filenames, :mimetypes)
|
class Lexer < Struct.new(:name, :aliases, :filenames, :mimetypes)
|
||||||
|
@lexers = []
|
||||||
@name_index = {}
|
@name_index = {}
|
||||||
@alias_index = {}
|
@alias_index = {}
|
||||||
|
|
||||||
|
# Public: Get all Lexers
|
||||||
|
#
|
||||||
|
# Returns an Array of Lexers
|
||||||
|
def self.all
|
||||||
|
@lexers
|
||||||
|
end
|
||||||
|
|
||||||
# Public: Look up Lexer by its proper name.
|
# Public: Look up Lexer by its proper name.
|
||||||
#
|
#
|
||||||
# name - The case-insensitive String name of the Lexer
|
# name - The case-insensitive String name of the Lexer
|
||||||
@@ -92,6 +100,8 @@ module Linguist
|
|||||||
# `bin/pygments-lexers` dumps a YAML list of all the available
|
# `bin/pygments-lexers` dumps a YAML list of all the available
|
||||||
# Pygments lexers.
|
# Pygments lexers.
|
||||||
YAML.load_file(File.expand_path("../lexers.yml", __FILE__)).each do |lexer|
|
YAML.load_file(File.expand_path("../lexers.yml", __FILE__)).each do |lexer|
|
||||||
|
@lexers << lexer
|
||||||
|
|
||||||
# All Lexer names should be unique. Warn if there is a duplicate.
|
# All Lexer names should be unique. Warn if there is a duplicate.
|
||||||
if @name_index.key?(lexer.name.downcase)
|
if @name_index.key?(lexer.name.downcase)
|
||||||
warn "Duplicate lexer name: #{lexer.name}"
|
warn "Duplicate lexer name: #{lexer.name}"
|
||||||
|
|||||||
@@ -11,12 +11,26 @@ class TestLexer < Test::Unit::TestCase
|
|||||||
assert_equal Lexer['Ruby'], Lexer.find_by_name('RUBY')
|
assert_equal Lexer['Ruby'], Lexer.find_by_name('RUBY')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_find_all_by_name
|
||||||
|
Lexer.all.each do |lexer|
|
||||||
|
assert_equal lexer, Lexer.find_by_name(lexer.name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_find_by_alias
|
def test_find_by_alias
|
||||||
assert_equal Lexer['Ruby'], Lexer.find_by_alias('rb')
|
assert_equal Lexer['Ruby'], Lexer.find_by_alias('rb')
|
||||||
assert_equal Lexer['Ruby'], Lexer.find_by_alias('ruby')
|
assert_equal Lexer['Ruby'], Lexer.find_by_alias('ruby')
|
||||||
assert_equal Lexer['Ruby'], Lexer.find_by_alias('duby')
|
assert_equal Lexer['Ruby'], Lexer.find_by_alias('duby')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_find_all_by_alias
|
||||||
|
Lexer.all.each do |lexer|
|
||||||
|
lexer.aliases.each do |name|
|
||||||
|
assert_equal lexer, Lexer.find_by_alias(name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_name
|
def test_name
|
||||||
assert_equal 'Ruby', Lexer['Ruby'].name
|
assert_equal 'Ruby', Lexer['Ruby'].name
|
||||||
assert_equal 'Python', Lexer['Python'].name
|
assert_equal 'Python', Lexer['Python'].name
|
||||||
|
|||||||
Reference in New Issue
Block a user