# frozen_string_literal: true require 'test/unit' require File.join(File.dirname(__FILE__), '..', '/lib/pygments.rb') ENV['mentos-test'] = 'yes' P = Pygments PE = Pygments.engine class PygmentsHighlightTest < Test::Unit::TestCase RUBY_CODE = "#!/usr/bin/ruby\nputs 'foo'" RUBY_CODE_TRAILING_NEWLINE = "#!/usr/bin/ruby\nputs 'foo'\n" TEST_CODE = File.read( File.join(__dir__, '..', 'lib', 'pygments', 'mentos.py') ) def test_highlight_defaults_to_html code = P.highlight(RUBY_CODE) assert_match '#!/usr/bin/ruby', code assert_equal '
#!/usr/bin/ruby
puts 'foo'
test', code
end
def test_highlight_works_with_trailing_newline
code = P.highlight(RUBY_CODE_TRAILING_NEWLINE)
assert_match '#!/usr/bin/ruby', code
end
def test_highlight_works_with_multiple_newlines
code = P.highlight(RUBY_CODE_TRAILING_NEWLINE + "derp\n\n")
assert_match '#!/usr/bin/ruby', code
end
def test_highlight_works_with_trailing_cr
code = P.highlight(RUBY_CODE_TRAILING_NEWLINE + "\r")
assert_match '#!/usr/bin/ruby', code
end
def test_highlight_still_works_with_invalid_code
code = P.highlight('importr python; wat?', lexer: 'py')
assert_match '>importr', code
end
def test_highlight_on_multi_threads
omit 'We do not actually support multithreading'
10.times.map do
Thread.new do
test_full_html_highlight
end
end.each(&:join)
end
end
class PygmentsLexerTest < Test::Unit::TestCase
RUBY_CODE = "#!/usr/bin/ruby\nputs 'foo'"
def test_lexer_by_mimetype
assert_equal 'rb', P.lexer_name_for(mimetype: 'text/x-ruby')
assert_equal 'json', P.lexer_name_for(mimetype: 'application/json')
end
def test_lexer_by_filename
assert_equal 'rb', P.lexer_name_for(filename: 'test.rb')
assert_equal 'scala', P.lexer_name_for(filename: 'test.scala')
end
def test_lexer_by_name
assert_equal 'rb', P.lexer_name_for(lexer: 'ruby')
assert_equal 'python', P.lexer_name_for(lexer: 'python')
assert_equal 'c', P.lexer_name_for(lexer: 'c')
end
def test_lexer_by_filename_and_content
assert_equal 'rb', P.lexer_name_for(RUBY_CODE, filename: 'test.rb')
end
def test_lexer_by_content
assert_equal 'rb', P.lexer_name_for(RUBY_CODE)
end
def test_lexer_by_nothing
assert_raise MentosError do
P.lexer_name_for(invalid: true)
end
end
end
class PygmentsLexerClassTest < Test::Unit::TestCase
def test_find
assert_equal 'Ruby', P::Lexer['Ruby'].name
assert_equal 'Ruby', P::Lexer['ruby'].name
assert_equal 'Ruby', P::Lexer['rb'].name
assert_equal 'Ruby', P::Lexer['rake'].name
assert_equal 'Ruby', P::Lexer['gemspec'].name
end
def test_find_by_name
assert_equal P::Lexer['Ruby'], P::Lexer.find_by_name('Ruby')
assert_equal P::Lexer['C'], P::Lexer.find_by_name('C')
end
def test_find_by_alias
assert_equal P::Lexer['Ruby'], P::Lexer.find_by_alias('rb')
assert_equal P::Lexer['Ruby'], P::Lexer.find_by_alias('ruby')
assert_equal P::Lexer['Scala'], P::Lexer.find_by_alias('scala')
assert_equal P::Lexer['Go'], P::Lexer.find_by_alias('go')
end
def test_find_lexer_by_extname
assert_equal P::Lexer['Ruby'], P::Lexer.find_by_extname('.rb')
assert_equal P::Lexer['PHP'], P::Lexer.find_by_extname('.php4')
assert_equal P::Lexer['PHP'], P::Lexer.find_by_extname('.php5')
assert_equal P::Lexer['Groff'], P::Lexer.find_by_extname('.1')
assert_equal P::Lexer['Groff'], P::Lexer.find_by_extname('.3')
assert_equal P::Lexer['C'], P::Lexer.find_by_extname('.c')
assert_equal P::Lexer['Python'], P::Lexer.find_by_extname('.py')
assert_equal P::Lexer['Java'], P::Lexer.find_by_extname('.java')
end
def test_find_lexer_by_mimetype
assert_equal P::Lexer['Ruby'], P::Lexer.find_by_mimetype('text/x-ruby')
assert_equal P::Lexer['JSON'], P::Lexer.find_by_mimetype('application/json')
assert_equal P::Lexer['Python'], P::Lexer.find_by_mimetype('text/x-python')
end
end
class PygmentsCssTest < Test::Unit::TestCase
include Pygments
def test_css
assert_match(/^\.err \{/, P.css)
end
def test_css_prefix
assert_match(/^\.highlight \.err \{/, P.css('.highlight'))
end
def test_css_options
assert_match(/^\.codeerr \{/, P.css(classprefix: 'code'))
end
def test_css_prefix_and_options
assert_match(/^\.mycode \.codeerr \{/, P.css('.mycode', classprefix: 'code'))
end
def test_css_default
assert_match '.c { color: #408080; font-style: italic }', P.css
end
def test_css_colorful
assert_match '.c { color: #888888 }', P.css(style: 'colorful')
end
end
class PygmentsConfigTest < Test::Unit::TestCase
def test_styles
assert P.styles.include?('colorful')
end
def test_filters
assert P.filters.include?('codetagify')
end
def test_lexers
list = P.lexers
assert list.key?('Ruby')
assert list['Ruby'][:aliases].include?('duby')
end
def test_formatters
list = P.formatters
assert list.key?('Html')
assert list['Html'][:aliases].include?('html')
end
end