diff --git a/lib/rocco.rb b/lib/rocco.rb index 6f1c9eb..9112d4e 100644 --- a/lib/rocco.rb +++ b/lib/rocco.rb @@ -30,6 +30,7 @@ # [so]: http://github.com/rtomayko/rocco/blob/master/lib/rocco.rb#commit #### Prerequisites +require 'rocco/code_segmenter' # We'll need a Markdown library. Try to load one if not already established. unless defined?(Markdown) @@ -87,7 +88,7 @@ def initialize(filename, sources=[], options={}) @data = if block_given? then yield else File.read(filename) end @options = { - :language => 'ruby', + :language => 'rb', :comment_chars => '#', :template_file => nil, :stylesheet => 'http://jashkenas.github.io/docco/resources/linear/docco.css' @@ -102,7 +103,7 @@ def initialize(filename, sources=[], options={}) # If we didn't detect a language, but the user provided one, use it # to look around for comment characters to override the default. - elsif @options[:language] + elsif options[:language] @options[:comment_chars] = generate_comment_chars # If neither is true, then convert the default comment character string @@ -120,7 +121,8 @@ def initialize(filename, sources=[], options={}) # `parse()` the file contents stored in `@data`. Run the result through # `split()` and that result through `highlight()` to generate the final # section list. - @sections = highlight(split(parse(@data))) + code_segments = CodeSegmenter.new(@options).segment(@data) + @sections = highlight(split(code_segments)) end # The filename as given to `Rocco.new`. @@ -212,136 +214,6 @@ def generate_comment_chars end end - # Internal Parsing and Highlighting - # --------------------------------- - - # Parse the raw file data into a list of two-tuples. Each tuple has the - # form `[docs, code]` where both elements are arrays containing the - # raw lines parsed from the input file, comment characters stripped. - def parse(data) - sections, docs, code = [], [], [] - lines = data.split("\n") - - # The first line is ignored if it is a shebang line. We also ignore the - # PEP 263 encoding information in python sourcefiles, and the similar ruby - # 1.9 syntax. - lines.shift if lines[0] =~ /^\#\!/ - lines.shift if lines[0] =~ /coding[:=]\s*[-\w.]+/ && - [ "python", "rb" ].include?(@options[:language]) - - # To detect both block comments and single-line comments, we'll set - # up a tiny state machine, and loop through each line of the file. - # This requires an `in_comment_block` boolean, and a few regular - # expressions for line tests. We'll do the same for fake heredoc parsing. - in_comment_block = false - in_heredoc = false - 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?") - end - if not @options[:comment_chars][:multi].nil? - 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_one_liner = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:start])}\\s*(.*?)\\s*#{Regexp.escape(@options[:comment_chars][:multi][:end])}\\s*$") - block_comment_start_with = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:start])}\\s*(.*?)$") - block_comment_end_with = Regexp.new("\\s*(.*?)\\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 - if not @options[:comment_chars][:heredoc].nil? - heredoc_start = Regexp.new("#{Regexp.escape(@options[:comment_chars][:heredoc])}(\\S+)$") - end - lines.each do |line| - # If we're currently in a comment block, check whether the line matches - # the _end_ of a comment block or the _end_ of a comment block with a - # comment. - if in_comment_block - if block_comment_end && line.match(block_comment_end) - in_comment_block = false - elsif block_comment_end_with && line.match(block_comment_end_with) - in_comment_block = false - docs << line.match(block_comment_end_with).captures.first. - sub(block_comment_mid || '', '') - else - docs << line.sub(block_comment_mid || '', '') - end - # If we're currently in a heredoc, we're looking for the end of the - # heredoc, and everything it contains is code. - elsif in_heredoc - if line.match(Regexp.new("^#{Regexp.escape(in_heredoc)}$")) - in_heredoc = false - end - code << line - # Otherwise, check whether the line starts a heredoc. If so, note the end - # pattern, and the line is code. 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 code, start a new section. - else - if heredoc_start && line.match(heredoc_start) - in_heredoc = $1 - code << line - elsif block_comment_one_liner && line.match(block_comment_one_liner) - if code.any? - sections << [docs, code] - docs, code = [], [] - end - docs << line.match(block_comment_one_liner).captures.first - elsif block_comment_start && line.match(block_comment_start) - in_comment_block = true - if code.any? - sections << [docs, code] - docs, code = [], [] - end - elsif block_comment_start_with && line.match(block_comment_start_with) - in_comment_block = true - if code.any? - sections << [docs, code] - docs, code = [], [] - end - docs << line.match(block_comment_start_with).captures.first - elsif single_line_comment && line.match(single_line_comment) - if code.any? - sections << [docs, code] - docs, code = [], [] - end - docs << line.sub(single_line_comment || '', '') - else - code << line - end - end - end - sections << [docs, code] if docs.any? || code.any? - normalize_leading_spaces(sections) - end - - # Normalizes documentation whitespace by checking for leading whitespace, - # removing it, and then removing the same amount of whitespace from each - # succeeding line. That is: - # - # def func(): - # """ - # Comment 1 - # Comment 2 - # """ - # print "omg!" - # - # should yield a comment block of `Comment 1\nComment 2` and code of - # `def func():\n print "omg!"` - def normalize_leading_spaces(sections) - sections.map do |section| - if section.any? && section[0].any? - leading_space = section[0][0].match("^\s+") - if leading_space - section[0] = - section[0].map{ |line| line.sub(/^#{leading_space.to_s}/, '') } - end - end - section - end - end - # Take the list of paired *sections* two-tuples and split into two # separate lists: one holding the comments with leaders removed and # one with the code blocks. diff --git a/lib/rocco/code_segmenter.rb b/lib/rocco/code_segmenter.rb new file mode 100644 index 0000000..7ac52b2 --- /dev/null +++ b/lib/rocco/code_segmenter.rb @@ -0,0 +1,157 @@ +require 'rocco/comment_styles' + +class Rocco + class CodeSegmenter + include CommentStyles + + DEFAULT_OPTIONS = { + :language => 'rb', + :comment_chars => '#' + } + + def initialize(options = {}) + @options = DEFAULT_OPTIONS.merge options + @options[:comment_chars] = generate_comment_chars if @options[:comment_chars].is_a? String + end + # Internal Parsing and Highlighting + # --------------------------------- + + # Parse the raw file source_code into a list of two-tuples. Each tuple has the + # form `[docs, code]` where both elements are arrays containing the + # raw lines parsed from the input file, comment characters stripped. + def segment(source_code) + sections, docs, code = [], [], [] + lines = source_code.split("\n") + + # The first line is ignored if it is a shebang line. We also ignore the + # PEP 263 encoding information in python sourcefiles, and the similar ruby + # 1.9 syntax. + lines.shift if lines[0] =~ /^\#\!/ + lines.shift if lines[0] =~ /coding[:=]\s*[-\w.]+/ && + [ "python", "rb" ].include?(@options[:language]) + + # To detect both block comments and single-line comments, we'll set + # up a tiny state machine, and loop through each line of the file. + # This requires an `in_comment_block` boolean, and a few regular + # expressions for line tests. We'll do the same for fake heredoc parsing. + in_comment_block = false + in_heredoc = false + 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?") + end + if not @options[:comment_chars][:multi].nil? + 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_one_liner = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:start])}\\s*(.*?)\\s*#{Regexp.escape(@options[:comment_chars][:multi][:end])}\\s*$") + block_comment_start_with = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:start])}\\s*(.*?)$") + block_comment_end_with = Regexp.new("\\s*(.*?)\\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 + if not @options[:comment_chars][:heredoc].nil? + heredoc_start = Regexp.new("#{Regexp.escape(@options[:comment_chars][:heredoc])}(\\S+)$") + end + lines.each do |line| + # If we're currently in a comment block, check whether the line matches + # the _end_ of a comment block or the _end_ of a comment block with a + # comment. + if in_comment_block + if block_comment_end && line.match(block_comment_end) + in_comment_block = false + elsif block_comment_end_with && line.match(block_comment_end_with) + in_comment_block = false + docs << line.match(block_comment_end_with).captures.first. + sub(block_comment_mid || '', '') + else + docs << line.sub(block_comment_mid || '', '') + end + # If we're currently in a heredoc, we're looking for the end of the + # heredoc, and everything it contains is code. + elsif in_heredoc + if line.match(Regexp.new("^#{Regexp.escape(in_heredoc)}$")) + in_heredoc = false + end + code << line + # Otherwise, check whether the line starts a heredoc. If so, note the end + # pattern, and the line is code. 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 code, start a new section. + else + if heredoc_start && line.match(heredoc_start) + in_heredoc = $1 + code << line + elsif block_comment_one_liner && line.match(block_comment_one_liner) + if code.any? + sections << [docs, code] + docs, code = [], [] + end + docs << line.match(block_comment_one_liner).captures.first + elsif block_comment_start && line.match(block_comment_start) + in_comment_block = true + if code.any? + sections << [docs, code] + docs, code = [], [] + end + elsif block_comment_start_with && line.match(block_comment_start_with) + in_comment_block = true + if code.any? + sections << [docs, code] + docs, code = [], [] + end + docs << line.match(block_comment_start_with).captures.first + elsif single_line_comment && line.match(single_line_comment) + if code.any? + sections << [docs, code] + docs, code = [], [] + end + docs << line.sub(single_line_comment || '', '') + else + code << line + end + end + end + sections << [docs, code] if docs.any? || code.any? + normalize_leading_spaces(sections) + end + + # Normalizes documentation whitespace by checking for leading whitespace, + # removing it, and then removing the same amount of whitespace from each + # succeeding line. That is: + # + # def func(): + # """ + # Comment 1 + # Comment 2 + # """ + # print "omg!" + # + # should yield a comment block of `Comment 1\nComment 2` and code of + # `def func():\n print "omg!"` + def normalize_leading_spaces(sections) + sections.map do |section| + if section.any? && section[0].any? + leading_space = section[0][0].match("^\s+") + if leading_space + section[0] = + section[0].map{ |line| line.sub(/^#{leading_space.to_s}/, '') } + end + end + section + end + end + + private + + def generate_comment_chars + @_commentchar ||= + if COMMENT_STYLES[@options[:language]] + COMMENT_STYLES[@options[:language]] + else + { :single => @options[:comment_chars], :multi => nil, :heredoc => nil } + end + end + end +end \ No newline at end of file diff --git a/test/test_basics.rb b/test/test_basics.rb index 40ee725..7b1cd6a 100644 --- a/test/test_basics.rb +++ b/test/test_basics.rb @@ -23,20 +23,20 @@ def test_sections assert_equal "def codeblock\nend", r.sections[ 0 ][ 1 ] end - def test_parsing - r = Rocco.new( 'test' ) { "" } # Generate throwaway instance so I can test `parse` + def test_segmenting + cs = Rocco::CodeSegmenter.new # Generate throwaway instance so I can test `segment` assert_equal( [ [ [ "Comment 1" ], [ "def codeblock", "end" ] ] ], - r.parse( "# Comment 1\ndef codeblock\nend\n" ) + cs.segment( "# Comment 1\ndef codeblock\nend\n" ) ) assert_equal( [ [ [ "Comment 1" ], [ "def codeblock" ] ], [ [ "Comment 2" ], [ "end" ] ] ], - r.parse( "# Comment 1\ndef codeblock\n# Comment 2\nend\n" ) + cs.segment( "# Comment 1\ndef codeblock\n# Comment 2\nend\n" ) ) end diff --git a/test/test_block_comment_styles.rb b/test/test_block_comment_styles.rb index 8fb55d5..2663618 100644 --- a/test/test_block_comment_styles.rb +++ b/test/test_block_comment_styles.rb @@ -2,62 +2,62 @@ class RoccoBlockCommentTest < Test::Unit::TestCase def test_one_liner - r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "c" ) assert_equal( [ [ [ "Comment 1" ], [ "def codeblock", "end" ] ] ], - r.parse( "/** Comment 1 */\ndef codeblock\nend\n" ) + cs.segment( "/** Comment 1 */\ndef codeblock\nend\n" ) ) end def test_block_start_with_comment - r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "c" ) assert_equal( [ [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ] ], - r.parse( "/** Comment 1a\n * Comment 1b\n */\ndef codeblock\nend\n" ) + cs.segment( "/** Comment 1a\n * Comment 1b\n */\ndef codeblock\nend\n" ) ) end def test_block_end_with_comment - r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "c" ) assert_equal( [ [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ] ], - r.parse( "/**\n * Comment 1a\n Comment 1b */\ndef codeblock\nend\n" ) + cs.segment( "/**\n * Comment 1a\n Comment 1b */\ndef codeblock\nend\n" ) ) end def test_block_end_with_comment_and_middle - r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "c" ) assert_equal( [ [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ] ], - r.parse( "/**\n * Comment 1a\n * Comment 1b */\ndef codeblock\nend\n" ) + cs.segment( "/**\n * Comment 1a\n * Comment 1b */\ndef codeblock\nend\n" ) ) end def test_block_start_with_comment_and_end_with_comment - r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "c" ) assert_equal( [ [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ] ], - r.parse( "/** Comment 1a\n Comment 1b */\ndef codeblock\nend\n" ) + cs.segment( "/** Comment 1a\n Comment 1b */\ndef codeblock\nend\n" ) ) end def test_block_start_with_comment_and_end_with_comment_and_middle - r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "c" ) assert_equal( [ [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ] ], - r.parse( "/** Comment 1a\n * Comment 1b */\ndef codeblock\nend\n" ) + cs.segment( "/** Comment 1a\n * Comment 1b */\ndef codeblock\nend\n" ) ) end diff --git a/test/test_block_comments.rb b/test/test_block_comments.rb index 9cfb1e9..4b568e6 100644 --- a/test/test_block_comments.rb +++ b/test/test_block_comments.rb @@ -2,70 +2,70 @@ class RoccoBlockCommentTest < Test::Unit::TestCase def test_basics - r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "c" ) assert_equal( [ [ [ "Comment 1" ], [ "def codeblock", "end" ] ] ], - r.parse( "/**\n * Comment 1\n */\ndef codeblock\nend\n" ) + cs.segment( "/**\n * Comment 1\n */\ndef codeblock\nend\n" ) ) assert_equal( [ [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ] ], - r.parse( "/**\n * Comment 1a\n * Comment 1b\n */\ndef codeblock\nend\n" ) + cs.segment( "/**\n * Comment 1a\n * Comment 1b\n */\ndef codeblock\nend\n" ) ) end def test_multiple_blocks - r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "c" ) assert_equal( [ [ [ "Comment 1" ], [ "def codeblock", "end" ] ], [ [ "Comment 2" ], [] ] ], - r.parse( "/**\n * Comment 1\n */\ndef codeblock\nend\n/**\n * Comment 2\n */\n" ) + cs.segment( "/**\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" ] ] ], - r.parse( "/**\n * Comment 1\n */\ndef codeblock\nend\n/**\n * Comment 2\n */\nif false\nend" ) + cs.segment( "/**\n * Comment 1\n */\ndef codeblock\nend\n/**\n * Comment 2\n */\nif false\nend" ) ) end def test_block_without_middle_character - r = Rocco.new( 'test', '', { :language => "python" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "python" ) assert_equal( [ [ [ "Comment 1" ], [ "def codeblock", "end" ] ], [ [ "Comment 2" ], [] ] ], - r.parse( "\"\"\"\n Comment 1\n\"\"\"\ndef codeblock\nend\n\"\"\"\n Comment 2\n\"\"\"\n" ) + cs.segment( "\"\"\"\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" ] ] ], - r.parse( "\"\"\"\n Comment 1\n\"\"\"\ndef codeblock\nend\n\"\"\"\n Comment 2\n\"\"\"\nif false\nend" ) + cs.segment( "\"\"\"\n Comment 1\n\"\"\"\ndef codeblock\nend\n\"\"\"\n Comment 2\n\"\"\"\nif false\nend" ) ) end def test_language_without_single_line_comments_parse - r = Rocco.new( 'test', '', { :language => "css" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "css" ) assert_equal( [ [ [ "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" ) + cs.segment( "/**\n * Comment 1\n */\ndef codeblock\nend\n/**\n * Comment 2\n */\nif false\nend" ) ) end def test_language_without_single_line_comments_split - r = Rocco.new( 'test', '', { :language => "css" } ) { "" } # Generate throwaway instance so I can test `parse` + r = Rocco.new( 'test', '', { :language => "css" } ) { "" } # Generate throwaway instance so I can test `split` assert_equal( [ [ "Comment 1", "Comment 2" ], @@ -79,9 +79,10 @@ def test_language_without_single_line_comments_split end def test_language_without_single_line_comments_highlight - r = Rocco.new( 'test', '', { :language => "css" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "css" ) + r = Rocco.new( 'test', '', { :language => "css" } ) { "" } # Generate throwaway instance so I can test `highlight` - highlighted = r.highlight( r.split( r.parse( "/**\n * This is a comment!\n */\n.rule { goes: here; }\n/**\n * Comment 2\n */\n.rule2 { goes: here; }" ) ) ) + highlighted = r.highlight( r.split( cs.segment( "/**\n * This is a comment!\n */\n.rule { goes: here; }\n/**\n * Comment 2\n */\n.rule2 { goes: here; }" ) ) ) assert_equal( "

