Skip to content

Commit

Permalink
Add provider unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Étienne Lévesque committed Nov 23, 2020
1 parent 95f205a commit 26fe78e
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,22 @@ defmodule ElixirLS.LanguageServer.Providers.CodeLens.Test do
end
end

def get_test_lenses(test_blocks, file_path) do
test_blocks
|> Enum.map(fn block ->
CodeLens.build_code_lens(block.line, "Run test", @run_test_command, %{
defp get_test_lenses(test_blocks, file_path) do
args = fn block ->
%{
"filePath" => file_path,
"describe" =>
if block.describe != nil do
block.describe.name
else
nil
end,
"testName" => block.name
})
}
|> Map.merge(if block.describe != nil, do: %{"describe" => block.describe.name}, else: %{})
end

test_blocks
|> Enum.map(fn block ->
CodeLens.build_code_lens(block.line, "Run test", @run_test_command, args.(block))
end)
end

def get_describe_lenses(describe_blocks, file_path) do
defp get_describe_lenses(describe_blocks, file_path) do
describe_blocks
|> Enum.map(fn block ->
CodeLens.build_code_lens(block.line, "Run tests", @run_test_command, %{
Expand Down
185 changes: 185 additions & 0 deletions apps/language_server/test/providers/code_lens/test_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
defmodule ElixirLS.LanguageServer.Providers.CodeLens.TestTest do
use ExUnit.Case

alias ElixirLS.LanguageServer.Providers.CodeLens

setup context do
ElixirLS.LanguageServer.Build.load_all_modules()

unless context[:skip_server] do
server = ElixirLS.LanguageServer.Test.ServerTestHelpers.start_server()

{:ok, %{server: server}}
else
:ok
end
end

test "returns all module code lenses" do
uri = "file://project/file.ex"

text = """
defmodule MyModule do
use ExUnit.Case
end
defmodule MyModule2 do
use ExUnit.Case
end
"""

{:ok, lenses} = CodeLens.Test.code_lens(uri, text)

assert lenses ==
[
build_code_lens(0, :module, "/file.ex", %{"module" => MyModule}),
build_code_lens(4, :module, "/file.ex", %{"module" => MyModule2})
]
end

test "returns all nested module code lenses" do
uri = "file://project/file.ex"

text = """
defmodule MyModule do
use ExUnit.Case
defmodule MyModule2 do
use ExUnit.Case
end
end
"""

{:ok, lenses} = CodeLens.Test.code_lens(uri, text)

assert lenses ==
[
build_code_lens(0, :module, "/file.ex", %{"module" => MyModule}),
build_code_lens(3, :module, "/file.ex", %{"module" => MyModule.MyModule2})
]
end

test "does not return lenses for modules that don't import ExUnit.case" do
uri = "file://project/file.ex"

text = """
defmodule MyModule do
end
"""

{:ok, lenses} = CodeLens.Test.code_lens(uri, text)

assert lenses == []
end

test "returns lenses for all describe blocks" do
uri = "file://project/file.ex"

text = """
defmodule MyModule do
use ExUnit.Case
describe "describe1" do
end
describe "describe2" do
end
end
"""

{:ok, lenses} = CodeLens.Test.code_lens(uri, text)

assert Enum.member?(
lenses,
build_code_lens(3, :describe, "/file.ex", %{"describe" => "describe1"})
)

assert Enum.member?(
lenses,
build_code_lens(6, :describe, "/file.ex", %{"describe" => "describe2"})
)
end

test "returns lenses for all test blocks" do
uri = "file://project/file.ex"

text = """
defmodule MyModule do
use ExUnit.Case
test "test1" do
end
test "test2" do
end
end
"""

{:ok, lenses} = CodeLens.Test.code_lens(uri, text)

assert Enum.member?(
lenses,
build_code_lens(3, :test, "/file.ex", %{"testName" => "test1"})
)

assert Enum.member?(
lenses,
build_code_lens(6, :test, "/file.ex", %{"testName" => "test2"})
)
end

test "given test blocks inside describe blocks, should return code lenses with the test and describe name" do
uri = "file://project/file.ex"

text = """
defmodule MyModule do
use ExUnit.Case
describe "describe1" do
test "test1" do
end
end
end
"""

{:ok, lenses} = CodeLens.Test.code_lens(uri, text)

assert Enum.member?(
lenses,
build_code_lens(4, :test, "/file.ex", %{
"testName" => "test1",
"describe" => "describe1"
})
)
end

defp build_code_lens(line, target, file_path, args) do
arguments =
%{
"filePath" => file_path
}
|> Map.merge(args)

%{
"range" => %{
"start" => %{
"line" => line,
"character" => 0
},
"end" => %{
"line" => line,
"character" => 0
}
},
"command" => %{
"title" => get_lens_title(target),
"command" => "elixir.lens.test.run",
"arguments" => [arguments]
}
}
end

defp get_lens_title(:module), do: "Run tests in module"
defp get_lens_title(:describe), do: "Run tests"
defp get_lens_title(:test), do: "Run test"
end

0 comments on commit 26fe78e

Please sign in to comment.