Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: David Celis <[email protected]>
  • Loading branch information
davidcelis committed Sep 26, 2014
0 parents commit 3de504a
Show file tree
Hide file tree
Showing 18 changed files with 666 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.bundle
*.so
*.o
*.a
mkmf.log
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: ruby
rvm:
- 2.0.0
- 2.1.0
script: bundle exec rspec
notifications:
email: false
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in spec-me-maybe.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2014 David Celis

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# spec-me-maybe [![Build Status][travis-badge]][travis]

Are your tests order-dependent? Tired of all those randomly failing specs? Can't be bothered to use [Timecop][timecop]? Just give up and surrender. But at least use a proper syntax.

Introducing the `maybe` syntax for RSpec.

[timecop]: https://github.com/travisjeffery/timecop
[travis]: https://travis-ci.org/davidcelis/spec-me-maybe
[travis-badge]: https://travis-ci.org/davidcelis/spec-me-maybe.svg?branch=master

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'spec-me-maybe'
```

And then execute:

```sh
$ bundle
```

Or install it yourself as:

```sh
$ gem install spec-me-maybe
```

Then, in your `spec_helper.rb` file:

```ruby
require 'rspec/maybes'
```

## Usage

The "maybe" syntax looks and feels almost exactly like the "expect" syntax:

```ruby
describe User do
describe '#initialize' do
let(:user) { User.new(name: 'David Celis') }

it 'should set up a name' do
maybe(user.name).will eq 'David Celis'
end

it 'probably should not raise any sort of error' do
maybe { user }.will_not raise_error
end
end
end
```

Whereas `expect` would set up an `RSpec::Expectation`, `maybe` will instead set up an `RSpec::Maybe`. Also like Expectations, Maybes might or might not pass. In this case, however, it will pass randomly. But hey, maybe your Expectations were like that too.

If your colleagues' complaints of broken specs are totally bullshit because you're super sure they work on your machine, we've got you covered. Here's the same above example, but it'll totally always pass:

```ruby
describe User do
describe '#initialize' do
let(:user) { User.new(name: 'David Celis') }

it 'should set up a name' do
maybe(user.name).will eq('David Celis').on_my_machine
end

