Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor tests using didOpen notification #337

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 56 additions & 133 deletions test/next_ls/completions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,14 @@ defmodule NextLS.CompletionsTest do
test "global modules", %{client: client, foo: foo} do
uri = uri(foo)

notify client, %{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: %{
textDocument: %{
uri: uri,
languageId: "elixir",
version: 1,
text: """
defmodule Foo do
def run() do
En
:ok
end
end
"""
}
}
}
did_open(client, foo, """
defmodule Foo do
def run() do
En
:ok
end
end
""")

request client, %{
method: "textDocument/completion",
Expand All @@ -91,25 +80,14 @@ defmodule NextLS.CompletionsTest do
test "global module remote functions", %{client: client, foo: foo} do
uri = uri(foo)

notify client, %{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: %{
textDocument: %{
uri: uri,
languageId: "elixir",
version: 1,
text: """
defmodule Foo do
def run() do
Enum.fl
:ok
end
end
"""
}
}
}
did_open(client, foo, """
defmodule Foo do
def run() do
Enum.fl
:ok
end
end
""")

request client, %{
method: "textDocument/completion",
Expand Down Expand Up @@ -141,25 +119,14 @@ defmodule NextLS.CompletionsTest do
test "global structs", %{client: client, foo: foo} do
uri = uri(foo)

notify client, %{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: %{
textDocument: %{
uri: uri,
languageId: "elixir",
version: 1,
text: """
defmodule Foo do
def run() do
%U
:ok
end
end
"""
}
}
}
did_open(client, foo, """
defmodule Foo do
def run() do
%U
:ok
end
end
""")

request client, %{
method: "textDocument/completion",
Expand All @@ -182,25 +149,14 @@ defmodule NextLS.CompletionsTest do
test "structs fields", %{client: client, foo: foo} do
uri = uri(foo)

notify client, %{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: %{
textDocument: %{
uri: uri,
languageId: "elixir",
version: 1,
text: """
defmodule Foo do
def run() do
IO.inspect([%Bar{])
:ok
end
end
"""
}
}
}
did_open(client, foo, """
defmodule Foo do
def run() do
IO.inspect([%Bar{])
:ok
end
end
""")

request client, %{
method: "textDocument/completion",
Expand All @@ -227,25 +183,14 @@ defmodule NextLS.CompletionsTest do
test "special forms", %{client: client, foo: foo} do
uri = uri(foo)

notify client, %{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: %{
textDocument: %{
uri: uri,
languageId: "elixir",
version: 1,
text: """
defmodule Foo do
def run() do
qu
:ok
end
end
"""
}
}
}
did_open(client, foo, """
defmodule Foo do
def run() do
qu
:ok
end
end
""")

request client, %{
method: "textDocument/completion",
Expand All @@ -268,25 +213,14 @@ defmodule NextLS.CompletionsTest do
test "bitstring modifiers", %{client: client, foo: foo} do
uri = uri(foo)

notify client, %{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: %{
textDocument: %{
uri: uri,
languageId: "elixir",
version: 1,
text: """
defmodule Foo do
def run() do
<<one::
:ok
end
end
"""
}
}
}
did_open(client, foo, """
defmodule Foo do
def run() do
<<one::
:ok
end
end
""")

request client, %{
method: "textDocument/completion",
Expand Down Expand Up @@ -324,25 +258,14 @@ defmodule NextLS.CompletionsTest do
test "file system paths in strings", %{client: client, foo: foo} do
uri = uri(foo)

notify client, %{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: %{
textDocument: %{
uri: uri,
languageId: "elixir",
version: 1,
text: """
defmodule Foo do
def run() do
"./lib/
:ok
end
end
"""
}
}
}
did_open(client, foo, """
defmodule Foo do
def run() do
"./lib/
:ok
end
end
""")

request client, %{
method: "textDocument/completion",
Expand Down
52 changes: 15 additions & 37 deletions test/next_ls_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,15 @@ defmodule NextLSTest do
test "formats", %{client: client, cwd: cwd} = context do
assert :ok == notify(client, %{method: "initialized", jsonrpc: "2.0", params: %{}})

notify client, %{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: %{
textDocument: %{
uri: "file://#{cwd}/my_proj/lib/foo/bar.ex",
languageId: "elixir",
version: 1,
text: """
defmodule Foo.Bar do
def run() do


:ok
end
end
"""
}
}
}
did_open(client, Path.join(cwd, "my_proj/lib/foo/bar.ex"), """
defmodule Foo.Bar do
def run() do


:ok
end
end
""")

request client, %{
method: "textDocument/formatting",
Expand Down Expand Up @@ -186,25 +175,14 @@ defmodule NextLSTest do
test "formatting gracefully handles files with syntax errors", %{client: client, cwd: cwd} = context do
assert :ok == notify(client, %{method: "initialized", jsonrpc: "2.0", params: %{}})

notify client, %{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: %{
textDocument: %{
uri: "file://#{cwd}/my_proj/lib/foo/bar.ex",
languageId: "elixir",
version: 1,
text: """
defmodule Foo.Bar do
def run() do
did_open(client, Path.join(cwd, "my_proj/lib/foo/bar.ex"), """
defmodule Foo.Bar do
def run() do


:ok
end
"""
}
}
}
:ok
end
""")

assert_is_ready(context, "my_proj")
assert_notification "$/progress", %{"value" => %{"kind" => "end", "message" => "Finished indexing!"}}
Expand Down
29 changes: 29 additions & 0 deletions test/support/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,33 @@ defmodule NextLS.Support.Utils do
assert result == unquote(pattern)
end
end

defmacro did_open(
client,
file_path,
text
) do
quote do
assert :ok ==
notify(unquote(client), %{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: %{
textDocument: %{
uri: uri(unquote(file_path)),
text: unquote(text),
languageId: "elixir",
version: 1
}
}
})
end
end

defmacro did_open(
NJichev marked this conversation as resolved.
Show resolved Hide resolved
client,
file_path
) do
did_open(client, file_path, File.read!(file_path))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me if reading the file at compile time will work as the files are written to disk when the test is running

Copy link
Collaborator Author

@NJichev NJichev Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you're correct but I think I can fix it in the other PR and remove it for now, I will probably just inline the whole other macro and call File.read!(unquote(file_path)). I hadn't tested it here since the existing tests that use it just use text instead of real files.

end
end
Loading