From 59e4c9671eb04487ed6ccfdf7bccb5c642a2be1d Mon Sep 17 00:00:00 2001 From: Sergei Maximov Date: Tue, 29 Aug 2023 00:36:34 +0300 Subject: [PATCH] First attempt on Elixir location tests --- test/elixir_sense/location_test.exs | 32 +++++++++++++++++ .../mock_elixir_src/lib/elixir/lib/string.ex | 35 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 test/elixir_sense/location_test.exs create mode 100644 test/misc/mock_elixir_src/lib/elixir/lib/string.ex diff --git a/test/elixir_sense/location_test.exs b/test/elixir_sense/location_test.exs new file mode 100644 index 00000000..035d6741 --- /dev/null +++ b/test/elixir_sense/location_test.exs @@ -0,0 +1,32 @@ +defmodule ElixirSense.LocationTest do + use ExUnit.Case, async: false + + import ElixirSense.Location + + setup do + elixir_src = Path.join(File.cwd!(), "/test/misc/mock_elixir_src") + Application.put_env(:elixir_sense, :elixir_src, elixir_src) + + on_exit(fn -> + Application.delete_env(:elixir_sense, :elixir_src) + end) + end + + describe "find_mod_fun_source/3" do + test "returns location of a core Elixir function" do + assert %ElixirSense.Location{type: :function, line: 26, column: 3, file: file} = + find_mod_fun_source(String, :length, 1) + + assert String.ends_with?(file, "/mock_elixir_src/lib/elixir/lib/string.ex") + end + end + + describe "find_type_source/3" do + test "returns location of a core Elixir type" do + assert %ElixirSense.Location{type: :typespec, line: 11, column: 3, file: file} = + find_type_source(String, :t, 0) + + assert String.ends_with?(file, "/mock_elixir_src/lib/elixir/lib/string.ex") + end + end +end diff --git a/test/misc/mock_elixir_src/lib/elixir/lib/string.ex b/test/misc/mock_elixir_src/lib/elixir/lib/string.ex new file mode 100644 index 00000000..221ff5b1 --- /dev/null +++ b/test/misc/mock_elixir_src/lib/elixir/lib/string.ex @@ -0,0 +1,35 @@ +import Kernel, except: [length: 1] + +defmodule String do + @typedoc """ + A UTF-8 encoded binary. + + The types `String.t()` and `binary()` are equivalent to analysis tools. + Although, for those reading the documentation, `String.t()` implies + it is a UTF-8 encoded binary. + """ + @type t :: binary + + @doc """ + Returns the number of Unicode graphemes in a UTF-8 string. + + ## Examples + + iex> String.length("elixir") + 6 + + iex> String.length("եոգլի") + 5 + + """ + @spec length(t) :: non_neg_integer + def length(string) when is_binary(string), do: length(string, 0) + + defp length(gcs, acc) do + case :unicode_util.gc(gcs) do + [_ | rest] -> length(rest, acc + 1) + [] -> acc + {:error, <<_, rest::bits>>} -> length(rest, acc + 1) + end + end +end