Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixing tests for block comments.

  • Loading branch information...
commit 77dff765b6b226756b53ae9202a308dcfc63cf9f 1 parent f177a9d
@mikewest mikewest authored
View
19 lib/rocco.rb
@@ -247,15 +247,16 @@ def parse(data)
# This requires an `in_comment_block` boolean, and a few regular
# expressions for line tests.
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?
- 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
if not @options[:comment_chars][:multi].nil?
- require 'pp'
- pp @options[:comment_chars]
- block_comment_start = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:start])}\s*$")
- 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*$")
+ block_comment_end = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:end])}\\s*$")
+ if @options[:comment_chars][:multi][:middle]
+ block_comment_mid = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:middle])}\\s?")
+ end
end
lines.each do |line|
# If we're currently in a comment block, check whether the line matches
@@ -264,7 +265,7 @@ def parse(data)
if block_comment_end && line.match( block_comment_end )
in_comment_block = false
else
- docs << line
+ docs << line.sub( block_comment_mid, '' )
end
# 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
@@ -281,7 +282,7 @@ def parse(data)
sections << [docs, code]
docs, code = [], []
end
- docs << line
+ docs << line.sub( single_line_comment, '' )
else
code << line
end
@@ -297,7 +298,7 @@ def parse(data)
def split(sections)
docs_blocks, code_blocks = [], []
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|
tabs = line.match(/^(\t+)/)
tabs ? line.sub(/^\t+/, ' ' * tabs.captures[0].length) : line
View
12 test/test_basics.rb
@@ -27,14 +27,14 @@ def test_parsing
r = Rocco.new( 'test' ) { "" } # Generate throwaway instance so I can test `parse`
assert_equal(
[
- [ [ "# Comment 1" ], [ "def codeblock", "end" ] ]
+ [ [ "Comment 1" ], [ "def codeblock", "end" ] ]
],
r.parse( "# Comment 1\ndef codeblock\nend\n" )
)
assert_equal(
[
- [ [ "# Comment 1" ], [ "def codeblock" ] ],
- [ [ "# Comment 2" ], [ "end" ] ]
+ [ [ "Comment 1" ], [ "def codeblock" ] ],
+ [ [ "Comment 2" ], [ "end" ] ]
],
r.parse( "# Comment 1\ndef codeblock\n# Comment 2\nend\n" )
)
@@ -47,7 +47,7 @@ def test_splitting
[ "Comment 1" ],
[ "def codeblock\nend" ]
],
- r.split([ [ [ "# Comment 1" ], [ "def codeblock", "end" ] ] ])
+ r.split([ [ [ "Comment 1" ], [ "def codeblock", "end" ] ] ])
)
assert_equal(
[
@@ -55,8 +55,8 @@ def test_splitting
[ "def codeblock", "end" ]
],
r.split( [
- [ [ "# Comment 1" ], [ "def codeblock" ] ],
- [ [ "# Comment 2" ], [ "end" ] ]
+ [ [ "Comment 1" ], [ "def codeblock" ] ],
+ [ [ "Comment 2" ], [ "end" ] ]
] )
)
end
View
12 test/test_block_comments.rb
@@ -5,13 +5,13 @@ def test_basics
r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse`
assert_equal(
[
- [ [ " * Comment 1" ], [ "def codeblock", "end" ] ]
+ [ [ "Comment 1" ], [ "def codeblock", "end" ] ]
],
r.parse( "/**\n * Comment 1\n*/\ndef codeblock\nend\n" )
)
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" )
)
@@ -20,15 +20,15 @@ def test_multiple_blocks
r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse`
assert_equal(
[
- [ [ " * Comment 1" ], [ "def codeblock", "end" ] ],
- [ [ " * Comment 2" ], [] ]
+ [ [ "Comment 1" ], [ "def codeblock", "end" ] ],
+ [ [ "Comment 2" ], [] ]
],
r.parse( "/**\n * Comment 1\n*/\ndef codeblock\nend\n/**\n * Comment 2\n*/\n" )
)
assert_equal(
[
- [ [ " * Comment 1" ], [ "def codeblock", "end" ] ],
- [ [ " * Comment 2" ], [ "if false", "end" ] ]
+ [ [ "Comment 1" ], [ "def codeblock", "end" ] ],
+ [ [ "Comment 2" ], [ "if false", "end" ] ]
],
r.parse( "/**\n * Comment 1\n*/\ndef codeblock\nend\n/**\n * Comment 2\n*/\nif false\nend" )
)
View
21 test/test_skippable_lines.rb
@@ -5,8 +5,8 @@ def test_shebang_first_line
r = Rocco.new( 'filename.sh' ) { "" }
assert_equal(
[
- [ [ "# Comment 1" ], [ "def codeblock" ] ],
- [ [ "# Comment 2" ], [ "end" ] ]
+ [ [ "Comment 1" ], [ "def codeblock" ] ],
+ [ [ "Comment 2" ], [ "end" ] ]
],
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."
@@ -16,8 +16,9 @@ def test_shebang_in_content
r = Rocco.new( 'filename.sh' ) { "" }
assert_equal(
[
- [ [ "# Comment 1", "#!/usr/bin/env bash" ], [ "def codeblock" ] ],
- [ [ "# Comment 2" ], [ "end" ] ]
+ # @TODO: `#!/` shouldn't be recognized as a comment.
+ [ [ "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" ),
"Shebang shouldn't be stripped anywhere other than as the first line."
@@ -27,8 +28,8 @@ def test_encoding_in_ruby
r = Rocco.new( 'filename.rb' ) { "" }
assert_equal(
[
- [ [ "# Comment 1" ], [ "def codeblock" ] ],
- [ [ "# Comment 2" ], [ "end" ] ]
+ [ [ "Comment 1" ], [ "def codeblock" ] ],
+ [ [ "Comment 2" ], [ "end" ] ]
],
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."
@@ -38,8 +39,8 @@ def test_encoding_in_python
r = Rocco.new( 'filename.py' ) { "" }
assert_equal(
[
- [ [ "# Comment 1" ], [ "def codeblock" ] ],
- [ [ "# Comment 2" ], [ "end" ] ]
+ [ [ "Comment 1" ], [ "def codeblock" ] ],
+ [ [ "Comment 2" ], [ "end" ] ]
],
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."
@@ -49,8 +50,8 @@ def test_encoding_in_notpython
r = Rocco.new( 'filename.sh' ) { "" }
assert_equal(
[
- [ [ "# encoding: utf-8", "# Comment 1" ], [ "def codeblock" ] ],
- [ [ "# Comment 2" ], [ "end" ] ]
+ [ [ "encoding: utf-8", "Comment 1" ], [ "def codeblock" ] ],
+ [ [ "Comment 2" ], [ "end" ] ]
],
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."
Please sign in to comment.
Something went wrong with that request. Please try again.