Skip to content

Commit

Permalink
Coerce ints to floats
Browse files Browse the repository at this point in the history
  • Loading branch information
space-pope committed May 8, 2019
1 parent 048883d commit 2eaea08
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/spect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ defmodule Spect do

# float
defp to_type!(data, _module, :float, _args) do
if is_float(data) do
data
else
raise(ConvertError, "expected: float, found: #{inspect(data)}")
cond do
is_float(data) -> data
is_integer(data) -> data / 1.0
true -> raise(ConvertError, "expected: float, found: #{inspect(data)}")
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Spect.MixProject do
[
app: :spect,
name: "Spect",
version: "0.3.0",
version: "0.3.1",
elixir: "~> 1.7",
elixirc_paths: elixirc_paths(Mix.env()),
description: "Type specification extensions for Elixir.",
Expand Down
3 changes: 2 additions & 1 deletion test/spect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ defmodule Spect.Test do
{:error, %ConvertError{}} = to_spec("invalid", Specs, :basic_integer)

assert to_spec(1.0, Specs, :basic_float) === {:ok, 1.0}
{:error, %ConvertError{}} = to_spec(1, Specs, :basic_float)
assert to_spec(1, Specs, :basic_float) === {:ok, 1.0}
{:error, %ConvertError{}} = to_spec("1", Specs, :basic_float)

assert to_spec(1.0, Specs, :basic_number) === {:ok, 1.0}
assert to_spec(1, Specs, :basic_number) === {:ok, 1}
Expand Down

0 comments on commit 2eaea08

Please sign in to comment.