summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2011-03-07 22:44:28 +0000
committermurphy <murphy@rubychan.de>2011-03-07 22:44:28 +0000
commit5803d382b3d3f053eceba892d3de2649060840a2 (patch)
tree182d22a9713e60abc23c84ed8cffe2d295998bf6
parent0eadffaa8754af81e7cb946449c45a44bd7ee552 (diff)
downloadcoderay-5803d382b3d3f053eceba892d3de2649060840a2.tar.gz
added more basic tests, cleanups
-rw-r--r--test/executable/suite.rb2
-rwxr-xr-xtest/functional/basic.rb189
-rwxr-xr-xtest/functional/suite.rb6
3 files changed, 187 insertions, 10 deletions
diff --git a/test/executable/suite.rb b/test/executable/suite.rb
index fdfeb4d..ddb0527 100644
--- a/test/executable/suite.rb
+++ b/test/executable/suite.rb
@@ -3,8 +3,6 @@ require 'pathname'
$:.unshift 'lib'
require 'coderay'
-MYDIR = File.dirname(__FILE__)
-
puts "Running CodeRay #{CodeRay::VERSION} executable tests..."
class TestCodeRayExecutable < Test::Unit::TestCase
diff --git a/test/functional/basic.rb b/test/functional/basic.rb
index 013c0c4..1d0c760 100755
--- a/test/functional/basic.rb
+++ b/test/functional/basic.rb
@@ -1,8 +1,21 @@
+# encoding: utf-8
require 'test/unit'
require 'coderay'
class BasicTest < Test::Unit::TestCase
+ def assert_warning expected_warning
+ require 'stringio'
+ oldstderr = $stderr
+ $stderr = StringIO.new
+ yield
+ $stderr.rewind
+ given_warning = $stderr.read.chomp
+ assert_equal expected_warning, given_warning
+ ensure
+ $stderr = oldstderr
+ end
+
def test_version
assert_nothing_raised do
assert_match(/\A\d\.\d\.\d\z/, CodeRay::VERSION)
@@ -34,16 +47,40 @@ class BasicTest < Test::Unit::TestCase
end
end
+ def test_scan_file
+ CodeRay.scan_file __FILE__
+ end
+
+ def test_encode
+ assert_equal 1, CodeRay.encode('test', :python, :count)
+ end
+
+ def test_encode_tokens
+ assert_equal 1, CodeRay.encode_tokens(CodeRay::Tokens['test', :string], :count)
+ end
+
+ def test_encode_file
+ assert_equal File.read(__FILE__), CodeRay.encode_file(__FILE__, :text)
+ end
+
+ def test_highlight
+ assert_match '<div class="code"><pre>test</pre></div>', CodeRay.highlight('test', :python)
+ end
+
+ def test_highlight_file
+ assert_match "require <span class=\"s\"><span class=\"dl\">'</span><span class=\"k\">test/unit</span><span class=\"dl\">'</span></span>\n", CodeRay.highlight_file(__FILE__)
+ end
+
def test_duo
assert_equal(RUBY_TEST_CODE,
- CodeRay::Duo[:plain, :plain].highlight(RUBY_TEST_CODE))
+ CodeRay::Duo[:plain, :text].highlight(RUBY_TEST_CODE))
assert_equal(RUBY_TEST_CODE,
- CodeRay::Duo[:plain => :plain].highlight(RUBY_TEST_CODE))
+ CodeRay::Duo[:plain => :text].highlight(RUBY_TEST_CODE))
end
def test_duo_stream
assert_equal(RUBY_TEST_CODE,
- CodeRay::Duo[:plain, :plain].highlight(RUBY_TEST_CODE, :stream => true))
+ CodeRay::Duo[:plain, :text].highlight(RUBY_TEST_CODE, :stream => true))
end
def test_comment_filter
@@ -107,12 +144,154 @@ more code # and another comment, in-line.
def test_list_of_encoders
assert_kind_of(Array, CodeRay::Encoders.list)
- assert CodeRay::Encoders.list.include?('count')
+ assert CodeRay::Encoders.list.include?(:count)
end
def test_list_of_scanners
assert_kind_of(Array, CodeRay::Scanners.list)
- assert CodeRay::Scanners.list.include?('plaintext')
+ assert CodeRay::Scanners.list.include?(:plaintext)
+ end
+
+ def test_token_kinds
+ assert_kind_of Hash, CodeRay::TokenKinds
+ for kind, css_class in CodeRay::TokenKinds
+ assert_kind_of Symbol, kind
+ if css_class != false
+ assert_kind_of String, css_class, "TokenKinds[%p] == %p" % [kind, css_class]
+ end
+ end
+ assert_equal 'r', CodeRay::TokenKinds[:reserved]
+ assert_equal false, CodeRay::TokenKinds[:shibboleet]
+ end
+
+ class Milk < CodeRay::Encoders::Encoder
+ FILE_EXTENSION = 'cocoa'
+ end
+
+ class HoneyBee < CodeRay::Encoders::Encoder
+ end
+
+ def test_encoder_file_extension
+ assert_nothing_raised do
+ assert_equal 'html', CodeRay::Encoders::HTML::FILE_EXTENSION
+ assert_equal 'cocoa', Milk::FILE_EXTENSION
+ assert_equal 'cocoa', Milk.new.file_extension
+ assert_equal 'honeybee', HoneyBee::FILE_EXTENSION
+ assert_equal 'honeybee', HoneyBee.new.file_extension
+ end
+ assert_raise NameError do
+ HoneyBee::MISSING_CONSTANT
+ end
+ end
+
+ def test_encoder_tokens
+ encoder = CodeRay::Encoders::Encoder.new
+ encoder.send :setup, {}
+ assert_raise(ArgumentError) { encoder.token :strange, '' }
+ encoder.token 'test', :debug
+ end
+
+ def test_encoder_deprecated_interface
+ encoder = CodeRay::Encoders::Encoder.new
+ encoder.send :setup, {}
+ assert_warning 'Using old Tokens#<< interface.' do
+ encoder << ['test', :content]
+ end
+ assert_raise ArgumentError do
+ encoder << [:strange, :input]
+ end
+ assert_raise ArgumentError do
+ encoder.encode_tokens [['test', :token]]
+ end
+ end
+
+ def encoder_token_interface_deprecation_warning_given
+ CodeRay::Encoders::Encoder.send :class_variable_get, :@@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN
+ end
+
+ def test_scanner_file_extension
+ assert_equal 'rb', CodeRay::Scanners::Ruby.file_extension
+ assert_equal 'rb', CodeRay::Scanners::Ruby.new.file_extension
+ assert_equal 'java', CodeRay::Scanners::Java.file_extension
+ assert_equal 'java', CodeRay::Scanners::Java.new.file_extension
+ end
+
+ def test_scanner_lang
+ assert_equal :ruby, CodeRay::Scanners::Ruby.lang
+ assert_equal :ruby, CodeRay::Scanners::Ruby.new.lang
+ assert_equal :java, CodeRay::Scanners::Java.lang
+ assert_equal :java, CodeRay::Scanners::Java.new.lang
+ end
+
+ def test_scanner_tokenize
+ assert_equal ['foo', :plain], CodeRay::Scanners::Plain.new.tokenize('foo')
+ assert_equal [['foo', :plain], ['bar', :plain]], CodeRay::Scanners::Plain.new.tokenize(['foo', 'bar'])
+ assert_raise ArgumentError do
+ CodeRay::Scanners::Plain.new.tokenize 42
+ end
+ end
+
+ def test_scanner_tokens
+ scanner = CodeRay::Scanners::Plain.new
+ scanner.tokenize('foo')
+ assert_equal ['foo', :plain], scanner.tokens
+ scanner.string = ''
+ assert_equal ['', :plain], scanner.tokens
+ end
+
+ def test_scanner_line_and_column
+ scanner = CodeRay::Scanners::Plain.new "foo\nbär+quux"
+ assert_equal 0, scanner.pos
+ assert_equal 1, scanner.line
+ assert_equal 0, scanner.column
+ scanner.scan(/foo\nbär/)
+ assert_equal 8, scanner.pos
+ assert_equal 2, scanner.line
+ assert_equal 5, scanner.column
+ end
+
+ def test_scanner_use_subclasses
+ assert_raise NotImplementedError do
+ CodeRay::Scanners::Scanner.new
+ end
+ end
+
+ class InvalidScanner < CodeRay::Scanners::Scanner
+ end
+
+ def test_scanner_scan_tokens
+ assert_raise NotImplementedError do
+ InvalidScanner.new.tokenize ''
+ end
+ end
+
+ class RaisingScanner < CodeRay::Scanners::Scanner
+ def scan_tokens encoder, options
+ raise_inspect 'message', [], :initial
+ end
+ end
+
+ def test_scanner_raise_inspect
+ assert_raise CodeRay::Scanners::Scanner::ScanError do
+ RaisingScanner.new.tokenize ''
+ end
+ end
+
+ def test_scan_a_frozen_string
+ assert_nothing_raised do
+ CodeRay.scan RUBY_VERSION, :ruby
+ CodeRay.scan RUBY_VERSION, :plain
+ end
+ end
+
+ def test_scan_a_non_string
+ assert_nothing_raised do
+ CodeRay.scan 42, :ruby
+ CodeRay.scan nil, :ruby
+ CodeRay.scan self, :ruby
+ CodeRay.encode ENV.to_hash, :ruby, :page
+ CodeRay.highlight CodeRay, :plain
+ end
end
end
diff --git a/test/functional/suite.rb b/test/functional/suite.rb
index 4189953..6d795c0 100755
--- a/test/functional/suite.rb
+++ b/test/functional/suite.rb
@@ -2,12 +2,12 @@ require 'test/unit'
$:.unshift 'lib'
require 'coderay'
-MYDIR = File.dirname(__FILE__)
-suite = Dir[File.join(MYDIR, '*.rb')].
+mydir = File.dirname(__FILE__)
+suite = Dir[File.join(mydir, '*.rb')].
map { |tc| File.basename(tc).sub(/\.rb$/, '') } - %w'suite for_redcloth'
puts "Running basic CodeRay #{CodeRay::VERSION} tests: #{suite.join(', ')}"
for test_case in suite
- load File.join(MYDIR, test_case + '.rb')
+ load File.join(mydir, test_case + '.rb')
end