Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
Update indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Mar 18, 2024
1 parent 8412627 commit a884b05
Show file tree
Hide file tree
Showing 26 changed files with 232 additions and 218 deletions.
28 changes: 18 additions & 10 deletions DEV-README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
## Set up the dev environment

git clone https://github.com/rspec/rspec-expectations.git
cd rspec-expectations
gem install bundler
bundle install
```shell
git clone https://github.com/rspec/rspec-expectations.git
cd rspec-expectations
gem install bundler
bundle install
```

Now you should be able to run any of:

rake
rake spec
rake cucumber
```shell
rake
rake spec
rake cucumber
```

Or, if you prefer to use the rspec and cucumber commands directly, you can either:

bundle exec rspec
```shell
bundle exec rspec
```

Or ...

bundle install --binstubs
bin/rspec
```shell
bundle install --binstubs
bin/rspec
```

## Customize the dev environment

Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ If you want to use rspec-expectations with rspec, just install the rspec gem
and RubyGems will also install rspec-expectations for you (along with
rspec-core and rspec-mocks):

gem install rspec
```shell
gem install rspec
```

Want to run against the `main` branch? You'll need to include the dependent
RSpec repos as well. Add the following to your `Gemfile`:
Expand All @@ -27,7 +29,9 @@ end
If you want to use rspec-expectations with another tool, like Test::Unit,
Minitest, or Cucumber, you can install it directly:

gem install rspec-expectations
```shell
gem install rspec-expectations
```

## Contributing

Expand Down Expand Up @@ -67,8 +71,10 @@ The `describe` and `it` methods come from rspec-core. The `Order`, `LineItem`,
expresses an expected outcome. If `order.total == Money.new(5.55, :USD)`, then
the example passes. If not, it fails with a message like:

expected: #<Money @value=5.55 @currency=:USD>
got: #<Money @value=1.11 @currency=:USD>
```
expected: #<Money @value=5.55 @currency=:USD>
got: #<Money @value=1.11 @currency=:USD>
```

## Built-in matchers

Expand Down
142 changes: 71 additions & 71 deletions features/built_in_matchers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,172 +8,172 @@ respectively on an object. Most matchers can also be accessed using the `(...).s
e.g.

```ruby
expect(result).to eq(3)
expect(list).not_to be_empty
pi.should be > 3
expect(result).to eq(3)
expect(list).not_to be_empty
pi.should be > 3
```

## Object identity

```ruby
expect(actual).to be(expected) # passes if actual.equal?(expected)
expect(actual).to be(expected) # passes if actual.equal?(expected)
```

## Object equivalence

```ruby
expect(actual).to eq(expected) # passes if actual == expected
expect(actual).to eq(expected) # passes if actual == expected
```

## Optional APIs for identity/equivalence

```ruby
expect(actual).to eql(expected) # passes if actual.eql?(expected)
expect(actual).to equal(expected) # passes if actual.equal?(expected)
expect(actual).to eql(expected) # passes if actual.eql?(expected)
expect(actual).to equal(expected) # passes if actual.equal?(expected)

# NOTE: `expect` does not support `==` matcher.
# NOTE: `expect` does not support `==` matcher.
```

## Comparisons

```ruby
expect(actual).to be > expected
expect(actual).to be >= expected
expect(actual).to be <= expected
expect(actual).to be < expected
expect(actual).to be_between(minimum, maximum).inclusive
expect(actual).to be_between(minimum, maximum).exclusive
expect(actual).to match(/expression/)
expect(actual).to be_within(delta).of(expected)
expect(actual).to start_with expected
expect(actual).to end_with expected
expect(actual).to be > expected
expect(actual).to be >= expected
expect(actual).to be <= expected
expect(actual).to be < expected
expect(actual).to be_between(minimum, maximum).inclusive
expect(actual).to be_between(minimum, maximum).exclusive
expect(actual).to match(/expression/)
expect(actual).to be_within(delta).of(expected)
expect(actual).to start_with expected
expect(actual).to end_with expected