This is a comment!

", highlighted[0][0] diff --git a/test/test_comment_normalization.rb b/test/test_comment_normalization.rb index 50f0b14..7efe875 100644 --- a/test/test_comment_normalization.rb +++ b/test/test_comment_normalization.rb @@ -2,24 +2,24 @@ class RoccoCommentNormalization < Test::Unit::TestCase def test_normal_comments - r = Rocco.new( 'test', '', { :language => "python" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "python" ) assert_equal( [ [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ], [ [ "Comment 2a", " Comment 2b" ], [] ] ], - r.parse( "\"\"\"\n Comment 1a\n Comment 1b\n\"\"\"\ndef codeblock\nend\n\"\"\"\n Comment 2a\n Comment 2b\n\"\"\"\n" ) + cs.segment( "\"\"\"\n Comment 1a\n Comment 1b\n\"\"\"\ndef codeblock\nend\n\"\"\"\n Comment 2a\n Comment 2b\n\"\"\"\n" ) ) end def test_single_line_comments - r = Rocco.new( 'test', '', { :language => "python" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "python" ) assert_equal( [ [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ], [ [ "Comment 2a", " Comment 2b" ], [] ] ], - r.parse( "# Comment 1a\n# Comment 1b\ndef codeblock\nend\n# Comment 2a\n# Comment 2b\n" ) + cs.segment( "# Comment 1a\n# Comment 1b\ndef codeblock\nend\n# Comment 2a\n# Comment 2b\n" ) ) end end diff --git a/test/test_docblock_annotations.rb b/test/test_docblock_annotations.rb index 41af56d..0e7a3b6 100644 --- a/test/test_docblock_annotations.rb +++ b/test/test_docblock_annotations.rb @@ -11,8 +11,9 @@ def test_basics ) end def test_highlighted_in_blocks + cs = Rocco::CodeSegmenter.new( :language => "c" ) r = Rocco.new( 'test', '', { :language => "c", :docblocks => true } ) { "" } # Generate throwaway instance so I can test `parse` - highlighted = r.highlight( r.split( r.parse( "/**\n * Comment\n * @param type name\n */\ndef codeblock\nend\n" ) ) ) + highlighted = r.highlight( r.split( cs.segment( "/**\n * Comment\n * @param type name\n */\ndef codeblock\nend\n" ) ) ) assert_equal( "

