coderay 1.1.0 → 1.1.3.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,129 +0,0 @@
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">&quot;</span><span style="color:#D20">Hello, world!</span><span style="color:#710">&quot;</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"><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_match <<-PAGE, page
38
- <body>
39
-
40
- <table class="CodeRay"><tr>
41
- <td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
42
- </pre></td>
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>
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">&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>
96
- </div>
97
- DIV
98
-
99
- # highlight a file (HTML div); guess the file type base on the extension
100
- assert_equal :ruby, CodeRay::FileType[__FILE__]
101
-
102
- # get a new scanner for Python
103
- python_scanner = CodeRay.scanner :python
104
- assert_kind_of CodeRay::Scanners::Python, python_scanner
105
-
106
- # get a new encoder for terminal
107
- terminal_encoder = CodeRay.encoder :term
108
- assert_kind_of CodeRay::Encoders::Terminal, terminal_encoder
109
-
110
- # scanning into tokens
111
- tokens = python_scanner.tokenize 'import this; # The Zen of Python'
112
- assert_equal ["import", :keyword, " ", :space, "this", :include,
113
- ";", :operator, " ", :space, "# The Zen of Python", :comment], tokens
114
-
115
- # format the tokens
116
- term = terminal_encoder.encode_tokens(tokens)
117
- assert_equal "\e[32mimport\e[0m \e[31mthis\e[0m; \e[1;30m# The Zen of Python\e[0m", term
118
-
119
- # re-using scanner and encoder
120
- ruby_highlighter = CodeRay::Duo[:ruby, :div]
121
- div = ruby_highlighter.encode('puts "Hello, world!"')
122
- assert_equal <<-DIV, div
123
- <div class="CodeRay">
124
- <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>
125
- </div>
126
- DIV
127
- end
128
-
129
- end
@@ -1,78 +0,0 @@
1
- require 'test/unit'
2
-
3
- $:.unshift File.expand_path('../../../lib', __FILE__)
4
- require 'coderay'
5
-
6
- begin
7
- require 'rubygems' unless defined? Gem
8
- gem 'RedCloth', '>= 4.0.3' rescue nil
9
- require 'redcloth'
10
- rescue LoadError
11
- warn 'RedCloth not found - skipping for_redcloth tests.'
12
- undef RedCloth if defined? RedCloth
13
- end
14
-
15
- class BasicTest < Test::Unit::TestCase
16
-
17
- def test_for_redcloth
18
- require 'coderay/for_redcloth'
19
- assert_equal "<p><span lang=\"ruby\" class=\"CodeRay\">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></span></p>",
20
- RedCloth.new('@[ruby]puts "Hello, World!"@').to_html
21
- assert_equal <<-BLOCKCODE.chomp,
22
- <div lang="ruby" class="CodeRay">
23
- <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>
24
- </div>
25
- BLOCKCODE
26
- RedCloth.new('bc[ruby]. puts "Hello, World!"').to_html
27
- end
28
-
29
- def test_for_redcloth_no_lang
30
- require 'coderay/for_redcloth'
31
- assert_equal "<p><code>puts \"Hello, World!\"</code></p>",
32
- RedCloth.new('@puts "Hello, World!"@').to_html
33
- assert_equal <<-BLOCKCODE.chomp,
34
- <pre><code>puts \"Hello, World!\"</code></pre>
35
- BLOCKCODE
36
- RedCloth.new('bc. puts "Hello, World!"').to_html
37
- end
38
-
39
- def test_for_redcloth_style
40
- require 'coderay/for_redcloth'
41
- assert_equal <<-BLOCKCODE.chomp,
42
- <pre style=\"color: red;\"><code style=\"color: red;\">puts \"Hello, World!\"</code></pre>
43
- BLOCKCODE
44
- RedCloth.new('bc{color: red}. puts "Hello, World!"').to_html
45
- end
46
-
47
- def test_for_redcloth_escapes
48
- require 'coderay/for_redcloth'
49
- assert_equal '<p><span lang="ruby" class="CodeRay">&gt;</span></p>',
50
- RedCloth.new('@[ruby]>@').to_html
51
- assert_equal <<-BLOCKCODE.chomp,
52
- <div lang="ruby" class="CodeRay">
53
- <div class="code"><pre>&amp;</pre></div>
54
- </div>
55
- BLOCKCODE
56
- RedCloth.new('bc[ruby]. &').to_html
57
- end
58
-
59
- def test_for_redcloth_escapes2
60
- require 'coderay/for_redcloth'
61
- assert_equal "<p><span lang=\"c\" class=\"CodeRay\"><span style=\"color:#579\">#include</span> <span style=\"color:#B44;font-weight:bold\">&lt;test.h&gt;</span></span></p>",
62
- RedCloth.new('@[c]#include <test.h>@').to_html
63
- end
64
-
65
- # See http://jgarber.lighthouseapp.com/projects/13054/tickets/124-code-markup-does-not-allow-brackets.
66
- def test_for_redcloth_false_positive
67
- require 'coderay/for_redcloth'
68
- assert_equal '<p><code>[project]_dff.skjd</code></p>',
69
- RedCloth.new('@[project]_dff.skjd@').to_html
70
- # false positive, but expected behavior / known issue
71
- assert_equal "<p><span lang=\"ruby\" class=\"CodeRay\">_dff.skjd</span></p>",
72
- RedCloth.new('@[ruby]_dff.skjd@').to_html
73
- assert_equal <<-BLOCKCODE.chomp, RedCloth.new('bc. [project]_dff.skjd').to_html
74
- <pre><code>[project]_dff.skjd</code></pre>
75
- BLOCKCODE
76
- end
77
-
78
- end if defined? RedCloth
@@ -1,15 +0,0 @@
1
- require 'test/unit'
2
-
3
- $VERBOSE = $CODERAY_DEBUG = true
4
- $:.unshift File.expand_path('../../../lib', __FILE__)
5
- require 'coderay'
6
-
7
- mydir = File.dirname(__FILE__)
8
- suite = Dir[File.join(mydir, '*.rb')].
9
- map { |tc| File.basename(tc).sub(/\.rb$/, '') } - %w'suite for_redcloth'
10
-
11
- puts "Running basic CodeRay #{CodeRay::VERSION} tests: #{suite.join(', ')}"
12
-
13
- for test_case in suite
14
- load File.join(mydir, test_case + '.rb')
15
- end