To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / 38 / 38ca6dda2ba6a1320c43393fa70400cd1996e3e9.svn-base @ 1297:0a574315af3e
History | View | Annotate | Download (5.45 KB)
| 1 |
require 'test/unit' |
|---|---|
| 2 |
|
| 3 |
$:.unshift File.expand_path('../../../lib', __FILE__)
|
| 4 |
require 'coderay' |
| 5 |
|
| 6 |
class ExamplesTest < Test::Unit::TestCase |
| 7 |
|
| 8 |
def test_examples |
| 9 |
# output as HTML div (using inline CSS styles) |
| 10 |
div = CodeRay.scan('puts "Hello, world!"', :ruby).div
|
| 11 |
assert_equal <<-DIV, div |
| 12 |
<div class="CodeRay"> |
| 13 |
<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> |
| 14 |
</div> |
| 15 |
DIV |
| 16 |
|
| 17 |
# ...with line numbers |
| 18 |
div = CodeRay.scan(<<-CODE.chomp, :ruby).div(:line_numbers => :table) |
| 19 |
5.times do |
| 20 |
puts 'Hello, world!' |
| 21 |
end |
| 22 |
CODE |
| 23 |
assert_equal <<-DIV, div |
| 24 |
<table class="CodeRay"><tr> |
| 25 |
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
|
| 26 |
<a href="#n2" name="n2">2</a> |
| 27 |
<a href="#n3" name="n3">3</a> |
| 28 |
</pre></td> |
| 29 |
<td class="code"><pre><span style="color:#00D">5</span>.times <span style="color:#080;font-weight:bold">do</span> |
| 30 |
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> |
| 31 |
<span style="color:#080;font-weight:bold">end</span></pre></td> |
| 32 |
</tr></table> |
| 33 |
DIV |
| 34 |
|
| 35 |
# output as standalone HTML page (using CSS classes) |
| 36 |
page = CodeRay.scan('puts "Hello, world!"', :ruby).page
|
| 37 |
assert page[<<-PAGE] |
| 38 |
<body style="background-color: white;"> |
| 39 |
|
| 40 |
<table class="CodeRay"><tr> |
| 41 |
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>
|
| 42 |
</pre></td> |
| 43 |
<td class="code"><pre>puts <span class="string"><span class="delimiter">"</span><span class="content">Hello, world!</span><span class="delimiter">"</span></span></pre></td> |
| 44 |
</tr></table> |
| 45 |
|
| 46 |
</body> |
| 47 |
PAGE |
| 48 |
|
| 49 |
# keep scanned tokens for later use |
| 50 |
tokens = CodeRay.scan('{ "just": "an", "example": 42 }', :json)
|
| 51 |
assert_kind_of CodeRay::TokensProxy, tokens |
| 52 |
|
| 53 |
assert_equal ["{", :operator, " ", :space, :begin_group, :key,
|
| 54 |
"\"", :delimiter, "just", :content, "\"", :delimiter, |
| 55 |
:end_group, :key, ":", :operator, " ", :space, |
| 56 |
:begin_group, :string, "\"", :delimiter, "an", :content, |
| 57 |
"\"", :delimiter, :end_group, :string, ",", :operator, |
| 58 |
" ", :space, :begin_group, :key, "\"", :delimiter, |
| 59 |
"example", :content, "\"", :delimiter, :end_group, :key, |
| 60 |
":", :operator, " ", :space, "42", :integer, |
| 61 |
" ", :space, "}", :operator], tokens.tokens |
| 62 |
|
| 63 |
# produce a token statistic |
| 64 |
assert_equal <<-STATISTIC, tokens.statistic |
| 65 |
|
| 66 |
Code Statistics |
| 67 |
|
| 68 |
Tokens 26 |
| 69 |
Non-Whitespace 15 |
| 70 |
Bytes Total 31 |
| 71 |
|
| 72 |
Token Types (7): |
| 73 |
type count ratio size (average) |
| 74 |
------------------------------------------------------------- |
| 75 |
TOTAL 26 100.00 % 1.2 |
| 76 |
delimiter 6 23.08 % 1.0 |
| 77 |
operator 5 19.23 % 1.0 |
| 78 |
space 5 19.23 % 1.0 |
| 79 |
key 4 15.38 % 0.0 |
| 80 |
:begin_group 3 11.54 % 0.0 |
| 81 |
:end_group 3 11.54 % 0.0 |
| 82 |
content 3 11.54 % 4.3 |
| 83 |
string 2 7.69 % 0.0 |
| 84 |
integer 1 3.85 % 2.0 |
| 85 |
|
| 86 |
STATISTIC |
| 87 |
|
| 88 |
# count the tokens |
| 89 |
assert_equal 26, tokens.count |
| 90 |
|
| 91 |
# produce a HTML div, but with CSS classes |
| 92 |
div = tokens.div(:css => :class) |
| 93 |
assert_equal <<-DIV, div |
| 94 |
<div class="CodeRay"> |
| 95 |
<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>
|
| 96 |
</div> |
| 97 |
DIV |
| 98 |
|
| 99 |
# highlight a file (HTML div); guess the file type base on the extension |
| 100 |
require 'coderay/helpers/file_type' |
| 101 |
assert_equal :ruby, CodeRay::FileType[__FILE__] |
| 102 |
|
| 103 |
# get a new scanner for Python |
| 104 |
python_scanner = CodeRay.scanner :python |
| 105 |
assert_kind_of CodeRay::Scanners::Python, python_scanner |
| 106 |
|
| 107 |
# get a new encoder for terminal |
| 108 |
terminal_encoder = CodeRay.encoder :term |
| 109 |
assert_kind_of CodeRay::Encoders::Terminal, terminal_encoder |
| 110 |
|
| 111 |
# scanning into tokens |
| 112 |
tokens = python_scanner.tokenize 'import this; # The Zen of Python' |
| 113 |
assert_equal ["import", :keyword, " ", :space, "this", :include, |
| 114 |
";", :operator, " ", :space, "# The Zen of Python", :comment], tokens |
| 115 |
|
| 116 |
# format the tokens |
| 117 |
term = terminal_encoder.encode_tokens(tokens) |
| 118 |
assert_equal "\e[1;31mimport\e[0m \e[33mthis\e[0m; \e[37m# The Zen of Python\e[0m", term |
| 119 |
|
| 120 |
# re-using scanner and encoder |
| 121 |
ruby_highlighter = CodeRay::Duo[:ruby, :div] |
| 122 |
div = ruby_highlighter.encode('puts "Hello, world!"')
|
| 123 |
assert_equal <<-DIV, div |
| 124 |
<div class="CodeRay"> |
| 125 |
<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> |
| 126 |
</div> |
| 127 |
DIV |
| 128 |
end |
| 129 |
|
| 130 |
end |