From a1c7010de39650fcc2d45dadccabe79679a473f8 Mon Sep 17 00:00:00 2001 From: Nikola Jichev Date: Tue, 14 Nov 2023 16:15:00 +0200 Subject: [PATCH 1/3] Refactor tests using didOpen notification Uses a macro to hide the miscellaneous setup for the document didOpen notification. --- test/next_ls/completions_test.exs | 189 +++++++++--------------------- test/next_ls_test.exs | 52 +++----- test/support/utils.ex | 29 +++++ 3 files changed, 100 insertions(+), 170 deletions(-) diff --git a/test/next_ls/completions_test.exs b/test/next_ls/completions_test.exs index 91af40f6..22535849 100644 --- a/test/next_ls/completions_test.exs +++ b/test/next_ls/completions_test.exs @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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 - < %{"kind" => "end", "message" => "Finished indexing!"}} diff --git a/test/support/utils.ex b/test/support/utils.ex index 58b936f1..bf2a1002 100644 --- a/test/support/utils.ex +++ b/test/support/utils.ex @@ -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( + client, + file_path + ) do + did_open(client, file_path, File.read!(file_path)) + end end From 8783084bc781353940ed78eb3805cc8fc9a24bd2 Mon Sep 17 00:00:00 2001 From: Nikola Jichev Date: Tue, 14 Nov 2023 16:26:22 +0200 Subject: [PATCH 2/3] Pull the arguments on the same line --- test/support/utils.ex | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/test/support/utils.ex b/test/support/utils.ex index bf2a1002..c5bd96b2 100644 --- a/test/support/utils.ex +++ b/test/support/utils.ex @@ -141,11 +141,7 @@ defmodule NextLS.Support.Utils do end end - defmacro did_open( - client, - file_path, - text - ) do + defmacro did_open(client, file_path, text) do quote do assert :ok == notify(unquote(client), %{ @@ -163,10 +159,7 @@ defmodule NextLS.Support.Utils do end end - defmacro did_open( - client, - file_path - ) do + defmacro did_open(client, file_path) do did_open(client, file_path, File.read!(file_path)) end end From 8da3f9f7a783c2c152949bb65ef650309dc78032 Mon Sep 17 00:00:00 2001 From: Nikola Jichev Date: Tue, 14 Nov 2023 16:36:51 +0200 Subject: [PATCH 3/3] Remove did_open/2 macro for now --- test/support/utils.ex | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/support/utils.ex b/test/support/utils.ex index c5bd96b2..7733442d 100644 --- a/test/support/utils.ex +++ b/test/support/utils.ex @@ -158,8 +158,4 @@ defmodule NextLS.Support.Utils do }) end end - - defmacro did_open(client, file_path) do - did_open(client, file_path, File.read!(file_path)) - end end