annotate vendor/gems/coderay-1.0.0/test/functional/examples.rb @ 1483:7e57ccd5f44b insider

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