Created
November 9, 2022 19:24
-
-
Save ilikepi/9a21119d68b33d13fa3b2bd8ec7e2614 to your computer and use it in GitHub Desktop.
Experiments with rspec predicate matchers
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
require 'spec_helper' | |
class PredicateMatcherExperiment | |
attr_reader :ivar | |
def initialize(ivar: nil) | |
@ivar = ivar | |
end | |
def truthy? | |
@ivar | |
end | |
def simple_predicate? | |
@ivar | |
end | |
end | |
describe 'predicate matcher experiment' do | |
describe '#truthy?' do | |
it 'returns truthy when #ivar is truthy' do | |
thing = PredicateMatcherExperiment.new(ivar: :foo) | |
# Just checking... | |
expect(thing.truthy?).to equal :foo | |
expect(thing).to be_truthy | |
end | |
it 'returns truthy when #ivar is `true`' do | |
thing = PredicateMatcherExperiment.new(ivar: true) | |
# Just checking... | |
expect(thing.truthy?).to equal true | |
expect(thing).to be_truthy | |
end | |
# Failure/Error: expect(thing).not_to be_truthy | |
# | |
# expected: falsey value | |
# got: #<PredicateMatcherExperiment:0x000000010c644fa8 @ivar=nil> | |
it 'returns falsey when #ivar is `nil`' do | |
thing = PredicateMatcherExperiment.new(ivar: nil) | |
# Just checking... | |
expect(thing.truthy?).to equal nil | |
expect(thing).not_to be_truthy | |
end | |
# Failure/Error: expect(thing).not_to be_truthy | |
# | |
# expected: falsey value | |
# got: #<PredicateMatcherExperiment:0x000000010c1e1380 @ivar=false> | |
it 'returns falsey when #ivar is `false`' do | |
thing = PredicateMatcherExperiment.new(ivar: false) | |
# Just checking... | |
expect(thing.truthy?).to equal false | |
expect(thing).not_to be_truthy | |
end | |
end | |
describe '#simple_predicate?' do | |
it 'returns truthy when #ivar is truthy' do | |
thing = PredicateMatcherExperiment.new(ivar: :foo) | |
# Just checking... | |
expect(thing.simple_predicate?).to equal :foo | |
expect(thing).to be_simple_predicate | |
end | |
it 'returns truthy when #ivar is `true`' do | |
thing = PredicateMatcherExperiment.new(ivar: true) | |
# Just checking... | |
expect(thing.simple_predicate?).to equal true | |
expect(thing).to be_simple_predicate | |
end | |
it 'returns falsey when #ivar is `nil`' do | |
thing = PredicateMatcherExperiment.new(ivar: nil) | |
# Just checking... | |
expect(thing.simple_predicate?).to equal nil | |
expect(thing).not_to be_simple_predicate | |
end | |
it 'returns falsey when #ivar is `false`' do | |
thing = PredicateMatcherExperiment.new(ivar: false) | |
# Just checking... | |
expect(thing.simple_predicate?).to equal false | |
expect(thing).not_to be_simple_predicate | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment