Fixing tests for block comments.

This commit is contained in:
Mike West 2010-11-22 08:41:54 +01:00
parent f177a9d7e2
commit 77dff765b6
4 changed files with 33 additions and 31 deletions

View File

@ -247,15 +247,16 @@ class Rocco
# This requires an `in_comment_block` boolean, and a few regular # This requires an `in_comment_block` boolean, and a few regular
# expressions for line tests. # expressions for line tests.
in_comment_block = false in_comment_block = false
single_line_comment, block_comment_start, block_comment_end = nil, nil, nil single_line_comment, block_comment_start, block_comment_mid, block_comment_end = nil, nil, nil, nil
if not @options[:comment_chars][:single].nil? if not @options[:comment_chars][:single].nil?
single_line_comment = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:single])}\s?") single_line_comment = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:single])}\\s?")
end end
if not @options[:comment_chars][:multi].nil? if not @options[:comment_chars][:multi].nil?
require 'pp' block_comment_start = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:start])}\\s*$")
pp @options[:comment_chars] block_comment_end = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:end])}\\s*$")
block_comment_start = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:start])}\s*$") if @options[:comment_chars][:multi][:middle]
block_comment_end = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:end])}\s*$") block_comment_mid = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:middle])}\\s?")
end
end end
lines.each do |line| lines.each do |line|
# If we're currently in a comment block, check whether the line matches # If we're currently in a comment block, check whether the line matches
@ -264,7 +265,7 @@ class Rocco
if block_comment_end && line.match( block_comment_end ) if block_comment_end && line.match( block_comment_end )
in_comment_block = false in_comment_block = false
else else
docs << line docs << line.sub( block_comment_mid, '' )
end end
# Otherwise, check whether the line matches the beginning of a block, or # Otherwise, check whether the line matches the beginning of a block, or
# a single-line comment all on it's lonesome. In either case, if there's # a single-line comment all on it's lonesome. In either case, if there's
@ -281,7 +282,7 @@ class Rocco
sections << [docs, code] sections << [docs, code]
docs, code = [], [] docs, code = [], []
end end
docs << line docs << line.sub( single_line_comment, '' )
else else
code << line code << line
end end
@ -297,7 +298,7 @@ class Rocco
def split(sections) def split(sections)
docs_blocks, code_blocks = [], [] docs_blocks, code_blocks = [], []
sections.each do |docs,code| sections.each do |docs,code|
docs_blocks << docs.map { |line| line.sub(@comment_pattern, '') }.join("\n") docs_blocks << docs.join("\n")
code_blocks << code.map do |line| code_blocks << code.map do |line|
tabs = line.match(/^(\t+)/) tabs = line.match(/^(\t+)/)
tabs ? line.sub(/^\t+/, ' ' * tabs.captures[0].length) : line tabs ? line.sub(/^\t+/, ' ' * tabs.captures[0].length) : line

View File

@ -27,14 +27,14 @@ class RoccoBasicTests < Test::Unit::TestCase
r = Rocco.new( 'test' ) { "" } # Generate throwaway instance so I can test `parse` r = Rocco.new( 'test' ) { "" } # Generate throwaway instance so I can test `parse`
assert_equal( assert_equal(
[ [
[ [ "# Comment 1" ], [ "def codeblock", "end" ] ] [ [ "Comment 1" ], [ "def codeblock", "end" ] ]
], ],
r.parse( "# Comment 1\ndef codeblock\nend\n" ) r.parse( "# Comment 1\ndef codeblock\nend\n" )
) )
assert_equal( assert_equal(
[ [
[ [ "# Comment 1" ], [ "def codeblock" ] ], [ [ "Comment 1" ], [ "def codeblock" ] ],
[ [ "# Comment 2" ], [ "end" ] ] [ [ "Comment 2" ], [ "end" ] ]
], ],
r.parse( "# Comment 1\ndef codeblock\n# Comment 2\nend\n" ) r.parse( "# Comment 1\ndef codeblock\n# Comment 2\nend\n" )
) )
@ -47,7 +47,7 @@ class RoccoBasicTests < Test::Unit::TestCase
[ "Comment 1" ], [ "Comment 1" ],
[ "def codeblock\nend" ] [ "def codeblock\nend" ]
], ],
r.split([ [ [ "# Comment 1" ], [ "def codeblock", "end" ] ] ]) r.split([ [ [ "Comment 1" ], [ "def codeblock", "end" ] ] ])
) )
assert_equal( assert_equal(
[ [
@ -55,8 +55,8 @@ class RoccoBasicTests < Test::Unit::TestCase
[ "def codeblock", "end" ] [ "def codeblock", "end" ]
], ],
r.split( [ r.split( [
[ [ "# Comment 1" ], [ "def codeblock" ] ], [ [ "Comment 1" ], [ "def codeblock" ] ],
[ [ "# Comment 2" ], [ "end" ] ] [ [ "Comment 2" ], [ "end" ] ]
] ) ] )
) )
end end

View File

@ -5,13 +5,13 @@ class RoccoBlockCommentTest < Test::Unit::TestCase
r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse`
assert_equal( assert_equal(
[ [
[ [ " * Comment 1" ], [ "def codeblock", "end" ] ] [ [ "Comment 1" ], [ "def codeblock", "end" ] ]
], ],
r.parse( "/**\n * Comment 1\n*/\ndef codeblock\nend\n" ) r.parse( "/**\n * Comment 1\n*/\ndef codeblock\nend\n" )
) )
assert_equal( assert_equal(
[ [
[ [ " * Comment 1a", " * Comment 1b" ], [ "def codeblock", "end" ] ] [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ]
], ],
r.parse( "/**\n * Comment 1a\n * Comment 1b\n*/\ndef codeblock\nend\n" ) r.parse( "/**\n * Comment 1a\n * Comment 1b\n*/\ndef codeblock\nend\n" )
) )
@ -20,15 +20,15 @@ class RoccoBlockCommentTest < Test::Unit::TestCase
r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse`
assert_equal( assert_equal(
[ [
[ [ " * Comment 1" ], [ "def codeblock", "end" ] ], [ [ "Comment 1" ], [ "def codeblock", "end" ] ],
[ [ " * Comment 2" ], [] ] [ [ "Comment 2" ], [] ]
], ],
r.parse( "/**\n * Comment 1\n*/\ndef codeblock\nend\n/**\n * Comment 2\n*/\n" ) r.parse( "/**\n * Comment 1\n*/\ndef codeblock\nend\n/**\n * Comment 2\n*/\n" )
) )
assert_equal( assert_equal(
[ [
[ [ " * Comment 1" ], [ "def codeblock", "end" ] ], [ [ "Comment 1" ], [ "def codeblock", "end" ] ],
[ [ " * Comment 2" ], [ "if false", "end" ] ] [ [ "Comment 2" ], [ "if false", "end" ] ]
], ],
r.parse( "/**\n * Comment 1\n*/\ndef codeblock\nend\n/**\n * Comment 2\n*/\nif false\nend" ) r.parse( "/**\n * Comment 1\n*/\ndef codeblock\nend\n/**\n * Comment 2\n*/\nif false\nend" )
) )

View File

@ -5,8 +5,8 @@ class RoccoSkippableLines < Test::Unit::TestCase
r = Rocco.new( 'filename.sh' ) { "" } r = Rocco.new( 'filename.sh' ) { "" }
assert_equal( assert_equal(
[ [
[ [ "# Comment 1" ], [ "def codeblock" ] ], [ [ "Comment 1" ], [ "def codeblock" ] ],
[ [ "# Comment 2" ], [ "end" ] ] [ [ "Comment 2" ], [ "end" ] ]
], ],
r.parse( "#!/usr/bin/env bash\n# Comment 1\ndef codeblock\n# Comment 2\nend\n" ), r.parse( "#!/usr/bin/env bash\n# Comment 1\ndef codeblock\n# Comment 2\nend\n" ),
"Shebang should be stripped when it appears as the first line." "Shebang should be stripped when it appears as the first line."
@ -16,8 +16,9 @@ class RoccoSkippableLines < Test::Unit::TestCase
r = Rocco.new( 'filename.sh' ) { "" } r = Rocco.new( 'filename.sh' ) { "" }
assert_equal( assert_equal(
[ [
[ [ "# Comment 1", "#!/usr/bin/env bash" ], [ "def codeblock" ] ], # @TODO: `#!/` shouldn't be recognized as a comment.
[ [ "# Comment 2" ], [ "end" ] ] [ [ "Comment 1", "!/usr/bin/env bash" ], [ "def codeblock" ] ],
[ [ "Comment 2" ], [ "end" ] ]
], ],
r.parse( "# Comment 1\n#!/usr/bin/env bash\ndef codeblock\n# Comment 2\nend\n" ), r.parse( "# Comment 1\n#!/usr/bin/env bash\ndef codeblock\n# Comment 2\nend\n" ),
"Shebang shouldn't be stripped anywhere other than as the first line." "Shebang shouldn't be stripped anywhere other than as the first line."
@ -27,8 +28,8 @@ class RoccoSkippableLines < Test::Unit::TestCase
r = Rocco.new( 'filename.rb' ) { "" } r = Rocco.new( 'filename.rb' ) { "" }
assert_equal( assert_equal(
[ [
[ [ "# Comment 1" ], [ "def codeblock" ] ], [ [ "Comment 1" ], [ "def codeblock" ] ],
[ [ "# Comment 2" ], [ "end" ] ] [ [ "Comment 2" ], [ "end" ] ]
], ],
r.parse( "#!/usr/bin/env bash\n# encoding: utf-8\n# Comment 1\ndef codeblock\n# Comment 2\nend\n" ), r.parse( "#!/usr/bin/env bash\n# encoding: utf-8\n# Comment 1\ndef codeblock\n# Comment 2\nend\n" ),
"Strings matching the PEP 263 encoding definition regex should be stripped when they appear at the top of a python document." "Strings matching the PEP 263 encoding definition regex should be stripped when they appear at the top of a python document."
@ -38,8 +39,8 @@ class RoccoSkippableLines < Test::Unit::TestCase
r = Rocco.new( 'filename.py' ) { "" } r = Rocco.new( 'filename.py' ) { "" }
assert_equal( assert_equal(
[ [
[ [ "# Comment 1" ], [ "def codeblock" ] ], [ [ "Comment 1" ], [ "def codeblock" ] ],
[ [ "# Comment 2" ], [ "end" ] ] [ [ "Comment 2" ], [ "end" ] ]
], ],
r.parse( "#!/usr/bin/env bash\n# encoding: utf-8\n# Comment 1\ndef codeblock\n# Comment 2\nend\n" ), r.parse( "#!/usr/bin/env bash\n# encoding: utf-8\n# Comment 1\ndef codeblock\n# Comment 2\nend\n" ),
"Strings matching the PEP 263 encoding definition regex should be stripped when they appear at the top of a python document." "Strings matching the PEP 263 encoding definition regex should be stripped when they appear at the top of a python document."
@ -49,8 +50,8 @@ class RoccoSkippableLines < Test::Unit::TestCase
r = Rocco.new( 'filename.sh' ) { "" } r = Rocco.new( 'filename.sh' ) { "" }
assert_equal( assert_equal(
[ [
[ [ "# encoding: utf-8", "# Comment 1" ], [ "def codeblock" ] ], [ [ "encoding: utf-8", "Comment 1" ], [ "def codeblock" ] ],
[ [ "# Comment 2" ], [ "end" ] ] [ [ "Comment 2" ], [ "end" ] ]
], ],
r.parse( "#!/usr/bin/env bash\n# encoding: utf-8\n# Comment 1\ndef codeblock\n# Comment 2\nend\n" ), r.parse( "#!/usr/bin/env bash\n# encoding: utf-8\n# Comment 1\ndef codeblock\n# Comment 2\nend\n" ),
"Strings matching the PEP 263 encoding definition regex should be stripped when they appear at the top of a python document." "Strings matching the PEP 263 encoding definition regex should be stripped when they appear at the top of a python document."