|
| 1 | +defmodule NextLS.ElixirExtension.UndefinedFunctionTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + alias GenLSP.Structures.CodeAction |
| 5 | + alias GenLSP.Structures.Position |
| 6 | + alias GenLSP.Structures.Range |
| 7 | + alias GenLSP.Structures.TextEdit |
| 8 | + alias GenLSP.Structures.WorkspaceEdit |
| 9 | + alias NextLS.ElixirExtension.CodeAction.UndefinedFunction |
| 10 | + |
| 11 | + test "in outer module creates new private function inside current module" do |
| 12 | + text = |
| 13 | + String.split( |
| 14 | + """ |
| 15 | + defmodule Test.Foo do |
| 16 | + defmodule Bar do |
| 17 | + def run() do |
| 18 | + :ok |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + def hello() do |
| 23 | + bar(1, 2) |
| 24 | + end |
| 25 | +
|
| 26 | + defmodule Baz do |
| 27 | + def run() do |
| 28 | + :error |
| 29 | + end |
| 30 | + end |
| 31 | + end |
| 32 | + """, |
| 33 | + "\n" |
| 34 | + ) |
| 35 | + |
| 36 | + start = %Position{character: 4, line: 8} |
| 37 | + |
| 38 | + diagnostic = %GenLSP.Structures.Diagnostic{ |
| 39 | + data: %{ |
| 40 | + "namespace" => "elixir", |
| 41 | + "type" => "undefined-function", |
| 42 | + "info" => %{ |
| 43 | + "name" => "bar", |
| 44 | + "arity" => "2", |
| 45 | + "module" => "Elixir.Test.Foo" |
| 46 | + } |
| 47 | + }, |
| 48 | + message: |
| 49 | + "undefined function bar/2 (expected Test.Foo to define such a function or for it to be imported, but none are available)", |
| 50 | + source: "Elixir", |
| 51 | + range: %GenLSP.Structures.Range{ |
| 52 | + start: start, |
| 53 | + end: %{start | character: 6} |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + uri = "file:///home/owner/my_project/hello.ex" |
| 58 | + |
| 59 | + assert [public, private] = UndefinedFunction.new(diagnostic, text, uri) |
| 60 | + assert [diagnostic] == public.diagnostics |
| 61 | + assert public.title == "Create public function bar/2" |
| 62 | + |
| 63 | + edit_position = %Position{line: 16, character: 0} |
| 64 | + |
| 65 | + assert %WorkspaceEdit{ |
| 66 | + changes: %{ |
| 67 | + ^uri => [ |
| 68 | + %TextEdit{ |
| 69 | + new_text: """ |
| 70 | +
|
| 71 | + def bar(param1, param2) do |
| 72 | +
|
| 73 | + end |
| 74 | + """, |
| 75 | + range: %Range{start: ^edit_position, end: ^edit_position} |
| 76 | + } |
| 77 | + ] |
| 78 | + } |
| 79 | + } = public.edit |
| 80 | + |
| 81 | + assert [diagnostic] == private.diagnostics |
| 82 | + assert private.title == "Create private function bar/2" |
| 83 | + |
| 84 | + edit_position = %Position{line: 16, character: 0} |
| 85 | + |
| 86 | + assert %WorkspaceEdit{ |
| 87 | + changes: %{ |
| 88 | + ^uri => [ |
| 89 | + %TextEdit{ |
| 90 | + new_text: """ |
| 91 | +
|
| 92 | + defp bar(param1, param2) do |
| 93 | +
|
| 94 | + end |
| 95 | + """, |
| 96 | + range: %Range{start: ^edit_position, end: ^edit_position} |
| 97 | + } |
| 98 | + ] |
| 99 | + } |
| 100 | + } = private.edit |
| 101 | + end |
| 102 | + |
| 103 | + test "in inner module creates new private function inside current module" do |
| 104 | + text = |
| 105 | + String.split( |
| 106 | + """ |
| 107 | + defmodule Test.Foo do |
| 108 | + defmodule Bar do |
| 109 | + def run() do |
| 110 | + bar(1, 2) |
| 111 | + end |
| 112 | + end |
| 113 | +
|
| 114 | + defmodule Baz do |
| 115 | + def run() do |
| 116 | + :error |
| 117 | + end |
| 118 | + end |
| 119 | + end |
| 120 | + """, |
| 121 | + "\n" |
| 122 | + ) |
| 123 | + |
| 124 | + start = %Position{character: 6, line: 3} |
| 125 | + |
| 126 | + diagnostic = %GenLSP.Structures.Diagnostic{ |
| 127 | + data: %{ |
| 128 | + "namespace" => "elixir", |
| 129 | + "type" => "undefined-function", |
| 130 | + "info" => %{ |
| 131 | + "name" => "bar", |
| 132 | + "arity" => "2", |
| 133 | + "module" => "Elixir.Test.Foo.Bar" |
| 134 | + } |
| 135 | + }, |
| 136 | + message: |
| 137 | + "undefined function bar/2 (expected Test.Foo to define such a function or for it to be imported, but none are available)", |
| 138 | + source: "Elixir", |
| 139 | + range: %GenLSP.Structures.Range{ |
| 140 | + start: start, |
| 141 | + end: %{start | character: 9} |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + uri = "file:///home/owner/my_project/hello.ex" |
| 146 | + |
| 147 | + assert [_, code_action] = UndefinedFunction.new(diagnostic, text, uri) |
| 148 | + assert %CodeAction{} = code_action |
| 149 | + assert [diagnostic] == code_action.diagnostics |
| 150 | + assert code_action.title == "Create private function bar/2" |
| 151 | + |
| 152 | + edit_position = %Position{line: 5, character: 0} |
| 153 | + |
| 154 | + assert %WorkspaceEdit{ |
| 155 | + changes: %{ |
| 156 | + ^uri => [ |
| 157 | + %TextEdit{ |
| 158 | + new_text: """ |
| 159 | +
|
| 160 | + defp bar(param1, param2) do |
| 161 | +
|
| 162 | + end |
| 163 | + """, |
| 164 | + range: %Range{start: ^edit_position, end: ^edit_position} |
| 165 | + } |
| 166 | + ] |
| 167 | + } |
| 168 | + } = code_action.edit |
| 169 | + end |
| 170 | +end |
0 commit comments