To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / vendor / gems / coderay-0.9.7 / test / functional / basic.rb @ 442:753f1380d6bc

History | View | Annotate | Download (3.01 KB)

1
require 'test/unit'
2
require 'coderay'
3

    
4
class BasicTest < Test::Unit::TestCase
5
  
6
  def test_version
7
    assert_nothing_raised do
8
      assert_match(/\A\d\.\d\.\d\z/, CodeRay::VERSION)
9
    end
10
  end
11
  
12
  RUBY_TEST_CODE = 'puts "Hello, World!"'
13
  
14
  RUBY_TEST_TOKENS = [
15
    ['puts', :ident],
16
    [' ', :space],
17
    [:open, :string],
18
      ['"', :delimiter],
19
      ['Hello, World!', :content],
20
      ['"', :delimiter],
21
    [:close, :string]
22
  ]
23
  def test_simple_scan
24
    assert_nothing_raised do
25
      assert_equal RUBY_TEST_TOKENS, CodeRay.scan(RUBY_TEST_CODE, :ruby).to_ary
26
    end
27
  end
28
  
29
  RUBY_TEST_HTML = 'puts <span class="s"><span class="dl">&quot;</span>' + 
30
    '<span class="k">Hello, World!</span><span class="dl">&quot;</span></span>'
31
  def test_simple_highlight
32
    assert_nothing_raised do
33
      assert_equal RUBY_TEST_HTML, CodeRay.scan(RUBY_TEST_CODE, :ruby).html
34
    end
35
  end
36
  
37
  def test_duo
38
    assert_equal(RUBY_TEST_CODE,
39
      CodeRay::Duo[:plain, :plain].highlight(RUBY_TEST_CODE))
40
    assert_equal(RUBY_TEST_CODE,
41
      CodeRay::Duo[:plain => :plain].highlight(RUBY_TEST_CODE))
42
  end
43
  
44
  def test_duo_stream
45
    assert_equal(RUBY_TEST_CODE,
46
      CodeRay::Duo[:plain, :plain].highlight(RUBY_TEST_CODE, :stream => true))
47
  end
48
  
49
  def test_comment_filter
50
    assert_equal <<-EXPECTED, CodeRay.scan(<<-INPUT, :ruby).comment_filter.text
51
#!/usr/bin/env ruby
52

53
code
54

55
more code  
56
      EXPECTED
57
#!/usr/bin/env ruby
58
=begin
59
A multi-line comment.
60
=end
61
code
62
# A single-line comment.
63
more code  # and another comment, in-line.
64
      INPUT
65
  end
66
  
67
  def test_lines_of_code
68
    assert_equal 2, CodeRay.scan(<<-INPUT, :ruby).lines_of_code
69
#!/usr/bin/env ruby
70
=begin
71
A multi-line comment.
72
=end
73
code
74
# A single-line comment.
75
more code  # and another comment, in-line.
76
      INPUT
77
    rHTML = <<-RHTML
78
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
79
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
80

81
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
82
<head>
83
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
84
  <title><%= controller.controller_name.titleize %>: <%= controller.action_name %></title>
85
  <%= stylesheet_link_tag 'scaffold' %>
86
</head>
87
<body>
88

89
<p style="color: green"><%= flash[:notice] %></p>
90

91
<div id="main">
92
  <%= yield %>
93
</div>
94

95
</body>
96
</html>
97
      RHTML
98
    assert_equal 0, CodeRay.scan(rHTML, :html).lines_of_code
99
    assert_equal 0, CodeRay.scan(rHTML, :php).lines_of_code
100
    assert_equal 0, CodeRay.scan(rHTML, :yaml).lines_of_code
101
    assert_equal 4, CodeRay.scan(rHTML, :rhtml).lines_of_code
102
  end
103
  
104
  def test_rubygems_not_loaded
105
    assert_equal nil, defined? Gem
106
  end if ENV['check_rubygems'] && RUBY_VERSION < '1.9'
107
  
108
  def test_list_of_encoders
109
    assert_kind_of(Array, CodeRay::Encoders.list)
110
    assert CodeRay::Encoders.list.include?('count')
111
  end
112
  
113
  def test_list_of_scanners
114
    assert_kind_of(Array, CodeRay::Scanners.list)
115
    assert CodeRay::Scanners.list.include?('plaintext')
116
  end
117
  
118
  def test_scan_a_frozen_string
119
    CodeRay.scan RUBY_VERSION, :ruby
120
  end
121
  
122
end