Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Typed::Success convenience method
Browse files Browse the repository at this point in the history
maxveldink committed Nov 3, 2024
1 parent db62fde commit bc6d400
Showing 4 changed files with 19 additions and 4 deletions.
11 changes: 11 additions & 0 deletions lib/typed/result.rb
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@
require_relative "no_payload_on_failure_error"

module Typed
extend T::Sig

# A monad representing either a success or a failure. Contains payload and error information as well.
class Result
extend T::Sig
@@ -140,6 +142,15 @@ def ==(other)
end
end

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

class Failure < Result
extend T::Sig
extend T::Generic
4 changes: 2 additions & 2 deletions test/test_data/failure.out
Original file line number Diff line number Diff line change
@@ -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:162:
162 | sig { params(error: Error).void }
./lib/typed/result.rb:178:
178 | sig { params(error: Error).void }
^^^^^
Got `String("error")` originating from:
test/test_data/failure.rb:26:
4 changes: 2 additions & 2 deletions test/test_data/success.out
Original file line number Diff line number Diff line change
@@ -19,8 +19,8 @@ test/test_data/success.rb:26: Expected `Integer` but found `String("success")` f
26 | Typed::Success[Integer].new("success")
^^^^^^^^^
Expected `Integer` for argument `payload` of method `Typed::Success#initialize`:
./lib/typed/result.rb:87:
87 | sig { params(payload: Payload).void }
./lib/typed/result.rb:89:
89 | sig { params(payload: Payload).void }
^^^^^^^
Got `String("success")` originating from:
test/test_data/success.rb:26:
4 changes: 4 additions & 0 deletions test/typed/success_test.rb
Original file line number Diff line number Diff line change
@@ -15,6 +15,10 @@ def test_blank_is_convenience_for_nil_payload
assert_nil success.payload
end

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

def test_it_is_success
assert_predicate @success, :success?
assert_predicate @success_without_payload, :success?

0 comments on commit bc6d400

Please sign in to comment.