Comment

\n\n

param type name

\n", diff --git a/test/test_heredoc.rb b/test/test_heredoc.rb index bc38096..31cf79c 100644 --- a/test/test_heredoc.rb +++ b/test/test_heredoc.rb @@ -2,12 +2,12 @@ class RoccoHeredocTest < Test::Unit::TestCase def test_basics - r = Rocco.new( 'test', '', { :language => "rb" } ) { "" } # Generate throwaway instance so I can test `parse` + cs = Rocco::CodeSegmenter.new( :language => "rb" ) assert_equal( [ [ [ "Comment 1" ], [ "heredoc <<-EOH", "#comment", "code", "EOH" ] ] ], - r.parse( "# Comment 1\nheredoc <<-EOH\n#comment\ncode\nEOH" ) + cs.segment( "# Comment 1\nheredoc <<-EOH\n#comment\ncode\nEOH" ) ) end end diff --git a/test/test_language_detection.rb b/test/test_language_detection.rb index 5e2c1eb..ea12ead 100644 --- a/test/test_language_detection.rb +++ b/test/test_language_detection.rb @@ -13,7 +13,7 @@ def test_fallback_default r = Rocco.new( 'filename.an_extension_with_no_meaning_whatsoever' ) { "" } if r.pygmentize? assert_equal "text", r.detect_language(), "`detect_language()` should return `text` when nothing else is detected" - assert_equal "ruby", r.options[:language], "`@options[:language]` should be set to `ruby` when nothing else is detected" + assert_equal "rb", r.options[:language], "`@options[:language]` should be set to `rb` when nothing else is detected" end end diff --git a/test/test_skippable_lines.rb b/test/test_skippable_lines.rb index 88d8fe3..9b7e279 100644 --- a/test/test_skippable_lines.rb +++ b/test/test_skippable_lines.rb @@ -2,62 +2,67 @@ class RoccoSkippableLines < Test::Unit::TestCase def test_shebang_first_line - r = Rocco.new( 'filename.sh' ) { "" } + # CodeSegmenter doesn't currently handle the filename + cs = Rocco::CodeSegmenter.new # ( 'filename.sh' ) assert_equal( [ [ [ "Comment 1" ], [ "def codeblock" ] ], [ [ "Comment 2" ], [ "end" ] ] ], - r.parse( "#!/usr/bin/env bash\n# Comment 1\ndef codeblock\n# Comment 2\nend\n" ), + cs.segment( "#!/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." ) end def test_shebang_in_content - r = Rocco.new( 'filename.sh' ) { "" } + # CodeSegmenter doesn't currently handle the filename + cs = Rocco::CodeSegmenter.new # ( 'filename.sh' ) assert_equal( [ # @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" ), + cs.segment( "# 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." ) end def test_encoding_in_ruby - r = Rocco.new( 'filename.rb' ) { "" } + # CodeSegmenter doesn't currently handle the filename + cs = Rocco::CodeSegmenter.new # ( 'filename.rb' ) assert_equal( [ [ [ "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" ), + cs.segment( "#!/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." ) end def test_encoding_in_python - r = Rocco.new( 'filename.py' ) { "" } + # CodeSegmenter doesn't currently handle the filename + cs = Rocco::CodeSegmenter.new( :language => 'python' ) # ( 'filename.py' ) assert_equal( [ [ [ "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" ), + cs.segment( "#!/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." ) end def test_encoding_in_notpython - r = Rocco.new( 'filename.sh' ) { "" } + # CodeSegmenter doesn't currently handle the filename + cs = Rocco::CodeSegmenter.new( :language => 'bash' ) # ( 'filename.sh' ) assert_equal( [ [ [ "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" ), + cs.segment( "#!/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." ) end