Sha256: 77a135218815df2fdff50d0e8fd5bf8018e53536184f2904f1b5c96ecf6266d1

Contents?: true

Size: 1.65 KB

Versions: 8

Compression:

Stored size: 1.65 KB

Contents

require 'helper'
require 'parser/ruby18'

class TestSourceCommentAssociator < Minitest::Test
  def associate(code)
    parser = Parser::Ruby18.new

    buffer = Parser::Source::Buffer.new('(comments)')
    buffer.source = code

    ast, comments = parser.parse_with_comments(buffer)
    associations  = Parser::Source::Comment.associate(ast, comments)

    [ ast, associations ]
  end

  def test_associate
    ast, associations = associate(<<-END)
# Class comment
# another class comment
class Foo
  # attr_accessor comment
  attr_accessor :foo

  # method comment
  def bar
    # expr comment
    1 + # intermediate comment
      2
    # stray comment
  end
end
    END

    klass_node         = ast
    attr_accessor_node = ast.children[2].children[0]
    method_node        = ast.children[2].children[1]
    expr_node          = method_node.children[2]
    intermediate_node  = expr_node.children[2]

    assert_equal 5, associations.size
    assert_equal ['# Class comment', '# another class comment'],
                 associations[klass_node].map(&:text)
    assert_equal ['# attr_accessor comment'],
                 associations[attr_accessor_node].map(&:text)
    assert_equal ['# method comment'],
                 associations[method_node].map(&:text)
    assert_equal ['# expr comment'],
                 associations[expr_node].map(&:text)
    assert_equal ['# intermediate comment'],
                 associations[intermediate_node].map(&:text)
  end

  def test_associate_no_body
    ast, associations = associate(<<-END)
# foo
class Foo
end
    END

    assert_equal 1, associations.size
    assert_equal ['# foo'],
                 associations[ast].map(&:text)
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
parser-2.0.0.pre3 test/test_source_comment_associator.rb
parser-2.0.0.pre2 test/test_source_comment_associator.rb
parser-2.0.0.pre1 test/test_source_comment_associator.rb
parser-2.0.0.beta10 test/test_source_comment_associator.rb
parser-2.0.0.beta9 test/test_source_comment_associator.rb
parser-2.0.0.beta8 test/test_source_comment_associator.rb
parser-2.0.0.beta7 test/test_source_comment_associator.rb
parser-2.0.0.beta6 test/test_source_comment_associator.rb