annotate vendor/gems/coderay-0.9.7/test/functional/basic.rb @ 855:7294e8db2515 bug_162

Close obsolete branch bug_162
author Chris Cannam
date Thu, 14 Jul 2011 11:59:19 +0100
parents 0579821a129a
children
rev   line source
Chris@210 1 require 'test/unit'
Chris@210 2 require 'coderay'
Chris@210 3
Chris@210 4 class BasicTest < Test::Unit::TestCase
Chris@210 5
Chris@210 6 def test_version
Chris@210 7 assert_nothing_raised do
Chris@210 8 assert_match(/\A\d\.\d\.\d\z/, CodeRay::VERSION)
Chris@210 9 end
Chris@210 10 end
Chris@210 11
Chris@210 12 RUBY_TEST_CODE = 'puts "Hello, World!"'
Chris@210 13
Chris@210 14 RUBY_TEST_TOKENS = [
Chris@210 15 ['puts', :ident],
Chris@210 16 [' ', :space],
Chris@210 17 [:open, :string],
Chris@210 18 ['"', :delimiter],
Chris@210 19 ['Hello, World!', :content],
Chris@210 20 ['"', :delimiter],
Chris@210 21 [:close, :string]
Chris@210 22 ]
Chris@210 23 def test_simple_scan
Chris@210 24 assert_nothing_raised do
Chris@210 25 assert_equal RUBY_TEST_TOKENS, CodeRay.scan(RUBY_TEST_CODE, :ruby).to_ary
Chris@210 26 end
Chris@210 27 end
Chris@210 28
Chris@210 29 RUBY_TEST_HTML = 'puts <span class="s"><span class="dl">&quot;</span>' +
Chris@210 30 '<span class="k">Hello, World!</span><span class="dl">&quot;</span></span>'
Chris@210 31 def test_simple_highlight
Chris@210 32 assert_nothing_raised do
Chris@210 33 assert_equal RUBY_TEST_HTML, CodeRay.scan(RUBY_TEST_CODE, :ruby).html
Chris@210 34 end
Chris@210 35 end
Chris@210 36
Chris@210 37 def test_duo
Chris@210 38 assert_equal(RUBY_TEST_CODE,
Chris@210 39 CodeRay::Duo[:plain, :plain].highlight(RUBY_TEST_CODE))
Chris@210 40 assert_equal(RUBY_TEST_CODE,
Chris@210 41 CodeRay::Duo[:plain => :plain].highlight(RUBY_TEST_CODE))
Chris@210 42 end
Chris@210 43
Chris@210 44 def test_duo_stream
Chris@210 45 assert_equal(RUBY_TEST_CODE,
Chris@210 46 CodeRay::Duo[:plain, :plain].highlight(RUBY_TEST_CODE, :stream => true))
Chris@210 47 end
Chris@210 48
Chris@210 49 def test_comment_filter
Chris@210 50 assert_equal <<-EXPECTED, CodeRay.scan(<<-INPUT, :ruby).comment_filter.text
Chris@210 51 #!/usr/bin/env ruby
Chris@210 52
Chris@210 53 code
Chris@210 54
Chris@210 55 more code
Chris@210 56 EXPECTED
Chris@210 57 #!/usr/bin/env ruby
Chris@210 58 =begin
Chris@210 59 A multi-line comment.
Chris@210 60 =end
Chris@210 61 code
Chris@210 62 # A single-line comment.
Chris@210 63 more code # and another comment, in-line.
Chris@210 64 INPUT
Chris@210 65 end
Chris@210 66
Chris@210 67 def test_lines_of_code
Chris@210 68 assert_equal 2, CodeRay.scan(<<-INPUT, :ruby).lines_of_code
Chris@210 69 #!/usr/bin/env ruby
Chris@210 70 =begin
Chris@210 71 A multi-line comment.
Chris@210 72 =end
Chris@210 73 code
Chris@210 74 # A single-line comment.
Chris@210 75 more code # and another comment, in-line.
Chris@210 76 INPUT
Chris@210 77 rHTML = <<-RHTML
Chris@210 78 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
Chris@210 79 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Chris@210 80
Chris@210 81 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
Chris@210 82 <head>
Chris@210 83 <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
Chris@210 84 <title><%= controller.controller_name.titleize %>: <%= controller.action_name %></title>
Chris@210 85 <%= stylesheet_link_tag 'scaffold' %>
Chris@210 86 </head>
Chris@210 87 <body>
Chris@210 88
Chris@210 89 <p style="color: green"><%= flash[:notice] %></p>
Chris@210 90
Chris@210 91 <div id="main">
Chris@210 92 <%= yield %>
Chris@210 93 </div>
Chris@210 94
Chris@210 95 </body>
Chris@210 96 </html>
Chris@210 97 RHTML
Chris@210 98 assert_equal 0, CodeRay.scan(rHTML, :html).lines_of_code
Chris@210 99 assert_equal 0, CodeRay.scan(rHTML, :php).lines_of_code
Chris@210 100 assert_equal 0, CodeRay.scan(rHTML, :yaml).lines_of_code
Chris@210 101 assert_equal 4, CodeRay.scan(rHTML, :rhtml).lines_of_code
Chris@210 102 end
Chris@210 103
Chris@210 104 def test_rubygems_not_loaded
Chris@210 105 assert_equal nil, defined? Gem
Chris@210 106 end if ENV['check_rubygems'] && RUBY_VERSION < '1.9'
Chris@210 107
Chris@210 108 def test_list_of_encoders
Chris@210 109 assert_kind_of(Array, CodeRay::Encoders.list)
Chris@210 110 assert CodeRay::Encoders.list.include?('count')
Chris@210 111 end
Chris@210 112
Chris@210 113 def test_list_of_scanners
Chris@210 114 assert_kind_of(Array, CodeRay::Scanners.list)
Chris@210 115 assert CodeRay::Scanners.list.include?('plaintext')
Chris@210 116 end
Chris@210 117
Chris@210 118 def test_scan_a_frozen_string
Chris@210 119 CodeRay.scan RUBY_VERSION, :ruby
Chris@210 120 end
Chris@210 121
Chris@210 122 end