-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug fixes + finish writing unit tests
- Loading branch information
1 parent
b5a1117
commit 12955db
Showing
2 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
module RubyLsp | ||
module Rails | ||
class CompletionTest < ActiveSupport::TestCase | ||
test "recognizes Active Record .where call on an Active Record model when cursor is on (" do | ||
response = generate_completions_for_source(<<~RUBY, { line: 2, character: 10 }) | ||
# typed: false | ||
User.where( | ||
RUBY | ||
|
||
columns = [ | ||
"id", | ||
"first_name", | ||
"last_name", | ||
"age", | ||
"created_at", | ||
"updated_at", | ||
"country_id", | ||
"active", | ||
] | ||
assert_equal(columns.size, response.size) | ||
|
||
columns.each_with_index do |column, i| | ||
assert_equal(column, response[i].label) | ||
assert_equal(column, response[i].filter_text) | ||
end | ||
end | ||
|
||
test "Does not suggest column if it already exists within .where as an arg and parantheses are not closed" do | ||
response = generate_completions_for_source(<<~RUBY, { line: 2, character: 28 }) | ||
# typed: false | ||
User.where(id:, first_name:, | ||
RUBY | ||
|
||
columns = [ "last_name", "age", "created_at", "updated_at", "country_id", "active" ] | ||
assert_equal(columns.size, response.size) | ||
|
||
columns.each_with_index do |column, i| | ||
assert_equal(column, response[i].label) | ||
assert_equal(column, response[i].filter_text) | ||
end | ||
end | ||
|
||
test "Does not suggest column if it already exists within .where as an arg and parantheses are closed" do | ||
response = generate_completions_for_source(<<~RUBY, { line: 2, character: 28 }) | ||
# typed: false | ||
User.where(id:, first_name:, ) | ||
RUBY | ||
|
||
columns = [ "last_name", "age", "created_at", "updated_at", "country_id", "active" ] | ||
assert_equal(columns.size, response.size) | ||
|
||
columns.each_with_index do |column, i| | ||
assert_equal(column, response[i].label) | ||
assert_equal(column, response[i].filter_text) | ||
end | ||
end | ||
|
||
test "Does not provide suggestions when typing argument values" do | ||
response = generate_completions_for_source(<<~RUBY, { line: 1, character: 14}) | ||
# typed: false | ||
User.where(id: | ||
RUBY | ||
|
||
assert_equal(0, response.size) | ||
end | ||
|
||
|
||
private | ||
|
||
def generate_completions_for_source(source, position) | ||
with_server(source) do |server, uri| | ||
sleep(0.1) while RubyLsp::Addon.addons.first.instance_variable_get(:@rails_runner_client).is_a?(NullClient) | ||
|
||
server.process_message( | ||
id: 1, | ||
method: "textDocument/completion", | ||
params: { textDocument: { uri: uri }, position: position }, | ||
) | ||
|
||
result = pop_result(server) | ||
result.response | ||
end | ||
end | ||
end | ||
end | ||
end |