diff options
Diffstat (limited to 'lib/coderay/encoders')
-rw-r--r-- | lib/coderay/encoders/comment_filter.rb | 61 | ||||
-rw-r--r-- | lib/coderay/encoders/count.rb | 23 | ||||
-rw-r--r-- | lib/coderay/encoders/debug.rb | 49 | ||||
-rw-r--r-- | lib/coderay/encoders/filter.rb | 46 | ||||
-rw-r--r-- | lib/coderay/encoders/html/css.rb | 5 | ||||
-rw-r--r-- | lib/coderay/encoders/statistic.rb | 65 | ||||
-rw-r--r-- | lib/coderay/encoders/text.rb | 24 | ||||
-rw-r--r-- | lib/coderay/encoders/token_kind_filter.rb | 58 |
8 files changed, 2 insertions, 329 deletions
diff --git a/lib/coderay/encoders/comment_filter.rb b/lib/coderay/encoders/comment_filter.rb index 33e2dfb..28336b3 100644 --- a/lib/coderay/encoders/comment_filter.rb +++ b/lib/coderay/encoders/comment_filter.rb @@ -1,4 +1,3 @@ -($:.unshift '../..'; require 'coderay') unless defined? CodeRay module CodeRay module Encoders @@ -24,63 +23,3 @@ module Encoders end end - -if $0 == __FILE__ - $VERBOSE = true - $: << File.join(File.dirname(__FILE__), '..') - eval DATA.read, nil, $0, __LINE__ + 4 -end - -__END__ -require 'test/unit' - -class CommentFilterTest < Test::Unit::TestCase - - def test_filtering_comments - tokens = CodeRay.scan <<-RUBY, :ruby -#!/usr/bin/env ruby -# a minimal Ruby program -puts "Hello world!" - RUBY - assert_equal <<-RUBY_FILTERED, tokens.comment_filter.text -#!/usr/bin/env ruby - -puts "Hello world!" - RUBY_FILTERED - end - - def test_filtering_docstrings - tokens = CodeRay.scan <<-PYTHON, :python -''' -Assuming this is file mymodule.py then this string, being the -first statement in the file will become the mymodule modules -docstring when the file is imported -''' - -class Myclass(): - """ - The class's docstring - """ - - def mymethod(self): - '''The method's docstring''' - -def myfunction(): - """The function's docstring""" - PYTHON - assert_equal <<-PYTHON_FILTERED.chomp, tokens.comment_filter.text - - -class Myclass(): - - - def mymethod(self): - - -def myfunction(): - - -PYTHON_FILTERED - end - -end
\ No newline at end of file diff --git a/lib/coderay/encoders/count.rb b/lib/coderay/encoders/count.rb index f4dd0c8..c28d67f 100644 --- a/lib/coderay/encoders/count.rb +++ b/lib/coderay/encoders/count.rb @@ -1,4 +1,3 @@ -($:.unshift '../..'; require 'coderay') unless defined? CodeRay module CodeRay module Encoders @@ -30,25 +29,3 @@ module Encoders end end - -if $0 == __FILE__ - $VERBOSE = true - $: << File.join(File.dirname(__FILE__), '..') - eval DATA.read, nil, $0, __LINE__ + 4 -end - -__END__ -require 'test/unit' - -class CountTest < Test::Unit::TestCase - - def test_count - tokens = CodeRay.scan <<-RUBY.strip, :ruby -#!/usr/bin/env ruby -# a minimal Ruby program -puts "Hello world!" - RUBY - assert_equal 9, tokens.encode_with(:count) - end - -end
\ No newline at end of file diff --git a/lib/coderay/encoders/debug.rb b/lib/coderay/encoders/debug.rb index c97631b..ef76192 100644 --- a/lib/coderay/encoders/debug.rb +++ b/lib/coderay/encoders/debug.rb @@ -1,4 +1,3 @@ -($:.unshift '../..'; require 'coderay') unless defined? CodeRay module CodeRay module Encoders @@ -30,6 +29,7 @@ module Encoders if kind == :space @out << text else + # FIXME: Escape ( text = text.gsub(/[)\\]/, '\\\\\0') # escape ) and \ @out << kind.to_s << '(' << text << ')' end @@ -61,50 +61,3 @@ module Encoders end end - -if $0 == __FILE__ - $VERBOSE = true - $: << File.join(File.dirname(__FILE__), '..') - eval DATA.read, nil, $0, __LINE__ + 4 -end - -__END__ -require 'test/unit' - -class DebugEncoderTest < Test::Unit::TestCase - - def test_creation - assert CodeRay::Encoders::Debug < CodeRay::Encoders::Encoder - debug = nil - assert_nothing_raised do - debug = CodeRay.encoder :debug - end - assert_kind_of CodeRay::Encoders::Encoder, debug - end - - TEST_INPUT = CodeRay::Tokens[ - ['10', :integer], - ['(\\)', :operator], - [:begin_group, :string], - ['test', :content], - [:end_group, :string], - [:begin_line, :test], - ["\n", :space], - ["\n \t", :space], - [" \n", :space], - ["[]", :method], - [:end_line, :test], - ].flatten - TEST_OUTPUT = <<-'DEBUG'.chomp -integer(10)operator((\\\))string<content(test)>test[ - - -method([])] - DEBUG - - def test_filtering_text_tokens - assert_equal TEST_OUTPUT, CodeRay::Encoders::Debug.new.encode_tokens(TEST_INPUT) - assert_equal TEST_OUTPUT, TEST_INPUT.debug - end - -end diff --git a/lib/coderay/encoders/filter.rb b/lib/coderay/encoders/filter.rb index e06fba7..a71e43e 100644 --- a/lib/coderay/encoders/filter.rb +++ b/lib/coderay/encoders/filter.rb @@ -1,4 +1,3 @@ -($:.unshift '../..'; require 'coderay') unless defined? CodeRay module CodeRay module Encoders @@ -51,48 +50,3 @@ module Encoders end end - -if $0 == __FILE__ - $VERBOSE = true - $: << File.join(File.dirname(__FILE__), '..') - eval DATA.read, nil, $0, __LINE__ + 4 -end - -__END__ -require 'test/unit' - -class FilterTest < Test::Unit::TestCase - - def test_creation - assert CodeRay::Encoders::Filter < CodeRay::Encoders::Encoder - filter = nil - assert_nothing_raised do - filter = CodeRay.encoder :filter - end - assert_kind_of CodeRay::Encoders::Encoder, filter - end - - def test_filtering_text_tokens - tokens = CodeRay::Tokens.new - 10.times do |i| - tokens.text_token i.to_s, :index - end - assert_equal tokens, CodeRay::Encoders::Filter.new.encode_tokens(tokens) - assert_equal tokens, tokens.filter - end - - def test_filtering_block_tokens - tokens = CodeRay::Tokens.new - 10.times do |i| - tokens.begin_group :index - tokens.text_token i.to_s, :content - tokens.end_group :index - tokens.begin_line :index - tokens.text_token i.to_s, :content - tokens.end_line :index - end - assert_equal tokens, CodeRay::Encoders::Filter.new.encode_tokens(tokens) - assert_equal tokens, tokens.filter - end - -end diff --git a/lib/coderay/encoders/html/css.rb b/lib/coderay/encoders/html/css.rb index 10e89fc..c459222 100644 --- a/lib/coderay/encoders/html/css.rb +++ b/lib/coderay/encoders/html/css.rb @@ -63,8 +63,3 @@ module Encoders end end - -if $0 == __FILE__ - require 'pp' - pp CodeRay::Encoders::HTML::CSS.new -end diff --git a/lib/coderay/encoders/statistic.rb b/lib/coderay/encoders/statistic.rb index 455da46..38464ce 100644 --- a/lib/coderay/encoders/statistic.rb +++ b/lib/coderay/encoders/statistic.rb @@ -1,4 +1,3 @@ -($:.unshift '../..'; require 'coderay') unless defined? CodeRay module CodeRay module Encoders @@ -93,67 +92,3 @@ Token Types (%d): end end - -if $0 == __FILE__ - $VERBOSE = true - $: << File.join(File.dirname(__FILE__), '..') - eval DATA.read, nil, $0, __LINE__ + 4 -end - -__END__ -require 'test/unit' - -class StatisticEncoderTest < Test::Unit::TestCase - - def test_creation - assert CodeRay::Encoders::Statistic < CodeRay::Encoders::Encoder - stats = nil - assert_nothing_raised do - stats = CodeRay.encoder :statistic - end - assert_kind_of CodeRay::Encoders::Encoder, stats - end - - TEST_INPUT = CodeRay::Tokens[ - ['10', :integer], - ['(\\)', :operator], - [:begin_group, :string], - ['test', :content], - [:end_group, :string], - [:begin_line, :test], - ["\n", :space], - ["\n \t", :space], - [" \n", :space], - ["[]", :method], - [:end_line, :test], - ].flatten - TEST_OUTPUT = <<-'DEBUG' - -Code Statistics - -Tokens 11 - Non-Whitespace 4 -Bytes Total 20 - -Token Types (5): - type count ratio size (average) -------------------------------------------------------------- - TOTAL 11 100.00 % 1.8 - space 3 27.27 % 3.0 - begin_group 1 9.09 % 0.0 - begin_line 1 9.09 % 0.0 - content 1 9.09 % 4.0 - end_group 1 9.09 % 0.0 - end_line 1 9.09 % 0.0 - integer 1 9.09 % 2.0 - method 1 9.09 % 2.0 - operator 1 9.09 % 3.0 - - DEBUG - - def test_filtering_text_tokens - assert_equal TEST_OUTPUT, CodeRay::Encoders::Statistic.new.encode_tokens(TEST_INPUT) - assert_equal TEST_OUTPUT, TEST_INPUT.statistic - end - -end
\ No newline at end of file diff --git a/lib/coderay/encoders/text.rb b/lib/coderay/encoders/text.rb index c146038..54a00dc 100644 --- a/lib/coderay/encoders/text.rb +++ b/lib/coderay/encoders/text.rb @@ -1,4 +1,3 @@ -($:.unshift '../..'; require 'coderay') unless defined? CodeRay module CodeRay module Encoders @@ -24,7 +23,7 @@ module Encoders } def text_token text, kind - @out << text + super @out << @sep if @sep end @@ -42,24 +41,3 @@ module Encoders end end - -if $0 == __FILE__ - $VERBOSE = true - $: << File.join(File.dirname(__FILE__), '..') - eval DATA.read, nil, $0, __LINE__ + 4 -end - -__END__ -require 'test/unit' - -class CountTest < Test::Unit::TestCase - - def test_count - ruby = <<-RUBY -puts "Hello world!" - RUBY - tokens = CodeRay.scan ruby, :ruby - assert_equal ruby, tokens.encode_with(:text) - end - -end
\ No newline at end of file diff --git a/lib/coderay/encoders/token_kind_filter.rb b/lib/coderay/encoders/token_kind_filter.rb index e558594..1ecf6ae 100644 --- a/lib/coderay/encoders/token_kind_filter.rb +++ b/lib/coderay/encoders/token_kind_filter.rb @@ -1,4 +1,3 @@ -($:.unshift '../..'; require 'coderay') unless defined? CodeRay module CodeRay module Encoders @@ -109,60 +108,3 @@ module Encoders end end - -if $0 == __FILE__ - $VERBOSE = true - $: << File.join(File.dirname(__FILE__), '..') - eval DATA.read, nil, $0, __LINE__ + 4 -end - -__END__ -require 'test/unit' - -class TokenKindFilterTest < Test::Unit::TestCase - - def test_creation - assert CodeRay::Encoders::TokenKindFilter < CodeRay::Encoders::Encoder - assert CodeRay::Encoders::TokenKindFilter < CodeRay::Encoders::Filter - filter = nil - assert_nothing_raised do - filter = CodeRay.encoder :token_kind_filter - end - assert_instance_of CodeRay::Encoders::TokenKindFilter, filter - end - - def test_filtering_text_tokens - tokens = CodeRay::Tokens.new - for i in 1..10 - tokens.text_token i.to_s, :index - tokens.text_token ' ', :space if i < 10 - end - assert_equal 10, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :exclude => :space).count - assert_equal 10, tokens.token_kind_filter(:exclude => :space).count - assert_equal 9, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :include => :space).count - assert_equal 9, tokens.token_kind_filter(:include => :space).count - assert_equal 0, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :exclude => :all).count - assert_equal 0, tokens.token_kind_filter(:exclude => :all).count - end - - def test_filtering_block_tokens - tokens = CodeRay::Tokens.new - 10.times do |i| - tokens.begin_group :index - tokens.text_token i.to_s, :content - tokens.end_group :index - tokens.begin_group :naught if i == 5 - tokens.end_group :naught if i == 7 - tokens.begin_line :blubb - tokens.text_token i.to_s, :content - tokens.end_line :blubb - end - assert_equal 16, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :include => :blubb).count - assert_equal 16, tokens.token_kind_filter(:include => :blubb).count - assert_equal 24, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :include => [:blubb, :content]).count - assert_equal 24, tokens.token_kind_filter(:include => [:blubb, :content]).count - assert_equal 32, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :exclude => :index).count - assert_equal 32, tokens.token_kind_filter(:exclude => :index).count - end - -end |