Skip to content

Speed up HTML encoder for 1.2 #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix tests
  • Loading branch information
korny committed Jun 12, 2016
commit c999deb1e932b3290561a58923603eb87809c8df
9 changes: 7 additions & 2 deletions test/functional/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

class BasicTest < Test::Unit::TestCase

def normalize_html html
html.gsub('&#39;', "'").gsub('&quot;', '"')
end

def test_version
assert_nothing_raised do
assert_match(/\A\d\.\d\.\d?\z/, CodeRay::VERSION)
Expand Down Expand Up @@ -50,7 +54,7 @@ def test_simple_scan
'<span class="content">Hello, World!</span><span class="delimiter">"</span></span>'
def test_simple_highlight
assert_nothing_raised do
assert_equal RUBY_TEST_HTML, CodeRay.scan(RUBY_TEST_CODE, :ruby).html
assert_equal RUBY_TEST_HTML, normalize_html(CodeRay.scan(RUBY_TEST_CODE, :ruby).html)
end
end

Expand All @@ -75,7 +79,8 @@ def test_highlight
end

def test_highlight_file
assert_match "require <span class=\"string\"><span class=\"delimiter\">'</span><span class=\"content\">test/unit</span><span class=\"delimiter\">'</span></span>\n", CodeRay.highlight_file(__FILE__)
assert_match "require <span class=\"string\"><span class=\"delimiter\">'</span><span class=\"content\">test/unit</span><span class=\"delimiter\">'</span></span>\n",
normalize_html(CodeRay.highlight_file(__FILE__))
end

def test_duo
Expand Down
14 changes: 9 additions & 5 deletions test/functional/examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@

class ExamplesTest < Test::Unit::TestCase

def normalize_html html
html.gsub('&#39;', "'").gsub('&quot;', '"')
end

def test_examples
# output as HTML div (using inline CSS styles)
div = CodeRay.scan('puts "Hello, world!"', :ruby).div
assert_equal <<-DIV, div
assert_equal <<-DIV, normalize_html(div)
<div class="CodeRay">
<div class="code"><pre>puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Hello, world!</span><span style="color:#710">"</span></span></pre></div>
</div>
Expand All @@ -20,7 +24,7 @@ def test_examples
puts 'Hello, world!'
end
CODE
assert_equal <<-DIV, div
assert_equal <<-DIV, normalize_html(div)
<table class="CodeRay"><tr>
<td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
Expand All @@ -34,7 +38,7 @@ def test_examples

# output as standalone HTML page (using CSS classes)
page = CodeRay.scan('puts "Hello, world!"', :ruby).page
assert_match <<-PAGE, page
assert_match <<-PAGE, normalize_html(page)
<body>

<table class="CodeRay"><tr>
Expand Down Expand Up @@ -90,7 +94,7 @@ def test_examples

# produce a HTML div, but with CSS classes
div = tokens.div(:css => :class)
assert_equal <<-DIV, div
assert_equal <<-DIV, normalize_html(div)
<div class="CodeRay">
<div class="code"><pre>{ <span class="key"><span class="delimiter">"</span><span class="content">just</span><span class="delimiter">"</span></span>: <span class="string"><span class="delimiter">"</span><span class="content">an</span><span class="delimiter">"</span></span>, <span class="key"><span class="delimiter">"</span><span class="content">example</span><span class="delimiter">"</span></span>: <span class="integer">42</span> }</pre></div>
</div>
Expand Down Expand Up @@ -119,7 +123,7 @@ def test_examples
# re-using scanner and encoder
ruby_highlighter = CodeRay::Duo[:ruby, :div]
div = ruby_highlighter.encode('puts "Hello, world!"')
assert_equal <<-DIV, div
assert_equal <<-DIV, normalize_html(div)
<div class="CodeRay">
<div class="code"><pre>puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Hello, world!</span><span style="color:#710">"</span></span></pre></div>
</div>
Expand Down
8 changes: 6 additions & 2 deletions test/functional/for_redcloth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@

class BasicTest < Test::Unit::TestCase

def normalize_html html
html.gsub('&#39;', "'").gsub('&quot;', '"')
end

def test_for_redcloth
require 'coderay/for_redcloth'
assert_equal "<p><span lang=\"ruby\" class=\"CodeRay\">puts <span style=\"background-color:hsla(0,100%,50%,0.05)\"><span style=\"color:#710\">\"</span><span style=\"color:#D20\">Hello, World!</span><span style=\"color:#710\">\"</span></span></span></p>",
RedCloth.new('@[ruby]puts "Hello, World!"@').to_html
normalize_html(RedCloth.new('@[ruby]puts "Hello, World!"@').to_html)
assert_equal <<-BLOCKCODE.chomp,
<div lang="ruby" class="CodeRay">
<div class="code"><pre>puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Hello, World!</span><span style="color:#710">"</span></span></pre></div>
</div>
BLOCKCODE
RedCloth.new('bc[ruby]. puts "Hello, World!"').to_html
normalize_html(RedCloth.new('bc[ruby]. puts "Hello, World!"').to_html)
end

def test_for_redcloth_no_lang
Expand Down
10 changes: 7 additions & 3 deletions test/unit/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

class HtmlTest < Test::Unit::TestCase

def normalize_html html
html.gsub('&#39;', "'").gsub('&quot;', '"')
end

def test_break_lines_option
snippets = {}

Expand Down Expand Up @@ -95,9 +99,9 @@ def test_break_lines_option
for lang, code in snippets
tokens = CodeRay.scan code[:in], lang

assert_equal code[:expected_with_option_off], tokens.html
assert_equal code[:expected_with_option_off], tokens.html(:break_lines => false)
assert_equal code[:expected_with_option_on], tokens.html(:break_lines => true)
assert_equal code[:expected_with_option_off], normalize_html(tokens.html)
assert_equal code[:expected_with_option_off], normalize_html(tokens.html(:break_lines => false))
assert_equal code[:expected_with_option_on], normalize_html(tokens.html(:break_lines => true))
end
end
end