Last active
April 9, 2019 16:23
-
-
Save sovetnik/66b40e240c6169983cfbfcb843b4ab70 to your computer and use it in GitHub Desktop.
Monad matchers
This file contains hidden or 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
# frozen_string_literal: true | |
# For everything successful we can match it's value | |
RSpec::Matchers.define :hold do |expected| | |
match do |actual| | |
return false unless actual.success? | |
expect(actual.value!.inspect).to eq expected.inspect | |
end | |
failure_message do |actual| | |
"expected that #{actual} would be successful and contain an #{expected}" | |
end | |
diffable | |
end | |
RSpec::Matchers.define :cause do |expected| | |
match do |actual| | |
return false unless actual.failure? | |
expect(actual.failure).to eq expected | |
end | |
failure_message do |actual| | |
"expected that #{actual} would be failed and contain an #{expected}" | |
end | |
end | |
RSpec::Matchers.define :be_failed do |_expected| | |
match do |actual| | |
expect(actual).to be_a Dry::Monads::Result::Failure | |
end | |
failure_message do |actual| | |
"expected FAILURE instead #{actual}" | |
end | |
end | |
RSpec::Matchers.define :be_successful do |_expected| | |
match do |actual| | |
expect(actual).to be_a Dry::Monads::Result::Success | |
end | |
failure_message do |actual| | |
"expected SUCCESS instead #{actual}" | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment