Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/value_formatters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ defmodule ValueFormatters do
%DateTime{} -> "date"
%NaiveDateTime{} -> "date"
%Time{} -> "date"
[_lat, _lng, _radius] -> "coordinates"
[_lat, _lng] -> "coordinates"
[lat, _lng, _radius] when is_number(lat) or is_binary(lat) -> "coordinates"
[lat, _lng] when is_number(lat) or is_binary(lat) -> "coordinates"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should extend the check to all entries of the list (even though it won't make for the prettiest code), what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a bit of an overkill for now, but might be useful int he future I guess. I'll extend it

%{"lat" => _lat, "lng" => _lng, "radius" => _radius} -> "coordinates"
%{"lat" => _lat, "lng" => _lng} -> "coordinates"
_ -> "The type of value #{inspect(value)} is not supported."
Expand Down
38 changes: 37 additions & 1 deletion test/value_formatters_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,15 @@ defmodule ValueFormattersTest do
{:ok, "123.1345°, 34.123°, 2 m"}
end

test "full coordinates as string" do
assert ValueFormatters.to_string(
["123.1345", "34.123", "2"],
%{"format" => "coordinates"},
@opts
) ==
{:ok, "123.1345°, 34.123°, 2 m"}
end

test "inference object with radius" do
assert ValueFormatters.to_string(
%{"lat" => 43.1298, "lng" => 54.1234, "radius" => 1},
Expand All @@ -578,6 +587,11 @@ defmodule ValueFormattersTest do
{:ok, "123.1345°, 34.123°"}
end

test "inference list as string" do
assert ValueFormatters.to_string(["123.1345", "34.123"], %{}, @opts) ==
{:ok, "123.1345°, 34.123°"}
end

test "with radius show radius" do
assert ValueFormatters.to_string(
%{"lat" => 123.134567, "lng" => 34.12345, "radius" => 2},
Expand Down Expand Up @@ -606,10 +620,32 @@ defmodule ValueFormattersTest do
end
end

test "call with empty object format desription" do
test "call with empty object format description" do
assert ValueFormatters.to_string(3.14244453, %{"precision" => 2}, @opts) == {:ok, "3.14"}
end

describe "array" do
test "doesn't format an arrays of maps with 2 elements" do
assert ValueFormatters.to_string([%{"foo" => "bar"}, %{"bar" => "foo"}], %{}, @opts) ==
{
:error,
"Unsupported format The type of value [%{\"foo\" => \"bar\"}, %{\"bar\" => \"foo\"}] is not supported."
}
end

test "doesn't format an arrays of maps with 3 elements" do
assert ValueFormatters.to_string(
[%{"foo" => "bar"}, %{"bar" => "foo"}, %{"baz" => "qux"}],
%{},
@opts
) ==
{
:error,
"Unsupported format The type of value [%{\"foo\" => \"bar\"}, %{\"bar\" => \"foo\"}, %{\"baz\" => \"qux\"}] is not supported."
}
end
end

describe "render" do
test "render unit html" do
assert ValueFormatters.to_string(
Expand Down