From 77dff765b6b226756b53ae9202a308dcfc63cf9f Mon Sep 17 00:00:00 2001
From: Mike West <mike@mikewest.org>
Date: Mon, 22 Nov 2010 08:41:54 +0100
Subject: [PATCH] Fixing tests for block comments.

---
 lib/rocco.rb                 | 19 ++++++++++---------
 test/test_basics.rb          | 12 ++++++------
 test/test_block_comments.rb  | 12 ++++++------
 test/test_skippable_lines.rb | 21 +++++++++++----------
 4 files changed, 33 insertions(+), 31 deletions(-)

diff --git a/lib/rocco.rb b/lib/rocco.rb
index 880b139..5fc31e8 100644
--- a/lib/rocco.rb
+++ b/lib/rocco.rb
@@ -247,15 +247,16 @@ class Rocco
     # 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 @@ class Rocco
         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 @@ class Rocco
             sections << [docs, code]
             docs, code = [], []
           end
-          docs << line
+          docs << line.sub( single_line_comment, '' )
         else
           code << line
         end
@@ -297,7 +298,7 @@ class Rocco
   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
diff --git a/test/test_basics.rb b/test/test_basics.rb
index 18a1777..ad315ab 100644
--- a/test/test_basics.rb
+++ b/test/test_basics.rb
@@ -27,14 +27,14 @@ class RoccoBasicTests < Test::Unit::TestCase
         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 @@ class RoccoBasicTests < Test::Unit::TestCase
                 [ "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 @@ class RoccoBasicTests < Test::Unit::TestCase
                 [ "def codeblock", "end" ]
             ],
             r.split( [
-                [ [ "# Comment 1" ], [ "def codeblock" ] ],
-                [ [ "# Comment 2" ], [ "end" ] ]
+                [ [ "Comment 1" ], [ "def codeblock" ] ],
+                [ [ "Comment 2" ], [ "end" ] ]
             ] )
         )
     end
diff --git a/test/test_block_comments.rb b/test/test_block_comments.rb
index 48e968f..14b95b9 100644
--- a/test/test_block_comments.rb
+++ b/test/test_block_comments.rb
@@ -5,13 +5,13 @@ class RoccoBlockCommentTest < Test::Unit::TestCase
         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 @@ class RoccoBlockCommentTest < Test::Unit::TestCase
         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" )
         )
diff --git a/test/test_skippable_lines.rb b/test/test_skippable_lines.rb
index 1baaf10..2672afe 100644
--- a/test/test_skippable_lines.rb
+++ b/test/test_skippable_lines.rb
@@ -5,8 +5,8 @@ class RoccoSkippableLines < Test::Unit::TestCase
     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 @@ class RoccoSkippableLines < Test::Unit::TestCase
     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 @@ class RoccoSkippableLines < Test::Unit::TestCase
     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 @@ class RoccoSkippableLines < Test::Unit::TestCase
     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 @@ class RoccoSkippableLines < Test::Unit::TestCase
     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."