Skip to content

Commit

Permalink
test: refactor tests using didOpen notification (elixir-tools#337)
Browse files Browse the repository at this point in the history
* Refactor tests using didOpen notification

Uses a macro to hide the miscellaneous setup for the document didOpen
notification.

* Pull the arguments on the same line

* Remove did_open/2 macro for now
  • Loading branch information
NJichev committed Nov 14, 2023
1 parent dfa83c2 commit cc0bf62
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 170 deletions.
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
18 changes: 18 additions & 0 deletions test/support/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,22 @@ 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
end

0 comments on commit cc0bf62

Please sign in to comment.