# NOTE: `expect` does not support `=~` matcher.
# NOTE: `expect` does not support `=~` matcher.
```

## Types/classes/response

```ruby
expect(actual).to be_instance_of(expected)
expect(actual).to be_kind_of(expected)
expect(actual).to respond_to(expected)
expect(actual).to be_instance_of(expected)
expect(actual).to be_kind_of(expected)
expect(actual).to respond_to(expected)
```

## Truthiness and existentialism

```ruby
expect(actual).to be_truthy # passes if actual is truthy (not nil or false)
expect(actual).to be true # passes if actual == true
expect(actual).to be_falsey # passes if actual is falsy (nil or false)
expect(actual).to be false # passes if actual == false
expect(actual).to be_nil # passes if actual is nil
expect(actual).to exist # passes if actual.exist? and/or actual.exists? are truthy
expect(actual).to exist(*args) # passes if actual.exist?(*args) and/or actual.exists?(*args) are truthy
expect(actual).to be_truthy # passes if actual is truthy (not nil or false)
expect(actual).to be true # passes if actual == true
expect(actual).to be_falsey # passes if actual is falsy (nil or false)
expect(actual).to be false # passes if actual == false
expect(actual).to be_nil # passes if actual is nil
expect(actual).to exist # passes if actual.exist? and/or actual.exists? are truthy
expect(actual).to exist(*args) # passes if actual.exist?(*args) and/or actual.exists?(*args) are truthy
```

## Expecting errors

```ruby
expect { ... }.to raise_error
expect { ... }.to raise_error(ErrorClass)
expect { ... }.to raise_error("message")
expect { ... }.to raise_error(ErrorClass, "message")
expect { ... }.to raise_error
expect { ... }.to raise_error(ErrorClass)
expect { ... }.to raise_error("message")
expect { ... }.to raise_error(ErrorClass, "message")
```

## Expecting throws

```ruby
expect { ... }.to throw_symbol
expect { ... }.to throw_symbol(:symbol)
expect { ... }.to throw_symbol(:symbol, 'value')
expect { ... }.to throw_symbol
expect { ... }.to throw_symbol(:symbol)
expect { ... }.to throw_symbol(:symbol, 'value')
```

## Predicate matchers

```ruby
expect(actual).to be_xxx # passes if actual.xxx?
expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg)
expect(actual).to be_xxx # passes if actual.xxx?
expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg)
```

### Examples

```ruby
expect([]).to be_empty
expect(:a => 1).to have_key(:a)
expect([]).to be_empty
expect(:a => 1).to have_key(:a)
```

## Collection membership

```ruby
expect(actual).to include(expected)
expect(array).to match_array(expected_array)
# ...which is the same as:
expect(array).to contain_exactly(individual, elements)
expect(actual).to include(expected)
expect(array).to match_array(expected_array)
# ...which is the same as:
expect(array).to contain_exactly(individual, elements)
```

### Examples

```ruby
expect([1, 2, 3]).to include(1)
expect([1, 2, 3]).to include(1, 2)
expect(:a => 'b').to include(:a => 'b')
expect("this string").to include("is str")
expect([1, 2, 3]).to contain_exactly(2, 1, 3)
expect([1, 2, 3]).to match_array([3, 2, 1])
expect([1, 2, 3]).to include(1)
expect([1, 2, 3]).to include(1, 2)
expect(:a => 'b').to include(:a => 'b')
expect("this string").to include("is str")
expect([1, 2, 3]).to contain_exactly(2, 1, 3)
expect([1, 2, 3]).to match_array([3, 2, 1])
```

## Ranges (1.9+ only)

```ruby
expect(1..10).to cover(3)
expect(1..10).to cover(3)
```

## Change observation

```ruby
expect { object.action }.to change(object, :value).from(old).to(new)
expect { object.action }.to change(object, :value).by(delta)
expect { object.action }.to change(object, :value).by_at_least(minimum_delta)
expect { object.action }.to change(object, :value).by_at_most(maximum_delta)
expect { object.action }.to change(object, :value).from(old).to(new)
expect { object.action }.to change(object, :value).by(delta)
expect { object.action }.to change(object, :value).by_at_least(minimum_delta)
expect { object.action }.to change(object, :value).by_at_most(maximum_delta)
```

### Examples

```ruby
expect { a += 1 }.to change { a }.by(1)
expect { a += 3 }.to change { a }.from(2)
expect { a += 3 }.to change { a }.by_at_least(2)
expect { a += 1 }.to change { a }.by(1)
expect { a += 3 }.to change { a }.from(2)
expect { a += 3 }.to change { a }.by_at_least(2)
```

## Satisfy

```ruby
expect(actual).to satisfy { |value| value == expected }
expect(actual).to satisfy { |value| value == expected }
```

## Output capture

```ruby
expect { actual }.to output("some output").to_stdout
expect { actual }.to output("some error").to_stderr
expect { actual }.to output("some output").to_stdout
expect { actual }.to output("some error").to_stderr
```

## Block expectation

```ruby
expect { |b| object.action(&b) }.to yield_control
expect { |b| object.action(&b) }.to yield_with_no_args # only matches no args
expect { |b| object.action(&b) }.to yield_with_args # matches any args
expect { |b| object.action(&b) }.to yield_successive_args(*args) # matches args against multiple yields
expect { |b| object.action(&b) }.to yield_control
expect { |b| object.action(&b) }.to yield_with_no_args # only matches no args
expect { |b| object.action(&b) }.to yield_with_args # matches any args
expect { |b| object.action(&b) }.to yield_successive_args(*args) # matches args against multiple yields
```

### Examples

```ruby
expect { |b| User.transaction(&b) }.to yield_control
expect { |b| User.transaction(&b) }.to yield_with_no_args
expect { |b| 5.tap(&b) }.not_to yield_with_no_args # because it yields with `5`
expect { |b| 5.tap(&b) }.to yield_with_args(5) # because 5 == 5
expect { |b| 5.tap(&b) }.to yield_with_args(Integer) # because Integer === 5
expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3)
expect { |b| User.transaction(&b) }.to yield_control
expect { |b| User.transaction(&b) }.to yield_with_no_args
expect { |b| 5.tap(&b) }.not_to yield_with_no_args # because it yields with `5`
expect { |b| 5.tap(&b) }.to yield_with_args(5) # because 5 == 5
expect { |b| 5.tap(&b) }.to yield_with_args(Integer) # because Integer === 5
expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3)
```
12 changes: 6 additions & 6 deletions features/built_in_matchers/all.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ Feature: `all` matcher
Use the `all` matcher to specify that a collection's objects all pass an expected matcher. This works on any enumerable object.

```ruby
expect([1, 3, 5]).to all( be_odd )
expect([1, 3, 5]).to all( be_an(Integer) )
expect([1, 3, 5]).to all( be < 10 )
expect([1, 3, 4]).to all( be_odd ) # fails
expect([1, 3, 5]).to all( be_odd )
expect([1, 3, 5]).to all( be_an(Integer) )
expect([1, 3, 5]).to all( be < 10 )
expect([1, 3, 4]).to all( be_odd ) # fails
```
The matcher also supports compound matchers:
```ruby
expect([1, 3, 5]).to all( be_odd.and be < 10 )
expect([1, 4, 21]).to all( be_odd.or be < 10 )
expect([1, 3, 5]).to all( be_odd.and be < 10 )
expect([1, 4, 21]).to all( be_odd.or be < 10 )
```
If you are looking for "any" member of a collection that passes an expectation, look at the `include`-matcher.
Expand Down
8 changes: 4 additions & 4 deletions features/built_in_matchers/be.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Feature: `be` matchers
There are several related "be" matchers:

```ruby
expect(obj).to be_truthy # passes if obj is truthy (not nil or false)
expect(obj).to be_falsey # passes if obj is falsy (nil or false)
expect(obj).to be_nil # passes if obj is nil
expect(obj).to be # passes if obj is truthy (not nil or false)
expect(obj).to be_truthy # passes if obj is truthy (not nil or false)
expect(obj).to be_falsey # passes if obj is falsy (nil or false)
expect(obj).to be_nil # passes if obj is nil
expect(obj).to be # passes if obj is truthy (not nil or false)
```

Scenario: The `be_truthy` matcher
Expand Down
16 changes: 9 additions & 7 deletions features/built_in_matchers/be_within.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ Feature: `be_within` matcher
Normal equality expectations do not work well for floating point values.
Consider this irb session:

> radius = 3
=> 3
> area_of_circle = radius * radius * Math::PI
=> 28.2743338823081
> area_of_circle == 28.2743338823081
=> false
```shell
> radius = 3
=> 3
> area_of_circle = radius * radius * Math::PI
=> 28.2743338823081
> area_of_circle == 28.2743338823081
=> false
```

Instead, you should use the `be_within` matcher to check that the value is within a delta of
your expected value:

```ruby
expect(area_of_circle).to be_within(0.1).of(28.3)
expect(area_of_circle).to be_within(0.1).of(28.3)
```

Note that the difference between the actual and expected values must be smaller than your
Expand Down
Loading

0 comments on commit a884b05

Please sign in to comment.