Skip to content

Commit 372ab29

Browse files
committed
First attempt on Elixir location tests
1 parent 10978db commit 372ab29

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

test/elixir_sense/location_test.exs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
defmodule ElixirSense.LocationTest do
2+
use ExUnit.Case, async: false
3+
4+
import ElixirSense.Location
5+
6+
setup do
7+
elixir_src = Path.join(File.cwd!(), "/test/misc/mock_elixir_src")
8+
Application.put_env(:elixir_sense, :elixir_src, elixir_src)
9+
10+
on_exit(fn ->
11+
Application.delete_env(:elixir_sense, :elixir_src)
12+
end)
13+
end
14+
15+
describe "find_mod_fun_source/3" do
16+
test "returns location of a core Elixir function" do
17+
assert %ElixirSense.Location{type: :function, line: 26, column: 3, file: file} =
18+
find_mod_fun_source(String, :length, 1)
19+
20+
assert String.ends_with?(file, "/mock_elixir_src/lib/elixir/lib/string.ex")
21+
end
22+
end
23+
24+
describe "find_type_source/3" do
25+
test "returns location of a core Elixir type" do
26+
assert %ElixirSense.Location{type: :typespec, line: 11, column: 3, file: file} =
27+
find_type_source(String, :t, 0)
28+
29+
assert String.ends_with?(file, "/mock_elixir_src/lib/elixir/lib/string.ex")
30+
end
31+
end
32+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Kernel, except: [length: 1]
2+
3+
defmodule String do
4+
@typedoc """
5+
A UTF-8 encoded binary.
6+
7+
The types `String.t()` and `binary()` are equivalent to analysis tools.
8+
Although, for those reading the documentation, `String.t()` implies
9+
it is a UTF-8 encoded binary.
10+
"""
11+
@type t :: binary
12+
13+
@doc """
14+
Returns the number of Unicode graphemes in a UTF-8 string.
15+
16+
## Examples
17+
18+
iex> String.length("elixir")
19+
6
20+
21+
iex> String.length("եոգլի")
22+
5
23+
24+
"""
25+
@spec length(t) :: non_neg_integer
26+
def length(string) when is_binary(string), do: length(string, 0)
27+
28+
defp length(gcs, acc) do
29+
case :unicode_util.gc(gcs) do
30+
[_ | rest] -> length(rest, acc + 1)
31+
[] -> acc
32+
{:error, <<_, rest::bits>>} -> length(rest, acc + 1)
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)