Skip to content

Commit

Permalink
First attempt on Elixir location tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smaximov committed Aug 28, 2023
1 parent 10978db commit 372ab29
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/elixir_sense/location_test.exs
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions test/misc/mock_elixir_src/lib/elixir/lib/string.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 372ab29

Please sign in to comment.