-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First attempt on Elixir location tests
- Loading branch information
1 parent
7a983b3
commit 59e4c96
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |