aboutsummaryrefslogtreecommitdiffstats
path: root/test/yarp/comments_test.rb
blob: ac91eab4ac518889642ab4abbc10cc2f2d7e0c85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true

require_relative "test_helper"

module YARP
  class CommentsTest < TestCase
    def test_comment_inline
      source = "# comment"

      assert_comment source, :inline, 0..9
      assert_equal [0], Debug.newlines(source)
    end

    def test_comment_inline_def
      source = <<~RUBY
      def foo
        # a comment
      end
      RUBY

      assert_comment source, :inline, 10..22
    end

    def test_comment___END__
      source = <<~RUBY
        __END__
        comment
      RUBY

      assert_comment source, :__END__, 0..16
    end

    def test_comment___END__crlf
      source = "__END__\r\ncomment\r\n"

      assert_comment source, :__END__, 0..18
    end

    def test_comment_embedded_document
      source = <<~RUBY
        =begin
        comment
        =end
      RUBY

      assert_comment source, :embdoc, 0..20
    end

    def test_comment_embedded_document_with_content_on_same_line
      source = <<~RUBY
        =begin other stuff
        =end
      RUBY

      assert_comment source, :embdoc, 0..24
    end

    private

    def assert_comment(source, type, location)
      result = YARP.parse(source)
      assert result.errors.empty?, result.errors.map(&:message).join("\n")
      assert_equal result.comments.first.type, type
      assert_equal result.comments.first.location.start_offset, location.begin
      assert_equal result.comments.first.location.end_offset, location.end
    end
  end
end