# encoding: utf-8 class Nanoc::Filters::ColorizeSyntaxTest < MiniTest::Unit::TestCase include Nanoc::TestHelpers def test_coderay_simple if_have 'coderay', 'nokogiri' do # Create filter filter = ::Nanoc::Filters::ColorizeSyntax.new # Get input and expected output input = '
# comment
'
expected_output = '# comment
'
# Run filter
actual_output = filter.run(input)
assert_equal(expected_output, actual_output)
end
end
def test_dummy
if_have 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Get input and expected output
input = '# comment
'
expected_output = input # because we are using a dummy
# Run filter
actual_output = filter.run(input, :default_colorizer => :dummy)
assert_equal(expected_output, actual_output)
end
end
def test_full_page
if_have 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Get input and expected output
input = <# comment
EOS
expected_output = <# comment
EOS
# Run filter
actual_output = filter.run(input, :default_colorizer => :dummy, :is_fullpage => true)
assert_equal(expected_output, actual_output)
end
end
def test_coderay_with_comment
if_have 'coderay', 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Get input and expected output
input = %[#!ruby\n# comment
]
expected_output = '# comment
'
# Run filter
actual_output = filter.run(input)
assert_equal(expected_output, actual_output)
end
end
def test_coderay_with_comment_and_class
if_have 'coderay', 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Get input and expected output
input = %[#!ruby\n# comment
]
expected_output = %[#!ruby\n# comment
]
# Run filter
actual_output = filter.run(input)
assert_equal(expected_output, actual_output)
end
end
def test_coderay_with_more_classes
if_have 'coderay', 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Get input and expected output
input = '# comment
'
expected_output = '# comment
'
# Run filter
actual_output = filter.run(input)
assert_equal(expected_output, actual_output)
end
end
def test_pygmentize
if_have 'nokogiri', 'systemu' do
if `which pygmentize`.strip.empty?
skip "could not find pygmentize"
end
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Get input and expected output
input = '# comment
'
expected_output = '# comment
'
# Run filter
actual_output = filter.run(input, :colorizers => { :ruby => :pygmentize })
assert_equal(expected_output, actual_output)
end
end
def test_pygmentsrb
if_have 'pygments', 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Get input and expected output
input = '# comment…
'
expected_output = '# comment…
'
# Run filter
actual_output = filter.run(input, :colorizers => { :ruby => :pygmentsrb })
assert_equal(expected_output, actual_output)
end
end
def test_simon_highlight
if_have 'nokogiri', 'systemu' do
if `which highlight`.strip.empty?
skip "could not find `highlight`"
end
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Get input and expected output
input = %Q[\n# comment\n
]
expected_output = '# comment
'
# Run filter
actual_output = filter.run(input, :default_colorizer => :simon_highlight)
assert_equal(expected_output, actual_output)
end
end
def test_colorize_syntax_with_unknown_syntax
if_have 'coderay', 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Run filter
assert_raises RuntimeError do
filter.run('whatever
', :syntax => :kasflwafhaweoineurl) end end end def test_colorize_syntax_with_xml if_have 'coderay', 'nokogiri' do # Create filter filter = ::Nanoc::Filters::ColorizeSyntax.new # Get input and expected output input = 'foo
bar
foo
bar
foo
bar
foo
bar
puts "foo"
'
expected_output = 'puts "foo"
'
# Run filter
actual_output = filter.run(input, :default_colorizer => :pygmentize)
assert_equal(expected_output, actual_output)
end
end
def test_colorize_syntax_with_missing_executables
if_have 'nokogiri', 'systemu' do
begin
original_path = ENV['PATH']
ENV['PATH'] = './blooblooblah'
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Get input and expected output
input = 'puts "foo"
'
# Run filter
[ :albino, :pygmentize, :simon_highlight ].each do |colorizer|
begin
input = 'puts "foo"
'
actual_output = filter.run(
input,
:colorizers => { :ruby => colorizer })
flunk "expected colorizer to raise if no executable is available"
rescue => e
end
end
ensure
ENV['PATH'] = original_path
end
end
end
def test_colorize_syntax_with_non_language_shebang_line
if_have 'coderay', 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Get input and expected output
input = <
#!/usr/bin/env ruby
puts 'hi!'
after
EOS
expected_output = <
#!/usr/bin/env ruby
puts 'hi!'
after
EOS
# Run filter
actual_output = filter.run(input)
assert_equal(expected_output, actual_output)
end
end
def test_colorize_syntax_with_non_language_shebang_line_and_language_line
if_have 'coderay', 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Get input and expected output
input = <
#!ruby
#!/usr/bin/env ruby
puts 'hi!'
after
EOS
expected_output = <#!/usr/bin/env ruby
puts 'hi!'
after
EOS
# Run filter
actual_output = filter.run(input)
assert_equal(expected_output, actual_output)
end
end
def test_not_outside_pre
if_have 'coderay', 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Get input and expected output
input = '# comment
'
expected_output = '# comment
'
# Run filter
actual_output = filter.run(input, :outside_pre => false)
assert_equal(expected_output, actual_output)
end
end
def test_outside_pre
if_have 'coderay', 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Get input and expected output
input = '# comment
'
expected_output = '# comment
'
# Run filter
actual_output = filter.run(input, :outside_pre => true)
assert_equal(expected_output, actual_output)
end
end
def test_strip
if_have 'coderay', 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new
# Simple test
assert_equal " bar", filter.send(:strip, "\n bar")
# Get input and expected output
input = <
def foo
end
after
EOS
expected_output = < def foo
end
after
EOS
# Run filter
actual_output = filter.run(input)
assert_equal(expected_output, actual_output)
end
end
end