diff options
author | inoas <mail@inoas.com> | 2022-08-10 15:14:52 +0200 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-08-11 09:56:08 +0100 |
commit | a974efd66ed4904ce7c9cabc38c3ec0563715b8f (patch) | |
tree | f8c9b868ae82fd532e713876f28a313063b44b39 | |
parent | 81c7284468739bc64a65527d126f54fd8dff4120 (diff) | |
download | gleam_stdlib-a974efd66ed4904ce7c9cabc38c3ec0563715b8f.tar.gz gleam_stdlib-a974efd66ed4904ce7c9cabc38c3ec0563715b8f.zip |
//erl[1,2|3] output for inspecting improper Erlang lists
-rw-r--r-- | src/gleam_stdlib.erl | 32 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 10 | ||||
-rw-r--r-- | test/gleam_stdlib_test_ffi.erl | 14 |
3 files changed, 38 insertions, 18 deletions
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl index abb3b45..0d817fe 100644 --- a/src/gleam_stdlib.erl +++ b/src/gleam_stdlib.erl @@ -349,7 +349,10 @@ inspect(Binary) when is_binary(Binary) -> ["<<", lists:join(", ", Segments), ">>"] end; inspect(Elements) when is_list(Elements) -> - ["[", inspect_list(Elements), "]"]; + case is_proper_list(Elements) of + true -> ["[", inspect_proper_list(Elements), "]"]; + false -> ["//erl[", inspect_improper_list(Elements), "] %% improper list"] + end; inspect(Any) when is_tuple(Any) % Record constructors andalso is_atom(element(1, Any)) andalso element(1, Any) =/= false @@ -374,14 +377,27 @@ inspect(Any) when is_function(Any) -> inspect(Any) -> ["//erl(", io_lib:format("~p", [Any]), ")"]. -inspect_list([]) -> +is_proper_list([]) -> + true; +is_proper_list([_Head | Tail]) -> + is_proper_list(Tail); +is_proper_list(ImproperTail) when not is_list(ImproperTail) -> + false. + +inspect_proper_list(List) -> + lists:join(<<", ">>, lists:map(fun inspect/1, List)). + +inspect_improper_list(Elements) -> + do_inspect_improper_list(firstCall, Elements). + +do_inspect_improper_list(_NthCall, []) -> []; -inspect_list([Head]) -> - [inspect(Head)]; -inspect_list([First | Rest]) -> - [inspect(First), <<", ">> | inspect_list(Rest)]; -inspect_list(ImproperTail) -> - [<<"..">>, inspect(ImproperTail)]. +do_inspect_improper_list(firstCall, [First | Rest]) -> + [inspect(First) | do_inspect_improper_list(nthCall, Rest)]; +do_inspect_improper_list(nthCall, [First | Rest]) -> + [ <<",">>, inspect(First) | do_inspect_improper_list(nthCall, Rest)]; +do_inspect_improper_list(nthCall, ImproperTail) -> + [<<"|">>, inspect(ImproperTail)]. float_to_string(Float) when is_float(Float) -> erlang:iolist_to_binary(io_lib_format:fwrite_g(Float)). diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index 0637bdd..7c83c84 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -795,11 +795,15 @@ if erlang { if erlang { pub fn improper_list_inspect_test() { - let list = improper_list_append(1, 2) - assert "[1, ..2]" = string.inspect(list) + let list = improper_list_append(1, 2, 3) + assert "//erl[1,2|3] %% improper list" = string.inspect(list) } // Warning: The type of this function is incorrect - external fn improper_list_append(anything1, anything2) -> List(anything) = + external fn improper_list_append( + anything1, + anything2, + anything3, + ) -> List(anything) = "gleam_stdlib_test_ffi" "improper_list_append" } diff --git a/test/gleam_stdlib_test_ffi.erl b/test/gleam_stdlib_test_ffi.erl index c26a879..c34cb5e 100644 --- a/test/gleam_stdlib_test_ffi.erl +++ b/test/gleam_stdlib_test_ffi.erl @@ -2,7 +2,7 @@ -export([ main/0, should_equal/2, should_not_equal/2, should_be_ok/1, - should_be_error/1, improper_list_append/2 + should_be_error/1, improper_list_append/3 ]). -include_lib("eunit/include/eunit.hrl"). @@ -26,18 +26,18 @@ filepath_to_module(Path0) -> Path5 = list_to_binary(Path4), binary_to_atom(Path5). -should_equal(Actual, Expected) -> +should_equal(Actual, Expected) -> ?assertEqual(Expected, Actual), nil. -should_not_equal(Actual, Expected) -> +should_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual), nil. -should_be_ok(A) -> +should_be_ok(A) -> ?assertMatch({ok, _}, A), nil. -should_be_error(A) -> +should_be_error(A) -> ?assertMatch({error, _}, A), nil. -improper_list_append(X, Y) -> - [X | Y]. +improper_list_append(X, Y, Z) -> + [X, Y | Z]. |