-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c12b65
commit 537f0fc
Showing
6 changed files
with
101 additions
and
1 deletion.
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
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,21 @@ | ||
# typed: strict | ||
|
||
module Typed | ||
module Coercion | ||
class FloatCoercer | ||
extend T::Sig | ||
extend T::Generic | ||
|
||
extend Coercer | ||
|
||
Target = type_template { {fixed: Float} } | ||
|
||
sig { override.params(field: Field, value: Value).returns(Result[Target, CoercionError]) } | ||
def self.coerce(field:, value:) | ||
Success.new(Float(value)) | ||
rescue ArgumentError, TypeError | ||
Failure.new(CoercionError.new("'#{value}' cannot be coerced into Float.")) | ||
end | ||
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,21 @@ | ||
# typed: strict | ||
|
||
module Typed | ||
module Coercion | ||
class IntegerCoercer | ||
extend T::Sig | ||
extend T::Generic | ||
|
||
extend Coercer | ||
|
||
Target = type_template { {fixed: Integer} } | ||
|
||
sig { override.params(field: Field, value: Value).returns(Result[Target, CoercionError]) } | ||
def self.coerce(field:, value:) | ||
Success.new(Integer(value)) | ||
rescue ArgumentError, TypeError | ||
Failure.new(CoercionError.new("'#{value}' cannot be coerced into Integer.")) | ||
end | ||
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,17 @@ | ||
# typed: true | ||
|
||
class FloatCoercerTest < Minitest::Test | ||
def setup | ||
@field = Typed::Field.new(name: :testing, type: Float) | ||
end | ||
|
||
def test_when_coercable_returns_success | ||
assert_payload(1.1, Typed::Coercion::FloatCoercer.coerce(field: @field, value: "1.1")) | ||
assert_payload(1.0, Typed::Coercion::FloatCoercer.coerce(field: @field, value: 1)) | ||
end | ||
|
||
def test_when_not_coercable_returns_failure | ||
assert_error(Typed::Coercion::CoercionError.new("'a' cannot be coerced into Float."), Typed::Coercion::FloatCoercer.coerce(field: @field, value: "a")) | ||
assert_error(Typed::Coercion::CoercionError.new("'true' cannot be coerced into Float."), Typed::Coercion::FloatCoercer.coerce(field: @field, value: true)) | ||
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,17 @@ | ||
# typed: true | ||
|
||
class IntegerCoercerTest < Minitest::Test | ||
def setup | ||
@field = Typed::Field.new(name: :testing, type: Integer) | ||
end | ||
|
||
def test_when_coercable_returns_success | ||
assert_payload(1, Typed::Coercion::IntegerCoercer.coerce(field: @field, value: "1")) | ||
assert_payload(1, Typed::Coercion::IntegerCoercer.coerce(field: @field, value: 1.1)) | ||
end | ||
|
||
def test_when_not_coercable_returns_failure | ||
assert_error(Typed::Coercion::CoercionError.new("'a' cannot be coerced into Integer."), Typed::Coercion::IntegerCoercer.coerce(field: @field, value: "a")) | ||
assert_error(Typed::Coercion::CoercionError.new("'true' cannot be coerced into Integer."), Typed::Coercion::IntegerCoercer.coerce(field: @field, value: true)) | ||
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