Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Minitest Result assertions and bump version #64

Merged
merged 6 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.1.0] - 2024-02-21

### Added

- Add minitest assertions for Results

### Changed

- Switched to Standard over using Rubocop directly
Expand Down
42 changes: 21 additions & 21 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sorbet-result (1.0.0)
sorbet-result (1.1.0)
sorbet-runtime (~> 0.5)

GEM
Expand All @@ -13,39 +13,39 @@ GEM
reline (>= 0.3.8)
erubi (1.12.0)
io-console (0.7.2)
irb (1.11.1)
irb (1.11.2)
rdoc
reline (>= 0.4.2)
json (2.7.1)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
minitest (5.21.2)
minitest (5.22.2)
netrc (0.11.0)
parallel (1.24.0)
parser (3.3.0.4)
parser (3.3.0.5)
ast (~> 2.4.1)
racc
prettier_print (1.2.1)
prism (0.19.0)
prism (0.24.0)
psych (5.1.2)
stringio
racc (1.7.3)
rainbow (3.1.1)
rake (13.1.0)
rbi (0.1.6)
prism (>= 0.18.0, < 0.20)
rbi (0.1.9)
prism (>= 0.18.0, < 0.25)
sorbet-runtime (>= 0.5.9204)
rdoc (6.6.2)
psych (>= 4.0.0)
regexp_parser (2.9.0)
reline (0.4.2)
io-console (~> 0.5)
rexml (3.2.6)
rubocop (1.59.0)
rubocop (1.60.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.2.2.4)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
Expand All @@ -57,26 +57,26 @@ GEM
rubocop-performance (1.20.2)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.30.0, < 2.0)
rubocop-sorbet (0.7.6)
rubocop-sorbet (0.7.7)
rubocop (>= 0.90.0)
ruby-progressbar (1.13.0)
sorbet (0.5.11212)
sorbet-static (= 0.5.11212)
sorbet-runtime (0.5.11212)
sorbet-static (0.5.11212-universal-darwin)
sorbet-static (0.5.11212-x86_64-linux)
sorbet-static-and-runtime (0.5.11212)
sorbet (= 0.5.11212)
sorbet-runtime (= 0.5.11212)
sorbet (0.5.11262)
sorbet-static (= 0.5.11262)
sorbet-runtime (0.5.11262)
sorbet-static (0.5.11262-universal-darwin)
sorbet-static (0.5.11262-x86_64-linux)
sorbet-static-and-runtime (0.5.11262)
sorbet (= 0.5.11262)
sorbet-runtime (= 0.5.11262)
spoom (1.2.4)
erubi (>= 1.10.0)
sorbet-static-and-runtime (>= 0.5.10187)
syntax_tree (>= 6.1.1)
thor (>= 0.19.2)
standard (1.33.0)
standard (1.34.0)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.59.0)
rubocop (~> 1.60)
standard-custom (~> 1.0.0)
standard-performance (~> 1.3)
standard-custom (1.0.2)
Expand All @@ -91,7 +91,7 @@ GEM
stringio (3.1.0)
syntax_tree (6.2.0)
prettier_print (>= 1.2.0)
tapioca (0.11.17)
tapioca (0.12.0)
bundler (>= 2.2.25)
netrc (>= 0.11.0)
parallel (>= 1.21.0)
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,29 @@ res = retrieve_user(user_id)

If the above chain does not fail, the `puts` statement is never run. If the chain does yield a `Failure`, the `puts` block is executed and the `Failure` is ultimately returned.

### Testing

We ship with a few Minitest assertions that can be used to easily verify Results.

```ruby
# test_helper.rb

require "minitest/results_assertions"
# You also need add this to `sorbet/tapioca/require.rb` and rebuild the Minitest gem RBIs

# *_test.rb

@success = Typed::Success.new("Test Payload")
@failure = Typed::Failure.new("Test Error")

assert_success(@success)
assert_failure(@failure)
assert_payload("Test Payload", @success)
assert_error("Test Error", @failure)

# We also have the `refute_*` counterparts
```

## Why use Results?

Let's say you're working on a method that reaches out to an API and fetches a resource. We hope to get a successful response and continue on in our program, but you can imagine several scenarios where we don't get that response: our authentication could fail, the server could return a 5XX response code, or the resource we were querying could have moved or not exist any more.
Expand Down
48 changes: 48 additions & 0 deletions lib/minitest/result_assertions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# typed: true

require "minitest/assertions"
require "sorbet-result"

module Minitest
module Assertions
# Fails unless Result is a Success
def assert_success(result)
assert_kind_of(Typed::Success, result)
end

# Fails unless Result is a Failure
def assert_failure(result)
assert_kind_of(Typed::Failure, result)
end

# Fails unless exp is equal to payload
def assert_payload(exp, result)
assert_equal(exp, result.payload)
end

# Fails unless exp is equal to error
def assert_error(exp, result)
assert_equal(exp, result.error)
end

# Fails if Result is a Success
def refute_success(result)
refute_kind_of(Typed::Success, result)
end

# Fails if Result is a Failure
def refute_failure(result)
refute_kind_of(Typed::Failure, result)
end

# Fails if exp is equal to payload
def refute_payload(exp, result)
refute_equal(exp, result.payload)
end

# Fails if exp is equal to error
def refute_error(exp, result)
refute_equal(exp, result.error)
end
end
end
4 changes: 3 additions & 1 deletion sorbet-result.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |spec|
spec.name = "sorbet-result"
spec.version = "1.0.0"
spec.version = "1.1.0"
spec.authors = ["Max VelDink"]
spec.email = ["[email protected]"]

Expand Down Expand Up @@ -30,4 +30,6 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_runtime_dependency "sorbet-runtime", "~> 0.5"

spec.add_development_dependency "minitest", "~> 5.22"
end
Loading
Loading