-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathhover_test.rb
208 lines (152 loc) · 5.98 KB
/
hover_test.rb
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
205
206
207
208
# typed: true
# frozen_string_literal: true
require "test_helper"
module RubyLsp
module Rails
class HoverTest < ActiveSupport::TestCase
setup do
body = File.read("#{__dir__}/../../test/fixtures/search_index.js")
stub_request(:get, %r{https://api\.rubyonrails\.org/v.*/js/search_index\.js})
.with(
headers: {
"Host" => "api.rubyonrails.org",
"User-Agent" => %r{^ruby-lsp-rails\/.*$},
},
)
.to_return(status: 200, body: body, headers: {})
# Build the Rails documents index ahead of time
capture_io do
Support::RailsDocumentClient.send(:search_index)
end
end
test "hook returns model column information" do
response = hover_on_source(<<~RUBY, { line: 3, character: 0 })
class User < ApplicationRecord
end
User
RUBY
assert_equal(<<~CONTENT.chomp, response.contents.value)
```ruby
User
```
**Definitions**: [fake.rb](file:///fake.rb#L1,1-2,4)
[Schema](#{URI::Generic.from_path(path: dummy_root + "/db/schema.rb")})
**id**: integer (PK)
**first_name**: string
**last_name**: string
**age**: integer
**created_at**: datetime
**updated_at**: datetime
CONTENT
end
test "return column information for namespaced models" do
response = hover_on_source(<<~RUBY, { line: 4, character: 6 })
module Blog
class Post < ApplicationRecord
end
end
Blog::Post
RUBY
assert_equal(<<~CONTENT.chomp, response.contents.value)
[Schema](#{URI::Generic.from_path(path: dummy_root + "/db/schema.rb")})
**id**: integer (PK)
**title**: string
**body**: text
**created_at**: datetime
**updated_at**: datetime
CONTENT
end
test "returns column information for models with composite primary keys" do
response = hover_on_source(<<~RUBY, { line: 3, character: 0 })
class CompositePrimaryKey < ApplicationRecord
end
CompositePrimaryKey
RUBY
assert_equal(<<~CONTENT.chomp, response.contents.value)
```ruby
CompositePrimaryKey
```
**Definitions**: [fake.rb](file:///fake.rb#L1,1-2,4)
[Schema](#{URI::Generic.from_path(path: dummy_root + "/db/schema.rb")})
**order_id**: integer (PK)
**product_id**: integer (PK)
**note**: text
**created_at**: datetime
**updated_at**: datetime
CONTENT
end
test "handles `db/structure.sql` instead of `db/schema.rb`" do
expected_response = {
schema_file: "#{dummy_root}/db/structure.sql",
columns: [],
primary_keys: [],
}
RunnerClient.any_instance.stubs(model: expected_response)
response = hover_on_source(<<~RUBY, { line: 3, character: 0 })
class User < ApplicationRecord
end
User
RUBY
assert_includes(
response.contents.value,
"[Schema](#{URI::Generic.from_path(path: dummy_root + "/db/structure.sql")})",
)
end
test "handles neither `db/structure.sql` nor `db/schema.rb` being present" do
expected_response = {
schema_file: nil,
columns: [],
primary_keys: [],
}
RunnerClient.any_instance.stubs(model: expected_response)
response = hover_on_source(<<~RUBY, { line: 3, character: 0 })
class User < ApplicationRecord
end
User
RUBY
refute_match(/Schema/, response.contents.value)
end
test "shows documentation for routes DSLs" do
value = hover_on_source("root 'projects#index'", { line: 0, character: 0 }).contents.value
assert_match(/\[Rails Document: `ActionDispatch::Routing::Mapper::Resources#root`\]/, value)
assert_match(%r{\(https://api\.rubyonrails\.org/.*\.html#method-i-root\)}, value)
end
test "shows documentation for controller DSLs" do
value = hover_on_source("before_action :foo", { line: 0, character: 0 }).contents.value
assert_match(/\[Rails Document: `AbstractController::Callbacks::ClassMethods#before_action`\]/, value)
assert_match(%r{\(https://api\.rubyonrails\.org/.*\.html#method-i-before_action\)}, value)
end
test "shows documentation for job DSLs" do
value = hover_on_source("queue_as :default", { line: 0, character: 0 }).contents.value
assert_match(/\[Rails Document: `ActiveJob::QueueName::ClassMethods#queue_as`\]/, value)
assert_match(%r{\(https://api\.rubyonrails\.org/.*\.html#method-i-queue_as\)}, value)
end
test "shows documentation for model DSLs" do
value = hover_on_source("validate :foo", { line: 0, character: 0 }).contents.value
assert_match(/\[Rails Document: `ActiveModel::EachValidator#validate`\]/, value)
assert_match(%r{\(https://api\.rubyonrails\.org/.*\.html#method-i-validate\)}, value)
end
test "shows documentation for Rails constants" do
value = hover_on_source(<<~RUBY, { line: 2, character: 14 }).contents.value
class ActiveRecord::Base
end
ActiveRecord::Base
RUBY
assert_match(/\[Rails Document: `ActiveRecord::Base`\]/, value)
assert_match(%r{\(https://api\.rubyonrails\.org/.*Base\.html\)}, value)
end
private
def hover_on_source(source, position)
with_server(source) do |server, uri|
sleep(0.1) while RubyLsp::Addon.addons.first.instance_variable_get(:@client).is_a?(NullClient)
server.process_message(
id: 1,
method: "textDocument/hover",
params: { textDocument: { uri: uri }, position: position },
)
server.pop_response.response
end
end
end
end
end