diff --git a/test/elixir_sense/core/parser_test.exs b/test/elixir_sense/core/parser_test.exs index 1c0de7e5..5381a743 100644 --- a/test/elixir_sense/core/parser_test.exs +++ b/test/elixir_sense/core/parser_test.exs @@ -2,8 +2,12 @@ defmodule ElixirSense.Core.ParserTest do use ExUnit.Case, async: true import ExUnit.CaptureIO - import ElixirSense.Core.Parser - alias ElixirSense.Core.{Metadata, State.Env, State.VarInfo} + alias ElixirSense.Core.{Metadata, State.Env, State.VarInfo, State.CallInfo, Parser} + + defp parse(source, cursor) do + metadata = Parser.parse_string(source, true, false, cursor) + {metadata, Metadata.get_cursor_env(metadata, cursor)} + end test "parse_string creates a Metadata struct" do source = """ @@ -13,12 +17,11 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: nil, - mods_funs_to_positions: %{{MyModule, nil, nil} => %{positions: [{1, 1}]}}, - cursor_env: {_, %Env{functions: functions}}, - source: "defmodule MyModule" <> _ - } = parse_string(source, true, true, {3, 3}) + assert {%Metadata{ + error: nil, + mods_funs_to_positions: %{{MyModule, nil, nil} => %{positions: [{1, 1}]}}, + source: "defmodule MyModule" <> _ + }, %Env{functions: functions}} = parse(source, {3, 3}) assert Keyword.has_key?(functions, List) end @@ -31,10 +34,9 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: {:error, :parse_error}, - cursor_env: {_, %Env{functions: functions3, module: MyModule}} - } = parse_string(source, true, true, {3, 10}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{functions: functions3, module: MyModule}} = parse(source, {3, 10}) assert Keyword.has_key?(functions3, List) end @@ -47,10 +49,9 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: {:error, :parse_error}, - cursor_env: {_, %Env{functions: functions}} - } = parse_string(source, true, true, {3, 20}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{functions: functions}} = parse(source, {3, 20}) assert Keyword.has_key?(functions, List) end @@ -63,10 +64,9 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: {:error, :parse_error}, - closest_env: {_, _, %Env{functions: functions}} - } = parse_string(source, true, true, {3, 8}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{functions: functions}} = parse(source, {3, 8}) assert Keyword.has_key?(functions, List) end @@ -79,10 +79,9 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: {:error, :parse_error}, - closest_env: {_, _, %Env{functions: functions}} - } = parse_string(source, true, true, {3, 11}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{functions: functions}} = parse(source, {3, 11}) assert Keyword.has_key?(functions, List) end @@ -95,10 +94,9 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: {:error, :parse_error}, - closest_env: {_, _, %Env{functions: functions}} - } = parse_string(source, true, true, {3, 12}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{functions: functions}} = parse(source, {3, 12}) assert Keyword.has_key?(functions, List) end @@ -111,13 +109,9 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: {:error, :parse_error}, - lines_to_env: %{ - 1 => %Env{}, - 3 => %Env{functions: functions} - } - } = parse_string(source, true, true, {3, 10}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{functions: functions}} = parse(source, {3, 10}) assert Keyword.has_key?(functions, List) end @@ -130,13 +124,9 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: {:error, :parse_error}, - lines_to_env: %{ - 1 => %Env{}, - 3 => %Env{functions: functions} - } - } = parse_string(source, true, true, {3, 10}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{functions: functions}} = parse(source, {3, 10}) assert Keyword.has_key?(functions, List) end @@ -149,10 +139,9 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: {:error, :parse_error}, - closest_env: {_, _, %Env{functions: functions}} - } = parse_string(source, true, true, {3, 12}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{functions: functions}} = parse(source, {3, 12}) assert Keyword.has_key?(functions, List) end @@ -165,10 +154,9 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: {:error, :parse_error}, - closest_env: {_, _, %Env{functions: functions}} - } = parse_string(source, true, true, {3, 12}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{functions: functions}} = parse(source, {3, 12}) assert Keyword.has_key?(functions, List) end @@ -181,10 +169,9 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: {:error, :parse_error}, - closest_env: {_, _, %Env{functions: functions}} - } = parse_string(source, true, true, {3, 12}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{functions: functions}} = parse(source, {3, 12}) assert Keyword.has_key?(functions, List) end @@ -197,10 +184,9 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: {:error, :parse_error}, - cursor_env: {_, %Env{functions: functions}} - } = parse_string(source, true, true, {3, 14}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{functions: functions}} = parse(source, {3, 14}) assert Keyword.has_key?(functions, List) end @@ -215,23 +201,13 @@ defmodule ElixirSense.Core.ParserTest do end """ - # assert capture_io(:stderr, fn -> - result = parse_string(source, true, true, {3, 23}) - # send(self(), {:result, result}) - # end) =~ "an expression is always required on the right side of ->" - - # assert_received {:result, result} - - assert %Metadata{ - error: {:error, :parse_error}, - closest_env: - {_, _, - %Env{ - vars: [ - %VarInfo{name: :x} - ] - }} - } = result + {_metadata, env} = parse(source, {3, 23}) + + assert %Env{ + vars: [ + %VarInfo{name: :x} + ] + } = env end test "parse_string with missing terminator \"end\" attempts to insert `end` at correct indentation" do @@ -240,10 +216,9 @@ defmodule ElixirSense.Core.ParserTest do """ - assert %Metadata{ - error: {:error, :parse_error}, - cursor_env: {_, %Env{module: MyModule}} - } = parse_string(source, true, true, {2, 3}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{module: MyModule}} = parse(source, {2, 3}) source = """ defmodule MyModule do @@ -252,40 +227,29 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: {:error, :parse_error}, - closest_env: {_, _, %Env{module: MyModule}} - } = parse_string(source, true, true, {3, 1}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{module: MyModule}} = parse(source, {3, 1}) - assert %Metadata{ - error: {:error, :parse_error}, - cursor_env: {_, %Env{module: MyModule}} - } = parse_string(source, true, true, {2, 1}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{module: MyModule}} = parse(source, {2, 1}) source = """ defmodule MyModule do defmodule MyModule1 do + end """ - assert %Metadata{ - error: {:error, :parse_error}, - cursor_env: {_, %Env{module: MyModule}}, - lines_to_env: %{ - 1 => %Env{module: MyModule}, - 3 => %Env{module: MyModule.MyModule1} - } - } = parse_string(source, true, true, {2, 1}) - - assert %Metadata{ - error: {:error, :parse_error}, - closest_env: {_, _, %Env{module: MyModule}}, - lines_to_env: %{ - 1 => %Env{module: MyModule}, - 3 => %Env{module: MyModule.MyModule1} - } - } = parse_string(source, true, true, {3, 1}) + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{module: MyModule}} = parse(source, {2, 1}) + + assert {%Metadata{ + error: {:error, :parse_error} + }, %Env{module: MyModule.MyModule1}} = parse(source, {4, 5}) end test "parse_string with incomplete key for multiline keyword as argument" do @@ -299,10 +263,14 @@ defmodule ElixirSense.Core.ParserTest do end """ - capture_io(:stderr, fn -> - assert %Metadata{error: {:error, :parse_error}, cursor_env: {_, _}} = - parse_string(source, true, true, {5, 10}) - end) + assert {%Metadata{ + error: {:error, :parse_error}, + calls: %{ + 2 => [%CallInfo{func: :inspect}] + } + }, + %Env{module: MyModule}} = + parse(source, {5, 10}) end test "parse_string with missing value for multiline keyword as argument" do @@ -316,8 +284,14 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{error: {:error, :parse_error}, cursor_env: {_, _}} = - parse_string(source, true, true, {5, 12}) + assert {%Metadata{ + error: {:error, :parse_error}, + calls: %{ + 2 => [%CallInfo{func: :inspect}] + } + }, + %Env{module: MyModule}} = + parse(source, {5, 12}) end @tag capture_log: true @@ -331,12 +305,11 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %Metadata{ - error: nil, - mods_funs_to_positions: %{{MyModule, nil, nil} => %{positions: [{1, 1}]}}, - cursor_env: {_, %Env{functions: functions}}, - source: "defmodule MyModule" <> _ - } = parse_string(source, true, true, {5, 3}) + assert {%Metadata{ + error: nil, + mods_funs_to_positions: %{{MyModule, nil, nil} => %{positions: [{1, 1}]}}, + source: "defmodule MyModule" <> _ + }, %Env{functions: functions}} = parse(source, {5, 3}) assert Keyword.has_key?(functions, List) end @@ -346,12 +319,9 @@ defmodule ElixirSense.Core.ParserTest do defmodule MyModule, do """ - assert %ElixirSense.Core.Metadata{ - error: {:error, :parse_error}, - lines_to_env: %{ - 1 => %Env{module: MyModule} - } - } = parse_string(source, true, true, {1, 23}) + assert {%ElixirSense.Core.Metadata{ + error: {:error, :parse_error} + }, %Env{module: MyModule}} = parse(source, {1, 23}) end test "parse_string with literal strings" do @@ -366,13 +336,10 @@ defmodule ElixirSense.Core.ParserTest do end ''' - assert %ElixirSense.Core.Metadata{ - cursor_env: - {_, - %ElixirSense.Core.State.Env{ - attributes: [%ElixirSense.Core.State.AttributeInfo{name: :my_attr}] - }} - } = parse_string(source, true, true, {6, 6}) + assert {%ElixirSense.Core.Metadata{}, + %ElixirSense.Core.State.Env{ + attributes: [%ElixirSense.Core.State.AttributeInfo{name: :my_attr}] + }} = parse(source, {6, 6}) end test "parse_string with literal strings in sigils" do @@ -387,15 +354,10 @@ defmodule ElixirSense.Core.ParserTest do end ''' - assert %ElixirSense.Core.Metadata{ - closest_env: { - _, - _, - %ElixirSense.Core.State.Env{ - vars: vars - } - } - } = parse_string(source, true, true, {5, 14}) + assert {%Metadata{}, + %Env{ + vars: vars + }} = parse(source, {5, 14}) assert [ %ElixirSense.Core.State.VarInfo{name: :x}, @@ -414,11 +376,11 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %ElixirSense.Core.Metadata{ - calls: %{ - 4 => [%{func: :foo}] - } - } = parse_string(source, true, true, {4, 7}) + assert {%ElixirSense.Core.Metadata{ + calls: %{ + 4 => [%CallInfo{func: :foo}] + } + }, %Env{function: {:func, 0}}} = parse(source, {4, 7}) end test "parse struct with missing terminator" do @@ -432,10 +394,10 @@ defmodule ElixirSense.Core.ParserTest do end """ - assert %ElixirSense.Core.Metadata{ - calls: %{ - 4 => [%{func: :foo}] - } - } = parse_string(source, true, true, {4, 8}) + assert {%ElixirSense.Core.Metadata{ + calls: %{ + 4 => [%{func: :foo}] + } + }, %Env{function: {:func, 0}}} = parse(source, {4, 8}) end end