File: erb_document_test.rb

package info (click to toggle)
ruby-ruby-lsp 0.26.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,676 kB
  • sloc: ruby: 35,294; javascript: 29; sh: 7; makefile: 4
file content (204 lines) | stat: -rw-r--r-- 4,800 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# typed: true
# frozen_string_literal: true

require "test_helper"

class ERBDocumentTest < Minitest::Test
  def setup
    @global_state = RubyLsp::GlobalState.new
  end

  def test_erb_file_is_properly_parsed
    source = +<<~ERB
      <ul>
        <li><%= foo %><li>
        <li><%= bar %><li>
        <li><%== baz %><li>
        <li><%- quz %><li>
      </ul>
    ERB
    document = RubyLsp::ERBDocument.new(
      source: source,
      version: 1,
      uri: URI("file:///foo.erb"),
      global_state: @global_state,
    )

    document.parse!

    refute_predicate(document, :syntax_error?)
    assert_equal(
      "    \n          foo       \n          bar       \n           baz       \n          quz       \n     \n",
      document.parse_result.source.source,
    )
  end

  def test_erb_file_parses_in_eval_context
    source = +<<~ERB
      <html>
        <head>
          <%= yield :head %>
        </head>
        <body>
          <%= yield %>
        </body>
      </html>
    ERB
    document = RubyLsp::ERBDocument.new(
      source: source,
      version: 1,
      uri: URI("file:///foo.erb"),
      global_state: @global_state,
    )

    document.parse!

    refute_predicate(document, :syntax_error?)
    assert_equal(
      "      \n        \n        yield :head   \n         \n        \n        yield   \n         \n       \n",
      document.parse_result.source.source,
    )
  end

  def test_erb_document_handles_windows_newlines
    document = RubyLsp::ERBDocument.new(
      source: "<%=\r\nbar %>",
      version: 1,
      uri: URI("file:///foo.erb"),
      global_state: @global_state,
    )
    document.parse!

    refute_predicate(document, :syntax_error?)
    assert_equal("   \r\nbar   ", document.parse_result.source.source)
  end

  def test_erb_syntax_error_does_not_cause_crash
    [
      "<%=",
      "<%",
      "<%-",
      "<%#",
      "<%= foo %>\n<%= bar",
      "<%= foo %\n<%= bar %>",
    ].each do |source|
      document = RubyLsp::ERBDocument.new(
        source: source,
        version: 1,
        uri: URI("file:///foo.erb"),
        global_state: @global_state,
      )
      document.parse!
    end
  end

  def test_failing_to_parse_indicates_syntax_error
    source = +<<~ERB
      <ul>
        <li><%= foo %><li>
        <li><%= end %><li>
      </ul>
    ERB
    document = RubyLsp::ERBDocument.new(
      source: source,
      version: 1,
      uri: URI("file:///foo.erb"),
      global_state: @global_state,
    )

    assert_predicate(document, :syntax_error?)
  end

  def test_locate
    source = <<~ERB
      <% Post.all.each do |post| %>
        <h1><%= post.title %></h1>
      <% end %>
    ERB
    document = RubyLsp::ERBDocument.new(
      source: source,
      version: 1,
      uri: URI("file:///foo/bar.erb"),
      global_state: @global_state,
    )

    # Locate the `Post` class
    node_context = document.locate_node({ line: 0, character: 3 })
    assert_instance_of(Prism::ConstantReadNode, node_context.node)
    assert_equal(
      "Post",
      node_context.node #: as Prism::ConstantReadNode
        .location.slice,
    )

    # Locate the `each` call from block
    node_context = document.locate_node({ line: 0, character: 17 })
    assert_instance_of(Prism::BlockNode, node_context.node)
    assert_equal(
      :each,
      node_context.call_node #: as !nil
        .name,
    )

    # Locate the `title` invocation
    node_context = document.locate_node({ line: 1, character: 15 })
    assert_equal(
      "title",
      node_context.node #: as Prism::CallNode
        .message,
    )
  end

  def test_cache_set_and_get
    document = RubyLsp::ERBDocument.new(
      source: +"",
      version: 1,
      uri: URI("file:///foo/bar.erb"),
      global_state: @global_state,
    )
    value = [1, 2, 3]

    assert_equal(value, document.cache_set("textDocument/semanticHighlighting", value))
    assert_equal(value, document.cache_get("textDocument/semanticHighlighting"))
  end

  def test_keeps_track_of_virtual_host_language_source
    source = +<<~ERB
      <ul>
        <li><%= foo %><li>
        <li><%= end %><li>
      </ul>
    ERB
    document = RubyLsp::ERBDocument.new(
      source: source,
      version: 1,
      uri: URI("file:///foo.erb"),
      global_state: @global_state,
    )

    assert_equal(<<~HTML, document.host_language_source)
      <ul>
        <li>          <li>
        <li>          <li>
      </ul>
    HTML
  end

  def test_erb_is_parsed_as_a_partial_script
    source = +<<~ERB
      <ul>
        <li><%= redo %><li>
      </ul>
    ERB
    document = RubyLsp::ERBDocument.new(
      source: source,
      version: 1,
      uri: URI("file:///foo.erb"),
      global_state: @global_state,
    )

    document.parse!

    refute_predicate(document, :syntax_error?)
  end
end