Test all lexers can by found by name

This commit is contained in:
Joshua Peek
2011-05-25 09:21:53 -05:00
parent 4ba4257fdc
commit b047d67064
2 changed files with 24 additions and 0 deletions

View File

@@ -4,9 +4,17 @@ require 'yaml'
module Linguist
# Mirror of Pygments Lexer structure.
class Lexer < Struct.new(:name, :aliases, :filenames, :mimetypes)
@lexers = []
@name_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.
#
# 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
# Pygments lexers.
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.
if @name_index.key?(lexer.name.downcase)
warn "Duplicate lexer name: #{lexer.name}"

View File

@@ -11,12 +11,26 @@ class TestLexer < Test::Unit::TestCase
assert_equal Lexer['Ruby'], Lexer.find_by_name('RUBY')
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
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('duby')
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
assert_equal 'Ruby', Lexer['Ruby'].name
assert_equal 'Python', Lexer['Python'].name