it 'probably should not raise any sort of error' do
maybe { user }.will_not raise_error.on_my_machine
end
end
end
```

## Contributing

1. [Fork it](https://github.com/davidcelis/spec-me-maybe/fork)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "bundler/gem_tasks"

48 changes: 48 additions & 0 deletions lib/rspec/maybes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'rspec/expectations'
require 'rspec/maybes/configuration'

module RSpec
# RSpec::Maybes provides a simple, readable API to express possible outcomes
# in a code example. To express an potential outcome, wrap an object or block
# in `maybe`, call `will` or `will_not` and pass it a matcher object:
#
# maybe(order.total).will eq(Money.new(5.55, :USD))
# maybe(list).will include(user)
# maybe(message).will_not match(/foo/)
# maybe { do_something }.will raise_error
#
# The last form (the block form) is needed to match against ruby constructs
# that are not objects, but can only be observed when executing a block
# of code. This includes raising errors, throwing symbols, yielding,
# and changing values.
#
# When `maybe(...).will` is invoked with a matcher, it turns around
# and calls `matcher.matches?(<object wrapped by maybe>)`. For example,
# in the expression:
#
# maybe(order.total).will eq(Money.new(5.55, :USD))
#
# ...`eq(Money.new(5.55, :USD))` returns a matcher object, and it results
# in the equivalent of `eq.matches?(order.total)`. If `matches?` happens to
# return `true`, the expectation is met and execution continues. If `false`,
# then the spec fails with the message returned by `eq.failure_message`.
#
# Given the expression:
#
# maybe(order.entries).will_not include(entry)
#
# ... pretty much the same thing happens. `will_not` is simply an alias of
# `will`. It's random!
#
# spec-me-maybe ships with the same standard set of useful matchers as
# rspec-expectations does, and writing your own matchers is quite simple.
module Maybes
# Exception raised when a maybe fails.
#
# @note We subclass Exception so that in a stub implementation if
# the user sets an expectation, it can't be caught in their
# code by a bare `rescue`.
# @api public
MaybeNot = Class.new(::RSpec::Expectations::ExpectationNotMetError)
end
end
54 changes: 54 additions & 0 deletions lib/rspec/maybes/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'rspec/maybes/syntax'

module RSpec
module Core
class Configuration
def conditionally_disable_expectations_monkey_patching
return unless disable_monkey_patching && rspec_expectations_loaded?

RSpec::Expectations.configuration.syntax = :maybe
end
end
end

module Expectations
class Configuration
alias :original_syntax= :syntax=
alias :original_syntax :syntax

# Configures the supported syntax.
# @param [Array<Symbol>, Symbol] values the syntaxes to enable
# @example
# RSpec.configure do |rspec|
# rspec.expect_with :rspec do |c|
# c.syntax = :maybe
# # or
# c.syntax = :expect
# # or
# c.syntax = [:maybe, :expect]
# end
# end
def syntax=(values)
original_syntax = values

if Array(values).include?(:maybe)
Maybes::Syntax.enable_maybe
else
Maybes::Syntax.disable_maybe
end
end

# The list of configured syntaxes.
# @return [Array<Symbol>] the list of configured syntaxes.
# @example
# unless RSpec::Matchers.configuration.syntax.include?(:maybe)
# raise "this RSpec extension gem requires the spec-me-maybe `:maybe` syntax"
# end
def syntax
syntaxes = original_syntaxes
syntaxes << :maybe if Maybes::Syntax.maybe_enabled?
syntaxes
end
end
end
end
21 changes: 21 additions & 0 deletions lib/rspec/maybes/fail_with.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module RSpec
module Maybes
class << self
# Raises an RSpec::Maybes::MaybeNot with message.
# @param [String] message
#
# Adds a diff to the failure message when `expected` and `actual` are
# both present.
def fail_with(message, failure_message_method)
unless message
raise ArgumentError, "Failure message is nil. Does your matcher define the " \
"appropriate failure_message[_when_negated] method to return a string?"
end

message = "#{message} Maybe next time, though?"

raise RSpec::Maybes::MaybeNot, message
end
end
end
end
55 changes: 55 additions & 0 deletions lib/rspec/maybes/handlers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'rspec/maybes/fail_with'

module RSpec
module Maybes
# @private
module MaybeHelper
def self.handle_failure(matcher, message, failure_message_method)
message = message.call if message.respond_to?(:call)
message ||= matcher.__send__(failure_message_method)

::RSpec::Maybes.fail_with message, failure_message_method
end
end

class MaybeHandler < RSpec::Expectations::PositiveExpectationHandler
def self.handle_matcher(actual, initial_matcher, message = nil, &block)
matcher = Expectations::ExpectationHelper.setup(self, initial_matcher, message)
matcher.instance_variable_set(:@actual, actual)
return matcher
end

def self.passes?(matcher)
matcher.on_your_machine? || rand < 0.9
end
end

# @private
class PositiveMaybeHandler < MaybeHandler
def self.handle_matcher(actual, initial_matcher, message = nil, &block)
matcher = super

return ::RSpec::Matchers::BuiltIn::PositiveOperatorMatcher.new(actual) unless initial_matcher
passes?(matcher) || MaybeHelper.handle_failure(matcher, message, :failure_message)
end

def self.verb
'might'
end
end

# @private
class NegativeMaybeHandler < MaybeHandler
def self.handle_matcher(actual, initial_matcher, message=nil, &block)
matcher = super

return ::RSpec::Matchers::BuiltIn::NegativeOperatorMatcher.new(actual) unless initial_matcher
passes?(matcher) || MaybeHelper.handle_failure(matcher, message, :failure_message_when_negated)
end

def self.verb
'might not'
end
end
end
end
Loading

0 comments on commit 3de504a

Please sign in to comment.