summaryrefslogtreecommitdiff
path: root/test/unit
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2011-02-28 23:10:25 +0000
committermurphy <murphy@rubychan.de>2011-02-28 23:10:25 +0000
commit4d8afef1c46d1db99a7c71bc71906317441a90c3 (patch)
tree18bf4a476168de72959705aae7ff6a0ef6a4d2a5 /test/unit
parentfa8b580bfcf1082ac671acde60c4205681dec1dd (diff)
downloadcoderay-4d8afef1c46d1db99a7c71bc71906317441a90c3.tar.gz
Added lots of tests for Plugin and other helper classes.
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/file_type.rb7
-rw-r--r--test/unit/json_encoder.rb (renamed from test/unit/json.rb)0
-rwxr-xr-xtest/unit/plugin.rb72
-rw-r--r--test/unit/plugins/example.rb6
-rw-r--r--test/unit/plugins/user_defined/user_plugin.rb5
-rw-r--r--test/unit/plugins_with_default/default.rb5
-rw-r--r--test/unit/plugins_with_default/example_without_register_for.rb5
-rwxr-xr-xtest/unit/suite.rb6
-rw-r--r--test/unit/tokens.rb94
-rw-r--r--test/unit/vhdl.rb126
10 files changed, 181 insertions, 145 deletions
diff --git a/test/unit/file_type.rb b/test/unit/file_type.rb
index 4558850..aa54ba2 100644
--- a/test/unit/file_type.rb
+++ b/test/unit/file_type.rb
@@ -39,6 +39,7 @@ class FileTypeTests < Test::Unit::TestCase
end
def test_ruby
+ assert_equal :ruby, FileType[__FILE__]
assert_equal :ruby, FileType['test.rb']
assert_equal :ruby, FileType['test.java.rb']
assert_equal :java, FileType['test.rb.java']
@@ -109,7 +110,11 @@ class FileTypeTests < Test::Unit::TestCase
require 'tmpdir'
tmpfile = File.join(Dir.tmpdir, 'bla')
File.open(tmpfile, 'w') { } # touch
- assert_equal nil, FileType[tmpfile]
+ assert_equal nil, FileType[tmpfile, true]
+ end
+
+ def test_shebang_no_file
+ assert_equal nil, FileType['i do not exist', true]
end
def test_shebang
diff --git a/test/unit/json.rb b/test/unit/json_encoder.rb
index 4e44a64..4e44a64 100644
--- a/test/unit/json.rb
+++ b/test/unit/json_encoder.rb
diff --git a/test/unit/plugin.rb b/test/unit/plugin.rb
index 25bbc93..ee3e2cc 100755
--- a/test/unit/plugin.rb
+++ b/test/unit/plugin.rb
@@ -1,11 +1,79 @@
require 'test/unit'
require 'coderay'
+require 'pathname'
class PluginScannerTest < Test::Unit::TestCase
+ module Plugins
+ extend CodeRay::PluginHost
+ plugin_path File.dirname(__FILE__), 'plugins'
+ class Plugin
+ extend CodeRay::Plugin
+ plugin_host Plugins
+ end
+ end
+
+ module PluginsWithDefault
+ extend CodeRay::PluginHost
+ plugin_path File.dirname(__FILE__), 'plugins_with_default'
+ class Plugin
+ extend CodeRay::Plugin
+ plugin_host PluginsWithDefault
+ end
+ default :default
+ end
+
def test_load
- require File.join(File.dirname(__FILE__), 'vhdl')
- assert_equal 'VHDL', CodeRay.scanner(:vhdl).class.name
+ require Pathname.new(__FILE__).realpath.dirname + 'plugins' + 'user_defined' + 'user_plugin'
+ assert_equal 'UserPlugin', Plugins.load(:user_plugin).name
+ end
+
+ def test_load_all
+ assert_instance_of Symbol, Plugins.load_all.first
+ assert_operator Plugins.all_plugins.first, :<, Plugins::Plugin
+ assert_equal 'The Example', Plugins.all_titles.sort.first
+ end
+
+ def test_default
+ assert_nothing_raised do
+ assert_operator PluginsWithDefault[:gargamel], :<, PluginsWithDefault::Plugin
+ end
+ assert_equal PluginsWithDefault::Default, PluginsWithDefault.default
+ end
+
+ def test_plugin_not_found
+ assert_raise CodeRay::PluginHost::PluginNotFound do
+ Plugins[:thestral]
+ end
+ assert_raise ArgumentError do
+ Plugins[14]
+ end
+ assert_raise ArgumentError do
+ Plugins['test/test']
+ end
+ assert_raise CodeRay::PluginHost::PluginNotFound do
+ PluginsWithDefault[:example_without_register_for]
+ end
+ end
+
+ def test_autoload_constants
+ assert_operator Plugins::Example, :<, Plugins::Plugin
+ end
+
+ def test_title
+ assert_equal 'The Example', Plugins::Example.title
+ end
+
+ 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
end
diff --git a/test/unit/plugins/example.rb b/test/unit/plugins/example.rb
new file mode 100644
index 0000000..e28dc7c
--- /dev/null
+++ b/test/unit/plugins/example.rb
@@ -0,0 +1,6 @@
+class Example < PluginScannerTest::Plugins::Plugin
+
+ register_for :example
+ title 'The Example'
+
+end \ No newline at end of file
diff --git a/test/unit/plugins/user_defined/user_plugin.rb b/test/unit/plugins/user_defined/user_plugin.rb
new file mode 100644
index 0000000..61e5372
--- /dev/null
+++ b/test/unit/plugins/user_defined/user_plugin.rb
@@ -0,0 +1,5 @@
+class UserPlugin < PluginScannerTest::Plugins::Plugin
+
+ register_for :user_plugin
+
+end \ No newline at end of file
diff --git a/test/unit/plugins_with_default/default.rb b/test/unit/plugins_with_default/default.rb
new file mode 100644
index 0000000..e67c9f1
--- /dev/null
+++ b/test/unit/plugins_with_default/default.rb
@@ -0,0 +1,5 @@
+class Default < PluginScannerTest::PluginsWithDefault::Plugin
+
+ register_for :default
+
+end \ No newline at end of file
diff --git a/test/unit/plugins_with_default/example_without_register_for.rb b/test/unit/plugins_with_default/example_without_register_for.rb
new file mode 100644
index 0000000..d9f40be
--- /dev/null
+++ b/test/unit/plugins_with_default/example_without_register_for.rb
@@ -0,0 +1,5 @@
+class ExampleWithoutRegisterFor < PluginScannerTest::Plugins::Plugin
+
+ register_for :wrong_id
+
+end \ No newline at end of file
diff --git a/test/unit/suite.rb b/test/unit/suite.rb
index 845c5b2..769b6cc 100755
--- a/test/unit/suite.rb
+++ b/test/unit/suite.rb
@@ -1,13 +1,13 @@
require 'test/unit'
$:.unshift 'lib'
-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 vhdl'
puts "Running CodeRay unit tests: #{suite.join(', ')}"
helpers = %w(file_type word_list tokens)
for test_case in helpers + (suite - helpers)
- load File.join(MYDIR, test_case + '.rb')
+ load File.join(mydir, test_case + '.rb')
end
diff --git a/test/unit/tokens.rb b/test/unit/tokens.rb
index ae6d25a..51d286e 100644
--- a/test/unit/tokens.rb
+++ b/test/unit/tokens.rb
@@ -1,5 +1,5 @@
require 'test/unit'
-require 'coderay/tokens'
+require 'coderay'
class TokensTest < Test::Unit::TestCase
@@ -13,21 +13,13 @@ class TokensTest < Test::Unit::TestCase
end
def test_adding_tokens
- tokens = CodeRay::Tokens.new
- assert_nothing_raised do
- tokens.text_token 'string', :type
- tokens.text_token '()', :operator
- end
- assert_equal tokens.size, 4
- assert_equal tokens.count, 2
+ tokens = make_tokens
+ assert_equal tokens.size, 8
+ assert_equal tokens.count, 4
end
def test_dump_undump
- tokens = CodeRay::Tokens.new
- assert_nothing_raised do
- tokens.text_token 'string', :type
- tokens.text_token '()', :operator
- end
+ tokens = make_tokens
tokens2 = nil
assert_nothing_raised do
tokens2 = tokens.dump.undump
@@ -35,4 +27,80 @@ class TokensTest < Test::Unit::TestCase
assert_equal tokens, tokens2
end
+ def test_to_s
+ assert_equal 'string()', make_tokens.to_s
+ end
+
+ def test_encode_with_nonsense
+ assert_raise NoMethodError do
+ make_tokens.nonsense
+ end
+ end
+
+ def test_optimize
+ assert_raise NotImplementedError do
+ make_tokens.optimize
+ end
+ assert_raise NotImplementedError do
+ make_tokens.optimize!
+ end
+ end
+
+ def test_fix
+ assert_raise NotImplementedError do
+ make_tokens.fix
+ end
+ assert_raise NotImplementedError do
+ make_tokens.fix!
+ end
+ end
+
+ def test_split_into_lines
+ assert_raise NotImplementedError do
+ make_tokens.split_into_lines
+ end
+ assert_raise NotImplementedError do
+ make_tokens.split_into_lines!
+ end
+ end
+
+ def test_split_into_parts
+ parts = [
+ ["stri", :type],
+ ["ng", :type, :begin_group, :operator, "(", :content, :end_group, :operator],
+ [:begin_group, :operator, ")", :content, :end_group, :operator]
+ ]
+ assert_equal parts, make_tokens.split_into_parts(4, 3)
+ assert_equal [make_tokens.to_a], make_tokens.split_into_parts
+
+ line = CodeRay::Tokens[:begin_line, :head, '...', :plain]
+ line_parts = [
+ [:begin_line, :head, ".", :plain, :end_line, :head],
+ [:begin_line, :head, "..", :plain]
+ ]
+ assert_equal line_parts, line.split_into_parts(1)
+
+ assert_raise ArgumentError do
+ CodeRay::Tokens[:bullshit, :input].split_into_parts
+ end
+ assert_raise ArgumentError do
+ CodeRay::Tokens[42, 43].split_into_parts
+ end
+ end
+
+ def test_encode
+ assert_match(/\A\[\{(?:"type":"text"|"text":"string"|"kind":"type"|,){5}\},/, make_tokens.encode(:json))
+ end
+
+ def make_tokens
+ tokens = CodeRay::Tokens.new
+ assert_nothing_raised do
+ tokens.text_token 'string', :type
+ tokens.begin_group :operator
+ tokens.text_token '()', :content
+ tokens.end_group :operator
+ end
+ tokens
+ end
+
end \ No newline at end of file
diff --git a/test/unit/vhdl.rb b/test/unit/vhdl.rb
deleted file mode 100644
index 3b8262b..0000000
--- a/test/unit/vhdl.rb
+++ /dev/null
@@ -1,126 +0,0 @@
-class VHDL < CodeRay::Scanners::Scanner
-
- register_for :vhdl
-
- RESERVED_WORDS = [
- 'access','after','alias','all','assert','architecture','begin',
- 'block','body','buffer','bus','case','component','configuration','constant',
- 'disconnect','downto','else','elsif','end','entity','exit','file','for',
- 'function','generate','generic','group','guarded','if','impure','in',
- 'inertial','inout','is','label','library','linkage','literal','loop',
- 'map','new','next','null','of','on','open','others','out','package',
- 'port','postponed','procedure','process','pure','range','record','register',
- 'reject','report','return','select','severity','signal','shared','subtype',
- 'then','to','transport','type','unaffected','units','until','use','variable',
- 'wait','when','while','with','note','warning','error','failure','and',
- 'or','xor','not','nor',
- 'array'
- ]
-
- PREDEFINED_TYPES = [
- 'bit','bit_vector','character','boolean','integer','real','time','string',
- 'severity_level','positive','natural','signed','unsigned','line','text',
- 'std_logic','std_logic_vector','std_ulogic','std_ulogic_vector','qsim_state',
- 'qsim_state_vector','qsim_12state','qsim_12state_vector','qsim_strength',
- 'mux_bit','mux_vector','reg_bit','reg_vector','wor_bit','wor_vector'
- ]
-
- PREDEFINED_CONSTANTS = [
-
- ]
-
- IDENT_KIND = CodeRay::CaseIgnoringWordList.new(:ident).
- add(RESERVED_WORDS, :reserved).
- add(PREDEFINED_TYPES, :pre_type).
- add(PREDEFINED_CONSTANTS, :pre_constant)
-
- ESCAPE = / [rbfntv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x
- UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x
-
- def scan_tokens tokens, options
-
- state = :initial
-
- until eos?
-
- kind = nil
- match = nil
-
- case state
-
- when :initial
-
- if scan(/ \s+ | \\\n /x)
- kind = :space
-
- elsif scan(/-- .*/x)
- kind = :comment
-
- elsif scan(/ [-+*\/=<>?:;,!&^|()\[\]{}~%]+ | \.(?!\d) /x)
- kind = :operator
-
- elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
- kind = IDENT_KIND[match.downcase]
-
- elsif match = scan(/[a-z]?"/i)
- tokens << [:open, :string]
- state = :string
- kind = :delimiter
-
- elsif scan(/ L?' (?: [^\'\n\\] | \\ #{ESCAPE} )? '? /ox)
- kind = :char
-
- elsif scan(/(?:\d+)(?![.eEfF])/)
- kind = :integer
-
- elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
- kind = :float
-
- else
- getch
- kind = :error
-
- end
-
- when :string
- if scan(/[^\\\n"]+/)
- kind = :content
- elsif scan(/"/)
- tokens << ['"', :delimiter]
- tokens << [:close, :string]
- state = :initial
- next
- elsif scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox)
- kind = :char
- elsif scan(/ \\ | $ /x)
- tokens << [:close, :string]
- kind = :error
- state = :initial
- else
- raise_inspect "else case \" reached; %p not handled." % peek(1), tokens
- end
-
- else
- raise_inspect 'Unknown state', tokens
-
- end
-
- match ||= matched
- if $CODERAY_DEBUG and not kind
- raise_inspect 'Error token %p in line %d' %
- [[match, kind], line], tokens
- end
- raise_inspect 'Empty token', tokens unless match
-
- tokens << [match, kind]
-
- end
-
- if state == :string
- tokens << [:close, :string]
- end
-
- tokens
- end
-
-end