# encoding: utf-8 class Nanoc::Filters::ColorizeSyntaxTest < Nanoc::TestCase CODERAY_PRE = '
' CODERAY_POST = '
' 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 = CODERAY_PRE + '
# comment
' + CODERAY_POST # Run filter actual_output = filter.setup_and_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.setup_and_run(input, default_colorizer: :dummy) assert_equal(expected_output, actual_output) end end def test_with_frozen_input if_have 'nokogiri' do input = '
# comment
'.freeze input.freeze filter = ::Nanoc::Filters::ColorizeSyntax.new filter.setup_and_run(input, default_colorizer: :dummy) end end def test_full_page if_have 'nokogiri' do # Create filter filter = ::Nanoc::Filters::ColorizeSyntax.new # Get input and expected output input = < Foo
# comment
EOS expected_output_regex = %r{^\s*\s*\s*\s*Foo\s*\s*\s*
# comment
\s*\s*} # Run filter actual_output = filter.setup_and_run(input, default_colorizer: :dummy, is_fullpage: true) assert_match expected_output_regex, 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
# comment
) expected_output = CODERAY_PRE + '
# comment
' + CODERAY_POST # Run filter actual_output = filter.setup_and_run(input) assert_equal(expected_output, actual_output) end end def test_coderay_with_comment_in_middle if_have 'coderay', 'nokogiri' do # Create filter filter = ::Nanoc::Filters::ColorizeSyntax.new # Get input and expected output input = %(
def moo ; end
#!ruby
# comment
) expected_output = "
def moo ; end\n#!ruby\n# comment
" # Run filter actual_output = filter.setup_and_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
# comment
) expected_output = CODERAY_PRE + %(
#!ruby
# comment
) + CODERAY_POST # Run filter actual_output = filter.setup_and_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 = CODERAY_PRE + '
# comment
' + CODERAY_POST # Run filter actual_output = filter.setup_and_run(input) assert_equal(expected_output, actual_output) end end def test_pygmentize if_have 'nokogiri' do skip_unless_have_command 'pygmentize' # Create filter filter = ::Nanoc::Filters::ColorizeSyntax.new # Get input and expected output input = '
# comment
' expected_output = '
# comment
' # Run filter actual_output = filter.setup_and_run(input, colorizers: { ruby: :pygmentize }) assert_equal(expected_output, actual_output) end end def test_pygmentsrb skip 'pygments.rb does not support Windows' if on_windows? 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.setup_and_run(input, colorizers: { ruby: :pygmentsrb }) assert_equal(expected_output, actual_output) end end def test_simon_highlight if_have 'nokogiri' do skip_unless_have_command 'highlight' # Create filter filter = ::Nanoc::Filters::ColorizeSyntax.new # Get input and expected output input = %(

# comment
) expected_output = '
# comment
' # Run filter actual_output = filter.setup_and_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.setup_and_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

' expected_output = '

foo
bar

' # Run filter actual_output = filter.setup_and_run(input, syntax: :xml) assert_equal(expected_output, actual_output) end end def test_colorize_syntax_with_xhtml if_have 'coderay', 'nokogiri' do # Create filter filter = ::Nanoc::Filters::ColorizeSyntax.new # Get input and expected output input = '

foo
bar

' expected_output = '

foo
bar

' # Run filter actual_output = filter.setup_and_run(input, syntax: :xhtml) assert_equal(expected_output, actual_output) end end def test_colorize_syntax_with_default_colorizer skip_unless_have_command 'pygmentize' if_have 'nokogiri' do # Create filter filter = ::Nanoc::Filters::ColorizeSyntax.new # Get input and expected output input = '
puts "foo"
' expected_output = '
puts "foo"
' # Run filter actual_output = filter.setup_and_run(input, default_colorizer: :pygmentize) assert_equal(expected_output, actual_output) end end def test_colorize_syntax_with_missing_executables if_have 'nokogiri' 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"
' filter.setup_and_run( input, colorizers: { ruby: colorizer }) flunk 'expected colorizer to raise if no executable is available' rescue 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.setup_and_run(input).sub(/\s*\Z/m, '') 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!'#{CODERAY_POST} after EOS # Run filter actual_output = filter.setup_and_run(input).sub(/\s*\Z/m, '') 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.setup_and_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.setup_and_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#{CODERAY_POST} after EOS # Run filter actual_output = filter.setup_and_run(input).sub(/\s*\Z/m, '') assert_equal(expected_output, actual_output) end end def test_rouge if_have 'rouge', 'nokogiri' do # Create filter filter = ::Nanoc::Filters::ColorizeSyntax.new # Get input and expected output input = < def foo end after EOS expected_output = < def foo end after EOS # Run filter actual_output = filter.setup_and_run(input, default_colorizer: :rouge) assert_equal(expected_output, actual_output) end end def test_rouge_with_css_class if_have 'rouge', 'nokogiri' do # Create filter filter = ::Nanoc::Filters::ColorizeSyntax.new # Get input and expected output input = < def foo end after EOS expected_output = < def foo end after EOS # Run filter actual_output = filter.setup_and_run(input, default_colorizer: :rouge, rouge: { css_class: 'my-class' }) assert_equal(expected_output, actual_output) end end end