Skip to content

Commit

Permalink
feat: add Failure equivalents
Browse files Browse the repository at this point in the history
  • Loading branch information
maxveldink committed Nov 3, 2024
1 parent bc6d400 commit d5ac206
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
14 changes: 14 additions & 0 deletions lib/typed/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ def self.Success(payload)
Success.new(payload)
end

sig do
type_parameters(:T)
.params(error: T.type_parameter(:T))
.returns(Typed::Failure[T.type_parameter(:T)])
end
def self.Failure(error)
Failure.new(error)
end

class Failure < Result
extend T::Sig
extend T::Generic
Expand Down Expand Up @@ -225,5 +234,10 @@ def on_error(&block)
def payload_or(value)
value
end

sig { params(other: T.untyped).returns(T::Boolean) }
def ==(other)
other.is_a?(Failure) && other.error == error
end
end
end
4 changes: 2 additions & 2 deletions test/test_data/failure.out
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ test/test_data/failure.rb:26: Expected `Integer` but found `String("error")` for
26 | Typed::Failure[Integer].new("error")
^^^^^^^
Expected `Integer` for argument `error` of method `Typed::Failure#initialize`:
./lib/typed/result.rb:178:
178 | sig { params(error: Error).void }
./lib/typed/result.rb:187:
187 | sig { params(error: Error).void }
^^^^^
Got `String("error")` originating from:
test/test_data/failure.rb:26:
Expand Down
10 changes: 10 additions & 0 deletions test/typed/failure_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def test_blank_is_convenience_for_nil_error
assert_nil failure.error
end

def test_Failure_convenience_method
assert_equal @failure, Typed::Failure("Something bad")
end

def test_it_is_failure
assert_predicate @failure, :failure?
assert_predicate @failure_without_error, :failure?
Expand Down Expand Up @@ -50,4 +54,10 @@ def test_on_error_calls_block_with_error_and_returns_self
def test_payload_or_returns_value
assert_equal(2, @failure.payload_or(2))
end

def test_equals_works
assert_equal(@failure, Typed::Failure.new("Something bad"))
refute_equal(@failure, Typed::Failure.blank)
refute_equal(@failure, Typed::Success.new("Something bad"))
end
end
4 changes: 2 additions & 2 deletions test/typed/success_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_blank_is_convenience_for_nil_payload
end

def test_Success_convenience_method
assert_equal Typed::Success.new("Testing"), Typed::Success("Testing")
assert_equal @success, Typed::Success("Testing")
end

def test_it_is_success
Expand Down Expand Up @@ -55,6 +55,6 @@ def test_payload_or_returns_payload
def test_equals_works
assert_equal(@success, Typed::Success.new("Testing"))
refute_equal(@success, Typed::Success.blank)
refute_equal(@success, Typed::Failure.blank)
refute_equal(@success, Typed::Failure.new("Testing"))
end
end

0 comments on commit d5ac206

Please sign in to comment.