rspec-expectations-2.14.2/0000755000004100000410000000000012203207642015446 5ustar www-datawww-datarspec-expectations-2.14.2/Changelog.md0000644000004100000410000003331212203207642017661 0ustar www-datawww-data### 2.14.2 / 2013-08-14 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.14.1...v2.14.2) Bug fixes * Fix `be_` matcher to not support operator chaining like the `be` matcher does (e.g. `be == 5`). This led to some odd behaviors since `be_ == anything` returned a `BeComparedTo` matcher and was thus always truthy. This was a consequence of the implementation (e.g. subclassing the basic `Be` matcher) and was not intended behavior. (Myron Marston). * Fix `change` matcher to compare using `==` in addition to `===`. This is important for an expression like: `expect {}.to change { a.class }.from(ClassA).to(ClassB)` because `SomeClass === SomeClass` returns false. (Myron Marston) ### 2.14.1 / 2013-08-08 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.14.0...2.14.1) Bug fixes * Ensure diff output uses the same encoding as the encoding of the string being diff'd to prevent `Encoding::UndefinedConversionError` errors (Jon Rowe). ### 2.14.0 / 2013-07-06 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.14.0.rc1...v2.14.0) Bug fixes * Values that are not matchers use `#inspect`, rather than `#description` for documentation output (Andy Lindeman, Sam Phippen). * Make `expect(a).to be_within(x).percent_of(y)` work with negative y (Katsuhiko Nishimra). * Make the `be_predicate` matcher work as expected used with `expect{...}.to change...` (Sam Phippen). ### 2.14.0.rc1 / 2013-05-27 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.13.0...v2.14.0.rc1) Enhancements * Enhance `yield_control` so that you can specify an exact or relative number of times: `expect { }.to yield_control.exactly(3).times`, `expect { }.to yield_control.at_least(2).times`, etc (Bartek Borkowski). * Make the differ that is used when an expectation fails better handle arrays by splitting each element of the array onto its own line. (Sam Phippen) * Accept duck-typed strings that respond to `:to_str` as expectation messages. (Toby Ovod-Everett) Bug fixes * Fix differ to not raise errors when dealing with differently-encoded strings (Jon Rowe). * Fix `expect(something).to be_within(x).percent_of(y)` where x and y are both integers (Sam Phippen). * Fix `have` matcher to handle the fact that on ruby 2.0, `Enumerator#size` may return nil (Kenta Murata). * Fix `expect { raise s }.to raise_error(s)` where s is an error instance on ruby 2.0 (Sam Phippen). * Fix `expect(object).to raise_error` passing. This now warns the user and fails the spec (tomykaira). Deprecations * Deprecate `expect { }.not_to raise_error(SpecificErrorClass)` or `expect { }.not_to raise_error("some specific message")`. Using these was prone to hiding failures as they would allow _any other error_ to pass. (Sam Phippen and David Chelimsky) ### 2.13.0 / 2013-02-23 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.12.1...v2.13.0) Enhancements * Add support for percent deltas to `be_within` matcher: `expect(value).to be_within(10).percent_of(expected)` (Myron Marston). * Add support to `include` matcher to allow it to be given a list of matchers as the expecteds to match against (Luke Redpath). Bug fixes * Fix `change` matcher so that it dups strings in order to handle mutated strings (Myron Marston). * Fix `should be =~ /some regex/` / `expect(...).to be =~ /some regex/`. Previously, these either failed with a confusing `undefined method matches?' for false:FalseClass` error or were no-ops that didn't actually verify anything (Myron Marston). * Add compatibility for diff-lcs 1.2 and relax the version constraint (Peter Goldstein). * Fix DSL-generated matchers to allow multiple instances of the same matcher in the same example to have different description and failure messages based on the expected value (Myron Marston). * Prevent `undefined method #split for Array` error when dumping the diff of an array of multiline strings (Myron Marston). * Don't blow up when comparing strings that are in an encoding that is not ASCII compatible (Myron Marston). * Remove confusing "Check the implementation of #==" message printed for empty diffs (Myron Marston). ### 2.12.1 / 2012-12-15 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.12.0...v2.12.1) Bug fixes * Improve the failure message for an expression like `{}.should =~ {}`. (Myron Marston and Andy Lindeman) * Provide a `match_regex` alias so that custom matchers built using the matcher DSL can use it (since `match` is a different method in that context). (Steven Harman) ### 2.12.0 / 2012-11-12 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.11.3...v2.12.0) Enhancements * Colorize diffs if the `--color` option is configured. (Alex Coplan) * Include backtraces in unexpected errors handled by `raise_error` matcher (Myron Marston) * Print a warning when users accidentally pass a non-string argument as an expectation message (Sam Phippen) * `=~` and `match_array` matchers output a more useful error message when the actual value is not an array (or an object that responds to `#to_ary`) (Sam Phippen) Bug fixes * Fix `include` matcher so that `expect({}).to include(:a => nil)` fails as it should (Sam Phippen). * Fix `be_an_instance_of` matcher so that `Class#to_s` is used in the description rather than `Class#inspect`, since some classes (like `ActiveRecord::Base`) define a long, verbose `#inspect`. (Tom Stuart) ### 2.11.3 / 2012-09-04 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.11.2...v2.11.3) Bug fixes * Fix (and deprecate) `expect { }.should` syntax so that it works even though it was never a documented or intended syntax. It worked as a consequence of the implementation of `expect` in RSpec 2.10 and earlier. (Myron Marston) * Ensure #== is defined on built in matchers so that they can be composed. For example: expect { user.emailed! }.to change { user.last_emailed_at }.to be_within(1.second).of(Time.zone.now) ### 2.11.2 / 2012-07-25 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.11.1...v2.11.2) Bug fixes * Define `should` and `should_not` on `Object` rather than `BasicObject` on MacRuby. On MacRuby, `BasicObject` is defined but is not the root of the object hierarchy. (Gabriel Gilder) ### 2.11.1 / 2012-07-08 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.11.0...v2.11.1) Bug fixes * Constrain `actual` in `be_within` matcher to values that respond to `-` instead of requiring a specific type. * `Time`, for example, is a legit alternative. ### 2.11.0 / 2012-07-07 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.10.0...v2.11.0) Enhancements * Expand `expect` syntax so that it supports expections on bare values in addition to blocks (Myron Marston). * Add configuration options to control available expectation syntaxes (Myron Marston): * `RSpec.configuration.expect_with(:rspec) { |c| c.syntax = :expect }` * `RSpec.configuration.expect_with(:rspec) { |c| c.syntax = :should }` * `RSpec.configuration.expect_with(:rspec) { |c| c.syntax = [:should, :expect] }` * `RSpec.configuration.add_should_and_should_not_to Delegator` Bug fixes * Allow only `Numeric` values to be the "actual" in the `be_within` matcher. This prevents confusing error messages. (Su Zhang @zhangsu) * Define `should` and `should_not` on `BasicObject` rather than `Kernel` on 1.9. This makes `should` and `should_not` work properly with `BasicObject`-subclassed proxy objects like `Delegator`. (Myron Marston) ### 2.10.0 / 2012-05-03 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.9.1...v2.10.0) Enhancements * Add new `start_with` and `end_with` matchers (Jeremy Wadsack) * Add new matchers for specifying yields (Myron Marston): * `expect {...}.to yield_control` * `expect {...}.to yield_with_args(1, 2, 3)` * `expect {...}.to yield_with_no_args` * `expect {...}.to yield_successive_args(1, 2, 3)` * `match_unless_raises` takes multiple exception args Bug fixes * Fix `be_within` matcher to be inclusive of delta. * Fix message-specific specs to pass on Rubinius (John Firebaugh) ### 2.9.1 / 2012-04-03 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.9.0...v2.9.1) Bug fixes * Provide a helpful message if the diff between two objects is empty. * Fix bug diffing single strings with multiline strings. * Fix for error with using custom matchers inside other custom matchers (mirasrael) * Fix using execution context methods in nested DSL matchers (mirasrael) ### 2.9.0 / 2012-03-17 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.8.0...v2.9.0) Enhancements * Move built-in matcher classes to RSpec::Matchers::BuiltIn to reduce pollution of RSpec::Matchers (which is included in every example). * Autoload files with matcher classes to improve load time. Bug fixes * Align `respond_to?` and `method_missing` in DSL-defined matchers. * Clear out user-defined instance variables between invocations of DSL-defined matchers. * Dup the instance of a DSL generated matcher so its state is not changed by subsequent invocations. * Treat expected args consistently across positive and negative expectations (thanks to Ralf Kistner for the heads up) ### 2.8.0 / 2012-01-04 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.8.0.rc2...v2.8.0) Enhancements * Better diff output for Hash (Philippe Creux) * Eliminate Ruby warnings (Olek Janiszewski) ### 2.8.0.rc2 / 2011-12-19 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.8.0.rc1...v2.8.0.rc2) No changes for this release. Just releasing with the other rspec gems. ### 2.8.0.rc1 / 2011-11-06 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.7.0...v2.8.0.rc1) Enhancements * Use classes for the built-in matchers (they're faster). * Eliminate Ruby warnings (Matijs van Zuijlen) ### 2.7.0 / 2011-10-16 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.6.0...v2.7.0) Enhancements * `HaveMatcher` converts argument using `to_i` (Alex Bepple & Pat Maddox) * Improved failure message for the `have_xxx` matcher (Myron Marston) * `HaveMatcher` supports `count` (Matthew Bellantoni) * Change matcher dups `Enumerable` before the action, supporting custom `Enumerable` types like `CollectionProxy` in Rails (David Chelimsky) Bug fixes * Fix typo in `have(n).xyz` documentation (Jean Boussier) * fix `safe_sort` for ruby 1.9.2 (`Kernel` now defines `<=>` for Object) (Peter van Hardenberg) ### 2.6.0 / 2011-05-12 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.5.0...v2.6.0) Enhancements * `change` matcher accepts regexps (Robert Davis) * better descriptions for `have_xxx` matchers (Magnus Bergmark) * `range.should cover(*values)` (Anders Furseth) Bug fixes * Removed non-ascii characters that were choking rcov (Geoffrey Byers) * change matcher dups arrays and hashes so their before/after states can be compared correctly. * Fix the order of inclusion of RSpec::Matchers in Test::Unit::TestCase and MiniTest::Unit::TestCase to prevent a SystemStackError (Myron Marston) ### 2.5.0 / 2011-02-05 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.4.0...v2.5.0) Enhancements * `should exist` works with `exist?` or `exists?` (Myron Marston) * `expect { ... }.not_to do_something` (in addition to `to_not`) Documentation * improved docs for raise_error matcher (James Almond) ### 2.4.0 / 2011-01-02 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.3.0...v2.4.0) No functional changes in this release, which was made to align with the rspec-core-2.4.0 release. Enhancements * improved RDoc for change matcher (Jo Liss) ### 2.3.0 / 2010-12-12 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.2.1...v2.3.0) Enhancements * diff strings when include matcher fails (Mike Sassak) ### 2.2.0 / 2010-11-28 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.1.0...v2.2.0) ### 2.1.0 / 2010-11-07 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.0.1...v2.1.0) Enhancements * `be_within(delta).of(expected)` matcher (Myron Marston) * Lots of new Cucumber features (Myron Marston) * Raise error if you try `should != expected` on Ruby-1.9 (Myron Marston) * Improved failure messages from `throw_symbol` (Myron Marston) Bug fixes * Eliminate hard dependency on `RSpec::Core` (Myron Marston) * `have_matcher` - use pluralize only when ActiveSupport inflections are indeed defined (Josep M Bach) * throw_symbol matcher no longer swallows exceptions (Myron Marston) * fix matcher chaining to avoid name collisions (Myron Marston) ### 2.0.0 / 2010-10-10 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.0.0.rc...v2.0.0) Enhancements * Add match_for_should_not method to matcher DSL (Myron Marston) Bug fixes * `respond_to` matcher works correctly with `should_not` with multiple methods (Myron Marston) * `include` matcher works correctly with `should_not` with multiple values (Myron Marston) ### 2.0.0.rc / 2010-10-05 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.0.0.beta.22...v2.0.0.rc) Enhancements * `require 'rspec/expectations'` in a T::U or MiniUnit suite (Josep M. Bach) Bug fixes * change by 0 passes/fails correctly (Len Smith) * Add description to satisfy matcher ### 2.0.0.beta.22 / 2010-09-12 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.0.0.beta.20...v2.0.0.beta.22) Enhancements * diffing improvements * diff multiline strings * don't diff single line strings * don't diff numbers (silly) * diff regexp + multiline string Bug fixes * `should[_not]` change now handles boolean values correctly rspec-expectations-2.14.2/License.txt0000644000004100000410000000216212203207642017572 0ustar www-datawww-data(The MIT License) Copyright (c) 2006 David Chelimsky, The RSpec Development Team Copyright (c) 2005 Steven Baker 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. rspec-expectations-2.14.2/README.md0000644000004100000410000001160212203207642016725 0ustar www-datawww-data# RSpec Expectations [![Build Status](https://secure.travis-ci.org/rspec/rspec-expectations.png?branch=master)](http://travis-ci.org/rspec/rspec-expectations) [![Code Climate](https://codeclimate.com/github/rspec/rspec-expectations.png)](https://codeclimate.com/github/rspec/rspec-expectations) RSpec::Expectations lets you express expected outcomes on an object in an example. expect(account.balance).to eq(Money.new(37.42, :USD)) ## Install 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 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 ## Basic usage Here's an example using rspec-core: ```ruby describe Order do it "sums the prices of the items in its line items" do order = Order.new order.add_entry(LineItem.new(:item => Item.new( :price => Money.new(1.11, :USD) ))) order.add_entry(LineItem.new(:item => Item.new( :price => Money.new(2.22, :USD), :quantity => 2 ))) expect(order.total).to eq(Money.new(5.55, :USD)) end end ``` The `describe` and `it` methods come from rspec-core. The `Order`, `LineItem`, `Item` and `Money` classes would be from _your_ code. The last line of the example 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: # got: # ## Built-in matchers ### Equivalence ```ruby expect(actual).to eq(expected) # passes if actual == expected expect(actual).to eql(expected) # passes if actual.eql?(expected) ``` Note: The new `expect` syntax no longer supports `==` matcher. ### Identity ```ruby expect(actual).to be(expected) # passes if actual.equal?(expected) expect(actual).to equal(expected) # passes if actual.equal?(expected) ``` ### 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_within(delta).of(expected) ``` ### Regular expressions ```ruby expect(actual).to match(/expression/) ``` Note: The new `expect` syntax no longer supports `=~` matcher. ### Types/classes ```ruby expect(actual).to be_an_instance_of(expected) expect(actual).to be_a_kind_of(expected) ``` ### Truthiness ```ruby expect(actual).to be_true # passes if actual is truthy (not nil or false) expect(actual).to be_false # passes if actual is falsy (nil or false) expect(actual).to be_nil # passes if actual is nil ``` ### Expecting errors ```ruby 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') ``` ### Yielding ```ruby expect { |b| 5.tap(&b) }.to yield_control # passes regardless of yielded args expect { |b| yield_if_true(true, &b) }.to yield_with_no_args # passes only if no args are yielded expect { |b| 5.tap(&b) }.to yield_with_args(5) expect { |b| 5.tap(&b) }.to yield_with_args(Fixnum) expect { |b| "a string".tap(&b) }.to yield_with_args(/str/) expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3) expect { |b| { :a => 1, :b => 2 }.each(&b) }.to yield_successive_args([:a, 1], [:b, 2]) ``` ### Predicate matchers ```ruby expect(actual).to be_xxx # passes if actual.xxx? expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg) ``` ### Ranges (Ruby >= 1.9 only) ```ruby expect(1..10).to cover(3) ``` ### Collection membership ```ruby expect(actual).to include(expected) expect(actual).to start_with(expected) expect(actual).to end_with(expected) ``` #### Examples ```ruby expect([1,2,3]).to include(1) expect([1,2,3]).to include(1, 2) expect([1,2,3]).to start_with(1) expect([1,2,3]).to start_with(1,2) expect([1,2,3]).to end_with(3) expect([1,2,3]).to end_with(2,3) expect({:a => 'b'}).to include(:a => 'b') expect("this string").to include("is str") expect("this string").to start_with("this") expect("this string").to end_with("ring") ``` ## `should` syntax In addition to the `expect` syntax, rspec-expectations continues to support the `should` syntax: ```ruby actual.should eq expected actual.should be > 3 [1, 2, 3].should_not include 4 ``` See [detailed information on the `should` syntax and its usage.](https://github.com/rspec/rspec-expectations/blob/master/Should.md) ## Also see * [http://github.com/rspec/rspec](http://github.com/rspec/rspec) * [http://github.com/rspec/rspec-core](http://github.com/rspec/rspec-core) * [http://github.com/rspec/rspec-mocks](http://github.com/rspec/rspec-mocks) rspec-expectations-2.14.2/features/0000755000004100000410000000000012203207642017264 5ustar www-datawww-datarspec-expectations-2.14.2/features/custom_matchers/0000755000004100000410000000000012203207642022464 5ustar www-datawww-datarspec-expectations-2.14.2/features/custom_matchers/define_matcher_with_fluent_interface.feature0000644000004100000410000000134312203207642033427 0ustar www-datawww-dataFeature: define matcher with fluent interface Use the chain() method to define matchers with a fluent interface. Scenario: chained method with argumetn Given a file named "between_spec.rb" with: """ruby RSpec::Matchers.define :be_bigger_than do |first| match do |actual| (actual > first) && (actual < @second) end chain :but_smaller_than do |second| @second = second end end describe 5 do it { should be_bigger_than(4).but_smaller_than(6) } end """ When I run `rspec between_spec.rb --format documentation` Then the output should contain "1 example, 0 failures" And the output should contain "should be bigger than 4" rspec-expectations-2.14.2/features/custom_matchers/define_matcher.feature0000644000004100000410000002567512203207642027015 0ustar www-datawww-dataFeature: define matcher In order to express my domain clearly in my code examples As an RSpec user I want a shortcut to define custom matchers Scenario: define a matcher with default messages Given a file named "matcher_with_default_message_spec.rb" with: """ruby require 'rspec/expectations' RSpec::Matchers.define :be_a_multiple_of do |expected| match do |actual| actual % expected == 0 end end describe 9 do it {should be_a_multiple_of(3)} end describe 9 do it {should_not be_a_multiple_of(4)} end # fail intentionally to generate expected output describe 9 do it {should be_a_multiple_of(4)} end # fail intentionally to generate expected output describe 9 do it {should_not be_a_multiple_of(3)} end """ When I run `rspec ./matcher_with_default_message_spec.rb --format documentation` Then the exit status should not be 0 And the output should contain "should be a multiple of 3" And the output should contain "should not be a multiple of 4" And the output should contain "Failure/Error: it {should be_a_multiple_of(4)}" And the output should contain "Failure/Error: it {should_not be_a_multiple_of(3)}" And the output should contain "4 examples, 2 failures" And the output should contain "expected 9 to be a multiple of 4" And the output should contain "expected 9 not to be a multiple of 3" Scenario: overriding the failure_message_for_should Given a file named "matcher_with_failure_message_spec.rb" with: """ruby require 'rspec/expectations' RSpec::Matchers.define :be_a_multiple_of do |expected| match do |actual| actual % expected == 0 end failure_message_for_should do |actual| "expected that #{actual} would be a multiple of #{expected}" end end # fail intentionally to generate expected output describe 9 do it {should be_a_multiple_of(4)} end """ When I run `rspec ./matcher_with_failure_message_spec.rb` Then the exit status should not be 0 And the stdout should contain "1 example, 1 failure" And the stdout should contain "expected that 9 would be a multiple of 4" Scenario: overriding the failure_message_for_should_not Given a file named "matcher_with_failure_for_message_spec.rb" with: """ruby require 'rspec/expectations' RSpec::Matchers.define :be_a_multiple_of do |expected| match do |actual| actual % expected == 0 end failure_message_for_should_not do |actual| "expected that #{actual} would not be a multiple of #{expected}" end end # fail intentionally to generate expected output describe 9 do it {should_not be_a_multiple_of(3)} end """ When I run `rspec ./matcher_with_failure_for_message_spec.rb` Then the exit status should not be 0 And the stdout should contain "1 example, 1 failure" And the stdout should contain "expected that 9 would not be a multiple of 3" Scenario: overriding the description Given a file named "matcher_overriding_description_spec.rb" with: """ruby require 'rspec/expectations' RSpec::Matchers.define :be_a_multiple_of do |expected| match do |actual| actual % expected == 0 end description do "be multiple of #{expected}" end end describe 9 do it {should be_a_multiple_of(3)} end describe 9 do it {should_not be_a_multiple_of(4)} end """ When I run `rspec ./matcher_overriding_description_spec.rb --format documentation` Then the exit status should be 0 And the stdout should contain "2 examples, 0 failures" And the stdout should contain "should be multiple of 3" And the stdout should contain "should not be multiple of 4" Scenario: with no args Given a file named "matcher_with_no_args_spec.rb" with: """ruby require 'rspec/expectations' RSpec::Matchers.define :have_7_fingers do match do |thing| thing.fingers.length == 7 end end class Thing def fingers; (1..7).collect {"finger"}; end end describe Thing do it {should have_7_fingers} end """ When I run `rspec ./matcher_with_no_args_spec.rb --format documentation` Then the exit status should be 0 And the stdout should contain "1 example, 0 failures" And the stdout should contain "should have 7 fingers" Scenario: with multiple args Given a file named "matcher_with_multiple_args_spec.rb" with: """ruby require 'rspec/expectations' RSpec::Matchers.define :be_the_sum_of do |a,b,c,d| match do |sum| a + b + c + d == sum end end describe 10 do it {should be_the_sum_of(1,2,3,4)} end """ When I run `rspec ./matcher_with_multiple_args_spec.rb --format documentation` Then the exit status should be 0 And the stdout should contain "1 example, 0 failures" And the stdout should contain "should be the sum of 1, 2, 3, and 4" Scenario: with helper methods Given a file named "matcher_with_internal_helper_spec.rb" with: """ruby require 'rspec/expectations' RSpec::Matchers.define :have_same_elements_as do |sample| match do |actual| similar?(sample, actual) end def similar?(a, b) a.sort == b.sort end end describe "these two arrays" do specify "should be similar" do [1,2,3].should have_same_elements_as([2,3,1]) end end """ When I run `rspec ./matcher_with_internal_helper_spec.rb` Then the exit status should be 0 And the stdout should contain "1 example, 0 failures" Scenario: scoped in a module Given a file named "scoped_matcher_spec.rb" with: """ruby require 'rspec/expectations' module MyHelpers extend RSpec::Matchers::DSL matcher :be_just_like do |expected| match {|actual| actual == expected} end end describe "group with MyHelpers" do include MyHelpers it "has access to the defined matcher" do 5.should be_just_like(5) end end describe "group without MyHelpers" do it "does not have access to the defined matcher" do expect do 5.should be_just_like(5) end.to raise_exception end end """ When I run `rspec ./scoped_matcher_spec.rb` Then the stdout should contain "2 examples, 0 failures" Scenario: scoped in an example group Given a file named "scoped_matcher_spec.rb" with: """ruby require 'rspec/expectations' describe "group with matcher" do matcher :be_just_like do |expected| match {|actual| actual == expected} end it "has access to the defined matcher" do 5.should be_just_like(5) end describe "nested group" do it "has access to the defined matcher" do 5.should be_just_like(5) end end end describe "group without matcher" do it "does not have access to the defined matcher" do expect do 5.should be_just_like(5) end.to raise_exception end end """ When I run `rspec scoped_matcher_spec.rb` Then the output should contain "3 examples, 0 failures" Scenario: matcher with separate logic for should and should_not Given a file named "matcher_with_separate_should_not_logic_spec.rb" with: """ruby RSpec::Matchers.define :contain do |*expected| match_for_should do |actual| expected.all? { |e| actual.include?(e) } end match_for_should_not do |actual| expected.none? { |e| actual.include?(e) } end end describe [1, 2, 3] do it { should contain(1, 2) } it { should_not contain(4, 5, 6) } # deliberate failures it { should contain(1, 4) } it { should_not contain(1, 4) } end """ When I run `rspec matcher_with_separate_should_not_logic_spec.rb` Then the output should contain all of these: | 4 examples, 2 failures | | expected [1, 2, 3] to contain 1 and 4 | | expected [1, 2, 3] not to contain 1 and 4 | Scenario: use define_method to create a helper method with access to matcher params Given a file named "define_method_spec.rb" with: """ruby RSpec::Matchers.define :be_a_multiple_of do |expected| define_method :is_multiple? do |actual| actual % expected == 0 end match { |actual| is_multiple?(actual) } end describe 9 do it { should be_a_multiple_of(3) } it { should_not be_a_multiple_of(4) } # deliberate failures it { should be_a_multiple_of(2) } it { should_not be_a_multiple_of(3) } end """ When I run `rspec define_method_spec.rb` Then the output should contain all of these: | 4 examples, 2 failures | | expected 9 to be a multiple of 2 | | expected 9 not to be a multiple of 3 | Scenario: include a module with helper methods in the matcher Given a file named "include_module_spec.rb" with: """ruby module MatcherHelpers def is_multiple?(actual, expected) actual % expected == 0 end end RSpec::Matchers.define :be_a_multiple_of do |expected| include MatcherHelpers match { |actual| is_multiple?(actual, expected) } end describe 9 do it { should be_a_multiple_of(3) } it { should_not be_a_multiple_of(4) } # deliberate failures it { should be_a_multiple_of(2) } it { should_not be_a_multiple_of(3) } end """ When I run `rspec include_module_spec.rb` Then the output should contain all of these: | 4 examples, 2 failures | | expected 9 to be a multiple of 2 | | expected 9 not to be a multiple of 3 | Scenario: matching against a regular expression Given a file named "regular_expression_matcher_spec.rb" with: """ruby # Due to Ruby's method dispatch mechanism, use the `#match_regex` alias # rather than the `#match` matcher when defining custom matchers via the # DSL. RSpec::Matchers.define :be_valid_us_zipcode do match do |actual| expect(actual).to match_regex(/\A\d{5}(-\d{4})?\z/) end end describe "30316" do it { should be_valid_us_zipcode } end describe "30316-0001" do it { should be_valid_us_zipcode } end describe "1000-61303" do it { should_not be_valid_us_zipcode } end """ When I run `rspec regular_expression_matcher_spec.rb` Then the stdout should contain "3 examples, 0 failures" rspec-expectations-2.14.2/features/custom_matchers/define_matcher_outside_rspec.feature0000644000004100000410000000204112203207642031723 0ustar www-datawww-dataFeature: define matcher outside rspec In order to express my domain clearly in my code examples As a non-rspec user I want a shortcut to define custom matchers Scenario: define a matcher with default messages Given a file named "test_multiples.rb" with: """ruby require "rspec/expectations" require "test/unit" RSpec::Matchers.define :be_a_multiple_of do |expected| match do |actual| actual % expected == 0 end end class Test::Unit::TestCase include RSpec::Matchers end class TestMultiples < Test::Unit::TestCase def test_9_should_be_a_multiple_of_3 9.should be_a_multiple_of(3) end def test_9_should_be_a_multiple_of_4 9.should be_a_multiple_of(4) end end """ When I run `ruby test_multiples.rb` Then the exit status should not be 0 And the output should contain "expected 9 to be a multiple of 4" And the output should contain "2 tests, 0 assertions, 0 failures, 1 errors" rspec-expectations-2.14.2/features/custom_matchers/access_running_example.feature0000644000004100000410000000266412203207642030565 0ustar www-datawww-dataFeature: access running example In order to take advantage of services that are available in my examples when I'm writing matchers As a spec author I want to call methods on the running example If the method exists in the context of the example, it gets called. If not, a NoMethodError is raised on the Matcher itself (not the example). Scenario: call method defined on example from matcher Given a file named "example_spec.rb" with: """ruby RSpec::Matchers.define :bar do match do |_| foo == "foo" end end describe "something" do def foo "foo" end it "does something" do "foo".should bar end end """ When I run `rspec ./example_spec.rb` Then the output should contain "1 example, 0 failures" Scenario: call method _not_ defined on example from matcher Given a file named "example_spec.rb" with: """ruby RSpec::Matchers.define :bar do match do |_| foo == "foo" end end describe "something" do it "does something" do "foo".should bar end end """ When I run `rspec ./example_spec.rb` Then the output should contain "1 example, 1 failure" And the output should match /undefined.*method/ And the output should contain "RSpec::Matchers::DSL::Matcher" And the output should not contain "ExampleGroup" rspec-expectations-2.14.2/features/custom_matchers/define_diffable_matcher.feature0000644000004100000410000000136012203207642030612 0ustar www-datawww-dataFeature: define diffable matcher When a matcher is defined as diffable, and the --diff flag is set, the output will include a diff of the submitted objects. @wip Scenario: define a diffable matcher Given a file named "diffable_matcher_spec.rb" with: """ruby RSpec::Matchers.define :be_just_like do |expected| match do |actual| actual == expected end diffable end describe "this" do it {should be_just_like("that")} end """ When I run `rspec ./diffable_matcher_spec.rb --diff` Then the exit status should not be 0 And the output should contain "should be just like that" And the output should contain "Diff:\n@@ -1,2 +1,2 @@\n-that\n+this" rspec-expectations-2.14.2/features/diffing.feature0000644000004100000410000000411012203207642022243 0ustar www-datawww-dataFeature: diffing When appropriate, failure messages will automatically include a diff. Scenario: diff for a multiline string Given a file named "example_spec.rb" with: """ruby describe "a multiline string" do it "is like another string" do expected = <<-EXPECTED this is the expected string EXPECTED actual = <<-ACTUAL this is the actual string ACTUAL actual.should eq(expected) end end """ When I run `rspec example_spec.rb` Then the output should contain: """ Diff: @@ -1,4 +1,4 @@ this is the - expected + actual string """ Scenario: diff for a multiline string and a regexp Given a file named "example_spec.rb" with: """ruby describe "a multiline string" do it "is like another string" do expected = /expected/m actual = <<-ACTUAL this is the actual string ACTUAL actual.should =~ expected end end """ When I run `rspec example_spec.rb` Then the output should contain: """ Diff: @@ -1,2 +1,4 @@ -/expected/m +this is the + actual + string """ Scenario: no diff for a single line strings Given a file named "example_spec.rb" with: """ruby describe "a single line string" do it "is like another string" do expected = "this string" actual = "that string" actual.should eq(expected) end end """ When I run `rspec example_spec.rb` Then the output should not contain "Diff:" Scenario: no diff for numbers Given a file named "example_spec.rb" with: """ruby describe "a number" do it "is like another number" do 1.should eq(2) end end """ When I run `rspec example_spec.rb` Then the output should not contain "Diff:" rspec-expectations-2.14.2/features/built_in_matchers/0000755000004100000410000000000012203207642022757 5ustar www-datawww-datarspec-expectations-2.14.2/features/built_in_matchers/cover.feature0000644000004100000410000000300412203207642025447 0ustar www-datawww-data@ruby-1.9 Feature: cover matcher Use the cover matcher to specify that a range covers one or more expected objects. This works on any object that responds to #cover? (such as a Range): ```ruby (1..10).should cover(5) (1..10).should cover(4, 6) (1..10).should_not cover(11) ``` Scenario: range usage Given a file named "range_cover_matcher_spec.rb" with: """ruby describe (1..10) do it { should cover(4) } it { should cover(6) } it { should cover(8) } it { should cover(4, 6) } it { should cover(4, 6, 8) } it { should_not cover(11) } it { should_not cover(11, 12) } # deliberate failures it { should cover(11) } it { should_not cover(4) } it { should_not cover(6) } it { should_not cover(8) } it { should_not cover(4, 6, 8) } # both of these should fail since it covers 5 but not 11 it { should cover(5, 11) } it { should_not cover(5, 11) } end """ When I run `rspec range_cover_matcher_spec.rb` Then the output should contain all of these: | 14 examples, 7 failures | | expected 1..10 to cover 11 | | expected 1..10 not to cover 4 | | expected 1..10 not to cover 6 | | expected 1..10 not to cover 8 | | expected 1..10 not to cover 4, 6, and 8 | | expected 1..10 to cover 5 and 11 | | expected 1..10 not to cover 5 and 11 | rspec-expectations-2.14.2/features/built_in_matchers/yield.feature0000644000004100000410000002020512203207642025441 0ustar www-datawww-dataFeature: yield matchers There are four related matchers that allow you to specify whether or not a method yields, how many times it yields, whether or not it yields with arguments, and what those arguments are. * `yield_control` matches if the method-under-test yields, regardless of whether or not arguments are yielded. * `yield_with_args` matches if the method-under-test yields with arguments. If arguments are provided to this matcher, it will only pass if the actual yielded arguments match the expected ones using `===` or `==`. * `yield_with_no_args` matches if the method-under-test yields with no arguments. * `yield_successive_args` is designed for iterators, and will match if the method-under-test yields the same number of times as arguments passed to this matcher, and all actual yielded arguments match the expected ones using `===` or `==`. Note: your expect block _must_ accept an argument that is then passed on to the method-under-test as a block. This acts as a "probe" that allows the matcher to detect whether or not your method yields, and, if so, how many times and what the yielded arguments are. Background: Given a file named "my_class.rb" with: """ruby class MyClass def self.yield_once_with(*args) yield *args end def self.yield_twice_with(*args) 2.times { yield *args } end def self.raw_yield yield end def self.dont_yield end end """ Scenario: yield_control matcher Given a file named "yield_control_spec.rb" with: """ruby require './my_class' describe "yield_control matcher" do specify { expect { |b| MyClass.yield_once_with(1, &b) }.to yield_control } specify { expect { |b| MyClass.dont_yield(&b) }.not_to yield_control } specify { expect { |b| MyClass.yield_twice_with(1, &b) }.to yield_control.twice } specify { expect { |b| MyClass.yield_twice_with(1, &b) }.to yield_control.exactly(2).times } specify { expect { |b| MyClass.yield_twice_with(1, &b) }.to yield_control.at_least(1) } specify { expect { |b| MyClass.yield_twice_with(1, &b) }.to yield_control.at_most(3).times } # deliberate failures specify { expect { |b| MyClass.yield_once_with(1, &b) }.not_to yield_control } specify { expect { |b| MyClass.dont_yield(&b) }.to yield_control } specify { expect { |b| MyClass.yield_once_with(1, &b) }.to yield_control.at_least(2).times } specify { expect { |b| MyClass.yield_twice_with(1, &b) }.not_to yield_control.twice } specify { expect { |b| MyClass.yield_twice_with(1, &b) }.not_to yield_control.at_least(2).times } specify { expect { |b| MyClass.yield_twice_with(1, &b) }.not_to yield_control.at_least(1) } specify { expect { |b| MyClass.yield_twice_with(1, &b) }.not_to yield_control.at_most(3).times } end """ When I run `rspec yield_control_spec.rb` Then the output should contain all of these: | 13 examples, 7 failures | | expected given block to yield control | | expected given block not to yield control | | expected given block not to yield control at least twice | | expected given block not to yield control at most 3 times | Scenario: yield_with_args matcher Given a file named "yield_with_args_spec.rb" with: """ruby require './my_class' describe "yield_with_args matcher" do specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args("foo") } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args(String) } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args(/oo/) } specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args("foo", "bar") } specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(String, String) } specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(/fo/, /ar/) } specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.not_to yield_with_args(17, "baz") } # deliberate failures specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args("foo") } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args(String) } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args(/oo/) } specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.not_to yield_with_args("foo", "bar") } specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(17, "baz") } end """ When I run `rspec yield_with_args_spec.rb` Then the output should contain all of these: | 14 examples, 6 failures | | expected given block not to yield with arguments, but did | | expected given block not to yield with arguments, but yielded with expected arguments | | expected given block to yield with arguments, but yielded with unexpected arguments | Scenario: yield_with_no_args matcher Given a file named "yield_with_no_args_spec.rb" with: """ruby require './my_class' describe "yield_with_no_args matcher" do specify { expect { |b| MyClass.raw_yield(&b) }.to yield_with_no_args } specify { expect { |b| MyClass.dont_yield(&b) }.not_to yield_with_no_args } specify { expect { |b| MyClass.yield_once_with("a", &b) }.not_to yield_with_no_args } # deliberate failures specify { expect { |b| MyClass.raw_yield(&b) }.not_to yield_with_no_args } specify { expect { |b| MyClass.dont_yield(&b) }.to yield_with_no_args } specify { expect { |b| MyClass.yield_once_with("a", &b) }.to yield_with_no_args } end """ When I run `rspec yield_with_no_args_spec.rb` Then the output should contain all of these: | 6 examples, 3 failures | | expected given block not to yield with no arguments, but did | | expected given block to yield with no arguments, but did not yield | | expected given block to yield with no arguments, but yielded with arguments: ["a"] | Scenario: yield_successive_args matcher Given a file named "yield_successive_args_spec.rb" with: """ruby def array [1, 2, 3] end def array_of_tuples [[:a, :b], [:c, :d]] end describe "yield_successive_args matcher" do specify { expect { |b| array.each(&b) }.to yield_successive_args(1, 2, 3) } specify { expect { |b| array_of_tuples.each(&b) }.to yield_successive_args([:a, :b], [:c, :d]) } specify { expect { |b| array.each(&b) }.to yield_successive_args(Fixnum, Fixnum, Fixnum) } specify { expect { |b| array.each(&b) }.not_to yield_successive_args(1, 2) } # deliberate failures specify { expect { |b| array.each(&b) }.not_to yield_successive_args(1, 2, 3) } specify { expect { |b| array_of_tuples.each(&b) }.not_to yield_successive_args([:a, :b], [:c, :d]) } specify { expect { |b| array.each(&b) }.not_to yield_successive_args(Fixnum, Fixnum, Fixnum) } specify { expect { |b| array.each(&b) }.to yield_successive_args(1, 2) } end """ When I run `rspec yield_successive_args_spec.rb` Then the output should contain all of these: | 8 examples, 4 failures | | expected given block not to yield successively with arguments, but yielded with expected arguments | | expected given block to yield successively with arguments, but yielded with unexpected arguments | rspec-expectations-2.14.2/features/built_in_matchers/satisfy.feature0000644000004100000410000000213212203207642026014 0ustar www-datawww-dataFeature: satisfy matcher The satisfy matcher is extremely flexible and can handle almost anything you want to specify. It passes if the block you provide returns true: ```ruby 10.should satisfy { |v| v % 5 == 0 } 7.should_not satisfy { |v| v % 5 == 0 } ``` This flexibility comes at a cost, however: the failure message ("expected [actual] to satisfy block") is not very descriptive or helpful. You will usually be better served by using one of the other built-in matchers, or writing a custom matcher. Scenario: basic usage Given a file named "satisfy_matcher_spec.rb" with: """ruby describe 10 do it { should satisfy { |v| v > 5 } } it { should_not satisfy { |v| v > 15 } } # deliberate failures it { should_not satisfy { |v| v > 5 } } it { should satisfy { |v| v > 15 } } end """ When I run `rspec satisfy_matcher_spec.rb` Then the output should contain all of these: | 4 examples, 2 failures | | expected 10 not to satisfy block | | expected 10 to satisfy block | rspec-expectations-2.14.2/features/built_in_matchers/match.feature0000644000004100000410000000310712203207642025431 0ustar www-datawww-dataFeature: match matcher The match matcher calls #match on the object, passing if #match returns a truthy (not false or nil) value. Regexp and String both provide a #match method. ```ruby "a string".should match(/str/) # passes "a string".should match(/foo/) # fails /foo/.should match("food") # passes /foo/.should match("drinks") # fails ``` This is equivalent to using the =~ matcher (see the operator matchers feature for more details). Scenario: string usage Given a file named "string_match_spec.rb" with: """ruby describe "a string" do it { should match(/str/) } it { should_not match(/foo/) } # deliberate failures it { should_not match(/str/) } it { should match(/foo/) } end """ When I run `rspec string_match_spec.rb` Then the output should contain all of these: | 4 examples, 2 failures | | expected "a string" not to match /str/ | | expected "a string" to match /foo/ | Scenario: regular expression usage Given a file named "regexp_match_spec.rb" with: """ruby describe /foo/ do it { should match("food") } it { should_not match("drinks") } # deliberate failures it { should_not match("food") } it { should match("drinks") } end """ When I run `rspec regexp_match_spec.rb` Then the output should contain all of these: | 4 examples, 2 failures | | expected /foo/ not to match "food" | | expected /foo/ to match "drinks" | rspec-expectations-2.14.2/features/built_in_matchers/exist.feature0000644000004100000410000000233412203207642025472 0ustar www-datawww-dataFeature: exist matcher The exist matcher is used to specify that something exists (as indicated by #exist? or #exists?): ```ruby obj.should exist # passes if obj.exist? or obj.exists? ``` Scenario: basic usage Given a file named "exist_matcher_spec.rb" with: """ruby class Planet attr_reader :name def initialize(name) @name = name end def inspect "" end def exist? # also works with exists? %w[Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune].include?(name) end end describe "Earth" do let(:earth) { Planet.new("Earth") } specify { earth.should exist } specify { earth.should_not exist } # deliberate failure end describe "Tatooine" do let(:tatooine) { Planet.new("Tatooine") } it { tatooine.should exist } # deliberate failure it { tatooine.should_not exist } end """ When I run `rspec exist_matcher_spec.rb` Then the output should contain all of these: | 4 examples, 2 failures | | expected not to exist | | expected to exist | rspec-expectations-2.14.2/features/built_in_matchers/start_with.feature0000644000004100000410000000303312203207642026523 0ustar www-datawww-dataFeature: start_with matcher Use the `start_with` matcher to specify that a string or array starts with the expected characters or elements. ```ruby "this string".should start_with("this") "this string".should_not start_with("that") [0,1,2].should start_with(0, 1) ``` Scenario: with a string Given a file named "example_spec.rb" with: """ruby describe "this string" do it { should start_with "this" } it { should_not start_with "that" } # deliberate failures it { should_not start_with "this" } it { should start_with "that" } end """ When I run `rspec example_spec.rb` Then the output should contain all of these: | 4 examples, 2 failures | | expected "this string" not to start with "this" | | expected "this string" to start with "that" | Scenario: with an array Given a file named "example_spec.rb" with: """ruby describe [0, 1, 2, 3, 4] do it { should start_with 0 } it { should start_with(0, 1)} it { should_not start_with(2) } it { should_not start_with(0, 1, 2, 3, 4, 5) } # deliberate failures it { should_not start_with 0 } it { should start_with 3 } end """ When I run `rspec example_spec.rb` Then the output should contain all of these: | 6 examples, 2 failures | | expected [0, 1, 2, 3, 4] not to start with 0 | | expected [0, 1, 2, 3, 4] to start with 3 | rspec-expectations-2.14.2/features/built_in_matchers/README.md0000644000004100000410000000506712203207642024246 0ustar www-datawww-data# Built-in Matchers rspec-expectations ships with a number of built-in matchers. Each matcher can be used with `expect(..).to` or `expect(..).not_to` to define positive and negative expectations respectively on an object. Most matchers can also be accessed using the `(...).should` and `(...).should_not` syntax, see [using should syntax](https://github.com/rspec/rspec-expectations/blob/master/Should.md) for why we recommend using `expect`. e.g. expect(result).to eq(3) expect(list).not_to be_empty pi.should be > 3 ## Object identity expect(actual).to be(expected) # passes if actual.equal?(expected) ## Object equivalence expect(actual).to eq(expected) # passes if actual == expected ## Optional APIs for identity/equivalence 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. ## Comparisons expect(actual).to be > expected expect(actual).to be >= expected expect(actual).to be <= expected expect(actual).to be < expected expect(actual).to match(/expression/) expect(actual).to be_within(delta).of(expected) # NOTE: `expect` does not support `=~` matcher. ## Types/classes expect(actual).to be_instance_of(expected) expect(actual).to be_kind_of(expected) ## Truthiness and existentialism expect(actual).to be_true # passes if actual is truthy (not nil or false) expect(actual).to be_false # passes if actual is falsy (nil or false) expect(actual).to be_nil # passes if actual is nil expect(actual).to be # passes if actual is truthy (not nil or false) ## Expecting errors expect { ... }.to raise_error expect { ... }.to raise_error(ErrorClass) expect { ... }.to raise_error("message") expect { ... }.to raise_error(ErrorClass, "message") ## Expecting throws expect { ... }.to throw_symbol expect { ... }.to throw_symbol(:symbol) expect { ... }.to throw_symbol(:symbol, 'value') ## Predicate matchers expect(actual).to be_xxx # passes if actual.xxx? expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg) ### Examples expect([]).to be_empty expect(:a => 1).to have_key(:a) ## Collection membership expect(actual).to include(expected) ### Examples 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") ## Ranges (1.9 only) expect(1..10).to cover(3) rspec-expectations-2.14.2/features/built_in_matchers/have.feature0000644000004100000410000000733512203207642025267 0ustar www-datawww-dataFeature: have(n).items matcher RSpec provides several matchers that make it easy to set expectations about the size of a collection. There are three basic forms: ```ruby collection.should have(x).items collection.should have_at_least(x).items collection.should have_at_most(x).items ``` In addition, #have_exactly is provided as an alias to #have. These work on any collection-like object--the object just needs to respond to #size or #length (or both). When the matcher is called directly on a collection object, the #items call is pure syntactic sugar. You can use anything you want here. These are equivalent: ```ruby collection.should have(x).items collection.should have(x).things ``` You can also use this matcher on a non-collection object that returns a collection from one of its methods. For example, Dir#entries returns an array, so you could set an expectation using the following: ```ruby Dir.new("my/directory").should have(7).entries ``` Scenario: have(x).items on a collection Given a file named "have_items_spec.rb" with: """ruby describe [1, 2, 3] do it { should have(3).items } it { should_not have(2).items } it { should_not have(4).items } it { should have_exactly(3).items } it { should_not have_exactly(2).items } it { should_not have_exactly(4).items } it { should have_at_least(2).items } it { should have_at_most(4).items } # deliberate failures it { should_not have(3).items } it { should have(2).items } it { should have(4).items } it { should_not have_exactly(3).items } it { should have_exactly(2).items } it { should have_exactly(4).items } it { should have_at_least(4).items } it { should have_at_most(2).items } end """ When I run `rspec have_items_spec.rb` Then the output should contain "16 examples, 8 failures" And the output should contain "expected target not to have 3 items, got 3" And the output should contain "expected 2 items, got 3" And the output should contain "expected 4 items, got 3" And the output should contain "expected at least 4 items, got 3" And the output should contain "expected at most 2 items, got 3" Scenario: have(x).words on a String when String#words is defined Given a file named "have_words_spec.rb" with: """ruby class String def words split(' ') end end describe "a sentence with some words" do it { should have(5).words } it { should_not have(4).words } it { should_not have(6).words } it { should have_exactly(5).words } it { should_not have_exactly(4).words } it { should_not have_exactly(6).words } it { should have_at_least(4).words } it { should have_at_most(6).words } # deliberate failures it { should_not have(5).words } it { should have(4).words } it { should have(6).words } it { should_not have_exactly(5).words } it { should have_exactly(4).words } it { should have_exactly(6).words } it { should have_at_least(6).words } it { should have_at_most(4).words } end """ When I run `rspec have_words_spec.rb` Then the output should contain "16 examples, 8 failures" And the output should contain "expected target not to have 5 words, got 5" And the output should contain "expected 4 words, got 5" And the output should contain "expected 6 words, got 5" And the output should contain "expected at least 6 words, got 5" And the output should contain "expected at most 4 words, got 5" rspec-expectations-2.14.2/features/built_in_matchers/be.feature0000644000004100000410000001166612203207642024734 0ustar www-datawww-dataFeature: "be" matchers There are several related "be" matchers: ```ruby obj.should be_true # passes if obj is truthy (not nil or false) obj.should be_false # passes if obj is falsy (nil or false) obj.should be_nil # passes if obj is nil obj.should be # passes if obj is truthy (not nil or false) ``` Scenario: be_true matcher Given a file named "be_true_spec.rb" with: """ruby describe "be_true matcher" do specify { true.should be_true } specify { 7.should be_true } specify { "foo".should be_true } specify { nil.should_not be_true } specify { false.should_not be_true } # deliberate failures specify { true.should_not be_true } specify { 7.should_not be_true } specify { "foo".should_not be_true } specify { nil.should be_true } specify { false.should be_true } end """ When I run `rspec be_true_spec.rb` Then the output should contain "10 examples, 5 failures" And the output should contain: """ expected: non-true value got: true """ And the output should contain: """ expected: non-true value got: 7 """ And the output should contain: """ expected: non-true value got: "foo" """ And the output should contain: """ expected: true value got: nil """ And the output should contain: """ expected: true value got: false """ Scenario: be_false matcher Given a file named "be_false_spec.rb" with: """ruby describe "be_false matcher" do specify { nil.should be_false } specify { false.should be_false } specify { true.should_not be_false } specify { 7.should_not be_false } specify { "foo".should_not be_false } # deliberate failures specify { nil.should_not be_false } specify { false.should_not be_false } specify { true.should be_false } specify { 7.should be_false } specify { "foo".should be_false } end """ When I run `rspec be_false_spec.rb` Then the output should contain "10 examples, 5 failures" And the output should contain: """ expected: non-false value got: nil """ And the output should contain: """ expected: non-false value got: false """ And the output should contain: """ expected: false value got: true """ And the output should contain: """ expected: false value got: 7 """ And the output should contain: """ expected: false value got: "foo" """ Scenario: be_nil matcher Given a file named "be_nil_spec.rb" with: """ruby describe "be_nil matcher" do specify { nil.should be_nil } specify { false.should_not be_nil } specify { true.should_not be_nil } specify { 7.should_not be_nil } specify { "foo".should_not be_nil } # deliberate failures specify { nil.should_not be_nil } specify { false.should be_nil } specify { true.should be_nil } specify { 7.should be_nil } specify { "foo".should be_nil } end """ When I run `rspec be_nil_spec.rb` Then the output should contain "10 examples, 5 failures" And the output should contain: """ expected: not nil got: nil """ And the output should contain: """ expected: nil got: false """ And the output should contain: """ expected: nil got: true """ And the output should contain: """ expected: nil got: 7 """ And the output should contain: """ expected: nil got: "foo" """ Scenario: be matcher Given a file named "be_spec.rb" with: """ruby describe "be_matcher" do specify { true.should be } specify { 7.should be } specify { "foo".should be } specify { nil.should_not be } specify { false.should_not be } # deliberate failures specify { true.should_not be } specify { 7.should_not be } specify { "foo".should_not be } specify { nil.should be } specify { false.should be } end """ When I run `rspec be_spec.rb` Then the output should contain all of these: | 10 examples, 5 failures | | expected true to evaluate to false | | expected 7 to evaluate to false | | expected "foo" to evaluate to false | | expected nil to evaluate to true | | expected false to evaluate to true | rspec-expectations-2.14.2/features/built_in_matchers/expect_change.feature0000644000004100000410000000325012203207642027131 0ustar www-datawww-dataFeature: expect change Expect the execution of a block of code to change the state of an object. Background: Given a file named "lib/counter.rb" with: """ruby class Counter class << self def increment @count ||= 0 @count += 1 end def count @count ||= 0 end end end """ Scenario: expect change Given a file named "spec/example_spec.rb" with: """ruby require "counter" describe Counter, "#increment" do it "should increment the count" do expect { Counter.increment }.to change{Counter.count}.from(0).to(1) end # deliberate failure it "should increment the count by 2" do expect { Counter.increment }.to change{Counter.count}.by(2) end end """ When I run `rspec spec/example_spec.rb` Then the output should contain "1 failure" Then the output should contain "should have been changed by 2, but was changed by 1" Scenario: expect no change Given a file named "spec/example_spec.rb" with: """ruby require "counter" describe Counter, "#increment" do it "should not increment the count by 1 (using not_to)" do expect { Counter.increment }.not_to change{Counter.count} end it "should not increment the count by 1 (using to_not)" do expect { Counter.increment }.to_not change{Counter.count} end end """ When I run `rspec spec/example_spec.rb` Then the output should contain "2 failures" Then the output should contain "should not have changed, but did change from 1 to 2" rspec-expectations-2.14.2/features/built_in_matchers/throw_symbol.feature0000644000004100000410000000742412203207642027073 0ustar www-datawww-dataFeature: throw_symbol matcher The throw_symbol matcher is used to specify that a block of code throws a symbol. The most basic form passes if any symbol is thrown: ```ruby expect { throw :foo }.to throw_symbol ``` You'll often want to specify that a particular symbol is thrown: ```ruby expect { throw :foo }.to throw_symbol(:foo) ``` If you care about the additional argument given to throw, you can specify that as well: ```ruby expect { throw :foo, 7 }.to throw_symbol(:foo, 7) ``` Scenario: basic usage Given a file named "throw_symbol_matcher_spec.rb" with: """ruby describe "throw" do specify { expect { throw :foo }.to throw_symbol } specify { expect { throw :bar, 7 }.to throw_symbol } specify { expect { 5 + 5 }.not_to throw_symbol } # deliberate failures specify { expect { throw :foo }.not_to throw_symbol } specify { expect { throw :bar, 7 }.not_to throw_symbol } specify { expect { 5 + 5 }.to throw_symbol } end """ When I run `rspec throw_symbol_matcher_spec.rb` Then the output should contain all of these: | 6 examples, 3 failures | | expected no Symbol to be thrown, got :foo | | expected no Symbol to be thrown, got :bar | | expected a Symbol to be thrown, got nothing | Scenario: specify thrown symbol Given a file named "throw_symbol_matcher_spec.rb" with: """ruby describe "throw symbol" do specify { expect { throw :foo }.to throw_symbol(:foo) } specify { expect { throw :foo, 7 }.to throw_symbol(:foo) } specify { expect { 5 + 5 }.not_to throw_symbol(:foo) } specify { expect { throw :bar }.not_to throw_symbol(:foo) } # deliberate failures specify { expect { throw :foo }.not_to throw_symbol(:foo) } specify { expect { throw :foo, 7 }.not_to throw_symbol(:foo) } specify { expect { 5 + 5 }.to throw_symbol(:foo) } specify { expect { throw :bar }.to throw_symbol(:foo) } end """ When I run `rspec throw_symbol_matcher_spec.rb` Then the output should contain all of these: | 8 examples, 4 failures | | expected :foo not to be thrown, got :foo | | expected :foo not to be thrown, got :foo with 7 | | expected :foo to be thrown, got nothing | | expected :foo to be thrown, got :bar | Scenario: specify thrown symbol and argument Given a file named "throw_symbol_argument_matcher_spec.rb" with: """ruby describe "throw symbol with argument" do specify { expect { throw :foo, 7 }.to throw_symbol(:foo, 7) } specify { expect { throw :foo, 8 }.not_to throw_symbol(:foo, 7) } specify { expect { throw :bar, 7 }.not_to throw_symbol(:foo, 7) } specify { expect { throw :foo }.not_to throw_symbol(:foo, 7) } # deliberate failures specify { expect { throw :foo, 7 }.not_to throw_symbol(:foo, 7) } specify { expect { throw :foo, 8 }.to throw_symbol(:foo, 7) } specify { expect { throw :bar, 7 }.to throw_symbol(:foo, 7) } specify { expect { throw :foo }.to throw_symbol(:foo, 7) } end """ When I run `rspec throw_symbol_argument_matcher_spec.rb` Then the output should contain all of these: | 8 examples, 4 failures | | expected :foo with 7 not to be thrown, got :foo with 7 | | expected :foo with 7 to be thrown, got :foo with 8 | | expected :foo with 7 to be thrown, got :bar | | expected :foo with 7 to be thrown, got :foo with no argument | rspec-expectations-2.14.2/features/built_in_matchers/respond_to.feature0000644000004100000410000000655212203207642026520 0ustar www-datawww-dataFeature: respond_to matcher Use the respond_to matcher to specify details of an object's interface. In its most basic form: ```ruby obj.should respond_to(:foo) # pass if obj.respond_to?(:foo) ``` You can specify that an object responds to multiple messages in a single statement with multiple arguments passed to the matcher: ```ruby obj.should respond_to(:foo, :bar) # passes if obj.respond_to?(:foo) && obj.respond_to?(:bar) ``` If the number of arguments accepted by the method is important to you, you can specify that as well: ```ruby obj.should respond_to(:foo).with(1).argument obj.should respond_to(:bar).with(2).arguments ``` Note that this matcher relies entirely upon #respond_to?. If an object dynamically responds to a message via #method_missing, but does not indicate this via #respond_to?, then this matcher will give you false results. Scenario: basic usage Given a file named "respond_to_matcher_spec.rb" with: """ruby describe "a string" do it { should respond_to(:length) } it { should respond_to(:hash, :class, :to_s) } it { should_not respond_to(:to_model) } it { should_not respond_to(:compact, :flatten) } # deliberate failures it { should respond_to(:to_model) } it { should respond_to(:compact, :flatten) } it { should_not respond_to(:length) } it { should_not respond_to(:hash, :class, :to_s) } # mixed examples--String responds to :length but not :flatten # both specs should fail it { should respond_to(:length, :flatten) } it { should_not respond_to(:length, :flatten) } end """ When I run `rspec respond_to_matcher_spec.rb` Then the output should contain all of these: | 10 examples, 6 failures | | expected "a string" to respond to :to_model | | expected "a string" to respond to :compact, :flatten | | expected "a string" not to respond to :length | | expected "a string" not to respond to :hash, :class, :to_s | | expected "a string" to respond to :flatten | | expected "a string" not to respond to :length | Scenario: specify arguments Given a file named "respond_to_matcher_argument_checking_spec.rb" with: """ruby describe 7 do it { should respond_to(:zero?).with(0).arguments } it { should_not respond_to(:zero?).with(1).argument } it { should respond_to(:between?).with(2).arguments } it { should_not respond_to(:between?).with(7).arguments } # deliberate failures it { should respond_to(:zero?).with(1).argument } it { should_not respond_to(:zero?).with(0).arguments } it { should respond_to(:between?).with(7).arguments } it { should_not respond_to(:between?).with(2).arguments } end """ When I run `rspec respond_to_matcher_argument_checking_spec.rb` Then the output should contain all of these: | 8 examples, 4 failures | | expected 7 to respond to :zero? with 1 argument | | expected 7 not to respond to :zero? with 0 arguments | | expected 7 to respond to :between? with 7 arguments | | expected 7 not to respond to :between? with 2 arguments | rspec-expectations-2.14.2/features/built_in_matchers/predicates.feature0000644000004100000410000001075512203207642026467 0ustar www-datawww-dataFeature: predicate matchers Ruby objects commonly provide predicate methods: ```ruby 7.zero? # => false 0.zero? # => true [1].empty? # => false [].empty? # => true { :a => 5 }.has_key?(:b) # => false { :b => 5 }.has_key?(:b) # => true ``` You could use a basic equality matcher to set expectations on these: ```ruby 7.zero?.should == true # fails with "expected true, got false (using ==)" ``` ...but RSpec provides dynamic predicate matchers that are more readable and provide better failure output. For any predicate method, RSpec gives you a corresponding matcher. Simply prefix the method with `be_` and remove the question mark. Examples: ```ruby 7.should_not be_zero # calls 7.zero? [].should be_empty # calls [].empty? x.should be_multiple_of(3) # calls x.multiple_of?(3) ``` Alternately, for a predicate method that begins with `has_` like `Hash#has_key?`, RSpec allows you to use an alternate form since `be_has_key` makes no sense. ```ruby hash.should have_key(:foo) # calls hash.has_key?(:foo) array.should_not have_odd_values # calls array.has_odd_values? ``` In either case, RSpec provides nice, clear error messages, such as: `expected zero? to return true, got false` Any arguments passed to the matcher will be passed on to the predicate method. Scenario: should be_zero (based on Fixnum#zero?) Given a file named "should_be_zero_spec.rb" with: """ruby describe 0 do it { should be_zero } end describe 7 do it { should be_zero } # deliberate failure end """ When I run `rspec should_be_zero_spec.rb` Then the output should contain "2 examples, 1 failure" And the output should contain "expected zero? to return true, got false" Scenario: should_not be_empty (based on Array#empty?) Given a file named "should_not_be_empty_spec.rb" with: """ruby describe [1, 2, 3] do it { should_not be_empty } end describe [] do it { should_not be_empty } # deliberate failure end """ When I run `rspec should_not_be_empty_spec.rb` Then the output should contain "2 examples, 1 failure" And the output should contain "expected empty? to return false, got true" Scenario: should have_key (based on Hash#has_key?) Given a file named "should_have_key_spec.rb" with: """ruby describe Hash do subject { { :foo => 7 } } it { should have_key(:foo) } it { should have_key(:bar) } # deliberate failure end """ When I run `rspec should_have_key_spec.rb` Then the output should contain "2 examples, 1 failure" And the output should contain "expected #has_key?(:bar) to return true, got false" Scenario: should_not have_all_string_keys (based on custom #has_all_string_keys? method) Given a file named "should_not_have_all_string_keys_spec.rb" with: """ruby class Hash def has_all_string_keys? keys.all? { |k| String === k } end end describe Hash do context 'with symbol keys' do subject { { :foo => 7, :bar => 5 } } it { should_not have_all_string_keys } end context 'with string keys' do subject { { 'foo' => 7, 'bar' => 5 } } it { should_not have_all_string_keys } # deliberate failure end end """ When I run `rspec should_not_have_all_string_keys_spec.rb` Then the output should contain "2 examples, 1 failure" And the output should contain "expected #has_all_string_keys? to return false, got true" Scenario: matcher arguments are passed on to the predicate method Given a file named "predicate_matcher_argument_spec.rb" with: """ruby class Fixnum def multiple_of?(x) (self % x).zero? end end describe 12 do it { should be_multiple_of(3) } it { should_not be_multiple_of(7) } # deliberate failures it { should_not be_multiple_of(4) } it { should be_multiple_of(5) } end """ When I run `rspec predicate_matcher_argument_spec.rb` Then the output should contain "4 examples, 2 failures" And the output should contain "expected multiple_of?(4) to return false, got true" And the output should contain "expected multiple_of?(5) to return true, got false" rspec-expectations-2.14.2/features/built_in_matchers/be_within.feature0000644000004100000410000000315112203207642026304 0ustar www-datawww-dataFeature: 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 Instead, you should use the be_within matcher to check that the value is within a delta of your expected value: ```ruby area_of_circle.should be_within(0.1).of(28.3) ``` Note that the difference between the actual and expected values must be smaller than your delta; if it is equal, the matcher will fail. Scenario: basic usage Given a file named "be_within_matcher_spec.rb" with: """ruby describe 27.5 do it { should be_within(0.5).of(27.9) } it { should be_within(0.5).of(28.0) } it { should be_within(0.5).of(27.1) } it { should be_within(0.5).of(27.0) } it { should_not be_within(0.5).of(28.1) } it { should_not be_within(0.5).of(26.9) } # deliberate failures it { should_not be_within(0.5).of(28) } it { should_not be_within(0.5).of(27) } it { should be_within(0.5).of(28.1) } it { should be_within(0.5).of(26.9) } end """ When I run `rspec be_within_matcher_spec.rb` Then the output should contain all of these: | 10 examples, 4 failures | | expected 27.5 not to be within 0.5 of 28 | | expected 27.5 not to be within 0.5 of 27 | | expected 27.5 to be within 0.5 of 28.1 | | expected 27.5 to be within 0.5 of 26.9 | rspec-expectations-2.14.2/features/built_in_matchers/include.feature0000644000004100000410000001415012203207642025760 0ustar www-datawww-dataFeature: include matcher Use the include matcher to specify that a collection includes one or more expected objects. This works on any object that responds to #include? (such as a string or array): ```ruby "a string".should include("a") "a string".should include("str") "a string".should include("str", "g") "a string".should_not include("foo") [1, 2].should include(1) [1, 2].should include(1, 2) [1, 2].should_not include(17) ``` The matcher also provides flexible handling for hashes: ```ruby {:a => 1, :b => 2}.should include(:a) {:a => 1, :b => 2}.should include(:a, :b) {:a => 1, :b => 2}.should include(:a => 1) {:a => 1, :b => 2}.should include(:b => 2, :a => 1) {:a => 1, :b => 2}.should_not include(:c) {:a => 1, :b => 2}.should_not include(:a => 2) {:a => 1, :b => 2}.should_not include(:c => 3) ``` Scenario: array usage Given a file named "array_include_matcher_spec.rb" with: """ruby describe [1, 3, 7] do it { should include(1) } it { should include(3) } it { should include(7) } it { should include(1, 7) } it { should include(1, 3, 7) } it { should_not include(17) } it { should_not include(43, 100) } # deliberate failures it { should include(4) } it { should_not include(1) } it { should_not include(3) } it { should_not include(7) } it { should_not include(1, 3, 7) } # both of these should fail since it includes 1 but not 9 it { should include(1, 9) } it { should_not include(1, 9) } end """ When I run `rspec array_include_matcher_spec.rb` Then the output should contain all of these: | 14 examples, 7 failures | | expected [1, 3, 7] to include 4 | | expected [1, 3, 7] not to include 1 | | expected [1, 3, 7] not to include 3 | | expected [1, 3, 7] not to include 7 | | expected [1, 3, 7] not to include 1, 3, and 7 | | expected [1, 3, 7] to include 1 and 9 | | expected [1, 3, 7] not to include 1 and 9 | Scenario: string usage Given a file named "string_include_matcher_spec.rb" with: """ruby describe "a string" do it { should include("str") } it { should include("a", "str", "ng") } it { should_not include("foo") } it { should_not include("foo", "bar") } # deliberate failures it { should include("foo") } it { should_not include("str") } it { should include("str", "foo") } it { should_not include("str", "foo") } end """ When I run `rspec string_include_matcher_spec.rb` Then the output should contain all of these: | 8 examples, 4 failures | | expected "a string" to include "foo" | | expected "a string" not to include "str" | | expected "a string" to include "str" and "foo" | | expected "a string" not to include "str" and "foo" | Scenario: hash usage Given a file named "hash_include_matcher_spec.rb" with: """ruby describe Hash do subject { { :a => 7, :b => 5 } } it { should include(:a) } it { should include(:b, :a) } it { should include(:a => 7) } it { should include(:b => 5, :a => 7) } it { should_not include(:c) } it { should_not include(:c, :d) } it { should_not include(:d => 2) } it { should_not include(:a => 5) } it { should_not include(:b => 7, :a => 5) } # deliberate failures it { should_not include(:a) } it { should_not include(:b, :a) } it { should_not include(:a => 7) } it { should_not include(:a => 7, :b => 5) } it { should include(:c) } it { should include(:c, :d) } it { should include(:d => 2) } it { should include(:a => 5) } it { should include(:a => 5, :b => 7) } # Mixed cases--the hash includes one but not the other. # All 4 of these cases should fail. it { should include(:a, :d) } it { should_not include(:a, :d) } it { should include(:a => 7, :d => 3) } it { should_not include(:a => 7, :d => 3) } end """ When I run `rspec hash_include_matcher_spec.rb` Then the output should contain "13 failure" Scenario: fuzzy usage with matchers Given a file named "fuzzy_include_matcher_spec.rb" with: """ require 'ostruct' class User < OpenStruct def inspect name end end RSpec::Matchers.define :a_user_named do |expected| match do |actual| actual.is_a?(User) && (actual.name == expected) end description do "a user named '#{expected}'" end end describe "Collection of users" do subject do [User.new(:name => "Joe"), User.new(:name => "Fred"), User.new(:name => "John"), User.new(:name => "Luke"), User.new(:name => "David")] end it { should include( a_user_named "Joe" ) } it { should include( a_user_named "Luke" ) } it { should_not include( a_user_named "Richard" ) } it { should_not include( a_user_named "Hayley" ) } # deliberate failures it { should include( a_user_named "Richard" ) } it { should_not include( a_user_named "Fred" ) } it { should include( a_user_named "Sarah" ) } it { should_not include( a_user_named "Luke" ) } end """ When I run `rspec fuzzy_include_matcher_spec.rb` Then the output should contain all of these: | 8 examples, 4 failures | | expected [Joe, Fred, John, Luke, David] to include a user named 'Richard' | | expected [Joe, Fred, John, Luke, David] not to include a user named 'Fred' | | expected [Joe, Fred, John, Luke, David] to include a user named 'Sarah' | | expected [Joe, Fred, John, Luke, David] not to include a user named 'Luke' | rspec-expectations-2.14.2/features/built_in_matchers/equality.feature0000644000004100000410000001010012203207642026161 0ustar www-datawww-dataFeature: equality matchers Ruby exposes several different methods for handling equality: a.equal?(b) # object identity - a and b refer to the same object a.eql?(b) # object equivalence - a and b have the same value a == b # object equivalence - a and b have the same value with type conversions Note that these descriptions are guidelines but are not forced by the language. Any object can implement any of these methods with its own semantics. rspec-expectations ships with matchers that align with each of these methods: ```ruby a.should equal(b) # passes if a.equal?(b) a.should eql(b) # passes if a.eql?(b) a.should == b # passes if a == b ``` It also ships with two matchers that have more of a DSL feel to them: ```ruby a.should be(b) # passes if a.equal?(b) a.should eq(b) # passes if a == b ``` These are a useful pair if you wish to avoid the warning that Ruby emits on `a.should == b` Scenario: compare using eq (==) Given a file named "compare_using_eq.rb" with: """ruby describe "a string" do it "is equal to another string of the same value" do "this string".should eq("this string") end it "is not equal to another string of a different value" do "this string".should_not eq("a different string") end end describe "an integer" do it "is equal to a float of the same value" do 5.should eq(5.0) end end """ When I run `rspec compare_using_eq.rb` Then the output should contain "3 examples, 0 failures" Scenario: compare using == Given a file named "compare_using_==.rb" with: """ruby describe "a string" do it "is equal to another string of the same value" do "this string".should == "this string" end it "is not equal to another string of a different value" do "this string".should_not == "a different string" end end describe "an integer" do it "is equal to a float of the same value" do 5.should == 5.0 end end """ When I run `rspec compare_using_==.rb` Then the output should contain "3 examples, 0 failures" Scenario: compare using eql (eql?) Given a file named "compare_using_eql.rb" with: """ruby describe "an integer" do it "is equal to another integer of the same value" do 5.should eql(5) end it "is not equal to another integer of a different value" do 5.should_not eql(6) end it "is not equal to a float of the same value" do 5.should_not eql(5.0) end end """ When I run `rspec compare_using_eql.rb` Then the output should contain "3 examples, 0 failures" Scenario: compare using equal (equal?) Given a file named "compare_using_equal.rb" with: """ruby describe "a string" do it "is equal to itself" do string = "this string" string.should equal(string) end it "is not equal to another string of the same value" do "this string".should_not equal("this string") end it "is not equal to another string of a different value" do "this string".should_not equal("a different string") end end """ When I run `rspec compare_using_equal.rb` Then the output should contain "3 examples, 0 failures" Scenario: compare using be (equal?) Given a file named "compare_using_be.rb" with: """ruby describe "a string" do it "is equal to itself" do string = "this string" string.should be(string) end it "is not equal to another string of the same value" do "this string".should_not be("this string") end it "is not equal to another string of a different value" do "this string".should_not be("a different string") end end """ When I run `rspec compare_using_be.rb` Then the output should contain "3 examples, 0 failures" rspec-expectations-2.14.2/features/built_in_matchers/types.feature0000644000004100000410000001007112203207642025477 0ustar www-datawww-dataFeature: specify types of objects rspec-expectations includes two matchers to specify types of objects: * `obj.should be_kind_of(type)`: calls `obj.kind_of?(type)`, which returns true if type is in obj's class hierarchy or is a module and is included in a class in obj's class hierarchy. * `obj.should be_instance_of(type)`: calls `obj.instance_of?(type)`, which returns true if and only if type if obj's class. Both of these matchers have aliases: ```ruby obj.should be_a_kind_of(type) # same as obj.should be_kind_of(type) obj.should be_a(type) # same as obj.should be_kind_of(type) obj.should be_an(type) # same as obj.should be_kind_of(type) obj.should be_an_instance_of(type) # same as obj.should be_instance_of(type) ``` Scenario: be_(a_)kind_of matcher Given a file named "be_kind_of_matcher_spec.rb" with: """ruby module MyModule; end class Fixnum include MyModule end describe 17 do # the actual class it { should be_kind_of(Fixnum) } it { should be_a_kind_of(Fixnum) } it { should be_a(Fixnum) } # the superclass it { should be_kind_of(Integer) } it { should be_a_kind_of(Integer) } it { should be_an(Integer) } # an included module it { should be_kind_of(MyModule) } it { should be_a_kind_of(MyModule) } it { should be_a(MyModule) } # negative passing case it { should_not be_kind_of(String) } it { should_not be_a_kind_of(String) } it { should_not be_a(String) } # deliberate failures it { should_not be_kind_of(Fixnum) } it { should_not be_a_kind_of(Fixnum) } it { should_not be_a(Fixnum) } it { should_not be_kind_of(Integer) } it { should_not be_a_kind_of(Integer) } it { should_not be_an(Integer) } it { should_not be_kind_of(MyModule) } it { should_not be_a_kind_of(MyModule) } it { should_not be_a(MyModule) } it { should be_kind_of(String) } it { should be_a_kind_of(String) } it { should be_a(String) } end """ When I run `rspec be_kind_of_matcher_spec.rb` Then the output should contain all of these: | 24 examples, 12 failures | | expected 17 not to be a kind of Fixnum | | expected 17 not to be a kind of Integer | | expected 17 not to be a kind of MyModule | | expected 17 to be a kind of String | Scenario: be_(an_)instance_of matcher Given a file named "be_instance_of_matcher_spec.rb" with: """ruby module MyModule; end class Fixnum include MyModule end describe 17 do # the actual class it { should be_instance_of(Fixnum) } it { should be_an_instance_of(Fixnum) } # the superclass it { should_not be_instance_of(Integer) } it { should_not be_an_instance_of(Integer) } # an included module it { should_not be_instance_of(MyModule) } it { should_not be_an_instance_of(MyModule) } # another class with no relation to the subject's hierarchy it { should_not be_instance_of(String) } it { should_not be_an_instance_of(String) } # deliberate failures it { should_not be_instance_of(Fixnum) } it { should_not be_an_instance_of(Fixnum) } it { should be_instance_of(Integer) } it { should be_an_instance_of(Integer) } it { should be_instance_of(MyModule) } it { should be_an_instance_of(MyModule) } it { should be_instance_of(String) } it { should be_an_instance_of(String) } end """ When I run `rspec be_instance_of_matcher_spec.rb` Then the output should contain all of these: | 16 examples, 8 failures | | expected 17 not to be an instance of Fixnum | | expected 17 to be an instance of Integer | | expected 17 to be an instance of MyModule | | expected 17 to be an instance of String | rspec-expectations-2.14.2/features/built_in_matchers/end_with.feature0000644000004100000410000000277612203207642026151 0ustar www-datawww-dataFeature: end_with matcher Use the `end_with` matcher to specify that a string or array ends with the expected characters or elements. ```ruby "this string".should end_with "string" "this string".should_not end_with "stringy" [0, 1, 2].should end_with 1, 2 ``` Scenario: string usage Given a file named "example_spec.rb" with: """ruby describe "this string" do it { should end_with "string" } it { should_not end_with "stringy" } # deliberate failures it { should_not end_with "string" } it { should end_with "stringy" } end """ When I run `rspec example_spec.rb` Then the output should contain all of these: | 4 examples, 2 failures | | expected "this string" not to end with "string" | | expected "this string" to end with "stringy" | Scenario: array usage Given a file named "example_spec.rb" with: """ruby describe [0, 1, 2, 3, 4] do it { should end_with 4 } it { should end_with 3, 4 } it { should_not end_with 3 } it { should_not end_with 0, 1, 2, 3, 4, 5 } # deliberate failures it { should_not end_with 4 } it { should end_with 3 } end """ When I run `rspec example_spec.rb` Then the output should contain all of these: | 6 examples, 2 failures | | expected [0, 1, 2, 3, 4] not to end with 4 | | expected [0, 1, 2, 3, 4] to end with 3 | rspec-expectations-2.14.2/features/built_in_matchers/operators.feature0000644000004100000410000001510012203207642026347 0ustar www-datawww-dataFeature: operator matchers RSpec provides a number of matchers that are based on Ruby's built-in operators. These pretty much work like you expect. For example, each of these pass: ```ruby 7.should == 7 [1, 2, 3].should == [1, 2, 3] "this is a string".should =~ /^this/ "this is a string".should_not =~ /^that/ String.should === "this is a string" ``` You can also use comparison operators combined with the "be" matcher like this: ```ruby 37.should be < 100 37.should be <= 38 37.should be >= 2 37.should be > 7 ``` RSpec also provides a `=~` matcher for arrays that disregards differences in the ording between the actual and expected array. For example: ```ruby [1, 2, 3].should =~ [2, 3, 1] # pass [:a, :c, :b].should =~ [:a, :c] # fail ``` Scenario: numeric operator matchers Given a file named "numeric_operator_matchers_spec.rb" with: """ruby describe 18 do it { should == 18 } it { should be < 20 } it { should be > 15 } it { should be <= 19 } it { should be >= 17 } it { should_not == 28 } # deliberate failures it { should == 28 } it { should be < 15 } it { should be > 20 } it { should be <= 17 } it { should be >= 19 } it { should_not == 18 } end """ When I run `rspec numeric_operator_matchers_spec.rb` Then the output should contain "12 examples, 6 failures" And the output should contain: """ Failure/Error: it { should == 28 } expected: 28 got: 18 (using ==) """ And the output should contain: """ Failure/Error: it { should be < 15 } expected: < 15 got: 18 """ And the output should contain: """ Failure/Error: it { should be > 20 } expected: > 20 got: 18 """ And the output should contain: """ Failure/Error: it { should be <= 17 } expected: <= 17 got: 18 """ And the output should contain: """ Failure/Error: it { should be >= 19 } expected: >= 19 got: 18 """ And the output should contain: """ Failure/Error: it { should_not == 18 } expected not: == 18 got: 18 """ Scenario: string operator matchers Given a file named "string_operator_matchers_spec.rb" with: """ruby describe "Strawberry" do it { should == "Strawberry" } it { should be < "Tomato" } it { should be > "Apple" } it { should be <= "Turnip" } it { should be >= "Banana" } it { should =~ /berry/ } it { should_not == "Peach" } it { should_not =~ /apple/ } it "reports that it is a string using ===" do String.should === subject end # deliberate failures it { should == "Peach" } it { should be < "Cranberry" } it { should be > "Zuchini" } it { should be <= "Potato" } it { should be >= "Tomato" } it { should =~ /apple/ } it { should_not == "Strawberry" } it { should_not =~ /berry/ } it "fails a spec asserting that it is a symbol" do Symbol.should === subject end end """ When I run `rspec string_operator_matchers_spec.rb` Then the output should contain "18 examples, 9 failures" And the output should contain: """ Failure/Error: it { should == "Peach" } expected: "Peach" got: "Strawberry" (using ==) """ And the output should contain: """ Failure/Error: it { should be < "Cranberry" } expected: < "Cranberry" got: "Strawberry" """ And the output should contain: """ Failure/Error: it { should be > "Zuchini" } expected: > "Zuchini" got: "Strawberry" """ And the output should contain: """ Failure/Error: it { should be <= "Potato" } expected: <= "Potato" got: "Strawberry" """ And the output should contain: """ Failure/Error: it { should be >= "Tomato" } expected: >= "Tomato" got: "Strawberry" """ And the output should contain: """ Failure/Error: it { should =~ /apple/ } expected: /apple/ got: "Strawberry" (using =~) """ And the output should contain: """ Failure/Error: it { should_not == "Strawberry" } expected not: == "Strawberry" got: "Strawberry" """ And the output should contain: """ Failure/Error: it { should_not =~ /berry/ } expected not: =~ /berry/ got: "Strawberry" """ And the output should contain: """ Failure/Error: Symbol.should === subject expected: "Strawberry" got: Symbol (using ===) """ Scenario: array operator matchers Given a file named "array_operator_matchers_spec.rb" with: """ruby describe [1, 2, 3] do it { should == [1, 2, 3] } it { should_not == [1, 3, 2] } it { should =~ [1, 2, 3] } it { should =~ [1, 3, 2] } it { should =~ [2, 1, 3] } it { should =~ [2, 3, 1] } it { should =~ [3, 1, 2] } it { should =~ [3, 2, 1] } # deliberate failures it { should_not == [1, 2, 3] } it { should == [1, 3, 2] } it { should =~ [1, 2, 1] } end """ When I run `rspec array_operator_matchers_spec.rb` Then the output should contain "11 examples, 3 failures" And the output should contain: """ Failure/Error: it { should_not == [1, 2, 3] } expected not: == [1, 2, 3] got: [1, 2, 3] """ And the output should contain: """ Failure/Error: it { should == [1, 3, 2] } expected: [1, 3, 2] got: [1, 2, 3] (using ==) """ And the output should contain: """ Failure/Error: it { should =~ [1, 2, 1] } expected collection contained: [1, 1, 2] actual collection contained: [1, 2, 3] the missing elements were: [1] the extra elements were: [3] """ rspec-expectations-2.14.2/features/built_in_matchers/expect_error.feature0000644000004100000410000001037512203207642027043 0ustar www-datawww-dataFeature: raise_error matcher Use the `raise_error` matcher to specify that a block of code raises an error. The most basic form passes if any error is thrown: ```ruby expect { raise StandardError }.to raise_error ``` You can use `raise_exception` instead if you prefer that wording: ```ruby expect { 3 / 0 }.to raise_exception ``` `raise_error` and `raise_exception` are functionally interchangeable, so use the one that makes the most sense to you in any given context. In addition to the basic form, above, there are a number of ways to specify details of an error/exception: ```ruby expect { raise "oops" }.to raise_error expect { raise "oops" }.to raise_error(RuntimeError) expect { raise "oops" }.to raise_error("oops") expect { raise "oops" }.to raise_error(/op/) expect { raise "oops" }.to raise_error(RuntimeError, "oops") expect { raise "oops" }.to raise_error(RuntimeError, /op/) ``` Scenario: expect any error Given a file named "example_spec" with: """ describe "calling a missing method" do it "raises" do expect { Object.new.foo }.to raise_error end end """ When I run `rspec example_spec` Then the example should pass Scenario: expect specific error Given a file named "example_spec" with: """ describe "calling a missing method" do it "raises" do expect { Object.new.foo }.to raise_error(NameError) end end """ When I run `rspec example_spec` Then the example should pass Scenario: match message with a string Given a file named "example_spec.rb" with: """ruby describe "matching error message with string" do it "matches the error message" do expect { raise StandardError, 'this message exactly'}. to raise_error('this message exactly') end end """ When I run `rspec example_spec.rb` Then the example should pass Scenario: match message with a regexp Given a file named "example_spec.rb" with: """ruby describe "matching error message with regex" do it "matches the error message" do expect { raise StandardError, "my message" }. to raise_error(/my mess/) end end """ When I run `rspec example_spec.rb` Then the example should pass Scenario: match type + message with string Given a file named "example_spec.rb" with: """ruby describe "matching error message with string" do it "matches the error message" do expect { raise StandardError, 'this message exactly'}. to raise_error(StandardError, 'this message exactly') end end """ When I run `rspec example_spec.rb` Then the example should pass Scenario: match type + message with regexp Given a file named "example_spec.rb" with: """ruby describe "matching error message with regex" do it "matches the error message" do expect { raise StandardError, "my message" }. to raise_error(StandardError, /my mess/) end end """ When I run `rspec example_spec.rb` Then the example should pass Scenario: set expectations on error object passed to block Given a file named "example_spec" with: """ describe "#foo" do it "raises NameError" do expect { Object.new.foo }.to raise_error { |error| error.should be_a(NameError) } end end """ When I run `rspec example_spec` Then the example should pass Scenario: expect no occurence of a specific error Given a file named "example_spec" with: """ describe Object, "#public_instance_methods" do it "does not raise" do expect { Object.public_instance_methods }. not_to raise_error(NameError) end end """ When I run `rspec example_spec` Then the example should pass Scenario: expect no error at all Given a file named "example_spec" with: """ describe "#to_s" do it "does not raise" do expect { Object.new.to_s }.not_to raise_error end end """ When I run `rspec example_spec` Then the example should pass rspec-expectations-2.14.2/features/Upgrade.md0000644000004100000410000000220512203207642021174 0ustar www-datawww-data# Upgrade to rspec-expectations-2.0 ## What's new ### New `eq` matcher. `RSpec::Matchers` now offers you two approaches to differentiating between object identity. You can use the rspec-1 approach: actual.should == expected # object equality actual.should equal(expected) # object identity ... or, if you prefer: actual.should eq(expected) # object equality actual.should be(expected) # object identity ## What's been removed ### simple_matcher Use RSpec::Matchers.define instead. For example, if you had: def eat_cheese simple_matcher("eat cheese") do |actual| actual.eat?(:cheese) end end Change it to: RSpec::Matchers.define :eat_cheese do match do |actual| actual.eat?(:cheese) end end ### wrap_expectation Use RSpec::Matchers.define instead. RSpec::Matchers.define :eat_cheese do match do |actual| actual.should eat?(:cheese) end end RSpec::Matchers.define :eat_cheese do include MyCheesyAssertions match_unless_raises Test::Unit::AssertionFailedError do |actual| assert_eats_chesse actual end end rspec-expectations-2.14.2/features/test_frameworks/0000755000004100000410000000000012203207642022503 5ustar www-datawww-datarspec-expectations-2.14.2/features/test_frameworks/test_unit.feature0000644000004100000410000000316612203207642026104 0ustar www-datawww-dataFeature: Test::Unit integration RSpec-expectations is a stand-alone gem that can be used without the rest of RSpec. If you like the way Test::Unit (or MiniTest) organizes tests, but prefer RSpec's approach to expressing expectations, you can have both. The one downside is that failures are reported as errors with MiniTest. Scenario: use rspec/expectations with Test::Unit Given a file named "rspec_expectations_test.rb" with: """ruby require 'test/unit' require 'rspec/expectations' class RSpecExpectationsTest < Test::Unit::TestCase RSpec::Matchers.define :be_an_integer do match { |actual| Integer === actual } end def be_an_int # This is actually an internal rspec-expectations API, but is used # here to demonstrate that deprecation warnings from within # rspec-expectations work correcty without depending on rspec-core RSpec.deprecate(:be_an_int, :replacement => :be_an_integer) be_an_integer end def test_passing_expectation expect(1 + 3).to eq 4 end def test_failing_expectation expect([1,2]).to be_empty end def test_custom_matcher_with_deprecation_warning expect(1).to be_an_int end end """ When I run `ruby rspec_expectations_test.rb` Then the output should contain "3 tests, 0 assertions, 0 failures, 1 errors" or "3 tests, 0 assertions, 1 failures, 0 errors" And the output should contain "expected empty? to return true, got false" And the output should contain "be_an_int is deprecated" rspec-expectations-2.14.2/features/README.md0000644000004100000410000000264012203207642020545 0ustar www-datawww-datarspec-expectations is used to define expected outcomes. describe Account do it "has a balance of zero when first created" do expect(Account.new.balance).to eq(Money.new(0)) end end ## Basic structure The basic structure of an rspec expectation is: expect(actual).to matcher(expected) expect(actual).not_to matcher(expected) Note: You can also use `expect(..).to_not` instead of `expect(..).not_to`. One is an alias to the other, so you can use whichever reads better to you. #### Examples expect(5).to eq(5) expect(5).not_to eq(4) ## What is a matcher? A Matcher is any object that responds to the following methods: matches?(actual) failure_message_for_should These methods are also part of the matcher protocol, but are optional: does_not_match?(actual) failure_message_for_should_not description RSpec ships with a number of built-in matchers and a DSL for writing custom matchers. ## Issues The documentation for rspec-expectations is a work in progress. We'll be adding Cucumber features over time, and clarifying existing ones. If you have specific features you'd like to see added, find the existing documentation incomplete or confusing, or, better yet, wish to write a missing Cucumber feature yourself, please [submit an issue](http://github.com/rspec/rspec-expectations/issues) or a [pull request](http://github.com/rspec/rspec-expectations). rspec-expectations-2.14.2/features/implicit_docstrings.feature0000644000004100000410000000267512203207642024724 0ustar www-datawww-dataFeature: implicit docstrings As an RSpec user I want examples to generate their own names So that I can reduce duplication between example names and example code Scenario: run passing examples Given a file named "implicit_docstrings_spec.rb" with: """ruby describe "Examples with no docstrings generate their own:" do specify { 3.should be < 5 } specify { [1,2,3].should include(2) } specify { [1,2,3].should respond_to(:size) } end """ When I run `rspec ./implicit_docstrings_spec.rb -fdoc` Then the output should contain "should be < 5" And the output should contain "should include 2" And the output should contain "should respond to #size" Scenario: run failing examples Given a file named "failing_implicit_docstrings_spec.rb" with: """ruby describe "Failing examples with no descriptions" do # description is auto-generated as "should equal(5)" based on the last #should it do 3.should equal(2) 5.should equal(5) end it { 3.should be > 5 } it { [1,2,3].should include(4) } it { [1,2,3].should_not respond_to(:size) } end """ When I run `rspec ./failing_implicit_docstrings_spec.rb -fdoc` Then the output should contain "should equal 2" And the output should contain "should be > 5" And the output should contain "should include 4" And the output should contain "should not respond to #size" rspec-expectations-2.14.2/features/customized_message.feature0000644000004100000410000000145312203207642024536 0ustar www-datawww-dataFeature: customized message RSpec tries to provide useful failure messages, but for cases in which you want more specific information, you can define your own message right in the example. This works for any matcher _other than the operator matchers_. Scenario: customize failure message Given a file named "example_spec.rb" with: """ruby describe Array do context "when created with `new`" do it "is empty" do array = Array.new array << 1 # trigger a failure to demonstrate the message array.should be_empty, "expected empty array, got #{array.inspect}" end end end """ When I run `rspec example_spec.rb --format documentation` Then the output should contain "expected empty array, got [1]" rspec-expectations-2.14.2/features/syntax_configuration.feature0000644000004100000410000000432312203207642025120 0ustar www-datawww-dataFeature: Syntax Configuration In addition to the long-supported `should` syntax, rspec-expectations supports an alternate `expect` syntax. If you want your project to only use one syntax, you can configure the available syntaxes. Background: Given a file named "syntaxes_spec.rb" with: """ruby describe "using the should syntax" do specify { 3.should eq(3) } specify { 3.should_not eq(4) } specify { lambda { raise "boom" }.should raise_error("boom") } specify { lambda { }.should_not raise_error } end describe "using the expect syntax" do specify { expect(3).to eq(3) } specify { expect(3).not_to eq(4) } specify { expect { raise "boom" }.to raise_error("boom") } specify { expect { }.not_to raise_error } end """ Scenario: Both syntaxes are available by default When I run `rspec syntaxes_spec.rb` Then the examples should all pass Scenario: Disable should syntax Given a file named "disable_should_syntax.rb" with: """ruby RSpec.configure do |config| config.expect_with :rspec do |c| c.syntax = :expect end end """ When I run `rspec disable_should_syntax.rb syntaxes_spec.rb` Then the output should contain all of these: | 8 examples, 4 failures | | undefined method `should' | Scenario: Disable expect syntax Given a file named "disable_expect_syntax.rb" with: """ruby RSpec.configure do |config| config.expect_with :rspec do |c| c.syntax = :should end config.mock_with :rspec do |c| c.syntax = :should end end """ When I run `rspec disable_expect_syntax.rb syntaxes_spec.rb` Then the output should contain all of these: | 8 examples, 4 failures | | undefined method `expect' | Scenario: Explicitly enable both syntaxes Given a file named "enable_both_syntaxes.rb" with: """ruby RSpec.configure do |config| config.expect_with :rspec do |c| c.syntax = [:should, :expect] end end """ When I run `rspec enable_both_syntaxes.rb syntaxes_spec.rb` Then the examples should all pass rspec-expectations-2.14.2/features/step_definitions/0000755000004100000410000000000012203207642022632 5ustar www-datawww-datarspec-expectations-2.14.2/features/step_definitions/additional_cli_steps.rb0000644000004100000410000000135412203207642027337 0ustar www-datawww-data# Useful for when the output is slightly different on different versions of ruby Then /^the output should contain "([^"]*)" or "([^"]*)"$/ do |string1, string2| unless [string1, string2].any? { |s| all_output =~ regexp(s) } fail %Q{Neither "#{string1}" or "#{string2}" were found in:\n#{all_output}} end end Then /^the output should contain all of these:$/ do |table| table.raw.flatten.each do |string| assert_partial_output(string, all_output) end end Then /^the example(?:s)? should(?: all)? pass$/ do step %q{the output should contain "0 failures"} step %q{the exit status should be 0} end Then /^the example should fail$/ do step %q{the output should contain "1 failure"} step %q{the exit status should not be 0} end rspec-expectations-2.14.2/features/support/0000755000004100000410000000000012203207642021000 5ustar www-datawww-datarspec-expectations-2.14.2/features/support/env.rb0000644000004100000410000000052312203207642022115 0ustar www-datawww-datarequire 'aruba/cucumber' timeouts = { 'java' => 60 } Before do @aruba_timeout_seconds = timeouts.fetch(RUBY_PLATFORM) { 15 } end Aruba.configure do |config| config.before_cmd do |cmd| set_env('JRUBY_OPTS', "-X-C #{ENV['JRUBY_OPTS']}") # disable JIT since these processes are so short lived end end if RUBY_PLATFORM == 'java' rspec-expectations-2.14.2/features/support/rubinius.rb0000644000004100000410000000034112203207642023163 0ustar www-datawww-data# Required until https://github.com/rubinius/rubinius/issues/2430 is resolved ENV['RBXOPT'] = "#{ENV["RBXOPT"]} -Xcompiler.no_rbc" Around "@unsupported-on-rbx" do |scenario, block| block.call unless defined?(Rubinius) end rspec-expectations-2.14.2/spec/0000755000004100000410000000000012203207642016400 5ustar www-datawww-datarspec-expectations-2.14.2/spec/spec_helper.rb0000644000004100000410000000234012203207642021215 0ustar www-datawww-dataDir['./spec/support/**/*'].each {|f| require f} RSpec::configure do |config| config.treat_symbols_as_metadata_keys_with_true_values = true config.color_enabled = true config.filter_run :focused config.run_all_when_everything_filtered = true config.order = :random config.expect_with :rspec do |expectations| $default_expectation_syntax = expectations.syntax expectations.syntax = :expect end end shared_context "with #should enabled", :uses_should do orig_syntax = nil before(:all) do orig_syntax = RSpec::Matchers.configuration.syntax RSpec::Matchers.configuration.syntax = [:expect, :should] end after(:all) do RSpec::Matchers.configuration.syntax = orig_syntax end end shared_context "with #should exclusively enabled", :uses_only_should do orig_syntax = nil before(:all) do orig_syntax = RSpec::Matchers.configuration.syntax RSpec::Matchers.configuration.syntax = :should end after(:all) do RSpec::Matchers.configuration.syntax = orig_syntax end end module TestUnitIntegrationSupport include InSubProcess def with_test_unit_loaded in_sub_process do require 'test/unit' load 'rspec/matchers/test_unit_integration.rb' yield end end end rspec-expectations-2.14.2/spec/rspec/0000755000004100000410000000000012203207642017514 5ustar www-datawww-datarspec-expectations-2.14.2/spec/rspec/expectations/0000755000004100000410000000000012203207642022222 5ustar www-datawww-datarspec-expectations-2.14.2/spec/rspec/expectations/syntax_spec.rb0000644000004100000410000001135512203207642025114 0ustar www-datawww-datarequire 'spec_helper' module RSpec module Expectations describe Syntax do context "when passing a message to an expectation" do let(:warner) { ::Kernel } let(:string_like_object) do Struct.new(:to_str, :to_s).new(*(["Ceci n'est pas une Chaine."]*2)) end let(:insufficiently_string_like_object) do Struct.new(:to_s).new("Ceci n'est pas une Chaine.") end let(:callable_object) do Struct.new(:call).new("Ceci n'est pas une Chaine.") end describe "expect(...).to" do it "prints a warning when the message object isn't a String" do warner.should_receive(:warn).with(/ignoring.*message/) expect(3).to eq(3), :not_a_string end it "doesn't print a warning when message is a String" do warner.should_not_receive(:warn) expect(3).to eq(3), "a string" end it "doesn't print a warning when message responds to to_str" do warner.should_not_receive(:warn) expect(3).to eq(3), string_like_object end it "prints a warning when the message object handles to_s but not to_str" do warner.should_receive(:warn).with(/ignoring.*message/) expect(3).to eq(3), insufficiently_string_like_object end it "doesn't print a warning when message responds to call" do warner.should_not_receive(:warn) expect(3).to eq(3), callable_object end end describe "expect(...).not_to" do it "prints a warning when the message object isn't a String" do warner.should_receive(:warn).with(/ignoring.*message/) expect(3).not_to eq(4), :not_a_string end it "doesn't print a warning when message is a String" do warner.should_not_receive(:warn) expect(3).not_to eq(4), "a string" end it "doesn't print a warning when message responds to to_str" do warner.should_not_receive(:warn) expect(3).not_to eq(4), string_like_object end it "prints a warning when the message object handles to_s but not to_str" do warner.should_receive(:warn).with(/ignoring.*message/) expect(3).not_to eq(4), insufficiently_string_like_object end it "doesn't print a warning when message responds to call" do warner.should_not_receive(:warn) expect(3).not_to eq(4), callable_object end end end describe "expression generation" do let(:target) { "foo" } let(:expectation) { "eq('bar')" } let(:positive_expect_example) { "expect(foo).to eq('bar')" } let(:positive_should_example) { "foo.should eq('bar')" } let(:negative_expect_example) { "expect(foo).not_to eq('bar')" } let(:negative_should_example) { "foo.should_not eq('bar')" } def positive_expression Syntax.positive_expression(target, expectation) end def negative_expression Syntax.negative_expression(target, expectation) end context "when only :expect is enabled" do before do expect(Syntax.should_enabled?).to be_false expect(Syntax.expect_enabled?).to be_true end it 'generates a positive expression using the expect syntax' do expect(positive_expression).to eq(positive_expect_example) end it 'generates a negative expression using the expect syntax' do expect(negative_expression).to eq(negative_expect_example) end end context "when both :should and :expect are enabled", :uses_should do before do expect(Syntax.should_enabled?).to be_true expect(Syntax.expect_enabled?).to be_true end it 'generates a positive expression using the expect syntax' do expect(positive_expression).to eq(positive_expect_example) end it 'generates a negative expression using the expect syntax' do expect(negative_expression).to eq(negative_expect_example) end end context "when only :should is enabled", :uses_only_should do before do Syntax.should_enabled?.should be_true Syntax.expect_enabled?.should be_false end it 'generates a positive expression using the expect syntax' do positive_expression.should eq(positive_should_example) end it 'generates a negative expression using the expect syntax' do negative_expression.should eq(negative_should_example) end end end end end end rspec-expectations-2.14.2/spec/rspec/expectations/handler_spec.rb0000644000004100000410000002015712203207642025203 0ustar www-datawww-datarequire 'spec_helper' module ExampleExpectations class ArbitraryMatcher def initialize(*args, &block) if args.last.is_a? Hash @expected = args.last[:expected] end @expected = block.call if block @block = block end def matches?(target) @target = target return @expected == target end def with(new_value) @expected = new_value self end def failure_message "expected #{@expected}, got #{@target}" end def negative_failure_message "expected not #{@expected}, got #{@target}" end end class PositiveOnlyMatcher < ArbitraryMatcher undef negative_failure_message rescue nil end def arbitrary_matcher(*args, &block) ArbitraryMatcher.new(*args, &block) end def positive_only_matcher(*args, &block) PositiveOnlyMatcher.new(*args, &block) end end module RSpec module Expectations describe PositiveExpectationHandler do describe "#handle_matcher" do it "asks the matcher if it matches" do matcher = double("matcher") actual = Object.new matcher.should_receive(:matches?).with(actual).and_return(true) RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher) end it "returns the match value" do matcher = double("matcher") actual = Object.new matcher.should_receive(:matches?).with(actual).and_return(:this_value) expect(RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher)).to eq :this_value end it "calls failure_message_for_should if the matcher implements it" do matcher = double("matcher", :failure_message_for_should => "message", :matches? => false) actual = Object.new ::RSpec::Expectations.should_receive(:fail_with).with("message") RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher) end it "calls fail if matcher.diffable?" do matcher = double("matcher", :diffable? => true, :failure_message_for_should => "message", :matches? => false, :expected => 1, :actual => 2 ) actual = Object.new ::RSpec::Expectations.should_receive(:fail_with).with("message", 1, 2) RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher) end it "calls failure_message if the matcher does not implement failure_message_for_should" do matcher = double("matcher", :failure_message => "message", :matches? => false) actual = Object.new ::RSpec::Expectations.should_receive(:fail_with).with("message") RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher) end it "uses the custom failure message when one is provided" do matcher = double("matcher", :failure_message_for_should => "message", :matches? => false) actual = Object.new ::RSpec::Expectations.should_receive(:fail_with).with("custom") RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher, "custom") end it "uses the custom failure message when one is provided as a callable object" do matcher = double("matcher", :failure_message_for_should => "message", :matches? => false) actual = Object.new failure_message = double("failure message", :call => "custom") ::RSpec::Expectations.should_receive(:fail_with).with("custom") RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher, failure_message) end end end describe NegativeExpectationHandler do describe "#handle_matcher" do it "asks the matcher if it doesn't match when the matcher responds to #does_not_match?" do matcher = double("matcher", :does_not_match? => true, :negative_failure_message => nil) actual = Object.new matcher.should_receive(:does_not_match?).with(actual).and_return(true) RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher) end it "asks the matcher if it matches when the matcher doesn't respond to #does_not_match?" do matcher = double("matcher") actual = Object.new matcher.stub(:negative_failure_message) matcher.should_receive(:matches?).with(actual).and_return(false) RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher) end it "returns the match value" do matcher = double("matcher") actual = Object.new matcher.should_receive(:matches?).with(actual).and_return(false) matcher.stub(:negative_failure_message).and_return("ignore") expect(RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher)).to be_false end it "calls failure_message_for_should_not if the matcher implements it" do matcher = double("matcher", :failure_message_for_should_not => "message", :matches? => true) actual = Object.new ::RSpec::Expectations.should_receive(:fail_with).with("message") RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher) end it "calls negative_failure_message if the matcher does not implement failure_message_for_should_not" do matcher = double("matcher", :negative_failure_message => "message", :matches? => true) actual = Object.new ::RSpec::Expectations.should_receive(:fail_with).with("message") RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher) end it "calls fail if matcher.diffable?" do matcher = double("matcher", :diffable? => true, :failure_message_for_should_not => "message", :matches? => true, :expected => 1, :actual => 2 ) actual = Object.new ::RSpec::Expectations.should_receive(:fail_with).with("message", 1, 2) RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher) end it "uses the custom failure message when one is provided" do matcher = double("matcher", :failure_message_for_should_not => "message", :matches? => true) actual = Object.new ::RSpec::Expectations.should_receive(:fail_with).with("custom") RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher, "custom") end it "uses the custom failure message when one is provided as a callable object" do matcher = double("matcher", :failure_message_for_should_not => "message", :matches? => true) actual = Object.new failure_message = double("failure message", :call => "custom") ::RSpec::Expectations.should_receive(:fail_with).with("custom") RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher, failure_message) end end end describe PositiveExpectationHandler do include ExampleExpectations it "handles submitted args" do expect(5).to arbitrary_matcher(:expected => 5) expect(5).to arbitrary_matcher(:expected => "wrong").with(5) expect { expect(5).to arbitrary_matcher(:expected => 4) }.to fail_with("expected 4, got 5") expect { expect(5).to arbitrary_matcher(:expected => 5).with(4) }.to fail_with("expected 4, got 5") expect(5).not_to arbitrary_matcher(:expected => 4) expect(5).not_to arbitrary_matcher(:expected => 5).with(4) expect { expect(5).not_to arbitrary_matcher(:expected => 5) }.to fail_with("expected not 5, got 5") expect { expect(5).not_to arbitrary_matcher(:expected => 4).with(5) }.to fail_with("expected not 5, got 5") end it "handles the submitted block" do expect(5).to arbitrary_matcher { 5 } expect(5).to arbitrary_matcher(:expected => 4) { 5 } expect(5).to arbitrary_matcher(:expected => 4).with(5) { 3 } end end end end rspec-expectations-2.14.2/spec/rspec/expectations/extensions/0000755000004100000410000000000012203207642024421 5ustar www-datawww-datarspec-expectations-2.14.2/spec/rspec/expectations/extensions/kernel_spec.rb0000644000004100000410000000361612203207642027246 0ustar www-datawww-datarequire 'spec_helper' describe Object, "#should" do before(:each) do @target = "target" @matcher = double("matcher") @matcher.stub(:matches?).and_return(true) @matcher.stub(:failure_message_for_should) end it "accepts and interacts with a matcher" do @matcher.should_receive(:matches?).with(@target).and_return(true) expect(@target).to @matcher end it "asks for a failure_message_for_should when matches? returns false" do @matcher.should_receive(:matches?).with(@target).and_return(false) @matcher.should_receive(:failure_message_for_should).and_return("the failure message") expect { expect(@target).to @matcher }.to fail_with("the failure message") end context "on interpretters that have BasicObject", :if => defined?(BasicObject) do let(:proxy_class) do Class.new(BasicObject) do def initialize(target) @target = target end def proxied? true end def method_missing(name, *args) @target.send(name, *args) end end end it 'works properly on BasicObject-subclassed proxy objects' do expect(proxy_class.new(Object.new)).to be_proxied end end end describe Object, "#should_not" do before(:each) do @target = "target" @matcher = double("matcher") end it "accepts and interacts with a matcher" do @matcher.should_receive(:matches?).with(@target).and_return(false) @matcher.stub(:failure_message_for_should_not) expect(@target).not_to @matcher end it "asks for a failure_message_for_should_not when matches? returns true" do @matcher.should_receive(:matches?).with(@target).and_return(true) @matcher.should_receive(:failure_message_for_should_not).and_return("the failure message for should not") expect { expect(@target).not_to @matcher }.to fail_with("the failure message for should not") end end rspec-expectations-2.14.2/spec/rspec/expectations/fail_with_spec.rb0000644000004100000410000000673012203207642025535 0ustar www-datawww-data# encoding: utf-8 require 'spec_helper' describe RSpec::Expectations, "#fail_with with diff of arrays" do before { RSpec::Matchers.configuration.stub(:color? => false) } it "splits items with newlines" do expected_diff = "\nDiff:\n@@ -1 +1,3 @@\n+a\\nb\n+c\\nd\n" expect { RSpec::Expectations.fail_with("", [], ["a\nb", "c\nd"]) }.to fail_with(expected_diff) end it "shows inner arrays on a single line" do expected_diff = "\nDiff:\n@@ -1 +1,3 @@\n+a\\nb\n+[\"c\\nd\"]\n" expect { RSpec::Expectations.fail_with("", [], ["a\nb", ["c\nd"]]) }.to fail_with(expected_diff) end end describe RSpec::Expectations, "#fail_with with diff" do let(:differ) { double("differ") } before(:each) do RSpec::Expectations.stub(:differ) { differ } end it "calls differ if expected/actual are not strings (or numbers or procs)" do differ.should_receive(:diff_as_object).and_return("diff") expect { RSpec::Expectations.fail_with "the message", Object.new, Object.new }.to fail_with("the message\nDiff:diff") end context "with two strings" do context "and actual is multiline" do it "calls differ" do differ.should_receive(:diff_as_string).and_return("diff") expect { RSpec::Expectations.fail_with "the message", "expected\nthis", "actual" }.to fail_with("the message\nDiff:diff") end end context "and expected is multiline" do it "calls differ" do differ.should_receive(:diff_as_string).and_return("diff") expect { RSpec::Expectations.fail_with "the message", "expected", "actual\nthat" }.to fail_with("the message\nDiff:diff") end end context "and both are single line strings" do it "does not call differ" do differ.should_not_receive(:diff_as_string) expect { RSpec::Expectations.fail_with("the message", "expected", "actual") }.to fail_with("the message") end end context "and they are UTF-16LE encoded", :if => String.method_defined?(:encode) do it 'does not diff when they are not multiline' do differ.should_not_receive(:diff_as_string) str_1 = "This is a pile of poo: 💩".encode("UTF-16LE") str_2 = "This is a pile of poo: 💩".encode("UTF-16LE") expect { RSpec::Expectations.fail_with("the message", str_1, str_2) }.to fail_with("the message") end it 'diffs when they are multiline' do differ.should_receive(:diff_as_string).and_return("diff") str_1 = "This is a pile of poo:\n💩".encode("UTF-16LE") str_2 = "This is a pile of poo:\n💩".encode("UTF-16LE") expect { RSpec::Expectations.fail_with("the message", str_1, str_2) }.to fail_with("the message\nDiff:diff") end end end it "does not call differ if no expected/actual" do expect { RSpec::Expectations.fail_with "the message" }.to fail_with("the message") end it "does not call differ expected is Numeric" do expect { RSpec::Expectations.fail_with "the message", 1, "1" }.to fail_with("the message") end it "does not call differ when actual is Numeric" do expect { RSpec::Expectations.fail_with "the message", "1", 1 }.to fail_with("the message") end it "does not call differ if expected or actual are procs" do expect { RSpec::Expectations.fail_with "the message", lambda {}, lambda {} }.to fail_with("the message") end end rspec-expectations-2.14.2/spec/rspec/expectations/expectation_target_spec.rb0000644000004100000410000000440212203207642027452 0ustar www-datawww-datarequire 'spec_helper' module RSpec module Expectations # so our examples below can set expectations about the target ExpectationTarget.send(:attr_reader, :target) describe ExpectationTarget do context 'when constructed via #expect' do it 'constructs a new instance targetting the given argument' do expect(expect(7).target).to eq(7) end it 'constructs a new instance targetting the given block' do block = lambda {} expect(expect(&block).target).to be(block) end it 'raises an ArgumentError when given an argument and a block' do expect { expect(7) { } }.to raise_error(ArgumentError) end it 'raises an ArgumentError when given neither an argument nor a block' do expect { expect }.to raise_error(ArgumentError) end it 'can be passed nil' do expect(expect(nil).target).to be_nil end it 'passes a valid positive expectation' do expect(5).to eq(5) end it 'passes a valid negative expectation' do expect(5).not_to eq(4) end it 'passes a valid negative expectation with a split infinitive' do expect(5).to_not eq(4) end it 'fails an invalid positive expectation' do expect { expect(5).to eq(4) }.to fail_with(/expected: 4.*got: 5/m) end it 'fails an invalid negative expectation' do message = /expected 5 not to be a kind of Fixnum/ expect { expect(5).not_to be_a(Fixnum) }.to fail_with(message) end it 'fails an invalid negative expectation with a split infinitive' do message = /expected 5 not to be a kind of Fixnum/ expect { expect(5).to_not be_a(Fixnum) }.to fail_with(message) end it 'does not support operator matchers from #to' do expect { expect(3).to == 3 }.to raise_error(ArgumentError) end it 'does not support operator matchers from #not_to' do expect { expect(3).not_to == 4 }.to raise_error(ArgumentError) end end end end end rspec-expectations-2.14.2/spec/rspec/expectations/differ_spec.rb0000644000004100000410000001264112203207642025024 0ustar www-datawww-data# encoding: utf-8 require 'spec_helper' require 'ostruct' module RSpec module Expectations describe Differ do context "without --color" do before { RSpec::Matchers.configuration.stub(:color? => false) } let(:differ) { RSpec::Expectations::Differ.new } # color disabled context describe '#diff_as_string' do subject { differ.diff_as_string(@expected, @actual) } it "outputs unified diff of two strings" do @expected="foo\nbar\nzap\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nline\n" @actual="foo\nzap\nbar\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nanother\nline\n" expect(subject).to eql(<<-'EOD') @@ -1,6 +1,6 @@ foo -zap bar +zap this is soo @@ -9,6 +9,5 @@ equal insert a -another line EOD end if RUBY_VERSION.to_f > 1.9 it 'copes with encoded strings', :pending => (Diff::LCS::VERSION < '1.2.2') do @expected="Tu avec carté {count} itém has".encode('UTF-16LE') @actual="Tu avec carte {count} item has".encode('UTF-16LE') expect(subject).to eql(<<-EOD.encode('UTF-16LE')) @@ -1,2 +1,2 @@ -Tu avec carte {count} item has +Tu avec carté {count} itém has EOD end it 'copes with encoded strings', :pending => (Diff::LCS::VERSION >= '1.2.2') do @expected="Tu avec carté {count} itém has".encode('UTF-16LE') @actual="Tu avec carte {count} item has".encode('UTF-16LE') expect(subject).to eql 'Could not produce a diff because of the encoding of the string (UTF-16LE)' end it 'ouputs a message when encountering differently encoded strings' do @expected="Tu avec carté {count} itém has".encode('UTF-16LE') @actual="Tu avec carte {count} item has" expect(subject).to eql 'Could not produce a diff because the encoding of the actual string (UTF-8) differs from the encoding of the expected string (UTF-16LE)' end end end describe '#diff_as_object' do it "outputs unified diff message of two objects" do animal_class = Class.new do def initialize(name, species) @name, @species = name, species end def inspect <<-EOA EOA end end expected = animal_class.new "bob", "giraffe" actual = animal_class.new "bob", "tortoise" expected_diff = <<'EOD' @@ -1,5 +1,5 @@ EOD diff = differ.diff_as_object(expected,actual) expect(diff).to eq expected_diff end it "outputs unified diff message of two arrays" do expected = [ :foo, 'bar', :baz, 'quux', :metasyntactic, 'variable', :delta, 'charlie', :width, 'quite wide' ] actual = [ :foo, 'bar', :baz, 'quux', :metasyntactic, 'variable', :delta, 'tango' , :width, 'very wide' ] expected_diff = <<'EOD' @@ -5,7 +5,7 @@ :metasyntactic, "variable", :delta, - "tango", + "charlie", :width, - "very wide"] + "quite wide"] EOD diff = differ.diff_as_object(expected,actual) expect(diff).to eq expected_diff end it "outputs unified diff message of two hashes" do expected = { :foo => 'bar', :baz => 'quux', :metasyntactic => 'variable', :delta => 'charlie', :width =>'quite wide' } actual = { :foo => 'bar', :metasyntactic => 'variable', :delta => 'charlotte', :width =>'quite wide' } expected_diff = <<'EOD' @@ -1,4 +1,5 @@ -:delta => "charlotte", +:baz => "quux", +:delta => "charlie", :foo => "bar", :metasyntactic => "variable", :width => "quite wide" EOD diff = differ.diff_as_object(expected,actual) expect(diff).to eq expected_diff end it 'outputs unified diff messaoge of two hashes with differing encoding' do expected_diff = %Q{ @@ -1,2 +1,2 @@ -"a" => "a" #{ (RUBY_VERSION.to_f > 1.8) ? %Q{+"ö" => "ö"} : '+"\303\266" => "\303\266"' } } diff = differ.diff_as_object({'ö' => 'ö'}, {'a' => 'a'}) expect(diff).to eq expected_diff end it "outputs unified diff of single line strings" do expected = "this is one string" actual = "this is another string" expected_diff = <<'EOD' @@ -1,2 +1,2 @@ -"this is another string" +"this is one string" EOD diff = differ.diff_as_object(expected,actual) expect(diff).to eq expected_diff end it "outputs unified diff of multi line strings" do expected = "this is:\n one string" actual = "this is:\n another string" expected_diff = <<'EOD' @@ -1,3 +1,3 @@ this is: - another string + one string EOD diff = differ.diff_as_object(expected,actual) expect(diff).to eq expected_diff end end end context "with --color" do before { RSpec::Matchers.configuration.stub(:color? => true) } let(:differ) { RSpec::Expectations::Differ.new } it "outputs colored diffs" do expected = "foo bar baz" actual = "foo bang baz" expected_diff = "\n\e[34m@@ -1,2 +1,2 @@\n\e[0m\e[31m-foo bang baz\n\e[0m\e[32m+foo bar baz\n\e[0m" diff = differ.diff_as_string(expected,actual) expect(diff).to eq expected_diff end end end end end rspec-expectations-2.14.2/spec/rspec/matchers/0000755000004100000410000000000012203207642021322 5ustar www-datawww-datarspec-expectations-2.14.2/spec/rspec/matchers/yield_spec.rb0000644000004100000410000004413512203207642023776 0ustar www-datawww-datarequire 'spec_helper' module YieldHelpers # these helpers are prefixed with an underscore to prevent # collisions with the matchers (some of which have the same names) def _dont_yield end def _yield_with_no_args yield end def _yield_with_args(*args) yield(*args) end end class InstanceEvaler include RSpec::Matchers::Extensions::InstanceEvalWithArgs def yield_with_no_args(&block) instance_eval_with_args(&block) end def yield_with_args(*args, &block) instance_eval_with_args(*args, &block) end def each_arg(*args, &block) args.each do |arg| instance_eval_with_args(arg, &block) end end end describe "yield_control matcher" do include YieldHelpers extend YieldHelpers it_behaves_like "an RSpec matcher", :valid_value => lambda { |b| _yield_with_no_args(&b) }, :invalid_value => lambda { |b| _dont_yield(&b) } do let(:matcher) { yield_control } end it 'has a description' do expect(yield_control.description).to eq("yield control") end describe "expect {...}.to yield_control" do it 'passes if the block yields, regardless of the number of yielded arguments' do expect { |b| _yield_with_no_args(&b) }.to yield_control expect { |b| _yield_with_args(1, 2, &b) }.to yield_control end it 'passes if the block yields using instance_eval' do expect { |b| InstanceEvaler.new.yield_with_no_args(&b) }.to yield_control end it 'fails if the block does not yield' do expect { expect { |b| _dont_yield(&b) }.to yield_control }.to fail_with(/expected given block to yield control/) end context "with exact count" do it 'fails if the block yields wrong number of times' do expect { expect { |b| [1, 2, 3].each(&b) }.to yield_control.twice }.to fail_with(/expected given block to yield control twice/) expect { expect { |b| [1, 2].each(&b) }.to yield_control.exactly(3).times }.to fail_with(/expected given block to yield control 3 times/) end it 'passes if the block yields the specified number of times' do expect { |b| [1].each(&b) }.to yield_control.once expect { |b| [1, 2].each(&b) }.to yield_control.twice expect { |b| [1, 2, 3].each(&b) }.to yield_control.exactly(3).times end end context "with at_least count" do it 'passes if the block yields the given number of times' do expect { |b| [1, 2].each(&b) }.to yield_control.at_least(2).times expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_least(3).times end it 'passes if the block yields more times' do expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_least(2).times expect { |b| [1, 2, 3, 4].each(&b) }.to yield_control.at_least(3).times end it 'allows :once and :twice to be passed as counts' do expect { |b| [1].each(&b) }.to yield_control.at_least(:once) expect { |b| [1, 2].each(&b) }.to yield_control.at_least(:once) expect { expect { |b| [].each(&b) }.to yield_control.at_least(:once) }.to fail_with(/at least once/) expect { |b| [1, 2].each(&b) }.to yield_control.at_least(:twice) expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_least(:twice) expect { expect { |b| [1].each(&b) }.to yield_control.at_least(:twice) }.to fail_with(/at least twice/) end it 'fails if the block yields too few times' do expect { expect { |b| _yield_with_no_args(&b) }.to yield_control.at_least(2).times }.to fail_with(/expected given block to yield control at least twice/) end end context "with at_most count" do it 'passes if the block yields the given number of times' do expect { |b| [1, 2].each(&b) }.to yield_control.at_most(2).times expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_most(3).times end it 'passes if the block yields fewer times' do expect { |b| [1, 2].each(&b) }.to yield_control.at_most(3).times end it 'allows :once and :twice to be passed as counts' do expect { |b| [1].each(&b) }.to yield_control.at_most(:once) expect { expect { |b| [1, 2].each(&b) }.to yield_control.at_most(:once) }.to fail_with(/expected given block to yield control at most once/) expect { |b| [1, 2].each(&b) }.to yield_control.at_most(:twice) expect { expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_most(:twice) }.to fail_with(/expected given block to yield control at most twice/) end it 'fails if the block yields too many times' do expect { expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_most(2).times }.to fail_with(/expected given block to yield control at most twice/) end end end describe "expect {...}.not_to yield_control" do it 'passes if the block does not yield' do expect { |b| _dont_yield(&b) }.not_to yield_control end it 'fails if the block does yield' do expect { expect { |b| _yield_with_no_args(&b) }.not_to yield_control }.to fail_with(/expected given block not to yield control/) end it 'fails if the expect block does not accept an argument' do expect { expect { }.not_to yield_control }.to raise_error(/expect block must accept an argument/) end it 'raises an error if the expect block arg is not passed to a method as a block' do expect { expect { |b| }.not_to yield_control }.to raise_error(/must pass the argument.*as a block/) end end end describe "yield_with_no_args matcher" do include YieldHelpers extend YieldHelpers it_behaves_like "an RSpec matcher", :valid_value => lambda { |b| _yield_with_no_args(&b) }, :invalid_value => lambda { |b| _dont_yield(&b) } do let(:matcher) { yield_with_no_args } end it 'has a description' do expect(yield_with_no_args.description).to eq("yield with no args") end describe "expect {...}.to yield_with_no_args" do it 'passes if the block yields with no args' do expect { |b| _yield_with_no_args(&b) }.to yield_with_no_args end it 'passes if the block yields with no args using instance_eval' do expect { |b| InstanceEvaler.new.yield_with_no_args(&b) }.to yield_with_no_args end it 'fails if the block does not yield' do expect { expect { |b| _dont_yield(&b) }.to yield_with_no_args }.to fail_with(/expected given block to yield with no arguments, but did not yield/) end it 'fails if the block yields with args' do expect { expect { |b| _yield_with_args(1, &b) }.to yield_with_no_args }.to fail_with(/expected given block to yield with no arguments, but yielded with arguments/) end it 'fails if the block yields with arg false' do expect { expect { |b| _yield_with_args(false, &b) }.to yield_with_no_args }.to fail_with(/expected given block to yield with no arguments, but yielded with arguments/) end it 'raises an error if it yields multiple times' do expect { expect { |b| [1, 2].each(&b) }.to yield_with_no_args }.to raise_error(/not designed.*yields multiple times/) end end describe "expect {...}.not_to yield_with_no_args" do it "passes if the block does not yield" do expect { |b| _dont_yield(&b) }.not_to yield_with_no_args end it "passes if the block yields with args" do expect { |b| _yield_with_args(1, &b) }.not_to yield_with_no_args end it "fails if the block yields with no args" do expect { expect { |b| _yield_with_no_args(&b) }.not_to yield_with_no_args }.to fail_with(/expected given block not to yield with no arguments, but did/) end it 'fails if the expect block does not accept an argument' do expect { expect { }.not_to yield_with_no_args }.to raise_error(/expect block must accept an argument/) end it 'raises an error if the expect block arg is not passed to a method as a block' do expect { expect { |b| }.not_to yield_with_no_args }.to raise_error(/must pass the argument.*as a block/) end end end describe "yield_with_args matcher" do include YieldHelpers extend YieldHelpers it_behaves_like "an RSpec matcher", :valid_value => lambda { |b| _yield_with_args(1, &b) }, :invalid_value => lambda { |b| _dont_yield(&b) } do let(:matcher) { yield_with_args } end it 'has a description' do expect(yield_with_args.description).to eq("yield with args") expect(yield_with_args(1, 3).description).to eq("yield with args(1, 3)") expect(yield_with_args(false).description).to eq("yield with args(false)") end describe "expect {...}.to yield_with_args" do it 'passes if the block yields with arguments' do expect { |b| _yield_with_args(1, &b) }.to yield_with_args end it 'fails if the block does not yield' do expect { expect { |b| _dont_yield(&b) }.to yield_with_args }.to fail_with(/expected given block to yield with arguments, but did not yield/) end it 'fails if the block yields with no arguments' do expect { expect { |b| _yield_with_no_args(&b) }.to yield_with_args }.to fail_with(/expected given block to yield with arguments, but yielded with no arguments/) end it 'raises an error if it yields multiple times' do expect { expect { |b| [1, 2].each(&b) }.to yield_with_args }.to raise_error(/not designed.*yields multiple times/) end end describe "expect {...}.not_to yield_with_args" do it 'fails if the block yields with arguments' do expect { expect { |b| _yield_with_args(1, &b) }.not_to yield_with_args }.to fail_with(/expected given block not to yield with arguments, but did/) end it 'passes if the block does not yield' do expect { |b| _dont_yield(&b) }.not_to yield_with_args end it 'passes if the block yields with no arguments' do expect { |b| _yield_with_no_args(&b) }.not_to yield_with_args end it 'fails if the expect block does not accept an argument' do expect { expect { }.not_to yield_with_args }.to raise_error(/expect block must accept an argument/) end it 'raises an error if the expect block arg is not passed to a method as a block' do expect { expect { |b| }.not_to yield_with_args }.to raise_error(/must pass the argument.*as a block/) end end describe "expect {...}.to yield_with_args(3, 17)" do it 'passes if the block yields with the given arguments' do expect { |b| _yield_with_args(3, 17, &b) }.to yield_with_args(3, 17) end it 'passes if the block yields with the given arguments using instance_eval' do expect { |b| InstanceEvaler.new.yield_with_args(3, 17, &b) }.to yield_with_args(3, 17) end it 'fails if the block does not yield' do expect { expect { |b| _dont_yield(&b) }.to yield_with_args(3, 17) }.to fail_with(/expected given block to yield with arguments, but did not yield/) end it 'fails if the block yields with no arguments' do expect { expect { |b| _yield_with_no_args(&b) }.to yield_with_args(3, 17) }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/) end it 'fails if the block yields with different arguments' do expect { expect { |b| _yield_with_args("a", "b", &b) }.to yield_with_args("a", "c") }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/) end end describe "expect {...}.not_to yield_with_args(3, 17)" do it 'passes if the block yields with different arguments' do expect { |b| _yield_with_args("a", "b", &b) }.not_to yield_with_args("a", "c") end it 'fails if the block yields with the given arguments' do expect { expect { |b| _yield_with_args("a", "b", &b) }.not_to yield_with_args("a", "b") }.to fail_with(/expected given block not to yield with arguments, but yielded with expected arguments/) end end describe "expect {...}.to yield_with_args( false )" do it 'passes if the block yields with the given arguments' do expect { |b| _yield_with_args(false, &b) }.to yield_with_args(false) end it 'passes if the block yields with the given arguments using instance_eval' do expect { |b| InstanceEvaler.new.yield_with_args(false, &b) }.to yield_with_args(false) end it 'fails if the block does not yield' do expect { expect { |b| _dont_yield(&b) }.to yield_with_args(false) }.to fail_with(/expected given block to yield with arguments, but did not yield/) end it 'fails if the block yields with no arguments' do expect { expect { |b| _yield_with_no_args(&b) }.to yield_with_args(false) }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/) end it 'fails if the block yields with different arguments' do expect { expect { |b| _yield_with_args(false, &b) }.to yield_with_args(true) }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/) end end describe "expect {...}.to yield_with_args(/reg/, /ex/)" do it "passes if the block yields strings matching the regexes" do expect { |b| _yield_with_args("regular", "expression", &b) }.to yield_with_args(/reg/, /ex/) end it "fails if the block yields strings that do not match the regexes" do expect { expect { |b| _yield_with_args("no", "match", &b) }.to yield_with_args(/reg/, /ex/) }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/) end end describe "expect {...}.to yield_with_args(String, Fixnum)" do it "passes if the block yields objects of the given types" do expect { |b| _yield_with_args("string", 15, &b) }.to yield_with_args(String, Fixnum) end it "passes if the block yields the given types" do expect { |b| _yield_with_args(String, Fixnum, &b) }.to yield_with_args(String, Fixnum) end it "fails if the block yields objects of different types" do expect { expect { |b| _yield_with_args(15, "string", &b) }.to yield_with_args(String, Fixnum) }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/) end end end describe "yield_successive_args matcher" do include YieldHelpers extend YieldHelpers it_behaves_like "an RSpec matcher", :valid_value => lambda { |b| [1, 2].each(&b) }, :invalid_value => lambda { |b| _dont_yield(&b) } do let(:matcher) { yield_successive_args(1, 2) } end it 'has a description' do expect(yield_successive_args(1, 3).description).to eq("yield successive args(1, 3)") expect(yield_successive_args([:a, 1], [:b, 2]).description).to eq("yield successive args([:a, 1], [:b, 2])") end describe "expect {...}.to yield_successive_args([:a, 1], [:b, 2])" do it 'passes when the block successively yields the given args' do expect { |b| [ [:a, 1], [:b, 2] ].each(&b) }.to yield_successive_args([:a, 1], [:b, 2]) end it 'fails when the block does not yield that many times' do expect { expect { |b| [[:a, 1]].each(&b) }.to yield_successive_args([:a, 1], [:b, 2]) }.to fail_with(/but yielded with unexpected arguments/) end it 'fails when the block yields the right number of times but with different arguments' do expect { expect { |b| [ [:a, 1], [:b, 3] ].each(&b) }.to yield_successive_args([:a, 1], [:b, 2]) }.to fail_with(/but yielded with unexpected arguments/) end end describe "expect {...}.to yield_successive_args(1, 2, 3)" do it 'passes when the block successively yields the given args' do expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3) end it 'passes when the block successively yields the given args using instance_eval' do expect { |b| InstanceEvaler.new.each_arg(1, 2, 3, &b) }.to yield_successive_args(1, 2, 3) end it 'fails when the block does not yield the expected args' do expect { expect { |b| [1, 2, 4].each(&b) }.to yield_successive_args([:a, 1], [:b, 2]) }.to fail_with(/but yielded with unexpected arguments/) end end describe "expect {...}.not_to yield_successive_args(1, 2, 3)" do it 'passes when the block does not yield' do expect { |b| _dont_yield(&b) }.not_to yield_successive_args(1, 2, 3) end it 'passes when the block yields the wrong number of times' do expect { |b| [1, 2].each(&b) }.not_to yield_successive_args(1, 2, 3) end it 'passes when the block yields the wrong arguments' do expect { |b| [1, 2, 4].each(&b) }.not_to yield_successive_args(1, 2, 3) end it 'fails when the block yields the given arguments' do expect { expect { |b| [1, 2, 3].each(&b) }.not_to yield_successive_args(1, 2, 3) }.to fail_with(/expected given block not to yield successively/) end it 'fails if the expect block does not accept an argument' do expect { expect { }.not_to yield_successive_args(1, 2, 3) }.to raise_error(/expect block must accept an argument/) end it 'raises an error if the expect block arg is not passed to a method as a block' do expect { expect { |b| }.not_to yield_successive_args(1, 2, 3) }.to raise_error(/must pass the argument.*as a block/) end end describe "expect {...}.to yield_successive_args(String, Fixnum)" do it "passes if the block successively yields objects of the given types" do expect { |b| ["string", 15].each(&b) }.to yield_successive_args(String, Fixnum) end it "passes if the block yields the given types" do expect { |b| [String, Fixnum].each(&b) }.to yield_successive_args(String, Fixnum) end it "fails if the block yields objects of different types" do expect { expect { |b| [15, "string"].each(&b) }.to yield_successive_args(String, Fixnum) }.to fail_with(/expected given block to yield successively with arguments/) end end end rspec-expectations-2.14.2/spec/rspec/matchers/eq_spec.rb0000644000004100000410000000317212203207642023271 0ustar www-datawww-datarequire 'spec_helper' module RSpec module Matchers describe "eq" do it_behaves_like "an RSpec matcher", :valid_value => 1, :invalid_value => 2 do let(:matcher) { eq(1) } end it "is diffable" do expect(eq(1)).to be_diffable end it "matches when actual == expected" do expect(1).to eq(1) end it "does not match when actual != expected" do expect(1).not_to eq(2) end it "compares by sending == to actual (not expected)" do called = false actual = Class.new do define_method :== do |other| called = true end end.new expect(actual).to eq :anything # to trigger the matches? method expect(called).to be_true end it "describes itself" do matcher = eq(1) matcher.matches?(1) expect(matcher.description).to eq "eq 1" end it "provides message, expected and actual on #failure_message" do matcher = eq("1") matcher.matches?(1) expect(matcher.failure_message_for_should).to eq "\nexpected: \"1\"\n got: 1\n\n(compared using ==)\n" end it "provides message, expected and actual on #negative_failure_message" do matcher = eq(1) matcher.matches?(1) expect(matcher.failure_message_for_should_not).to eq "\nexpected: value != 1\n got: 1\n\n(compared using ==)\n" end it 'fails properly when the actual is an array of multiline strings' do expect { expect(["a\nb", "c\nd"]).to eq([]) }.to fail_matching("expected: []") end end end end rspec-expectations-2.14.2/spec/rspec/matchers/be_kind_of_spec.rb0000644000004100000410000000251012203207642024736 0ustar www-datawww-datarequire 'spec_helper' module RSpec module Matchers [:be_a_kind_of, :be_kind_of].each do |method| describe "expect(actual).to #{method}(expected)" do it_behaves_like "an RSpec matcher", :valid_value => 5, :invalid_value => "a" do let(:matcher) { send(method, Fixnum) } end it "passes if actual is instance of expected class" do expect(5).to send(method, Fixnum) end it "passes if actual is instance of subclass of expected class" do expect(5).to send(method, Numeric) end it "fails with failure message for should unless actual is kind of expected class" do expect { expect("foo").to send(method, Array) }.to fail_with(%Q{expected "foo" to be a kind of Array}) end it "provides a description" do matcher = be_a_kind_of(String) matcher.matches?("this") expect(matcher.description).to eq "be a kind of String" end end describe "expect(actual).not_to #{method}(expected)" do it "fails with failure message for should_not if actual is kind of expected class" do expect { expect("foo").not_to send(method, String) }.to fail_with(%Q{expected "foo" not to be a kind of String}) end end end end end rspec-expectations-2.14.2/spec/rspec/matchers/be_instance_of_spec.rb0000644000004100000410000000373712203207642025631 0ustar www-datawww-datarequire 'spec_helper' module RSpec module Matchers [:be_an_instance_of, :be_instance_of].each do |method| describe "expect(actual).to #{method}(expected)" do it_behaves_like "an RSpec matcher", :valid_value => 5, :invalid_value => "a" do let(:matcher) { send(method, Fixnum) } end it "passes if actual is instance of expected class" do expect(5).to send(method, Fixnum) end it "fails if actual is instance of subclass of expected class" do expect { expect(5).to send(method, Numeric) }.to fail_with(%Q{expected 5 to be an instance of Numeric}) end it "fails with failure message for should unless actual is instance of expected class" do expect { expect("foo").to send(method, Array) }.to fail_with(%Q{expected "foo" to be an instance of Array}) end it "provides a description" do matcher = be_an_instance_of(Fixnum) matcher.matches?(Numeric) expect(matcher.description).to eq "be an instance of Fixnum" end context "when expected provides an expanded inspect, e.g. AR::Base" do let(:user_klass) do Class.new do def self.inspect "User(id: integer, name: string)" end end end before { stub_const("User", user_klass) } it "provides a description including only the class name" do matcher = be_an_instance_of(User) expect(matcher.description).to eq "be an instance of User" end end end describe "expect(actual).not_to #{method}(expected)" do it "fails with failure message for should_not if actual is instance of expected class" do expect { expect("foo").not_to send(method, String) }.to fail_with(%Q{expected "foo" not to be an instance of String}) end end end end end rspec-expectations-2.14.2/spec/rspec/matchers/be_spec.rb0000644000004100000410000003451612203207642023260 0ustar www-datawww-datarequire 'spec_helper' describe "expect(...).to be_predicate" do it "passes when actual returns true for :predicate?" do actual = double("actual", :happy? => true) expect(actual).to be_happy end it "passes when actual returns true for :predicates? (present tense)" do actual = double("actual", :exists? => true, :exist? => true) expect(actual).to be_exist end it "fails when actual returns false for :predicate?" do actual = double("actual", :happy? => false) expect { expect(actual).to be_happy }.to fail_with("expected happy? to return true, got false") end it "fails when actual returns false for :predicate?" do actual = double("actual", :happy? => nil) expect { expect(actual).to be_happy }.to fail_with("expected happy? to return true, got nil") end it "fails when actual does not respond to :predicate?" do expect { expect(Object.new).to be_happy }.to raise_error(NameError, /happy\?/) end it "fails on error other than NameError" do actual = double("actual") actual.should_receive(:foo?).and_raise("aaaah") expect { expect(actual).to be_foo }.to raise_error(/aaaah/) end it "fails on error other than NameError (with the present tense predicate)" do actual = Object.new actual.should_receive(:foos?).and_raise("aaaah") expect { expect(actual).to be_foo }.to raise_error(/aaaah/) end it "does not support operator chaining like a basic `be` matcher does" do matcher = be_happy value = double(:happy? => false) expect(be_happy == value).to be false end end describe "expect(...).not_to be_predicate" do it "passes when actual returns false for :sym?" do actual = double("actual", :happy? => false) expect(actual).not_to be_happy end it "passes when actual returns nil for :sym?" do actual = double("actual", :happy? => nil) expect(actual).not_to be_happy end it "fails when actual returns true for :sym?" do actual = double("actual", :happy? => true) expect { expect(actual).not_to be_happy }.to fail_with("expected happy? to return false, got true") end it "fails when actual does not respond to :sym?" do expect { expect(Object.new).not_to be_happy }.to raise_error(NameError) end end describe "expect(...).to be_predicate(*args)" do it "passes when actual returns true for :predicate?(*args)" do actual = double("actual") actual.should_receive(:older_than?).with(3).and_return(true) expect(actual).to be_older_than(3) end it "fails when actual returns false for :predicate?(*args)" do actual = double("actual") actual.should_receive(:older_than?).with(3).and_return(false) expect { expect(actual).to be_older_than(3) }.to fail_with("expected older_than?(3) to return true, got false") end it "fails when actual does not respond to :predicate?" do expect { expect(Object.new).to be_older_than(3) }.to raise_error(NameError) end end describe "expect(...).not_to be_predicate(*args)" do it "passes when actual returns false for :predicate?(*args)" do actual = double("actual") actual.should_receive(:older_than?).with(3).and_return(false) expect(actual).not_to be_older_than(3) end it "fails when actual returns true for :predicate?(*args)" do actual = double("actual") actual.should_receive(:older_than?).with(3).and_return(true) expect { expect(actual).not_to be_older_than(3) }.to fail_with("expected older_than?(3) to return false, got true") end it "fails when actual does not respond to :predicate?" do expect { expect(Object.new).not_to be_older_than(3) }.to raise_error(NameError) end end describe "expect(...).to be_predicate(&block)" do it "passes when actual returns true for :predicate?(&block)" do actual = double("actual") delegate = double("delegate") actual.should_receive(:happy?).and_yield delegate.should_receive(:check_happy).and_return(true) expect(actual).to be_happy { delegate.check_happy } end it "fails when actual returns false for :predicate?(&block)" do actual = double("actual") delegate = double("delegate") actual.should_receive(:happy?).and_yield delegate.should_receive(:check_happy).and_return(false) expect { expect(actual).to be_happy { delegate.check_happy } }.to fail_with("expected happy? to return true, got false") end it "fails when actual does not respond to :predicate?" do delegate = double("delegate", :check_happy => true) expect { expect(Object.new).to be_happy { delegate.check_happy } }.to raise_error(NameError) end end describe "expect(...).not_to be_predicate(&block)" do it "passes when actual returns false for :predicate?(&block)" do actual = double("actual") delegate = double("delegate") actual.should_receive(:happy?).and_yield delegate.should_receive(:check_happy).and_return(false) expect(actual).not_to be_happy { delegate.check_happy } end it "fails when actual returns true for :predicate?(&block)" do actual = double("actual") delegate = double("delegate") actual.should_receive(:happy?).and_yield delegate.should_receive(:check_happy).and_return(true) expect { expect(actual).not_to be_happy { delegate.check_happy } }.to fail_with("expected happy? to return false, got true") end it "fails when actual does not respond to :predicate?" do delegate = double("delegate", :check_happy => true) expect { expect(Object.new).not_to be_happy { delegate.check_happy } }.to raise_error(NameError) end end describe "expect(...).to be_predicate(*args, &block)" do it "passes when actual returns true for :predicate?(*args, &block)" do actual = double("actual") delegate = double("delegate") actual.should_receive(:older_than?).with(3).and_yield(3) delegate.should_receive(:check_older_than).with(3).and_return(true) expect(actual).to be_older_than(3) { |age| delegate.check_older_than(age) } end it "fails when actual returns false for :predicate?(*args, &block)" do actual = double("actual") delegate = double("delegate") actual.should_receive(:older_than?).with(3).and_yield(3) delegate.should_receive(:check_older_than).with(3).and_return(false) expect { expect(actual).to be_older_than(3) { |age| delegate.check_older_than(age) } }.to fail_with("expected older_than?(3) to return true, got false") end it "fails when actual does not respond to :predicate?" do delegate = double("delegate", :check_older_than => true) expect { expect(Object.new).to be_older_than(3) { |age| delegate.check_older_than(age) } }.to raise_error(NameError) end end describe "expect(...).not_to be_predicate(*args, &block)" do it "passes when actual returns false for :predicate?(*args, &block)" do actual = double("actual") delegate = double("delegate") actual.should_receive(:older_than?).with(3).and_yield(3) delegate.should_receive(:check_older_than).with(3).and_return(false) expect(actual).not_to be_older_than(3) { |age| delegate.check_older_than(age) } end it "fails when actual returns true for :predicate?(*args, &block)" do actual = double("actual") delegate = double("delegate") actual.should_receive(:older_than?).with(3).and_yield(3) delegate.should_receive(:check_older_than).with(3).and_return(true) expect { expect(actual).not_to be_older_than(3) { |age| delegate.check_older_than(age) } }.to fail_with("expected older_than?(3) to return false, got true") end it "fails when actual does not respond to :predicate?" do delegate = double("delegate", :check_older_than => true) expect { expect(Object.new).not_to be_older_than(3) { |age| delegate.check_older_than(age) } }.to raise_error(NameError) end end describe "expect(...).to be_true" do it "passes when actual equal?(true)" do expect(true).to be_true end it "passes when actual is 1" do expect(1).to be_true end it "fails when actual equal?(false)" do expect { expect(false).to be_true }.to fail_with("expected: true value\n got: false") end end describe "expect(...).to be_false" do it "passes when actual equal?(false)" do expect(false).to be_false end it "passes when actual equal?(nil)" do expect(nil).to be_false end it "fails when actual equal?(true)" do expect { expect(true).to be_false }.to fail_with("expected: false value\n got: true") end end describe "expect(...).to be_nil" do it "passes when actual is nil" do expect(nil).to be_nil end it "fails when actual is not nil" do expect { expect(:not_nil).to be_nil }.to fail_with(/^expected: nil/) end end describe "expect(...).not_to be_nil" do it "passes when actual is not nil" do expect(:not_nil).not_to be_nil end it "fails when actual is nil" do expect { expect(nil).not_to be_nil }.to fail_with(/^expected: not nil/) end end describe "expect(...).to be <" do it "passes when < operator returns true" do expect(3).to be < 4 end it "fails when < operator returns false" do expect { expect(3).to be < 3 }.to fail_with("expected: < 3\n got: 3") end it "describes itself" do expect(be.<(4).description).to eq "be < 4" end end describe "expect(...).to be <=" do it "passes when <= operator returns true" do expect(3).to be <= 4 expect(4).to be <= 4 end it "fails when <= operator returns false" do expect { expect(3).to be <= 2 }.to fail_with("expected: <= 2\n got: 3") end end describe "expect(...).to be >=" do it "passes when >= operator returns true" do expect(4).to be >= 4 expect(5).to be >= 4 end it "fails when >= operator returns false" do expect { expect(3).to be >= 4 }.to fail_with("expected: >= 4\n got: 3") end end describe "expect(...).to be >" do it "passes when > operator returns true" do expect(5).to be > 4 end it "fails when > operator returns false" do expect { expect(3).to be > 4 }.to fail_with("expected: > 4\n got: 3") end end describe "expect(...).to be ==" do it "passes when == operator returns true" do expect(5).to be == 5 end it "fails when == operator returns false" do expect { expect(3).to be == 4 }.to fail_with("expected: == 4\n got: 3") end it 'works when the target overrides `#send`' do klass = Struct.new(:message) do def send :message_sent end end msg_1 = klass.new("hello") msg_2 = klass.new("hello") expect(msg_1).to be == msg_2 end end describe "expect(...).to be =~" do it "passes when =~ operator returns true" do expect("a string").to be =~ /str/ end it "fails when =~ operator returns false" do expect { expect("a string").to be =~ /blah/ }.to fail_with(%Q|expected: =~ /blah/\n got: "a string"|) end end describe "should be =~", :uses_should do it "passes when =~ operator returns true" do "a string".should be =~ /str/ end it "fails when =~ operator returns false" do expect { "a string".should be =~ /blah/ }.to fail_with(%Q|expected: =~ /blah/\n got: "a string"|) end end describe "expect(...).to be ===" do it "passes when === operator returns true" do expect(Hash).to be === Hash.new end it "fails when === operator returns false" do expect { expect(Hash).to be === "not a hash" }.to fail_with(%[expected: === "not a hash"\n got: Hash]) end end describe "expect(...).not_to with operators" do it "coaches user to stop using operators with expect().not_to" do expect { expect(5).not_to be < 6 }.to raise_error(/`expect\(actual\).not_to be < 6` not only FAILED,\nit is a bit confusing./m) end end describe "should_not with operators", :uses_only_should do it "coaches user to stop using operators with should_not" do lambda { 5.should_not be < 6 }.should raise_error(/`actual.should_not be < 6` not only FAILED,\nit is a bit confusing./m) end end describe "expect(...).to be" do it "passes if actual is truthy" do expect(true).to be expect(1).to be end it "fails if actual is false" do expect { expect(false).to be }.to fail_with("expected false to evaluate to true") end it "fails if actual is nil" do expect { expect(nil).to be }.to fail_with("expected nil to evaluate to true") end it "describes itself" do expect(be.description).to eq "be" end end describe "expect(...).not_to be" do it "passes if actual is falsy" do expect(false).not_to be expect(nil).not_to be end it "fails on true" do expect { expect(true).not_to be }.to fail_with("expected true to evaluate to false") end end describe "expect(...).to be(value)" do it "delegates to equal" do matcher = equal(5) self.should_receive(:equal).with(5).and_return(matcher) expect(5).to be(5) end end describe "expect(...).not_to be(value)" do it "delegates to equal" do matcher = equal(4) self.should_receive(:equal).with(4).and_return(matcher) expect(5).not_to be(4) end end describe "'expect(...).to be' with operator" do it "includes 'be' in the description" do expect((be > 6).description).to match(/be > 6/) expect((be >= 6).description).to match(/be >= 6/) expect((be <= 6).description).to match(/be <= 6/) expect((be < 6).description).to match(/be < 6/) end end describe "arbitrary predicate with DelegateClass" do it "accesses methods defined in the delegating class (LH[#48])" do require 'delegate' class ArrayDelegate < DelegateClass(Array) def initialize(array) @internal_array = array super(@internal_array) end def large? @internal_array.size >= 5 end end delegate = ArrayDelegate.new([1,2,3,4,5,6]) expect(delegate).to be_large end end describe "be_a, be_an" do it "passes when class matches" do expect("foobar").to be_a(String) expect([1,2,3]).to be_an(Array) end it "fails when class does not match" do expect("foobar").not_to be_a(Hash) expect([1,2,3]).not_to be_an(Integer) end end describe "be_an_instance_of" do it "passes when direct class matches" do expect(5).to be_an_instance_of(Fixnum) end it "fails when class is higher up hierarchy" do expect(5).not_to be_an_instance_of(Numeric) end end rspec-expectations-2.14.2/spec/rspec/matchers/be_within_spec.rb0000644000004100000410000001041512203207642024632 0ustar www-datawww-datarequire 'spec_helper' module RSpec module Matchers describe "expect(actual).to be_within(delta).of(expected)" do it_behaves_like "an RSpec matcher", :valid_value => 5, :invalid_value => -5 do let(:matcher) { be_within(2).of(4.0) } end it "passes when actual == expected" do expect(5.0).to be_within(0.5).of(5.0) end it "passes when actual < (expected + delta)" do expect(5.49).to be_within(0.5).of(5.0) end it "passes when actual > (expected - delta)" do expect(4.51).to be_within(0.5).of(5.0) end it "passes when actual == (expected - delta)" do expect(4.5).to be_within(0.5).of(5.0) end it "passes when actual == (expected + delta)" do expect(5.5).to be_within(0.5).of(5.0) end it "passes with integer arguments that are near each other" do expect(1.0001).to be_within(5).percent_of(1) end it "passes with negative arguments" do expect(-1.0001).to be_within(5).percent_of(-1) end it "fails when actual < (expected - delta)" do expect { expect(4.49).to be_within(0.5).of(5.0) }.to fail_with("expected 4.49 to be within 0.5 of 5.0") end it "fails when actual > (expected + delta)" do expect { expect(5.51).to be_within(0.5).of(5.0) }.to fail_with("expected 5.51 to be within 0.5 of 5.0") end it "works with Time" do expect(Time.now).to be_within(0.1).of(Time.now) end it "provides a description" do matcher = be_within(0.5).of(5.0) matcher.matches?(5.1) expect(matcher.description).to eq "be within 0.5 of 5.0" end it "raises an error if no expected value is given" do expect { expect(5.1).to be_within(0.5) }.to raise_error(ArgumentError, /must set an expected value using #of/) end it "raises an error if the actual does not respond to :-" do expect { expect(nil).to be_within(0.1).of(0) }.to raise_error(ArgumentError, /The actual value \(nil\) must respond to `-`/) end end describe "expect(actual).to be_within(delta).percent_of(expected)" do it "passes when actual is within the given percent variance" do expect(9.0).to be_within(10).percent_of(10.0) expect(10.0).to be_within(10).percent_of(10.0) expect(11.0).to be_within(10).percent_of(10.0) end it "fails when actual is outside the given percent variance" do expect { expect(8.9).to be_within(10).percent_of(10.0) }.to fail_with("expected 8.9 to be within 10% of 10.0") expect { expect(11.1).to be_within(10).percent_of(10.0) }.to fail_with("expected 11.1 to be within 10% of 10.0") end it "provides a description" do matcher = be_within(0.5).percent_of(5.0) matcher.matches?(5.1) expect(matcher.description).to eq "be within 0.5% of 5.0" end end describe "expect(actual).not_to be_within(delta).of(expected)" do it "passes when actual < (expected - delta)" do expect(4.49).not_to be_within(0.5).of(5.0) end it "passes when actual > (expected + delta)" do expect(5.51).not_to be_within(0.5).of(5.0) end it "fails when actual == expected" do expect { expect(5.0).not_to be_within(0.5).of(5.0) }.to fail_with("expected 5.0 not to be within 0.5 of 5.0") end it "fails when actual < (expected + delta)" do expect { expect(5.49).not_to be_within(0.5).of(5.0) }.to fail_with("expected 5.49 not to be within 0.5 of 5.0") end it "fails when actual > (expected - delta)" do expect { expect(4.51).not_to be_within(0.5).of(5.0) }.to fail_with("expected 4.51 not to be within 0.5 of 5.0") end it "fails when actual == (expected - delta)" do expect { expect(4.5).not_to be_within(0.5).of(5.0) }.to fail_with("expected 4.5 not to be within 0.5 of 5.0") end it "fails when actual == (expected + delta)" do expect { expect(5.5).not_to be_within(0.5).of(5.0) }.to fail_with("expected 5.5 not to be within 0.5 of 5.0") end end end end rspec-expectations-2.14.2/spec/rspec/matchers/have_spec.rb0000644000004100000410000004244712203207642023617 0ustar www-datawww-datarequire 'spec_helper' require 'stringio' describe "have matcher" do let(:inflector) do Class.new do def self.pluralize(string) string.to_s + 's' end end end before(:each) { stub_const("ActiveSupport::Inflector", inflector) } def create_collection_owner_with(n) owner = RSpec::Expectations::Helper::CollectionOwner.new (1..n).each do |number| owner.add_to_collection_with_length_method(number) owner.add_to_collection_with_size_method(number) owner.add_to_collection_with_count_method(number) end owner end describe "expect(...).to have(n).items" do it_behaves_like "an RSpec matcher", :valid_value => [1, 2], :invalid_value => [1] do let(:matcher) { have(2).items } end it "passes if target has a collection of items with n members" do owner = create_collection_owner_with(3) expect(owner).to have(3).items_in_collection_with_length_method expect(owner).to have(3).items_in_collection_with_size_method expect(owner).to have(3).items_in_collection_with_count_method end it "converts :no to 0" do owner = create_collection_owner_with(0) expect(owner).to have(:no).items_in_collection_with_length_method expect(owner).to have(:no).items_in_collection_with_size_method expect(owner).to have(:no).items_in_collection_with_count_method end it "converts a String argument to Integer" do owner = create_collection_owner_with(3) expect(owner).to have('3').items_in_collection_with_length_method expect(owner).to have('3').items_in_collection_with_size_method expect(owner).to have('3').items_in_collection_with_count_method end it "fails if target has a collection of items with < n members" do owner = create_collection_owner_with(3) expect { expect(owner).to have(4).items_in_collection_with_length_method }.to fail_with("expected 4 items_in_collection_with_length_method, got 3") expect { expect(owner).to have(4).items_in_collection_with_size_method }.to fail_with("expected 4 items_in_collection_with_size_method, got 3") expect { expect(owner).to have(4).items_in_collection_with_count_method }.to fail_with("expected 4 items_in_collection_with_count_method, got 3") end it "fails if target has a collection of items with > n members" do owner = create_collection_owner_with(3) expect { expect(owner).to have(2).items_in_collection_with_length_method }.to fail_with("expected 2 items_in_collection_with_length_method, got 3") expect { expect(owner).to have(2).items_in_collection_with_size_method }.to fail_with("expected 2 items_in_collection_with_size_method, got 3") expect { expect(owner).to have(2).items_in_collection_with_count_method }.to fail_with("expected 2 items_in_collection_with_count_method, got 3") end end describe 'expect(...).to have(1).item when ActiveSupport::Inflector is defined' do it 'pluralizes the collection name' do owner = create_collection_owner_with(1) expect(owner).to have(1).item end context "when ActiveSupport::Inflector is partially loaded without its inflectors" do it "does not pluralize the collection name" do stub_const("ActiveSupport::Inflector", Module.new) owner = create_collection_owner_with(1) expect { expect(owner).to have(1).item }.to raise_error(NoMethodError) end end end describe 'expect(...).to have(1).item when Inflector is defined' do before { stub_const("Inflector", inflector) } it 'pluralizes the collection name' do owner = create_collection_owner_with(1) expect(owner).to have(1).item end end describe "expect(...).to have(n).items where result responds to items but returns something other than a collection" do it "provides a meaningful error" do owner = Class.new do def items Object.new end end.new expect do expect(owner).to have(3).items end.to raise_error("expected items to be a collection but it does not respond to #length, #size or #count") end end describe "expect(...).not_to have(n).items" do it "passes if target has a collection of items with < n members" do owner = create_collection_owner_with(3) expect(owner).not_to have(4).items_in_collection_with_length_method expect(owner).not_to have(4).items_in_collection_with_size_method expect(owner).not_to have(4).items_in_collection_with_count_method end it "passes if target has a collection of items with > n members" do owner = create_collection_owner_with(3) expect(owner).not_to have(2).items_in_collection_with_length_method expect(owner).not_to have(2).items_in_collection_with_size_method expect(owner).not_to have(2).items_in_collection_with_count_method end it "fails if target has a collection of items with n members" do owner = create_collection_owner_with(3) expect { expect(owner).not_to have(3).items_in_collection_with_length_method }.to fail_with("expected target not to have 3 items_in_collection_with_length_method, got 3") expect { expect(owner).not_to have(3).items_in_collection_with_size_method }.to fail_with("expected target not to have 3 items_in_collection_with_size_method, got 3") expect { expect(owner).not_to have(3).items_in_collection_with_count_method }.to fail_with("expected target not to have 3 items_in_collection_with_count_method, got 3") end end describe "expect(...).to have_exactly(n).items" do it "passes if target has a collection of items with n members" do owner = create_collection_owner_with(3) expect(owner).to have_exactly(3).items_in_collection_with_length_method expect(owner).to have_exactly(3).items_in_collection_with_size_method expect(owner).to have_exactly(3).items_in_collection_with_count_method end it "converts :no to 0" do owner = create_collection_owner_with(0) expect(owner).to have_exactly(:no).items_in_collection_with_length_method expect(owner).to have_exactly(:no).items_in_collection_with_size_method expect(owner).to have_exactly(:no).items_in_collection_with_count_method end it "fails if target has a collection of items with < n members" do owner = create_collection_owner_with(3) expect { expect(owner).to have_exactly(4).items_in_collection_with_length_method }.to fail_with("expected 4 items_in_collection_with_length_method, got 3") expect { expect(owner).to have_exactly(4).items_in_collection_with_size_method }.to fail_with("expected 4 items_in_collection_with_size_method, got 3") expect { expect(owner).to have_exactly(4).items_in_collection_with_count_method }.to fail_with("expected 4 items_in_collection_with_count_method, got 3") end it "fails if target has a collection of items with > n members" do owner = create_collection_owner_with(3) expect { expect(owner).to have_exactly(2).items_in_collection_with_length_method }.to fail_with("expected 2 items_in_collection_with_length_method, got 3") expect { expect(owner).to have_exactly(2).items_in_collection_with_size_method }.to fail_with("expected 2 items_in_collection_with_size_method, got 3") expect { expect(owner).to have_exactly(2).items_in_collection_with_count_method }.to fail_with("expected 2 items_in_collection_with_count_method, got 3") end end describe "expect(...).to have_at_least(n).items" do it "passes if target has a collection of items with n members" do owner = create_collection_owner_with(3) expect(owner).to have_at_least(3).items_in_collection_with_length_method expect(owner).to have_at_least(3).items_in_collection_with_size_method expect(owner).to have_at_least(3).items_in_collection_with_count_method end it "passes if target has a collection of items with > n members" do owner = create_collection_owner_with(3) expect(owner).to have_at_least(2).items_in_collection_with_length_method expect(owner).to have_at_least(2).items_in_collection_with_size_method expect(owner).to have_at_least(2).items_in_collection_with_count_method end it "fails if target has a collection of items with < n members" do owner = create_collection_owner_with(3) expect { expect(owner).to have_at_least(4).items_in_collection_with_length_method }.to fail_with("expected at least 4 items_in_collection_with_length_method, got 3") expect { expect(owner).to have_at_least(4).items_in_collection_with_size_method }.to fail_with("expected at least 4 items_in_collection_with_size_method, got 3") expect { expect(owner).to have_at_least(4).items_in_collection_with_count_method }.to fail_with("expected at least 4 items_in_collection_with_count_method, got 3") end it "provides educational negative failure messages" do #given owner = create_collection_owner_with(3) length_matcher = have_at_least(3).items_in_collection_with_length_method size_matcher = have_at_least(3).items_in_collection_with_size_method count_matcher = have_at_least(3).items_in_collection_with_count_method #when length_matcher.matches?(owner) size_matcher.matches?(owner) count_matcher.matches?(owner) #then expect(length_matcher.failure_message_for_should_not).to eq <<-EOF Isn't life confusing enough? Instead of having to figure out the meaning of this: expect(actual).not_to have_at_least(3).items_in_collection_with_length_method We recommend that you use this instead: expect(actual).to have_at_most(2).items_in_collection_with_length_method EOF expect(size_matcher.failure_message_for_should_not).to eq <<-EOF Isn't life confusing enough? Instead of having to figure out the meaning of this: expect(actual).not_to have_at_least(3).items_in_collection_with_size_method We recommend that you use this instead: expect(actual).to have_at_most(2).items_in_collection_with_size_method EOF expect(count_matcher.failure_message_for_should_not).to eq <<-EOF Isn't life confusing enough? Instead of having to figure out the meaning of this: expect(actual).not_to have_at_least(3).items_in_collection_with_count_method We recommend that you use this instead: expect(actual).to have_at_most(2).items_in_collection_with_count_method EOF end end describe "expect(...).to have_at_most(n).items" do it "passes if target has a collection of items with n members" do owner = create_collection_owner_with(3) expect(owner).to have_at_most(3).items_in_collection_with_length_method expect(owner).to have_at_most(3).items_in_collection_with_size_method expect(owner).to have_at_most(3).items_in_collection_with_count_method end it "fails if target has a collection of items with > n members" do owner = create_collection_owner_with(3) expect { expect(owner).to have_at_most(2).items_in_collection_with_length_method }.to fail_with("expected at most 2 items_in_collection_with_length_method, got 3") expect { expect(owner).to have_at_most(2).items_in_collection_with_size_method }.to fail_with("expected at most 2 items_in_collection_with_size_method, got 3") expect { expect(owner).to have_at_most(2).items_in_collection_with_count_method }.to fail_with("expected at most 2 items_in_collection_with_count_method, got 3") end it "passes if target has a collection of items with < n members" do owner = create_collection_owner_with(3) expect(owner).to have_at_most(4).items_in_collection_with_length_method expect(owner).to have_at_most(4).items_in_collection_with_size_method expect(owner).to have_at_most(4).items_in_collection_with_count_method end it "provides educational negative failure messages" do #given owner = create_collection_owner_with(3) length_matcher = have_at_most(3).items_in_collection_with_length_method size_matcher = have_at_most(3).items_in_collection_with_size_method count_matcher = have_at_most(3).items_in_collection_with_count_method #when length_matcher.matches?(owner) size_matcher.matches?(owner) count_matcher.matches?(owner) #then expect(length_matcher.failure_message_for_should_not).to eq <<-EOF Isn't life confusing enough? Instead of having to figure out the meaning of this: expect(actual).not_to have_at_most(3).items_in_collection_with_length_method We recommend that you use this instead: expect(actual).to have_at_least(4).items_in_collection_with_length_method EOF expect(size_matcher.failure_message_for_should_not).to eq <<-EOF Isn't life confusing enough? Instead of having to figure out the meaning of this: expect(actual).not_to have_at_most(3).items_in_collection_with_size_method We recommend that you use this instead: expect(actual).to have_at_least(4).items_in_collection_with_size_method EOF expect(count_matcher.failure_message_for_should_not).to eq <<-EOF Isn't life confusing enough? Instead of having to figure out the meaning of this: expect(actual).not_to have_at_most(3).items_in_collection_with_count_method We recommend that you use this instead: expect(actual).to have_at_least(4).items_in_collection_with_count_method EOF end end describe "have(n).items(args, block)" do it "passes args to target" do target = double("target") target.should_receive(:items).with("arg1","arg2").and_return([1,2,3]) expect(target).to have(3).items("arg1","arg2") end it "passes block to target" do target = double("target") block = lambda { 5 } target.should_receive(:items).with("arg1","arg2", block).and_return([1,2,3]) expect(target).to have(3).items("arg1","arg2", block) end end describe "have(n).items where target IS a collection" do it "references the number of items IN the collection" do expect([1,2,3]).to have(3).items end it "fails when the number of items IN the collection is not as expected" do expect { expect([1,2,3]).to have(7).items }.to fail_with("expected 7 items, got 3") end end describe "have(n).characters where target IS a String" do it "passes if the length is correct" do expect("this string").to have(11).characters end it "fails if the length is incorrect" do expect { expect("this string").to have(12).characters }.to fail_with("expected 12 characters, got 11") end end describe "have(n).things on an object which is not a collection nor contains one" do it "fails" do expect { expect(Object.new).to have(2).things }.to raise_error(NoMethodError) { |e| expect(e.name).to eq :things } end end describe RSpec::Matchers::BuiltIn::Have, "for a collection owner that implements #send" do before(:each) do @collection = Object.new def @collection.floozles; [1,2] end def @collection.send; :sent; end end it "works in the straightforward case" do expect(@collection).to have(2).floozles end it "works when doing automatic pluralization" do expect(@collection).to have_at_least(1).floozle end it "blows up when the owner doesn't respond to that method" do expect { expect(@collection).to have(99).problems }.to raise_error(NoMethodError, /problems/) end it 'works when #send is defined directly on an array' do array = [1, 2] def array.send; :sent; end expect(array).to have(2).items end end if RUBY_VERSION >= '2.0' describe RSpec::Matchers::BuiltIn::Have, "for an Enumerator whose size is nil but count is supplied" do let(:enumerator) { %w[a b c d].to_enum(:each) } it 'works fine' do expect(enumerator).to have(4).items end end end describe RSpec::Matchers::BuiltIn::Have do it "has method_missing as private" do expect(described_class.private_instance_methods).to include_method(:method_missing) end it "does not respond_to? method_missing (because it's private)" do formatter = described_class.new(0, StringIO.new) expect(formatter).not_to respond_to(:method_missing) end describe "respond_to?" do before :each do @have = described_class.new(:foo) @a_method_which_have_defines = described_class.instance_methods.first @a_method_which_object_defines = Object.instance_methods.first end it "is true for a method which Have defines" do expect(@have).to respond_to(@a_method_which_have_defines) end it "is true for a method that it's superclass (Object) defines" do expect(@have).to respond_to(@a_method_which_object_defines) end it "is false for a method which neither Object nor nor Have defines" do expect(@have).not_to respond_to(:foo_bar_baz) end it "is false if the owner doesn't respond to the method" do have = described_class.new(99) expect(have).not_to respond_to(:problems) end it "is true if the owner responds to the method" do have = described_class.new(:a_symbol) expect(have).to respond_to(:to_sym) end end end end rspec-expectations-2.14.2/spec/rspec/matchers/raise_error_spec.rb0000644000004100000410000003540512203207642025204 0ustar www-datawww-datarequire 'spec_helper' describe "expect { ... }.to raise_error" do it_behaves_like("an RSpec matcher", :valid_value => lambda { raise "boom" }, :invalid_value => lambda { }) do let(:matcher) { raise_error(/boom/) } end it "passes if anything is raised" do expect {raise}.to raise_error end it "passes if an error instance is expected" do s = StandardError.new expect {raise s}.to raise_error(s) end it "fails if a different error instance is thrown from the one that is expected" do s = StandardError.new("Error 1") to_raise = StandardError.new("Error 2") expect do expect {raise to_raise}.to raise_error(s) end.to fail_with(Regexp.new("expected #{s.inspect}, got #{to_raise.inspect} with backtrace")) end it "passes if an error class is expected and an instance of that class is thrown" do s = StandardError.new :bees expect { raise s }.to raise_error(StandardError) end it "fails if nothing is raised" do expect { expect {}.to raise_error }.to fail_with("expected Exception but nothing was raised") end end describe "raise_exception aliased to raise_error" do it "passes if anything is raised" do expect {raise}.to raise_exception end end describe "expect { ... }.to raise_error {|err| ... }" do it "passes if there is an error" do ran = false expect { non_existent_method }.to raise_error {|e| ran = true } expect(ran).to be_true end it "passes the error to the block" do error = nil expect { non_existent_method }.to raise_error {|e| error = e } expect(error).to be_kind_of(NameError) end end describe "expect { ... }.not_to raise_error" do context "with a specific error class" do it "is deprecated" do RSpec.should_receive :deprecate expect {"bees"}.not_to raise_error(RuntimeError) end end context "with no specific error class" do it "is not deprecated" do run = nil allow(RSpec).to receive(:deprecate) { run = true } expect {"bees"}.not_to raise_error expect(run).to be_nil end it "passes if nothing is raised" do expect {}.not_to raise_error end it "fails if anything is raised" do expect { expect { raise RuntimeError, "example message" }.not_to raise_error }.to fail_with(/expected no Exception, got #/) end it 'includes the backtrace of the error that was raised in the error message' do expect { expect { raise "boom" }.not_to raise_error }.to raise_error { |e| backtrace_line = "#{File.basename(__FILE__)}:#{__LINE__ - 2}" expect(e.message).to include("with backtrace", backtrace_line) } end it 'formats the backtrace using the configured backtrace formatter' do RSpec::Matchers.configuration.backtrace_formatter. stub(:format_backtrace). and_return("formatted-backtrace") expect { expect { raise "boom" }.not_to raise_error }.to raise_error { |e| expect(e.message).to include("with backtrace", "formatted-backtrace") } end end end describe "expect { ... }.to raise_error(message)" do it "passes if RuntimeError is raised with the right message" do expect {raise 'blah'}.to raise_error('blah') end it "passes if RuntimeError is raised with a matching message" do expect {raise 'blah'}.to raise_error(/blah/) end it "passes if any other error is raised with the right message" do expect {raise NameError.new('blah')}.to raise_error('blah') end it "fails if RuntimeError error is raised with the wrong message" do expect do expect {raise 'blarg'}.to raise_error('blah') end.to fail_with(/expected Exception with \"blah\", got #/) end it "fails if any other error is raised with the wrong message" do expect do expect {raise NameError.new('blarg')}.to raise_error('blah') end.to fail_with(/expected Exception with \"blah\", got #/) end it 'includes the backtrace of any other error in the failure message' do expect { expect { raise "boom" }.to raise_error(ArgumentError) }.to raise_error { |e| backtrace_line = "#{File.basename(__FILE__)}:#{__LINE__ - 2}" expect(e.message).to include("with backtrace", backtrace_line) } end end describe "expect { ... }.not_to raise_error(message)" do before do allow(RSpec).to receive(:deprecate) end it "is deprecated" do expect(RSpec).to receive(:deprecate).with(/not_to raise_error\(message\)/, :replacement =>"`expect { }.not_to raise_error()`") expect {raise 'blarg'}.not_to raise_error('blah') end it "passes if RuntimeError error is raised with the different message" do expect {raise 'blarg'}.not_to raise_error('blah') end it "passes if any other error is raised with the wrong message" do expect {raise NameError.new('blarg')}.not_to raise_error('blah') end it "fails if RuntimeError is raised with message" do expect do expect {raise 'blah'}.not_to raise_error('blah') end.to fail_with(/expected no Exception with "blah", got #/) end it "fails if any other error is raised with message" do expect do expect {raise NameError.new('blah')}.not_to raise_error('blah') end.to fail_with(/expected no Exception with "blah", got #/) end end describe "expect { ... }.to raise_error(NamedError)" do it "passes if named error is raised" do expect { non_existent_method }.to raise_error(NameError) end it "fails if nothing is raised" do expect { expect { }.to raise_error(NameError) }.to fail_with(/expected NameError but nothing was raised/) end it "fails if another error is raised (NameError)" do expect { expect { raise RuntimeError, "example message" }.to raise_error(NameError) }.to fail_with(/expected NameError, got #/) end it "fails if another error is raised (NameError)" do expect { expect { load "non/existent/file" }.to raise_error(NameError) }.to fail_with(/expected NameError, got # "`expect { }.not_to raise_error()`") expect { }.not_to raise_error(NameError) end it "passes if nothing is raised" do expect { }.not_to raise_error(NameError) end it "passes if another error is raised" do expect { raise }.not_to raise_error(NameError) end it "fails if named error is raised" do expect { expect { 1 + 'b' }.not_to raise_error(TypeError) }.to fail_with(/expected no TypeError, got #/) end it "fails if correct error is raised with incorrect message" do expect { expect { raise RuntimeError.new("not the example message") }.to raise_error(RuntimeError, "example message") }.to fail_with(/expected RuntimeError with \"example message\", got #"`expect { }.not_to raise_error()`") expect {}.not_to raise_error(RuntimeError, "example message") end it "passes if nothing is raised" do expect {}.not_to raise_error(RuntimeError, "example message") end it "passes if a different error is raised" do expect { raise }.not_to raise_error(NameError, "example message") end it "passes if same error is raised with different message" do expect { raise RuntimeError.new("not the example message") }.not_to raise_error(RuntimeError, "example message") end it "fails if named error is raised with same message" do expect { expect { raise "example message" }.not_to raise_error(RuntimeError, "example message") }.to fail_with(/expected no RuntimeError with \"example message\", got #/) end end describe "expect { ... }.to raise_error(NamedError, error_message) with Regexp" do it "passes if named error is raised with matching message" do expect { raise "example message" }.to raise_error(RuntimeError, /ample mess/) end it "fails if nothing is raised" do expect { expect {}.to raise_error(RuntimeError, /ample mess/) }.to fail_with(/expected RuntimeError with message matching \/ample mess\/ but nothing was raised/) end it "fails if incorrect error is raised" do expect { expect { raise RuntimeError, "example message" }.to raise_error(NameError, /ample mess/) }.to fail_with(/expected NameError with message matching \/ample mess\/, got #/) end it "fails if correct error is raised with incorrect message" do expect { expect { raise RuntimeError.new("not the example message") }.to raise_error(RuntimeError, /less than ample mess/) }.to fail_with(/expected RuntimeError with message matching \/less than ample mess\/, got #/) end end describe "expect { ... }.not_to raise_error(NamedError, error_message) with Regexp" do before do allow(RSpec).to receive(:deprecate) end it "is deprecated" do expect(RSpec).to receive(:deprecate) expect {}.not_to raise_error(RuntimeError, /ample mess/) end it "passes if nothing is raised" do expect {}.not_to raise_error(RuntimeError, /ample mess/) end it "passes if a different error is raised" do expect { raise }.not_to raise_error(NameError, /ample mess/) end it "passes if same error is raised with non-matching message" do expect { raise RuntimeError.new("non matching message") }.not_to raise_error(RuntimeError, /ample mess/) end it "fails if named error is raised with matching message" do expect { expect { raise "example message" }.not_to raise_error(RuntimeError, /ample mess/) }.to fail_with(/expected no RuntimeError with message matching \/ample mess\/, got #/) end end describe "expect { ... }.to raise_error(NamedError, error_message) { |err| ... }" do it "yields exception if named error is raised with same message" do ran = false expect { raise "example message" }.to raise_error(RuntimeError, "example message") { |err| ran = true expect(err.class).to eq RuntimeError expect(err.message).to eq "example message" } expect(ran).to be(true) end it "yielded block fails on it's own right" do ran, passed = false, false expect { expect { raise "example message" }.to raise_error(RuntimeError, "example message") { |err| ran = true expect(5).to eq 4 passed = true } }.to fail_with(/expected: 4/m) expect(ran).to be_true expect(passed).to be_false end it "does NOT yield exception if no error was thrown" do ran = false expect { expect {}.to raise_error(RuntimeError, "example message") { |err| ran = true } }.to fail_with(/expected RuntimeError with \"example message\" but nothing was raised/) expect(ran).to eq false end it "does not yield exception if error class is not matched" do ran = false expect { expect { raise "example message" }.to raise_error(SyntaxError, "example message") { |err| ran = true } }.to fail_with(/expected SyntaxError with \"example message\", got #/) expect(ran).to eq false end it "does NOT yield exception if error message is not matched" do ran = false expect { expect { raise "example message" }.to raise_error(RuntimeError, "different message") { |err| ran = true } }.to fail_with(/expected RuntimeError with \"different message\", got #/) expect(ran).to eq false end end describe "expect { ... }.not_to raise_error(NamedError, error_message) { |err| ... }" do before do allow(RSpec).to receive(:deprecate) end it "is deprecated" do expect(RSpec).to receive(:deprecate) expect {}.not_to raise_error(RuntimeError, "example message") { |err| } end it "passes if nothing is raised" do ran = false expect {}.not_to raise_error(RuntimeError, "example message") { |err| ran = true } expect(ran).to eq false end it "passes if a different error is raised" do ran = false expect { raise }.not_to raise_error(NameError, "example message") { |err| ran = true } expect(ran).to eq false end it "passes if same error is raised with different message" do ran = false expect { raise RuntimeError.new("not the example message") }.not_to raise_error(RuntimeError, "example message") { |err| ran = true } expect(ran).to eq false end it "fails if named error is raised with same message" do ran = false expect { expect { raise "example message" }.not_to raise_error(RuntimeError, "example message") { |err| ran = true } }.to fail_with(/expected no RuntimeError with \"example message\", got #/) expect(ran).to eq false end it 'skips the error verification block when using the expect {...}.to syntax' do ran = false expect { expect { raise "example message" }.not_to raise_error(RuntimeError, "example message") { |err| ran = true } }.to fail_with(/expected no RuntimeError with \"example message\", got #/) expect(ran).to eq false end end describe "misuse of raise_error, with (), not {}" do it "fails with warning" do ::Kernel.should_receive(:warn).with(/`raise_error` was called with non-proc object 1\.7/) expect { expect(Math.sqrt(3)).to raise_error }.to fail_with(/nothing was raised/) end end rspec-expectations-2.14.2/spec/rspec/matchers/eql_spec.rb0000644000004100000410000000220312203207642023437 0ustar www-datawww-datarequire 'spec_helper' module RSpec module Matchers describe "eql" do it_behaves_like "an RSpec matcher", :valid_value => 1, :invalid_value => 2 do let(:matcher) { eql(1) } end it "is diffable" do expect(eql(1)).to be_diffable end it "matches when actual.eql?(expected)" do expect(1).to eql(1) end it "does not match when !actual.eql?(expected)" do expect(1).not_to eql(2) end it "describes itself" do matcher = eql(1) matcher.matches?(1) expect(matcher.description).to eq "eql 1" end it "provides message, expected and actual on #failure_message" do matcher = eql("1") matcher.matches?(1) expect(matcher.failure_message_for_should).to eq "\nexpected: \"1\"\n got: 1\n\n(compared using eql?)\n" end it "provides message, expected and actual on #negative_failure_message" do matcher = eql(1) matcher.matches?(1) expect(matcher.failure_message_for_should_not).to eq "\nexpected: value != 1\n got: 1\n\n(compared using eql?)\n" end end end end rspec-expectations-2.14.2/spec/rspec/matchers/cover_spec.rb0000644000004100000410000000372012203207642024001 0ustar www-datawww-datarequire 'spec_helper' if (1..2).respond_to?(:cover?) describe "expect(...).to cover(expected)" do it_behaves_like "an RSpec matcher", :valid_value => (1..10), :invalid_value => (20..30) do let(:matcher) { cover(5) } end context "for a range target" do it "passes if target covers expected" do expect((1..10)).to cover(5) end it "fails if target does not cover expected" do expect { expect((1..10)).to cover(11) }.to fail_with("expected 1..10 to cover 11") end end end describe "expect(...).to cover(with, multiple, args)" do context "for a range target" do it "passes if target covers all items" do expect((1..10)).to cover(4, 6) end it "fails if target does not cover any one of the items" do expect { expect((1..10)).to cover(4, 6, 11) }.to fail_with("expected 1..10 to cover 4, 6, and 11") end end end describe "expect(...).not_to cover(expected)" do context "for a range target" do it "passes if target does not cover expected" do expect((1..10)).not_to cover(11) end it "fails if target covers expected" do expect { expect((1..10)).not_to cover(5) }.to fail_with("expected 1..10 not to cover 5") end end end describe "expect(...).not_to cover(with, multiple, args)" do context "for a range target" do it "passes if the target does not cover any of the expected" do expect((1..10)).not_to cover(11, 12, 13) end it "fails if the target covers all of the expected" do expect { expect((1..10)).not_to cover(4, 6) }.to fail_with("expected 1..10 not to cover 4 and 6") end it "fails if the target covers some (but not all) of the expected" do expect { expect((1..10)).not_to cover(5, 11) }.to fail_with("expected 1..10 not to cover 5 and 11") end end end end rspec-expectations-2.14.2/spec/rspec/matchers/base_matcher_spec.rb0000644000004100000410000000376012203207642025304 0ustar www-datawww-datarequire 'spec_helper' module RSpec::Matchers::BuiltIn describe BaseMatcher do describe "#match_unless_raises" do let(:matcher) do Class.new(BaseMatcher).new end it "returns true if there are no errors" do expect(matcher.match_unless_raises {}).to be_true end it "returns false if there is an error" do expect(matcher.match_unless_raises { raise }).to be_false end it "returns false if the only submitted error is raised" do expect(matcher.match_unless_raises(RuntimeError){ raise "foo" }).to be_false end it "returns false if any of several errors submitted is raised" do expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise "foo" }).to be_false expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise ArgumentError.new('') }).to be_false expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise NameError.new('') }).to be_false end it "re-raises any error other than one of those specified" do expect do matcher.match_unless_raises(ArgumentError){ raise "foo" } end.to raise_error end it "stores the rescued exception for use in messages" do matcher.match_unless_raises(RuntimeError){ raise "foo" } expect(matcher.rescued_exception).to be_a(RuntimeError) expect(matcher.rescued_exception.message).to eq("foo") end end describe "#==" do it "responds the same way as matches?" do matcher = Class.new(BaseMatcher) do def initialize(expected) @expected = expected end def matches?(actual) (@actual = actual) == @expected end end expect(matcher.new(3).matches?(3)).to be_true expect(matcher.new(3)).to eq(3) expect(matcher.new(3).matches?(4)).to be_false expect(matcher.new(3)).not_to eq(4) end end end end rspec-expectations-2.14.2/spec/rspec/matchers/matchers_spec.rb0000644000004100000410000000157712203207642024501 0ustar www-datawww-datarequire 'spec_helper' module RSpec describe Matchers do let(:sample_matchers) do [:be, :be_close, :be_instance_of, :be_kind_of] end context "once required" do include TestUnitIntegrationSupport it "includes itself in Test::Unit::TestCase" do with_test_unit_loaded do test_unit_case = Test::Unit::TestCase.allocate sample_matchers.each do |sample_matcher| expect(test_unit_case).to respond_to(sample_matcher) end end end it "includes itself in MiniTest::Unit::TestCase", :if => defined?(MiniTest) do with_test_unit_loaded do minitest_case = MiniTest::Unit::TestCase.allocate sample_matchers.each do |sample_matcher| expect(minitest_case).to respond_to(sample_matcher) end end end end end end rspec-expectations-2.14.2/spec/rspec/matchers/dsl_spec.rb0000644000004100000410000000265512203207642023453 0ustar www-datawww-datarequire 'spec_helper' describe "a matcher defined using the matcher DSL" do def question? :answer end def ok "ok" end it "supports calling custom matchers from within other custom matchers" do RSpec::Matchers.define :be_ok do match { |actual| actual == ok } end RSpec::Matchers.define :be_well do match { |actual| expect(actual).to be_ok } end expect(ok).to be_well end it "has access to methods available in the scope of the example" do RSpec::Matchers::define(:matcher_a) {} expect(matcher_a.question?).to eq(:answer) end it "raises when method is missing from local scope as well as matcher" do RSpec::Matchers::define(:matcher_b) {} expect { matcher_b.i_dont_exist }.to raise_error(NameError) end it "clears user instance variables between invocations" do RSpec::Matchers::define(:be_just_like) do |expected| match do |actual| @foo ||= expected @foo == actual end end expect(3).to be_just_like(3) expect(4).to be_just_like(4) end describe "#respond_to?" do it "returns true for methods in example scope" do RSpec::Matchers::define(:matcher_c) {} expect(matcher_c).to respond_to(:question?) end it "returns false for methods not defined in matcher or example scope" do RSpec::Matchers::define(:matcher_d) {} expect(matcher_d).not_to respond_to(:i_dont_exist) end end end rspec-expectations-2.14.2/spec/rspec/matchers/start_with_end_with_spec.rb0000644000004100000410000001312112203207642026730 0ustar www-datawww-datarequire "spec_helper" describe "expect(...).to start_with" do it_behaves_like "an RSpec matcher", :valid_value => "ab", :invalid_value => "bc" do let(:matcher) { start_with("a") } end context "with a string" do it "passes if it matches the start of the actual string" do expect("this string").to start_with "this str" end it "fails if it does not match the start of the actual string" do expect { expect("this string").to start_with "that str" }.to fail_with("expected \"this string\" to start with \"that str\"") end end context "with an array" do it "passes if it is the first element of the array" do expect([0, 1, 2]).to start_with 0 end it "passes if the first elements of the array match" do expect([0, 1, 2]).to start_with 0, 1 end it "fails if it does not match the first element of the array" do expect { expect([0, 1, 2]).to start_with 2 }.to fail_with("expected [0, 1, 2] to start with 2") end it "fails if it the first elements of the array do not match" do expect { expect([0, 1, 2]).to start_with 1, 2 }.to fail_with("expected [0, 1, 2] to start with [1, 2]") end end context "with an object that does not respond to :[]" do it "raises an ArgumentError" do expect { expect(Object.new).to start_with 0 }.to raise_error(ArgumentError, /does not respond to :\[\]/) end end context "with a hash" do it "raises an ArgumentError if trying to match more than one element" do expect{ expect({:a => 'b', :b => 'b', :c => 'c'}).to start_with({:a => 'b', :b => 'b'}) }.to raise_error(ArgumentError, /does not have ordered elements/) end end end describe "expect(...).not_to start_with" do context "with a string" do it "passes if it does not match the start of the actual string" do expect("this string").not_to start_with "that str" end it "fails if it does match the start of the actual string" do expect { expect("this string").not_to start_with "this str" }.to fail_with("expected \"this string\" not to start with \"this str\"") end end context "with an array" do it "passes if it is not the first element of the array" do expect([0, 1, 2]).not_to start_with 2 end it "passes if the first elements of the array do not match" do expect([0, 1, 2]).not_to start_with 1, 2 end it "fails if it matches the first element of the array" do expect { expect([0, 1, 2]).not_to start_with 0 }.to fail_with("expected [0, 1, 2] not to start with 0") end it "fails if it the first elements of the array match" do expect { expect([0, 1, 2]).not_to start_with 0, 1 }.to fail_with("expected [0, 1, 2] not to start with [0, 1]") end end end describe "expect(...).to end_with" do it_behaves_like "an RSpec matcher", :valid_value => "ab", :invalid_value => "bc" do let(:matcher) { end_with("b") } end context "with a string" do it "passes if it matches the end of the actual string" do expect("this string").to end_with "is string" end it "fails if it does not match the end of the actual string" do expect { expect("this string").to end_with "is stringy" }.to fail_with("expected \"this string\" to end with \"is stringy\"") end end context "with an array" do it "passes if it is the last element of the array" do expect([0, 1, 2]).to end_with 2 end it "passes if the last elements of the array match" do expect([0, 1, 2]).to end_with [1, 2] end it "fails if it does not match the last element of the array" do expect { expect([0, 1, 2]).to end_with 1 }.to fail_with("expected [0, 1, 2] to end with 1") end it "fails if it the last elements of the array do not match" do expect { expect([0, 1, 2]).to end_with [0, 1] }.to fail_with("expected [0, 1, 2] to end with [0, 1]") end end context "with an object that does not respond to :[]" do it "raises an error if expected value can't be indexed'" do expect { expect(Object.new).to end_with 0 }.to raise_error(ArgumentError, /does not respond to :\[\]/) end end context "with a hash" do it "raises an ArgumentError if trying to match more than one element" do expect{ expect({:a => 'b', :b => 'b', :c => 'c'}).to end_with({:a => 'b', :b =>'b'}) }.to raise_error(ArgumentError, /does not have ordered elements/) end end end describe "expect(...).not_to end_with" do context "with a sting" do it "passes if it does not match the end of the actual string" do expect("this string").not_to end_with "stringy" end it "fails if it matches the end of the actual string" do expect { expect("this string").not_to end_with "string" }.to fail_with("expected \"this string\" not to end with \"string\"") end end context "an array" do it "passes if it is not the last element of the array" do expect([0, 1, 2]).not_to end_with 1 end it "passes if the last elements of the array do not match" do expect([0, 1, 2]).not_to end_with [0, 1] end it "fails if it matches the last element of the array" do expect { expect([0, 1, 2]).not_to end_with 2 }.to fail_with("expected [0, 1, 2] not to end with 2") end it "fails if it the last elements of the array match" do expect { expect([0, 1, 2]).not_to end_with [1, 2] }.to fail_with("expected [0, 1, 2] not to end with [1, 2]") end end end rspec-expectations-2.14.2/spec/rspec/matchers/match_spec.rb0000644000004100000410000000326412203207642023762 0ustar www-datawww-datarequire 'spec_helper' describe "expect(...).to match(expected)" do it_behaves_like "an RSpec matcher", :valid_value => 'ab', :invalid_value => 'bc' do let(:matcher) { match(/a/) } end it "passes when target (String) matches expected (Regexp)" do expect("string").to match(/tri/) end it "passes when target (String) matches expected (String)" do expect("string").to match("tri") end it "fails when target (String) does not match expected (Regexp)" do expect { expect("string").to match(/rings/) }.to fail end it "fails when target (String) does not match expected (String)" do expect { expect("string").to match("rings") }.to fail end it "provides message, expected and actual on failure" do matcher = match(/rings/) matcher.matches?("string") expect(matcher.failure_message_for_should).to eq "expected \"string\" to match /rings/" end end describe "expect(...).not_to match(expected)" do it "passes when target (String) matches does not match (Regexp)" do expect("string").not_to match(/rings/) end it "passes when target (String) matches does not match (String)" do expect("string").not_to match("rings") end it "fails when target (String) matches expected (Regexp)" do expect { expect("string").not_to match(/tri/) }.to fail end it "fails when target (String) matches expected (String)" do expect { expect("string").not_to match("tri") }.to fail end it "provides message, expected and actual on failure" do matcher = match(/tri/) matcher.matches?("string") expect(matcher.failure_message_for_should_not).to eq "expected \"string\" not to match /tri/" end end rspec-expectations-2.14.2/spec/rspec/matchers/exist_spec.rb0000644000004100000410000000651712203207642024026 0ustar www-datawww-datarequire 'spec_helper' require 'ostruct' describe "exist matcher" do it_behaves_like "an RSpec matcher", :valid_value => OpenStruct.new(:exist? => true), :invalid_value => OpenStruct.new(:exist? => false) do let(:matcher) { exist } end context "when the object does not respond to #exist? or #exists?" do subject { double } [:to, :not_to].each do |expect_method| describe "expect(...).#{expect_method} exist" do it "raises an error" do expect { expect(subject).send(expect_method, exist) }.to raise_error(NoMethodError) end end end end [:exist?, :exists?].each do |predicate| context "when the object responds to ##{predicate}" do describe "expect(...).to exist" do it "passes if #{predicate}" do expect(double(predicate => true)).to exist end it "fails if not #{predicate}" do expect { expect(double(predicate => false)).to exist }.to fail_with(/expected .* to exist/) end it 'works when the object overrides `send`' do klass = Struct.new(:message) do def send :message_sent end define_method predicate do true end end expect(klass.new("msg")).to exist end end describe "expect(...).not_to exist" do it "passes if not #{predicate}" do expect(double(predicate => false)).not_to exist end it "fails if #{predicate}" do expect { expect(double(predicate => true)).not_to exist }.to fail_with(/expected .* not to exist/) end end end end context "when the object responds to #exist? and #exists?" do context "when they both return falsey values" do subject { double(:exist? => false, :exists? => nil) } describe "expect(...).not_to exist" do it "passes" do expect(subject).not_to exist end end describe "expect(...).to exist" do it "fails" do expect { expect(subject).to exist }.to fail_with(/expected .* to exist/) end end end context "when they both return truthy values" do subject { double(:exist? => true, :exists? => "something true") } describe "expect(...).not_to exist" do it "fails" do expect { expect(subject).not_to exist }.to fail_with(/expected .* not to exist/) end end describe "expect(...).to exist" do it "passes" do expect(subject).to exist end end end context "when they return values with different truthiness" do subject { double(:exist? => true, :exists? => false) } [:to, :not_to].each do |expect_method| describe "expect(...).#{expect_method} exist" do it "raises an error" do expect { expect(subject).send(expect_method, exist) }.to raise_error(/#exist\? and #exists\? returned different values/) end end end end end it 'passes any provided arguments to the call to #exist?' do object = double object.should_receive(:exist?).with(:foo, :bar) { true } expect(object).to exist(:foo, :bar) end end rspec-expectations-2.14.2/spec/rspec/matchers/satisfy_spec.rb0000644000004100000410000000211512203207642024342 0ustar www-datawww-datarequire 'spec_helper' describe "expect(...).to satisfy { block }" do it_behaves_like "an RSpec matcher", :valid_value => true, :invalid_value => false do let(:matcher) { satisfy { |v| v } } end it "describes itself" do expect(satisfy.description).to eq("satisfy block") end it "passes if block returns true" do expect(true).to satisfy { |val| val } expect(true).to satisfy do |val| val end end it "fails if block returns false" do expect { expect(false).to satisfy { |val| val } }.to fail_with("expected false to satisfy block") expect do expect(false).to satisfy do |val| val end end.to fail_with("expected false to satisfy block") end end describe "expect(...).not_to satisfy { block }" do it "passes if block returns false" do expect(false).not_to satisfy { |val| val } expect(false).not_to satisfy do |val| val end end it "fails if block returns true" do expect { expect(true).not_to satisfy { |val| val } }.to fail_with("expected true not to satisfy block") end end rspec-expectations-2.14.2/spec/rspec/matchers/operator_matcher_spec.rb0000644000004100000410000001546212203207642026227 0ustar www-datawww-datarequire 'spec_helper' describe "operator matchers", :uses_should do describe "should ==" do it "delegates message to target" do subject = "apple" subject.should_receive(:==).with("apple").and_return(true) subject.should == "apple" end it "returns true on success" do subject = "apple" (subject.should == "apple").should be_true end it "fails when target.==(actual) returns false" do subject = "apple" RSpec::Expectations.should_receive(:fail_with).with(%[expected: "orange"\n got: "apple" (using ==)], "orange", "apple") subject.should == "orange" end end describe "unsupported operators", :if => RUBY_VERSION.to_f == 1.9 do it "raises an appropriate error for should != expected" do expect { "apple".should != "pear" }.to raise_error(/does not support `should != expected`. Use `should_not == expected`/) end it "raises an appropriate error for should_not != expected" do expect { "apple".should_not != "pear" }.to raise_error(/does not support `should_not != expected`. Use `should == expected`/) end it "raises an appropriate error for should !~ expected" do expect { "apple".should !~ /regex/ }.to raise_error(/does not support `should !~ expected`. Use `should_not =~ expected`/) end it "raises an appropriate error for should_not !~ expected" do expect { "apple".should_not !~ /regex/ }.to raise_error(/does not support `should_not !~ expected`. Use `should =~ expected`/) end end describe "should_not ==" do it "delegates message to target" do subject = "orange" subject.should_receive(:==).with("apple").and_return(false) subject.should_not == "apple" end it "returns true on success" do subject = "apple" (subject.should_not == "orange").should be_false end it "fails when target.==(actual) returns false" do subject = "apple" RSpec::Expectations.should_receive(:fail_with).with(%[expected not: == "apple"\n got: "apple"], "apple", "apple") subject.should_not == "apple" end end describe "should ===" do it "delegates message to target" do subject = "apple" subject.should_receive(:===).with("apple").and_return(true) subject.should === "apple" end it "fails when target.===(actual) returns false" do subject = "apple" subject.should_receive(:===).with("orange").and_return(false) RSpec::Expectations.should_receive(:fail_with).with(%[expected: "orange"\n got: "apple" (using ===)], "orange", "apple") subject.should === "orange" end end describe "should_not ===" do it "delegates message to target" do subject = "orange" subject.should_receive(:===).with("apple").and_return(false) subject.should_not === "apple" end it "fails when target.===(actual) returns false" do subject = "apple" subject.should_receive(:===).with("apple").and_return(true) RSpec::Expectations.should_receive(:fail_with).with(%[expected not: === "apple"\n got: "apple"], "apple", "apple") subject.should_not === "apple" end end describe "should =~" do it "delegates message to target" do subject = "foo" subject.should_receive(:=~).with(/oo/).and_return(true) subject.should =~ /oo/ end it "fails when target.=~(actual) returns false" do subject = "fu" subject.should_receive(:=~).with(/oo/).and_return(false) RSpec::Expectations.should_receive(:fail_with).with(%[expected: /oo/\n got: "fu" (using =~)], /oo/, "fu") subject.should =~ /oo/ end end describe "should_not =~" do it "delegates message to target" do subject = "fu" subject.should_receive(:=~).with(/oo/).and_return(false) subject.should_not =~ /oo/ end it "fails when target.=~(actual) returns false" do subject = "foo" subject.should_receive(:=~).with(/oo/).and_return(true) RSpec::Expectations.should_receive(:fail_with).with(%[expected not: =~ /oo/\n got: "foo"], /oo/, "foo") subject.should_not =~ /oo/ end end describe "should >" do it "passes if > passes" do 4.should > 3 end it "fails if > fails" do RSpec::Expectations.should_receive(:fail_with).with(%[expected: > 5\n got: 4], 5, 4) 4.should > 5 end end describe "should >=" do it "passes if actual == expected" do 4.should >= 4 end it "passes if actual > expected" do 4.should >= 3 end it "fails if > fails" do RSpec::Expectations.should_receive(:fail_with).with(%[expected: >= 5\n got: 4], 5, 4) 4.should >= 5 end end describe "should <" do it "passes if < passes" do 4.should < 5 end it "fails if > fails" do RSpec::Expectations.should_receive(:fail_with).with(%[expected: < 3\n got: 4], 3, 4) 4.should < 3 end end describe "should <=" do it "passes if actual == expected" do 4.should <= 4 end it "passes if actual < expected" do 4.should <= 5 end it "fails if > fails" do RSpec::Expectations.should_receive(:fail_with).with(%[expected: <= 3\n got: 4], 3, 4) 4.should <= 3 end end describe "OperatorMatcher registry" do let(:custom_klass) { Class.new } let(:custom_subklass) { Class.new(custom_klass) } after { RSpec::Matchers::OperatorMatcher.unregister(custom_klass, "=~") } it "allows operator matchers to be registered for types" do RSpec::Matchers::OperatorMatcher.register(custom_klass, "=~", RSpec::Matchers::BuiltIn::Match) expect(RSpec::Matchers::OperatorMatcher.get(custom_klass, "=~")).to eq(RSpec::Matchers::BuiltIn::Match) end it "considers ancestors when finding an operator matcher" do RSpec::Matchers::OperatorMatcher.register(custom_klass, "=~", RSpec::Matchers::BuiltIn::Match) expect(RSpec::Matchers::OperatorMatcher.get(custom_subklass, "=~")).to eq(RSpec::Matchers::BuiltIn::Match) end it "returns nil if there is no matcher registered for a type" do expect(RSpec::Matchers::OperatorMatcher.get(custom_klass, "=~")).to be_nil end end describe RSpec::Matchers::BuiltIn::PositiveOperatorMatcher do it "works when the target has implemented #send" do o = Object.new def o.send(*args); raise "DOH! Library developers shouldn't use #send!" end expect { o.should == o }.not_to raise_error end end describe RSpec::Matchers::BuiltIn::NegativeOperatorMatcher do it "works when the target has implemented #send" do o = Object.new def o.send(*args); raise "DOH! Library developers shouldn't use #send!" end expect { o.should_not == :foo }.not_to raise_error end end end rspec-expectations-2.14.2/spec/rspec/matchers/respond_to_spec.rb0000644000004100000410000002165212203207642025043 0ustar www-datawww-datarequire 'spec_helper' describe "expect(...).to respond_to(:sym)" do it_behaves_like "an RSpec matcher", :valid_value => "s", :invalid_value => 5 do let(:matcher) { respond_to(:upcase) } end it "passes if target responds to :sym" do expect(Object.new).to respond_to(:methods) end it "fails if target does not respond to :sym" do expect { expect("this string").to respond_to(:some_method) }.to fail_with(%q|expected "this string" to respond to :some_method|) end end describe "expect(...).to respond_to(:sym).with(1).argument" do it "passes if target responds to :sym with 1 arg" do obj = Object.new def obj.foo(arg); end expect(obj).to respond_to(:foo).with(1).argument end it "passes if target responds to any number of arguments" do obj = Object.new def obj.foo(*args); end expect(obj).to respond_to(:foo).with(1).argument end it "passes if target responds to one or more arguments" do obj = Object.new def obj.foo(a, *args); end expect(obj).to respond_to(:foo).with(1).argument end it "fails if target does not respond to :sym" do obj = Object.new expect { expect(obj).to respond_to(:some_method).with(1).argument }.to fail_with(/expected .* to respond to :some_method/) end it "fails if :sym expects 0 args" do obj = Object.new def obj.foo; end expect { expect(obj).to respond_to(:foo).with(1).argument }.to fail_with(/expected # to respond to :foo with 1 argument/) end it "fails if :sym expects 2 args" do obj = Object.new def obj.foo(arg, arg2); end expect { expect(obj).to respond_to(:foo).with(1).argument }.to fail_with(/expected # to respond to :foo with 1 argument/) end it "fails if :sym expects 2 or more args" do obj = Object.new def obj.foo(arg, arg2, *args); end expect { expect(obj).to respond_to(:foo).with(1).argument }.to fail_with(/expected # to respond to :foo with 1 argument/) end end describe "expect(...).to respond_to(message1, message2)" do it "passes if target responds to both messages" do expect(Object.new).to respond_to('methods', 'inspect') end it "fails if target does not respond to first message" do expect { expect(Object.new).to respond_to('method_one', 'inspect') }.to fail_with(/expected # to respond to "method_one"/) end it "fails if target does not respond to second message" do expect { expect(Object.new).to respond_to('inspect', 'method_one') }.to fail_with(/expected # to respond to "method_one"/) end it "fails if target does not respond to either message" do expect { expect(Object.new).to respond_to('method_one', 'method_two') }.to fail_with(/expected # to respond to "method_one", "method_two"/) end end describe "expect(...).to respond_to(:sym).with(2).arguments" do it "passes if target responds to :sym with 2 args" do obj = Object.new def obj.foo(a1, a2); end expect(obj).to respond_to(:foo).with(2).arguments end it "passes if target responds to any number of arguments" do obj = Object.new def obj.foo(*args); end expect(obj).to respond_to(:foo).with(2).arguments end it "passes if target responds to one or more arguments" do obj = Object.new def obj.foo(a, *args); end expect(obj).to respond_to(:foo).with(2).arguments end it "passes if target responds to two or more arguments" do obj = Object.new def obj.foo(a, b, *args); end expect(obj).to respond_to(:foo).with(2).arguments end it "fails if target does not respond to :sym" do obj = Object.new expect { expect(obj).to respond_to(:some_method).with(2).arguments }.to fail_with(/expected .* to respond to :some_method/) end it "fails if :sym expects 0 args" do obj = Object.new def obj.foo; end expect { expect(obj).to respond_to(:foo).with(2).arguments }.to fail_with(/expected # to respond to :foo with 2 arguments/) end it "fails if :sym expects 1 args" do obj = Object.new def obj.foo(arg); end expect { expect(obj).to respond_to(:foo).with(2).arguments }.to fail_with(/expected # to respond to :foo with 2 arguments/) end it "fails if :sym expects 3 or more args" do obj = Object.new def obj.foo(arg, arg2, arg3, *args); end expect { expect(obj).to respond_to(:foo).with(2).arguments }.to fail_with(/expected # to respond to :foo with 2 arguments/) end end describe "expect(...).not_to respond_to(:sym)" do it "passes if target does not respond to :sym" do expect(Object.new).not_to respond_to(:some_method) end it "fails if target responds to :sym" do expect { expect(Object.new).not_to respond_to(:methods) }.to fail_with(/expected # not to respond to :methods/) end end describe "expect(...).not_to respond_to(:sym).with(1).argument" do it "fails if target responds to :sym with 1 arg" do obj = Object.new def obj.foo(arg); end expect { expect(obj).not_to respond_to(:foo).with(1).argument }.to fail_with(/expected # not to respond to :foo with 1 argument/) end it "fails if target responds to :sym with any number of args" do obj = Object.new def obj.foo(*args); end expect { expect(obj).not_to respond_to(:foo).with(1).argument }.to fail_with(/expected # not to respond to :foo with 1 argument/) end it "fails if target responds to :sym with one or more args" do obj = Object.new def obj.foo(a, *args); end expect { expect(obj).not_to respond_to(:foo).with(1).argument }.to fail_with(/expected # not to respond to :foo with 1 argument/) end it "passes if target does not respond to :sym" do obj = Object.new expect(obj).not_to respond_to(:some_method).with(1).argument end it "passes if :sym expects 0 args" do obj = Object.new def obj.foo; end expect(obj).not_to respond_to(:foo).with(1).argument end it "passes if :sym expects 2 args" do obj = Object.new def obj.foo(arg, arg2); end expect(obj).not_to respond_to(:foo).with(1).argument end it "passes if :sym expects 2 or more args" do obj = Object.new def obj.foo(arg, arg2, *args); end expect(obj).not_to respond_to(:foo).with(1).argument end end describe "expect(...).not_to respond_to(message1, message2)" do it "passes if target does not respond to either message1 or message2" do expect(Object.new).not_to respond_to(:some_method, :some_other_method) end it "fails if target responds to message1 but not message2" do expect { expect(Object.new).not_to respond_to(:object_id, :some_method) }.to fail_with(/expected # not to respond to :object_id/) end it "fails if target responds to message2 but not message1" do expect { expect(Object.new).not_to respond_to(:some_method, :object_id) }.to fail_with(/expected # not to respond to :object_id/) end it "fails if target responds to both message1 and message2" do expect { expect(Object.new).not_to respond_to(:class, :object_id) }.to fail_with(/expected # not to respond to :class, :object_id/) end end describe "expect(...).not_to respond_to(:sym).with(2).arguments" do it "fails if target responds to :sym with 2 args" do obj = Object.new def obj.foo(a1, a2); end expect { expect(obj).not_to respond_to(:foo).with(2).arguments }.to fail_with(/expected .* not to respond to :foo with 2 arguments/) end it "fails if target responds to :sym with any number args" do obj = Object.new def obj.foo(*args); end expect { expect(obj).not_to respond_to(:foo).with(2).arguments }.to fail_with(/expected .* not to respond to :foo with 2 arguments/) end it "fails if target responds to :sym with one or more args" do obj = Object.new def obj.foo(a, *args); end expect { expect(obj).not_to respond_to(:foo).with(2).arguments }.to fail_with(/expected .* not to respond to :foo with 2 arguments/) end it "fails if target responds to :sym with two or more args" do obj = Object.new def obj.foo(a, b, *args); end expect { expect(obj).not_to respond_to(:foo).with(2).arguments }.to fail_with(/expected .* not to respond to :foo with 2 arguments/) end it "passes if target does not respond to :sym" do obj = Object.new expect(obj).not_to respond_to(:some_method).with(2).arguments end it "passes if :sym expects 0 args" do obj = Object.new def obj.foo; end expect(obj).not_to respond_to(:foo).with(2).arguments end it "passes if :sym expects 2 args" do obj = Object.new def obj.foo(arg); end expect(obj).not_to respond_to(:foo).with(2).arguments end it "passes if :sym expects 3 or more args" do obj = Object.new def obj.foo(a, b, c, *arg); end expect(obj).not_to respond_to(:foo).with(2).arguments end end rspec-expectations-2.14.2/spec/rspec/matchers/description_generation_spec.rb0000644000004100000410000001417612203207642027430 0ustar www-datawww-datarequire 'spec_helper' describe "Matchers should be able to generate their own descriptions" do after(:each) do RSpec::Matchers.clear_generated_description end it "expect(...).to eq expected" do expect("this").to eq "this" expect(RSpec::Matchers.generated_description).to eq "should eq \"this\"" end it "expect(...).to not eq expected" do expect("this").not_to eq "that" expect(RSpec::Matchers.generated_description).to eq "should not eq \"that\"" end it "expect(...).to be empty (arbitrary predicate)" do expect([]).to be_empty expect(RSpec::Matchers.generated_description).to eq "should be empty" end it "expect(...).to not be empty (arbitrary predicate)" do expect([1]).not_to be_empty expect(RSpec::Matchers.generated_description).to eq "should not be empty" end it "expect(...).to be true" do expect(true).to be_true expect(RSpec::Matchers.generated_description).to eq "should be true" end it "expect(...).to be false" do expect(false).to be_false expect(RSpec::Matchers.generated_description).to eq "should be false" end it "expect(...).to be nil" do expect(nil).to be_nil expect(RSpec::Matchers.generated_description).to eq "should be nil" end it "expect(...).to be > n" do expect(5).to be > 3 expect(RSpec::Matchers.generated_description).to eq "should be > 3" end it "expect(...).to be predicate arg1, arg2 and arg3" do expect(5.0).to be_between(0,10) expect(RSpec::Matchers.generated_description).to eq "should be between 0 and 10" end it "expect(...).to equal" do expected = "expected" expect(expected).to equal(expected) expect(RSpec::Matchers.generated_description).to eq "should equal \"expected\"" end it "expect(...).not_to equal" do expect(5).not_to equal(37) expect(RSpec::Matchers.generated_description).to eq "should not equal 37" end it "expect(...).to eql" do expect("string").to eql("string") expect(RSpec::Matchers.generated_description).to eq "should eql \"string\"" end it "expect(...).not_to eql" do expect("a").not_to eql(:a) expect(RSpec::Matchers.generated_description).to eq "should not eql :a" end it "expect(...).to have_key" do expect({:a => "a"}).to have_key(:a) expect(RSpec::Matchers.generated_description).to eq "should have key :a" end it "expect(...).to have_some_method" do object = Object.new def object.has_eyes_closed?; true; end expect(object).to have_eyes_closed expect(RSpec::Matchers.generated_description).to eq 'should have eyes closed' end it "expect(...).to have_some_method(args*)" do object = Object.new def object.has_taste_for?(*args); true; end expect(object).to have_taste_for("wine", "cheese") expect(RSpec::Matchers.generated_description).to eq 'should have taste for "wine", "cheese"' end it "expect(...).to have n items" do expect(team).to have(3).players expect(RSpec::Matchers.generated_description).to eq "should have 3 players" end it "expect(...).to have at least n items" do expect(team).to have_at_least(2).players expect(RSpec::Matchers.generated_description).to eq "should have at least 2 players" end it "expect(...).to have at most n items" do expect(team).to have_at_most(4).players expect(RSpec::Matchers.generated_description).to eq "should have at most 4 players" end it "expect(...).to include(x)" do expect([1,2,3]).to include(3) expect(RSpec::Matchers.generated_description).to eq "should include 3" end it "expect(...).to include(x) when x responds to description but is not a matcher" do obj = double(:description => "description", :inspect => "inspect") expect([obj]).to include(obj) expect(RSpec::Matchers.generated_description).to eq "should include inspect" end it "expect(...).to include(x) when x responds to description and is a matcher" do matcher = double(:description => "description", :matches? => true, :failure_message_for_should => "") expect([matcher]).to include(matcher) expect(RSpec::Matchers.generated_description).to eq "should include description" end it "expect(array).not_to match_array [1,2,3]" do expect([1,2,3]).to match_array [1,2,3] expect(RSpec::Matchers.generated_description).to eq "should contain exactly 1, 2 and 3" end it "expect(...).to match" do expect("this string").to match(/this string/) expect(RSpec::Matchers.generated_description).to eq "should match /this string/" end it "expect(...).to raise_error" do expect { raise }.to raise_error expect(RSpec::Matchers.generated_description).to eq "should raise Exception" end it "expect(...).to raise_error with type" do expect { raise }.to raise_error(RuntimeError) expect(RSpec::Matchers.generated_description).to eq "should raise RuntimeError" end it "expect(...).to raise_error with type and message" do expect { raise "there was an error" }.to raise_error(RuntimeError, "there was an error") expect(RSpec::Matchers.generated_description).to eq "should raise RuntimeError with \"there was an error\"" end it "expect(...).to respond_to" do expect([]).to respond_to(:insert) expect(RSpec::Matchers.generated_description).to eq "should respond to #insert" end it "expect(...).to throw symbol" do expect { throw :what_a_mess }.to throw_symbol expect(RSpec::Matchers.generated_description).to eq "should throw a Symbol" end it "expect(...).to throw symbol (with named symbol)" do expect { throw :what_a_mess }.to throw_symbol(:what_a_mess) expect(RSpec::Matchers.generated_description).to eq "should throw :what_a_mess" end def team Class.new do def players [1,2,3] end end.new end end describe "a Matcher with no description" do def matcher Class.new do def matches?(ignore); true; end def failure_message; ""; end end.new end it "provides a helpful message when used in a string-less example block" do expect(5).to matcher expect(RSpec::Matchers.generated_description).to match(/When you call.*description method/m) end end rspec-expectations-2.14.2/spec/rspec/matchers/match_array_spec.rb0000644000004100000410000001201712203207642025154 0ustar www-datawww-datarequire 'spec_helper' class UnsortableObject def initialize(id) @id = id end def inspect @id.to_s end def ==(other) false end end describe "should =~ array", :uses_should do it "passes a valid positive expectation" do [1, 2].should =~ [2, 1] end it "fails an invalid positive expectation" do expect { [1, 2, 3].should =~ [2, 1] }.to fail_with(/expected collection contained/) end context "when the array defines a `=~` method" do it 'delegates to that method rather than using the match_array matcher' do array = [] def array.=~(other) other == :foo end array.should =~ :foo expect { array.should =~ :bar }.to fail_with(/expected: :bar/) end end context 'when the array defines a `send` method' do it 'still works' do array = [1, 2] def array.send; :sent; end array.should =~ array end end end describe "should_not =~ [:with, :multiple, :args]", :uses_should do it "is not supported" do expect { [1,2,3].should_not =~ [1,2,3] }.to fail_with(/Matcher does not support should_not/) end end describe "using match_array with expect" do it "passes a valid positive expectation" do expect([1, 2]).to match_array [2, 1] end it "fails an invalid positive expectation" do expect { expect([1, 2, 3]).to match_array [2, 1] }.to fail_with(/expected collection contained/) end end describe "expect(array).to match_array other_array" do it_behaves_like "an RSpec matcher", :valid_value => [1, 2], :invalid_value => [1] do let(:matcher) { match_array([2, 1]) } end it "passes if target contains all items" do expect([1,2,3]).to match_array [1,2,3] end it "passes if target contains all items out of order" do expect([1,3,2]).to match_array [1,2,3] end it "fails if target includes extra items" do expect { expect([1,2,3,4]).to match_array [1,2,3] }.to fail_with(<<-MESSAGE) expected collection contained: [1, 2, 3] actual collection contained: [1, 2, 3, 4] the extra elements were: [4] MESSAGE end it "fails if target is missing items" do expect { expect([1,2]).to match_array [1,2,3] }.to fail_with(<<-MESSAGE) expected collection contained: [1, 2, 3] actual collection contained: [1, 2] the missing elements were: [3] MESSAGE end it "fails if target is missing items and has extra items" do expect { expect([1,2,4]).to match_array [1,2,3] }.to fail_with(<<-MESSAGE) expected collection contained: [1, 2, 3] actual collection contained: [1, 2, 4] the missing elements were: [3] the extra elements were: [4] MESSAGE end it "sorts items in the error message if they all respond to <=>" do expect { expect([6,2,1,5]).to match_array [4,1,2,3] }.to fail_with(<<-MESSAGE) expected collection contained: [1, 2, 3, 4] actual collection contained: [1, 2, 5, 6] the missing elements were: [3, 4] the extra elements were: [5, 6] MESSAGE end it "does not sort items in the error message if they don't all respond to <=>" do expect { expect([UnsortableObject.new(2), UnsortableObject.new(1)]).to match_array [UnsortableObject.new(4), UnsortableObject.new(3)] }.to fail_with(<<-MESSAGE) expected collection contained: [4, 3] actual collection contained: [2, 1] the missing elements were: [4, 3] the extra elements were: [2, 1] MESSAGE end it "accurately reports extra elements when there are duplicates" do expect { expect([1,1,1,5]).to match_array [1,5] }.to fail_with(<<-MESSAGE) expected collection contained: [1, 5] actual collection contained: [1, 1, 1, 5] the extra elements were: [1, 1] MESSAGE end it "accurately reports missing elements when there are duplicates" do expect { expect([1,5]).to match_array [1,1,5] }.to fail_with(<<-MESSAGE) expected collection contained: [1, 1, 5] actual collection contained: [1, 5] the missing elements were: [1] MESSAGE end end describe "expect(...).not_to match_array [:with, :multiple, :args]" do it "is not supported" do expect { expect([1,2,3]).not_to match_array [1,2,3] }.to fail_with(/Matcher does not support should_not/) end end describe "matching against things that aren't arrays" do it "fails with nil and the expected error message is given" do expect { expect(nil).to match_array([1,2,3]) }.to fail_with(/expected an array/) end it "fails with a float and the expected error message is given" do expect { expect((3.7)).to match_array([1,2,3]) }.to fail_with(/expected an array/) end it "fails with a string and the expected error message is given" do expect { expect("I like turtles").to match_array([1,2,3]) }.to fail_with(/expected an array/) end context "when using the `should =~` syntax", :uses_should do it 'fails with a clear message when given a hash' do expect { {}.should =~ {} }.to fail_with(/expected an array/) end end end rspec-expectations-2.14.2/spec/rspec/matchers/method_missing_spec.rb0000644000004100000410000000146712203207642025702 0ustar www-datawww-datarequire 'spec_helper' shared_examples_for "a well-behaved method_missing hook" do include TestUnitIntegrationSupport it "raises a NoMethodError (and not SystemStackError) for an undefined method" do with_test_unit_loaded do expect { subject.some_undefined_method }.to raise_error(NoMethodError) end end end describe "RSpec::Matchers method_missing hook" do subject { self } it_behaves_like "a well-behaved method_missing hook" context 'when invoked in a Test::Unit::TestCase' do subject { Test::Unit::TestCase.allocate } it_behaves_like "a well-behaved method_missing hook" end context 'when invoked in a MiniTest::Unit::TestCase', :if => defined?(MiniTest) do subject { MiniTest::Unit::TestCase.allocate } it_behaves_like "a well-behaved method_missing hook" end end rspec-expectations-2.14.2/spec/rspec/matchers/matcher_spec.rb0000644000004100000410000003411012203207642024303 0ustar www-datawww-datarequire 'spec_helper' class UnexpectedError < StandardError; end module MatcherHelperModule def self.included(base) base.module_eval do def included_method; end end end def self.extended(base) base.instance_eval do def extended_method; end end end def greeting "Hello, World" end end module RSpec::Matchers::DSL describe Matcher do it "can be stored aside and used later" do # Supports using rspec-expectation matchers as argument matchers in # rspec-mocks. RSpec::Matchers.define :example_matcher do |expected| match do |actual| actual == expected end end m1 = example_matcher(1) m2 = example_matcher(2) expect(m1.matches?(1)).to be_true expect(m2.matches?(2)).to be_true end context "with an included module" do let(:matcher) do RSpec::Matchers::DSL::Matcher.new(:be_a_greeting) do include MatcherHelperModule match { |actual| actual == greeting } end.for_expected end it "has access to the module's methods" do matcher.matches?("Hello, World") end it "runs the module's included hook" do expect(matcher).to respond_to(:included_method) end it "does not run the module's extended hook" do expect(matcher).not_to respond_to(:extended_method) end it 'allows multiple modules to be included at once' do m = RSpec::Matchers::DSL::Matcher.new(:multiple_modules) do include Enumerable, Comparable end.for_expected expect(m).to be_a(Enumerable) expect(m).to be_a(Comparable) end end context "without overrides" do before(:each) do @matcher = RSpec::Matchers::DSL::Matcher.new(:be_a_multiple_of) do |multiple| match do |actual| actual % multiple == 0 end end.for_expected(3) end it "provides a default description" do expect(@matcher.description).to eq "be a multiple of 3" end it "provides a default failure message for #should" do @matcher.matches?(8) expect(@matcher.failure_message_for_should).to eq "expected 8 to be a multiple of 3" end it "provides a default failure message for #should_not" do @matcher.matches?(9) expect(@matcher.failure_message_for_should_not).to eq "expected 9 not to be a multiple of 3" end end context "with separate match logic for should and should not" do let(:matcher) do RSpec::Matchers::DSL::Matcher.new(:to_be_composed_of) do |a, b| match_for_should do |actual| actual == a * b end match_for_should_not do |actual| actual == a + b end end.for_expected(7, 11) end it "invokes the match_for_should block for #matches?" do expect(matcher.matches?(77)).to be_true expect(matcher.matches?(18)).to be_false end it "invokes the match_for_should_not block for #does_not_match?" do expect(matcher.does_not_match?(77)).to be_false expect(matcher.does_not_match?(18)).to be_true end it "provides a default failure message for #should_not" do matcher.does_not_match?(77) expect(matcher.failure_message_for_should_not).to eq "expected 77 not to to be composed of 7 and 11" end end it "allows helper methods to be defined with #define_method to have access to matcher parameters" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) do |a, b| define_method(:sum) { a + b } end.for_expected(3,4) expect(matcher.sum).to eq 7 end it "is not diffable by default" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) {} expect(matcher).not_to be_diffable end it "is diffable when told to be" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) { diffable }.for_expected expect(matcher).to be_diffable end it "provides expected" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) {}.for_expected('expected string') expect(matcher.expected).to eq ['expected string'] end it "provides actual" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) do match {|actual|} end.for_expected('expected string') matcher.matches?('actual string') expect(matcher.actual).to eq 'actual string' end context "wrapping another expectation (should == ...)" do it "returns true if the wrapped expectation passes" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) do |expected| match do |actual| expect(actual).to eq expected end end.for_expected('value') expect(matcher.matches?('value')).to be_true end it "returns false if the wrapped expectation fails" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) do |expected| match do |actual| expect(actual).to eq expected end end.for_expected('value') expect(matcher.matches?('other value')).to be_false end end context "with overrides" do before(:each) do @matcher = RSpec::Matchers::DSL::Matcher.new(:be_boolean) do |boolean| match do |actual| actual end description do "be the boolean #{boolean}" end failure_message_for_should do |actual| "expected #{actual} to be the boolean #{boolean}" end failure_message_for_should_not do |actual| "expected #{actual} not to be the boolean #{boolean}" end end.for_expected(true) end it "does not hide result of match block when true" do expect(@matcher.matches?(true)).to be_true end it "does not hide result of match block when false" do expect(@matcher.matches?(false)).to be_false end it "overrides the description" do expect(@matcher.description).to eq "be the boolean true" end it "overrides the failure message for #should" do @matcher.matches?(false) expect(@matcher.failure_message_for_should).to eq "expected false to be the boolean true" end it "overrides the failure message for #should_not" do @matcher.matches?(true) expect(@matcher.failure_message_for_should_not).to eq "expected true not to be the boolean true" end end context "#new" do it "passes matches? arg to match block" do matcher = RSpec::Matchers::DSL::Matcher.new(:ignore) do match do |actual| actual == 5 end end.for_expected expect(matcher.matches?(5)).to be_true end it "exposes arg submitted through #new to matcher block" do matcher = RSpec::Matchers::DSL::Matcher.new(:ignore) do |expected| match do |actual| actual > expected end end.for_expected(4) expect(matcher.matches?(5)).to be_true end end context "with no args" do before(:each) do @matcher = RSpec::Matchers::DSL::Matcher.new(:matcher_name) do match do |actual| actual == 5 end end.for_expected end it "matches" do expect(@matcher.matches?(5)).to be_true end it "describes" do expect(@matcher.description).to eq "matcher name" end end context "with 1 arg" do before(:each) do @matcher = RSpec::Matchers::DSL::Matcher.new(:matcher_name) do |expected| match do |actual| actual == 5 && expected == 1 end end.for_expected(1) end it "matches" do expect(@matcher.matches?(5)).to be_true end it "describes" do expect(@matcher.description).to eq "matcher name 1" end end context "with multiple args" do before(:each) do @matcher = RSpec::Matchers::DSL::Matcher.new(:matcher_name) do |a,b,c,d| match do |sum| a + b + c + d == sum end end.for_expected(1,2,3,4) end it "matches" do expect(@matcher.matches?(10)).to be_true end it "describes" do expect(@matcher.description).to eq "matcher name 1, 2, 3, and 4" end end it "supports helper methods" do matcher = RSpec::Matchers::DSL::Matcher.new(:be_similar_to) do |sample| match do |actual| similar?(sample, actual) end def similar?(a, b) a.sort == b.sort end end.for_expected([1,2,3]) expect(matcher.matches?([2,3,1])).to be_true end it "supports fluent interface" do matcher = RSpec::Matchers::DSL::Matcher.new(:first_word) do def second_word self end end.for_expected expect(matcher.second_word).to eq matcher end it "treats method missing normally for undeclared methods" do matcher = RSpec::Matchers::DSL::Matcher.new(:ignore) { }.for_expected expect { matcher.non_existent_method }.to raise_error(NoMethodError) end it "has access to other matchers" do matcher = RSpec::Matchers::DSL::Matcher.new(:ignore) do |expected| match do |actual| extend RSpec::Matchers expect(actual).to eql(5 + expected) end end.for_expected(3) expect(matcher.matches?(8)).to be_true end context 'when multiple instances of the same matcher are used in the same example' do RSpec::Matchers.define(:be_like_a) do |expected| match { |actual| actual == expected } description { "be like a #{expected}" } failure_message_for_should { "expected to be like a #{expected}" } failure_message_for_should_not { "expected not to be like a #{expected}" } end # Note: these bugs were only exposed when creating both instances # first, then checking their descriptions/failure messages. # # That's why we eager-instantiate them here. let!(:moose) { be_like_a("moose") } let!(:horse) { be_like_a("horse") } it 'allows them to use the expected value in the description' do expect(horse.description).to eq("be like a horse") expect(moose.description).to eq("be like a moose") end it 'allows them to use the expected value in the positive failure message' do expect(moose.failure_message_for_should).to eq("expected to be like a moose") expect(horse.failure_message_for_should).to eq("expected to be like a horse") end it 'allows them to use the expected value in the negative failure message' do expect(moose.failure_message_for_should_not).to eq("expected not to be like a moose") expect(horse.failure_message_for_should_not).to eq("expected not to be like a horse") end it 'allows them to match separately' do expect("moose").to moose expect("horse").to horse expect("horse").not_to moose expect("moose").not_to horse end end describe "#match_unless_raises" do context "with an assertion" do let(:mod) do Module.new do def assert_equal(a,b) a == b ? nil : (raise UnexpectedError.new("#{b} does not equal #{a}")) end end end let(:matcher) do m = mod RSpec::Matchers::DSL::Matcher.new :equal do |expected| extend m match_unless_raises UnexpectedError do assert_equal expected, actual end end.for_expected(4) end context "with passing assertion" do it "passes" do expect(matcher.matches?(4)).to be_true end end context "with failing assertion" do it "fails" do expect(matcher.matches?(5)).to be_false end it "provides the raised exception" do matcher.matches?(5) expect(matcher.rescued_exception.message).to eq("5 does not equal 4") end end end context "with an unexpected error" do let(:matcher) do RSpec::Matchers::DSL::Matcher.new :foo do |expected| match_unless_raises SyntaxError do |actual| raise "unexpected exception" end end.for_expected(:bar) end it "raises the error" do expect do matcher.matches?(:bar) end.to raise_error("unexpected exception") end end end it "can define chainable methods" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) do chain(:expecting) do |expected_value| @expected_value = expected_value end match { |actual| actual == @expected_value } end.for_expected expect(matcher.expecting('value').matches?('value')).to be_true expect(matcher.expecting('value').matches?('other value')).to be_false end it "prevents name collisions on chainable methods from different matchers" do m1 = RSpec::Matchers::DSL::Matcher.new(:m1) { chain(:foo) { raise "foo in m1" } }.for_expected m2 = RSpec::Matchers::DSL::Matcher.new(:m2) { chain(:foo) { raise "foo in m2" } }.for_expected expect { m1.foo }.to raise_error("foo in m1") expect { m2.foo }.to raise_error("foo in m2") end context "defined using the dsl" do def a_method_in_the_example "method defined in the example" end it "can access methods in the running example" do RSpec::Matchers.define(:__access_running_example) do match do |actual| a_method_in_the_example == "method defined in the example" end end expect(example).to __access_running_example end it "raises NoMethodError for methods not in the running_example" do RSpec::Matchers.define(:__raise_no_method_error) do match do |actual| a_method_not_in_the_example == "method defined in the example" end end expect do expect(example).to __raise_no_method_error end.to raise_error(/RSpec::Matchers::DSL::Matcher/) end end end end rspec-expectations-2.14.2/spec/rspec/matchers/has_spec.rb0000644000004100000410000000663712203207642023450 0ustar www-datawww-datarequire 'spec_helper' describe "expect(...).to have_sym(*args)" do it_behaves_like "an RSpec matcher", :valid_value => { :a => 1 }, :invalid_value => {} do let(:matcher) { have_key(:a) } end it "passes if #has_sym?(*args) returns true" do expect({:a => "A"}).to have_key(:a) end it "fails if #has_sym?(*args) returns false" do expect { expect({:b => "B"}).to have_key(:a) }.to fail_with("expected #has_key?(:a) to return true, got false") end it 'does not include any args in the failure message if no args were given to the matcher' do o = Object.new def o.has_some_stuff?; false; end expect { expect(o).to have_some_stuff }.to fail_with("expected #has_some_stuff? to return true, got false") end it 'includes multiple args in the failure message if multiple args were given to the matcher' do o = Object.new def o.has_some_stuff?(*_); false; end expect { expect(o).to have_some_stuff(:a, 7, "foo") }.to fail_with('expected #has_some_stuff?(:a, 7, "foo") to return true, got false') end it "fails if #has_sym?(*args) returns nil" do klass = Class.new do def has_foo? end end expect { expect(klass.new).to have_foo }.to fail_with(/expected #has_foo.* to return true, got false/) end it "fails if target does not respond to #has_sym?" do expect { expect(Object.new).to have_key(:a) }.to raise_error(NoMethodError) end it "reraises an exception thrown in #has_sym?(*args)" do o = Object.new def o.has_sym?(*args) raise "Funky exception" end expect { expect(o).to have_sym(:foo) }.to raise_error("Funky exception") end end describe "expect(...).not_to have_sym(*args)" do it "passes if #has_sym?(*args) returns false" do expect({:a => "A"}).not_to have_key(:b) end it "passes if #has_sym?(*args) returns nil" do klass = Class.new do def has_foo? end end expect(klass.new).not_to have_foo end it "fails if #has_sym?(*args) returns true" do expect { expect({:a => "A"}).not_to have_key(:a) }.to fail_with("expected #has_key?(:a) to return false, got true") end it "fails if target does not respond to #has_sym?" do expect { expect(Object.new).to have_key(:a) }.to raise_error(NoMethodError) end it "reraises an exception thrown in #has_sym?(*args)" do o = Object.new def o.has_sym?(*args) raise "Funky exception" end expect { expect(o).not_to have_sym(:foo) }.to raise_error("Funky exception") end it 'does not include any args in the failure message if no args were given to the matcher' do o = Object.new def o.has_some_stuff?; true; end expect { expect(o).not_to have_some_stuff }.to fail_with("expected #has_some_stuff? to return false, got true") end it 'includes multiple args in the failure message if multiple args were given to the matcher' do o = Object.new def o.has_some_stuff?(*_); true; end expect { expect(o).not_to have_some_stuff(:a, 7, "foo") }.to fail_with('expected #has_some_stuff?(:a, 7, "foo") to return false, got true') end end describe "has" do it "works when the target implements #send" do o = {:a => "A"} def o.send(*args); raise "DOH! Library developers shouldn't use #send!" end expect { expect(o).to have_key(:a) }.not_to raise_error end end rspec-expectations-2.14.2/spec/rspec/matchers/include_spec.rb0000644000004100000410000004363512203207642024317 0ustar www-datawww-datarequire 'spec_helper' require 'uri' describe "#include matcher" do it "is diffable" do expect(include("a")).to be_diffable end describe "expect(...).to include(with_one_arg)" do it_behaves_like "an RSpec matcher", :valid_value => [1, 2], :invalid_value => [1] do let(:matcher) { include(2) } end context "for a string target" do it "passes if target includes expected" do expect("abc").to include("a") end it "fails if target does not include expected" do expect { expect("abc").to include("d") }.to fail_matching("expected \"abc\" to include \"d\"") end it "includes a diff when actual is multiline" do expect { expect("abc\ndef").to include("g") }.to fail_matching("expected \"abc\\ndef\" to include \"g\"\nDiff") end it "includes a diff when actual is multiline and there are multiple expecteds" do expect { expect("abc\ndef").to include("g", "h") }.to fail_matching("expected \"abc\\ndef\" to include \"g\" and \"h\"\nDiff") end end context "for an array target" do it "passes if target includes expected" do expect([1,2,3]).to include(3) end it "fails if target does not include expected" do expect { expect([1,2,3]).to include(4) }.to fail_matching("expected [1, 2, 3] to include 4") end it 'fails when given differing null doubles' do dbl_1 = double.as_null_object dbl_2 = double.as_null_object expect { expect([dbl_1]).to include(dbl_2) }.to fail_matching("expected [#{dbl_1.inspect}] to include") end it 'passes when given the same null double' do dbl = double.as_null_object expect([dbl]).to include(dbl) end end context "for a hash target" do it 'passes if target has the expected as a key' do expect({:key => 'value'}).to include(:key) end it "fails if target does not include expected" do expect { expect({:key => 'value'}).to include(:other) }.to fail_matching(%Q|expected {:key=>"value"} to include :other|) end it "fails if target doesn't have a key and we expect nil" do expect { expect({}).to include(:something => nil) }.to fail_matching(%Q|expected {} to include {:something=>nil}|) end it 'works even when an entry in the hash overrides #send' do hash = { :key => 'value' } def hash.send; :sent; end expect(hash).to include(hash) end context 'that overrides #send' do it 'still works' do array = [1, 2] def array.send; :sent; end expect(array).to include(*array) end end end end describe "expect(...).to include(with, multiple, args)" do it "has a description" do matcher = include("a") expect(matcher.description).to eq("include \"a\"") end context "for a string target" do it "passes if target includes all items" do expect("a string").to include("str", "a") end it "fails if target does not include any one of the items" do expect { expect("a string").to include("str", "a", "foo") }.to fail_matching(%Q{expected "a string" to include "str", "a", and "foo"}) end end context "for an array target" do it "passes if target includes all items" do expect([1,2,3]).to include(1,2,3) end it "fails if target does not include any one of the items" do expect { expect([1,2,3]).to include(1,2,4) }.to fail_matching("expected [1, 2, 3] to include 1, 2, and 4") end end context "for a hash target" do it 'passes if target includes all items as keys' do expect({:key => 'value', :other => 'value'}).to include(:key, :other) end it 'fails if target is missing any item as a key' do expect { expect({:key => 'value'}).to include(:key, :other) }.to fail_matching(%Q|expected {:key=>"value"} to include :key and :other|) end end end describe "expect(...).not_to include(expected)" do context "for a string target" do it "passes if target does not include expected" do expect("abc").not_to include("d") end it "fails if target includes expected" do expect { expect("abc").not_to include("c") }.to fail_with("expected \"abc\" not to include \"c\"") end end context "for an array target" do it "passes if target does not include expected" do expect([1,2,3]).not_to include(4) end it "fails if target includes expected" do expect { expect([1,2,3]).not_to include(3) }.to fail_with("expected [1, 2, 3] not to include 3") end it 'passes when given differing null doubles' do expect([double.as_null_object]).not_to include(double.as_null_object) end it 'fails when given the same null double' do dbl = double.as_null_object expect { expect([dbl]).not_to include(dbl) }.to fail_matching("expected [#{dbl.inspect}] not to include") end end context "for a hash target" do it 'passes if target does not have the expected as a key' do expect({:other => 'value'}).not_to include(:key) end it "fails if target includes expected key" do expect { expect({:key => 'value'}).not_to include(:key) }.to fail_matching(%Q|expected {:key=>"value"} not to include :key|) end end end describe "expect(...).not_to include(with, multiple, args)" do context "for a string target" do it "passes if the target does not include any of the expected" do expect("abc").not_to include("d", "e", "f") end it "fails if the target includes all of the expected" do expect { expect("abc").not_to include("c", "a") }.to fail_with('expected "abc" not to include "c" and "a"') end it "fails if the target includes some (but not all) of the expected" do expect { expect("abc").not_to include("d", "a") }.to fail_with(%q{expected "abc" not to include "d" and "a"}) end end context "for a hash target" do it "passes if it does not include any of the expected keys" do expect({ :a => 1, :b => 2 }).not_to include(:c, :d) end it "fails if the target includes all of the expected keys" do expect { expect({ :a => 1, :b => 2 }).not_to include(:a, :b) }.to fail_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} not to include :a and :b|) end it "fails if the target includes some (but not all) of the expected keys" do expect { expect({ :a => 1, :b => 2 }).not_to include(:d, :b) }.to fail_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} not to include :d and :b|) end end context "for an array target" do it "passes if the target does not include any of the expected" do expect([1, 2, 3]).not_to include(4, 5, 6) end it "fails if the target includes all of the expected" do expect { expect([1, 2, 3]).not_to include(3, 1) }.to fail_with(%q{expected [1, 2, 3] not to include 3 and 1}) end it "fails if the target includes some (but not all) of the expected" do expect { expect([1, 2, 3]).not_to include(4, 1) }.to fail_with(%q{expected [1, 2, 3] not to include 4 and 1}) end end end describe "expect(...).to include(:key => value)" do context 'for a hash target' do it "passes if target includes the key/value pair" do expect({:key => 'value'}).to include(:key => 'value') end it "passes if target includes the key/value pair among others" do expect({:key => 'value', :other => 'different'}).to include(:key => 'value') end it "fails if target has a different value for key" do expect { expect({:key => 'different'}).to include(:key => 'value') }.to fail_matching(%Q|expected {:key=>"different"} to include {:key=>"value"}|) end it "fails if target has a different key" do expect { expect({:other => 'value'}).to include(:key => 'value') }.to fail_matching(%Q|expected {:other=>"value"} to include {:key=>"value"}|) end end context 'for a non-hash target' do it "fails if the target does not contain the given hash" do expect { expect(['a', 'b']).to include(:key => 'value') }.to fail_matching(%q|expected ["a", "b"] to include {:key=>"value"}|) end it "passes if the target contains the given hash" do expect(['a', { :key => 'value' } ]).to include(:key => 'value') end end end describe "expect(...).not_to include(:key => value)" do context 'for a hash target' do it "fails if target includes the key/value pair" do expect { expect({:key => 'value'}).not_to include(:key => 'value') }.to fail_matching(%Q|expected {:key=>"value"} not to include {:key=>"value"}|) end it "fails if target includes the key/value pair among others" do expect { expect({:key => 'value', :other => 'different'}).not_to include(:key => 'value') }.to fail_matching(%Q|expected #{{:key=>"value", :other=>"different"}.inspect} not to include {:key=>"value"}|) end it "passes if target has a different value for key" do expect({:key => 'different'}).not_to include(:key => 'value') end it "passes if target has a different key" do expect({:other => 'value'}).not_to include(:key => 'value') end end context "for a non-hash target" do it "passes if the target does not contain the given hash" do expect(['a', 'b']).not_to include(:key => 'value') end it "fails if the target contains the given hash" do expect { expect(['a', { :key => 'value' } ]).not_to include(:key => 'value') }.to fail_matching(%Q|expected ["a", {:key=>"value"}] not to include {:key=>"value"}|) end end end describe "expect(...).to include(:key1 => value1, :key2 => value2)" do context 'for a hash target' do it "passes if target includes the key/value pairs" do expect({:a => 1, :b => 2}).to include(:b => 2, :a => 1) end it "passes if target includes the key/value pairs among others" do expect({:a => 1, :c => 3, :b => 2}).to include(:b => 2, :a => 1) end it "fails if target has a different value for one of the keys" do expect { expect({:a => 1, :b => 2}).to include(:a => 2, :b => 2) }.to fail_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} to include #{{:a=>2, :b=>2}.inspect}|) end it "fails if target has a different value for both of the keys" do expect { expect({:a => 1, :b => 1}).to include(:a => 2, :b => 2) }.to fail_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} to include #{{:a=>2, :b=>2}.inspect}|) end it "fails if target lacks one of the keys" do expect { expect({:a => 1, :b => 1}).to include(:a => 1, :c => 1) }.to fail_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} to include #{{:a=>1, :c=>1}.inspect}|) end it "fails if target lacks both of the keys" do expect { expect({:a => 1, :b => 1}).to include(:c => 1, :d => 1) }.to fail_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} to include #{{:c=>1, :d=>1}.inspect}|) end end context 'for a non-hash target' do it "fails if the target does not contain the given hash" do expect { expect(['a', 'b']).to include(:a => 1, :b => 1) }.to fail_matching(%Q|expected ["a", "b"] to include #{{:a=>1, :b=>1}.inspect}|) end it "passes if the target contains the given hash" do expect(['a', { :a => 1, :b => 2 } ]).to include(:a => 1, :b => 2) end end end describe "expect(...).not_to include(:key1 => value1, :key2 => value2)" do context 'for a hash target' do it "fails if target includes the key/value pairs" do expect { expect({:a => 1, :b => 2}).not_to include(:a => 1, :b => 2) }.to fail_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} not to include #{{:a=>1, :b=>2}.inspect}|) end it "fails if target includes the key/value pairs among others" do hash = {:a => 1, :b => 2, :c => 3} expect { expect(hash).not_to include(:a => 1, :b => 2) }.to fail_matching(%Q|expected #{hash.inspect} not to include #{{:a=>1, :b=>2}.inspect}|) end it "fails if target has a different value for one of the keys" do expect { expect({:a => 1, :b => 2}).not_to include(:a => 2, :b => 2) }.to fail_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} not to include #{{:a=>2, :b=>2}.inspect}|) end it "passes if target has a different value for both of the keys" do expect({:a => 1, :b => 1}).not_to include(:a => 2, :b => 2) end it "fails if target lacks one of the keys" do expect { expect({:a => 1, :b => 1}).not_to include(:a => 1, :c => 1) }.to fail_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} not to include #{{:a=>1, :c=>1}.inspect}|) end it "passes if target lacks both of the keys" do expect({:a => 1, :b => 1}).not_to include(:c => 1, :d => 1) end end context 'for a non-hash target' do it "passes if the target does not contain the given hash" do expect(['a', 'b']).not_to include(:a => 1, :b => 1) end it "fails if the target contains the given hash" do expect { expect(['a', { :a => 1, :b => 2 } ]).not_to include(:a => 1, :b => 2) }.to fail_matching(%Q|expected #{["a", {:a=>1, :b=>2}].inspect} not to include #{{:a=>1, :b=>2}.inspect}|) end end end end RSpec::Matchers.define :a_string_containing do |expected| match do |actual| actual.include?(expected) end description do "a string containing '#{expected}'" end end describe "expect(...).to include(matcher)" do context 'for an array target' do it "passes if target includes an object that satisfies the matcher" do expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar")) end it "fails if target doesn't include object that satisfies the matcher" do expect { expect(['foo', 'bar', 'baz']).to include(a_string_containing("abc")) }.to fail_matching(%Q|expected #{['foo', 'bar', 'baz'].inspect} to include a string containing 'abc'|) end it 'does not include a diff when the match fails' do expect { expect(['foo', 'bar', 'baz']).to include(a_string_containing("abc")) }.to raise_error { |e| expect(e.message).not_to match(/diff/i) } end it 'does not treat an object that only implements #matches? as a matcher' do domain = Struct.new(:domain) do def matches?(url) URI(url).host == self.domain end end expect([domain.new("rspec.info")]).to include(domain.new("rspec.info")) expect { expect([domain.new("rspec.info")]).to include(domain.new("foo.com")) }.to fail_matching("expected [#{domain.new("rspec.info").inspect}] to include") end it 'works with an old-style matcher that implements failure_message rather than failure_message_for_should' do a_multiple_of = Class.new do def initialize(expected) @expected = expected end def matches?(actual) (actual % @expected).zero? end def failure_message "expected a multiple of #{@expected}" end end # Verify the matcher works normally expect(10).to a_multiple_of.new(5) expect { expect(10).to a_multiple_of.new(7) }.to fail_with("expected a multiple of 7") expect([12, 13, 14]).to include(a_multiple_of.new(6)) expect { expect([12, 13, 14]).to include(a_multiple_of.new(10)) }.to fail_matching("expected [12, 13, 14] to include") end end end describe "expect(...).to include(multiple, matcher, arguments)" do context 'for an array target' do it "passes if target includes items satisfying all matchers" do expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar"), a_string_containing('oo')) end it "fails if target does not include an item satisfying any one of the items" do expect { expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar"), a_string_containing("abc")) }.to fail_matching(%Q|expected #{['foo', 'bar', 'baz'].inspect} to include a string containing 'ar' and a string containing 'abc'|) end it 'does not include a diff when the match fails' do expect { expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar"), a_string_containing("abc")) }.to raise_error { |e| expect(e.message).not_to match(/diff/i) } end end end describe "expect(...).not_to include(multiple, matcher, arguments)" do it "passes if none of the target values satisfies any of the matchers" do expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("gh"), a_string_containing('de')) end it 'fails if all of the matchers are satisfied by one of the target values' do expect { expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("ar"), a_string_containing('az')) }.to fail_matching(%Q|expected #{['foo', 'bar', 'baz'].inspect} not to include a string containing 'ar' and a string containing 'az'|) end it 'fails if the some (but not all) of the matchers are satisifed' do expect { expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("ar"), a_string_containing('bz')) }.to fail_matching(%Q|expected #{['foo', 'bar', 'baz'].inspect} not to include a string containing 'ar' and a string containing 'bz'|) end end rspec-expectations-2.14.2/spec/rspec/matchers/change_spec.rb0000644000004100000410000004445312203207642024120 0ustar www-datawww-data#Based on patch from Wilson Bilkovich require 'spec_helper' class SomethingExpected attr_accessor :some_value end describe "expect { ... }.to change(actual, message)" do context "with a numeric value" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 5 end it "passes when actual is modified by the block" do expect {@instance.some_value = 6.0}.to change(@instance, :some_value) end it "fails when actual is not modified by the block" do expect do expect {}.to change(@instance, :some_value) end.to fail_with("some_value should have changed, but is still 5") end it "provides a #description" do expect(change(@instance, :some_value).description).to eq "change #some_value" end end it "can specify the change of a variable's class" do val = nil expect { val = 42 }.to change { val.class }.from(NilClass).to(Fixnum) expect { expect { val = "string" }.to change { val.class }.from(Fixnum).to(NilClass) }.to fail_with(/but is now String/) end context "with boolean values" do before(:each) do @instance = SomethingExpected.new @instance.some_value = true end it "passes when actual is modified by the block" do expect {@instance.some_value = false}.to change(@instance, :some_value) end it "fails when actual is not modified by the block" do expect do expect {}.to change(@instance, :some_value) end.to fail_with("some_value should have changed, but is still true") end end context "with nil value" do before(:each) do @instance = SomethingExpected.new @instance.some_value = nil end it "passes when actual is modified by the block" do expect {@instance.some_value = false}.to change(@instance, :some_value) end it "fails when actual is not modified by the block" do expect do expect {}.to change(@instance, :some_value) end.to fail_with("some_value should have changed, but is still nil") end end context "with an array" do before(:each) do @instance = SomethingExpected.new @instance.some_value = [] end it "passes when actual is modified by the block" do expect {@instance.some_value << 1}.to change(@instance, :some_value) end it "fails when a predicate on the actual fails" do expect do expect {@instance.some_value << 1}.to change { @instance.some_value }.to be_empty end.to fail_with(/result should have been changed to/) end it "passes when a predicate on the actual passes" do @instance.some_value = [1] expect {@instance.some_value.pop}.to change { @instance.some_value }.to be_empty end it "fails when actual is not modified by the block" do expect do expect {}.to change(@instance, :some_value) end.to fail_with("some_value should have changed, but is still []") end end context "with a hash" do before(:each) do @instance = SomethingExpected.new @instance.some_value = {:a => 'a'} end it "passes when actual is modified by the block" do expect {@instance.some_value[:a] = 'A'}.to change(@instance, :some_value) end it "fails when actual is not modified by the block" do expect do expect {}.to change(@instance, :some_value) end.to fail end end context "with a string" do it "passes when actual is modified by the block" do string = "ab" expect { string << "c" }.to change { string } end it 'fails when actual is not modified by the block' do string = "ab" expect { expect { }.to change { string } }.to fail_with(/should have changed/) end end context "with an arbitrary enumerable" do before(:each) do @instance = SomethingExpected.new @instance.some_value = Class.new do include Enumerable attr_reader :elements def initialize(*elements) @elements = elements.dup end def <<(element) elements << element end def dup self.class.new(*elements) end def ==(other) elements == other.elements end end.new end it "passes when actual is modified by the block" do expect {@instance.some_value << 1}.to change(@instance, :some_value) end it "fails when actual is not modified by the block" do expect do expect {}.to change(@instance, :some_value) end.to fail_with(/^some_value should have changed, but is still/) end end end describe "expect { ... }.not_to change(actual, message)" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 5 end it "passes when actual is not modified by the block" do expect { }.not_to change(@instance, :some_value) end it "fails when actual is not modified by the block" do expect do expect {@instance.some_value = 6}.not_to change(@instance, :some_value) end.to fail_with("some_value should not have changed, but did change from 5 to 6") end end describe "expect { ... }.to change { block }" do o = SomethingExpected.new it_behaves_like "an RSpec matcher", :valid_value => lambda { o.some_value = 5 }, :invalid_value => lambda { } do let(:matcher) { change { o.some_value } } end before(:each) do @instance = SomethingExpected.new @instance.some_value = 5 end it "passes when actual is modified by the block" do expect {@instance.some_value = 6}.to change { @instance.some_value } end it "fails when actual is not modified by the block" do expect do expect {}.to change{ @instance.some_value } end.to fail_with("result should have changed, but is still 5") end it "warns if passed a block using do/end instead of {}" do expect do expect {}.to change do; end end.to raise_error(SyntaxError, /block passed to should or should_not/) end it "provides a #description" do expect(change { @instance.some_value }.description).to eq "change #result" end end describe "expect { ... }.not_to change { block }" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 5 end it "passes when actual is modified by the block" do expect {}.not_to change{ @instance.some_value } end it "fails when actual is not modified by the block" do expect do expect {@instance.some_value = 6}.not_to change { @instance.some_value } end.to fail_with("result should not have changed, but did change from 5 to 6") end it "warns if passed a block using do/end instead of {}" do expect do expect {}.not_to change do; end end.to raise_error(SyntaxError, /block passed to should or should_not/) end end describe "expect { ... }.to change(actual, message).by(expected)" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 5 end it "passes when attribute is changed by expected amount" do expect { @instance.some_value += 1 }.to change(@instance, :some_value).by(1) end it "passes when attribute is not changed and expected amount is 0" do expect { @instance.some_value += 0 }.to change(@instance, :some_value).by(0) end it "fails when the attribute is changed by unexpected amount" do expect do expect { @instance.some_value += 2 }.to change(@instance, :some_value).by(1) end.to fail_with("some_value should have been changed by 1, but was changed by 2") end it "fails when the attribute is changed by unexpected amount in the opposite direction" do expect do expect { @instance.some_value -= 1 }.to change(@instance, :some_value).by(1) end.to fail_with("some_value should have been changed by 1, but was changed by -1") end end describe "expect { ... }.to change { block }.by(expected)" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 5 end it "passes when attribute is changed by expected amount" do expect { @instance.some_value += 1 }.to change{@instance.some_value}.by(1) end it "fails when the attribute is changed by unexpected amount" do expect do expect { @instance.some_value += 2 }.to change{@instance.some_value}.by(1) end.to fail_with("result should have been changed by 1, but was changed by 2") end it "fails when the attribute is changed by unexpected amount in the opposite direction" do expect do expect { @instance.some_value -= 1 }.to change{@instance.some_value}.by(1) end.to fail_with("result should have been changed by 1, but was changed by -1") end end describe "expect { ... }.to change(actual, message).by_at_least(expected)" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 5 end it "passes when attribute is changed by greater than the expected amount" do expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_least(1) end it "passes when attribute is changed by the expected amount" do expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_least(2) end it "fails when the attribute is changed by less than the expected amount" do expect do expect { @instance.some_value += 1 }.to change(@instance, :some_value).by_at_least(2) end.to fail_with("some_value should have been changed by at least 2, but was changed by 1") end end describe "expect { ... }.to change { block }.by_at_least(expected)" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 5 end it "passes when attribute is changed by greater than expected amount" do expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_least(1) end it "passes when attribute is changed by the expected amount" do expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_least(2) end it "fails when the attribute is changed by less than the unexpected amount" do expect do expect { @instance.some_value += 1 }.to change{@instance.some_value}.by_at_least(2) end.to fail_with("result should have been changed by at least 2, but was changed by 1") end end describe "expect { ... }.to change(actual, message).by_at_most(expected)" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 5 end it "passes when attribute is changed by less than the expected amount" do expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_most(3) end it "passes when attribute is changed by the expected amount" do expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_most(2) end it "fails when the attribute is changed by greater than the expected amount" do expect do expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_most(1) end.to fail_with("some_value should have been changed by at most 1, but was changed by 2") end end describe "expect { ... }.to change { block }.by_at_most(expected)" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 5 end it "passes when attribute is changed by less than expected amount" do expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_most(3) end it "passes when attribute is changed by the expected amount" do expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_most(2) end it "fails when the attribute is changed by greater than the unexpected amount" do expect do expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_most(1) end.to fail_with("result should have been changed by at most 1, but was changed by 2") end end describe "expect { ... }.to change(actual, message).from(old)" do context "with boolean values" do before(:each) do @instance = SomethingExpected.new @instance.some_value = true end it "passes when attribute is == to expected value before executing block" do expect { @instance.some_value = false }.to change(@instance, :some_value).from(true) end it "fails when attribute is not == to expected value before executing block" do expect do expect { @instance.some_value = 'foo' }.to change(@instance, :some_value).from(false) end.to fail_with("some_value should have initially been false, but was true") end end context "with non-boolean values" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 'string' end it "passes when attribute is === to expected value before executing block" do expect { @instance.some_value = "astring" }.to change(@instance, :some_value).from("string") end it "compares the expected and actual values with ===" do expected = "string" expected.should_receive(:===).and_return true expect { @instance.some_value = "astring" }.to change(@instance, :some_value).from(expected) end it "fails when attribute is not === to expected value before executing block" do expect do expect { @instance.some_value = "knot" }.to change(@instance, :some_value).from("cat") end.to fail_with("some_value should have initially been \"cat\", but was \"string\"") end end end describe "expect { ... }.to change { block }.from(old)" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 'string' end it "passes when attribute is === to expected value before executing block" do expect { @instance.some_value = "astring" }.to change{@instance.some_value}.from("string") end it "compares the expected and actual values with ===" do expected = "string" expected.should_receive(:===).and_return true expect { @instance.some_value = "astring" }.to change{@instance.some_value}.from(expected) end it "fails when attribute is not === to expected value before executing block" do expect do expect { @instance.some_value = "knot" }.to change{@instance.some_value}.from("cat") end.to fail_with("result should have initially been \"cat\", but was \"string\"") end end describe "expect { ... }.to change(actual, message).to(new)" do context "with boolean values" do before(:each) do @instance = SomethingExpected.new @instance.some_value = true end it "passes when attribute is == to expected value after executing block" do expect { @instance.some_value = false }.to change(@instance, :some_value).to(false) end it "fails when attribute is not == to expected value after executing block" do expect do expect { @instance.some_value = 1 }.to change(@instance, :some_value).from(true).to(false) end.to fail_with("some_value should have been changed to false, but is now 1") end end context "with non-boolean values" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 'string' end it "passes when attribute is === to expected value after executing block" do expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to("cat") end it "compares the expected and actual values with ===" do expected = "cat" expected.should_receive(:===).and_return true expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to(expected) end it "fails when attribute is not === to expected value after executing block" do expect do expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("dog") end.to fail_with("some_value should have been changed to \"dog\", but is now \"cat\"") end end end describe "expect { ... }.to change { block }.to(new)" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 'string' end it "passes when attribute is === to expected value after executing block" do expect { @instance.some_value = "cat" }.to change{@instance.some_value}.to("cat") end it "compares the expected and actual values with ===" do expected = "cat" expected.should_receive(:===).and_return true expect { @instance.some_value = "cat" }.to change{@instance.some_value}.to(expected) end it "fails when attribute is not === to expected value after executing block" do expect do expect { @instance.some_value = "cat" }.to change{@instance.some_value}.from("string").to("dog") end.to fail_with("result should have been changed to \"dog\", but is now \"cat\"") end end describe "expect { ... }.to change(actual, message).from(old).to(new)" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 'string' end it "passes when #to comes before #from" do expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to("cat").from("string") end it "passes when #from comes before #to" do expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("cat") end it "shows the correct messaging when #after and #to are different" do expect do expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("dog") end.to fail_with("some_value should have been changed to \"dog\", but is now \"cat\"") end it "shows the correct messaging when #before and #from are different" do expect do expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("not_string").to("cat") end.to fail_with("some_value should have initially been \"not_string\", but was \"string\"") end end describe "expect { ... }.to change { block }.from(old).to(new)" do before(:each) do @instance = SomethingExpected.new @instance.some_value = 'string' end it "passes when #to comes before #from" do expect { @instance.some_value = "cat" }.to change{@instance.some_value}.to("cat").from("string") end it "passes when #from comes before #to" do expect { @instance.some_value = "cat" }.to change{@instance.some_value}.from("string").to("cat") end end describe RSpec::Matchers::BuiltIn::Change do it "works when the receiver has implemented #send" do @instance = SomethingExpected.new @instance.some_value = "string" def @instance.send(*args); raise "DOH! Library developers shouldn't use #send!" end expect { expect { @instance.some_value = "cat" }.to change(@instance, :some_value) }.not_to raise_error end end rspec-expectations-2.14.2/spec/rspec/matchers/configuration_spec.rb0000644000004100000410000001551512203207642025537 0ustar www-datawww-datarequire 'spec_helper' require 'delegate' module RSpec module Matchers describe "RSpec::Matchers.configuration" do it 'returns a memoized configuration instance' do expect(RSpec::Matchers.configuration).to be_a(RSpec::Matchers::Configuration) expect(RSpec::Matchers.configuration).to be(RSpec::Matchers.configuration) end end describe Configuration do let(:config) { Configuration.new } describe "#backtrace_formatter" do let(:original_backtrace) { %w[ clean-me/a.rb other/file.rb clean-me/b.rb ] } let(:cleaned_backtrace) { %w[ other/file.rb ] } let(:formatted_backtrace) do config.backtrace_formatter.format_backtrace(original_backtrace) end before do @old_patterns = RSpec.configuration.backtrace_exclusion_patterns RSpec.configuration.backtrace_exclusion_patterns = [/clean-me/] end after do RSpec.configuration.backtrace_exclusion_patterns = @old_patterns end it "defaults to rspec-core's backtrace formatter when rspec-core is loaded" do expect(config.backtrace_formatter).to be(RSpec::Core::BacktraceFormatter) expect(formatted_backtrace).to eq(cleaned_backtrace) end it "defaults to a null formatter when rspec-core is not loaded" do hide_const("RSpec::Core::BacktraceFormatter") expect(formatted_backtrace).to eq(original_backtrace) end it "can be set to another backtrace formatter" do config.backtrace_formatter = double(:format_backtrace => ['a']) expect(formatted_backtrace).to eq(['a']) end end context 'on an interpreter that does not provide BasicObject', :uses_should, :unless => defined?(::BasicObject) do before { RSpec::Expectations::Syntax.disable_should(Delegator) } let(:klass) do Class.new(SimpleDelegator) do def delegated?; true; end end end let(:instance) { klass.new(Object.new) } it 'provides a means to manually add it Delegator' do instance.should_not respond_to(:delegated?) # because #should is being delegated... config.add_should_and_should_not_to Delegator instance.should respond_to(:delegated?) # now it should work! end end shared_examples_for "configuring the expectation syntax" do before do @orig_syntax = RSpec::Matchers.configuration.syntax end after do configure_syntax(@orig_syntax) end it 'can limit the syntax to :should' do configure_syntax :should configured_syntax.should eq([:should]) 3.should eq(3) 3.should_not eq(4) lambda { expect(6).to eq(6) }.should raise_error(NameError) end it 'is a no-op when configured to :should twice' do configure_syntax :should Expectations::Syntax.default_should_host.should_not_receive(:method_added) configure_syntax :should RSpec::Mocks.verify # because configure_syntax is called again in an after hook end it 'can limit the syntax to :expect' do configure_syntax :expect expect(configured_syntax).to eq([:expect]) expect(3).to eq(3) expect { 3.should eq(3) }.to raise_error(NameError) expect { 3.should_not eq(3) }.to raise_error(NameError) end it 'is a no-op when configured to :expect twice' do RSpec::Matchers.stub(:method_added).and_raise("no methods should be added here") configure_syntax :expect configure_syntax :expect end it 'can re-enable the :should syntax' do configure_syntax :expect configure_syntax [:should, :expect] configured_syntax.should eq([:should, :expect]) 3.should eq(3) 3.should_not eq(4) expect(3).to eq(3) end it 'can re-enable the :expect syntax' do configure_syntax :should configure_syntax [:should, :expect] configured_syntax.should eq([:should, :expect]) 3.should eq(3) 3.should_not eq(4) expect(3).to eq(3) end it 'does not add the deprecated #should to ExpectationTarget when only :should is enabled' do et = Expectations::ExpectationTarget configure_syntax :should et.new(Proc.new {}).should be_an(et) et.new(Proc.new {}).should_not be_a(Proc) end it 'does not add the deprecated #should to ExpectationTarget when only :expect is enabled' do configure_syntax :expect expect(expect(3)).not_to respond_to(:should) expect(expect(3)).not_to respond_to(:should_not) end context 'when both :expect and :should are enabled' do before { allow(RSpec).to receive(:deprecate) } it 'allows `expect {}.should` to be used' do configure_syntax [:should, :expect] expect { raise "boom" }.should raise_error("boom") expect { }.should_not raise_error end it 'prints a deprecation notice when `expect {}.should` is used' do configure_syntax [:should, :expect] expect(RSpec).to receive(:deprecate) expect { raise "boom" }.should raise_error("boom") expect(RSpec).to receive(:deprecate) expect { }.should_not raise_error end end end describe "configuring rspec-expectations directly" do it_behaves_like "configuring the expectation syntax" do def configure_syntax(syntax) RSpec::Matchers.configuration.syntax = syntax end def configured_syntax RSpec::Matchers.configuration.syntax end end end describe "configuring using the rspec-core config API" do it_behaves_like "configuring the expectation syntax" do def configure_syntax(syntax) RSpec.configure do |rspec| rspec.expect_with :rspec do |c| c.syntax = syntax end end end def configured_syntax RSpec.configure do |rspec| rspec.expect_with :rspec do |c| return c.syntax end end end end end it 'enables both syntaxes by default' do # This is kinda a hack, but since we want to enforce use of # the expect syntax within our specs here, we have modified the # config setting, which makes it hard to get at the original # default value. in spec_helper.rb we store the default value # in $default_expectation_syntax so we can use it here. expect($default_expectation_syntax).to match_array([:expect, :should]) end end end end rspec-expectations-2.14.2/spec/rspec/matchers/be_close_spec.rb0000644000004100000410000000115412203207642024435 0ustar www-datawww-datarequire 'spec_helper' module RSpec module Matchers describe "expect(actual).to be_close(expected, delta)" do before(:each) do allow(RSpec).to receive(:deprecate) end it "is deprecated" do expect(RSpec).to receive(:deprecate).with(/be_close.*/, :replacement => "be_within(0.5).of(3.0)") be_close(3.0, 0.5) end it "delegates to be_within(delta).of(expected)" do should_receive(:be_within).with(0.5).and_return( be_within_matcher = double ) be_within_matcher.should_receive(:of).with(3.0) be_close(3.0, 0.5) end end end end rspec-expectations-2.14.2/spec/rspec/matchers/equal_spec.rb0000644000004100000410000000404712203207642023775 0ustar www-datawww-datarequire 'spec_helper' module RSpec module Matchers describe "equal" do it_behaves_like "an RSpec matcher", :valid_value => :a, :invalid_value => :b do let(:matcher) { equal(:a) } end def inspect_object(o) "#<#{o.class}:#{o.object_id}> => #{o.inspect}" end it "matches when actual.equal?(expected)" do expect(1).to equal(1) end it "does not match when !actual.equal?(expected)" do expect("1").not_to equal("1") end it "describes itself" do matcher = equal(1) matcher.matches?(1) expect(matcher.description).to eq "equal 1" end it "suggests the `eq` matcher on failure" do expected, actual = "1", "1" expect { expect(actual).to equal(expected) }.to fail_with <<-MESSAGE expected #{inspect_object(expected)} got #{inspect_object(actual)} Compared using equal?, which compares object identity, but expected and actual are not the same object. Use `expect(actual).to eq(expected)` if you don't care about object identity in this example. MESSAGE end context "when using only `should`", :uses_only_should do it "suggests the `eq` matcher on failure" do expected, actual = "1", "1" lambda { actual.should equal(expected) }.should fail_with <<-MESSAGE expected #{inspect_object(expected)} got #{inspect_object(actual)} Compared using equal?, which compares object identity, but expected and actual are not the same object. Use `actual.should eq(expected)` if you don't care about object identity in this example. MESSAGE end end it "provides message on #negative_failure_message" do expected = actual = "1" matcher = equal(expected) matcher.matches?(actual) expect(matcher.failure_message_for_should_not).to eq <<-MESSAGE expected not #{inspect_object(expected)} got #{inspect_object(actual)} Compared using equal?, which compares object identity. MESSAGE end end end end rspec-expectations-2.14.2/spec/rspec/matchers/throw_symbol_spec.rb0000644000004100000410000001156212203207642025416 0ustar www-datawww-datarequire 'spec_helper' module RSpec::Matchers::BuiltIn describe ThrowSymbol do it_behaves_like("an RSpec matcher", :valid_value => lambda { throw :foo }, :invalid_value => lambda { }) do let(:matcher) { throw_symbol(:foo) } end describe "with no args" do before(:each) { @matcher = throw_symbol } it "matches if any Symbol is thrown" do expect(@matcher.matches?(lambda{ throw :sym })).to be_true end it "matches if any Symbol is thrown with an arg" do expect(@matcher.matches?(lambda{ throw :sym, "argument" })).to be_true end it "does not match if no Symbol is thrown" do expect(@matcher.matches?(lambda{ })).to be_false end it "provides a failure message" do @matcher.matches?(lambda{}) expect(@matcher.failure_message_for_should).to eq "expected a Symbol to be thrown, got nothing" end it "provides a negative failure message" do @matcher.matches?(lambda{ throw :sym}) expect(@matcher.failure_message_for_should_not).to eq "expected no Symbol to be thrown, got :sym" end end describe "with a symbol" do before(:each) { @matcher = throw_symbol(:sym) } it "matches if correct Symbol is thrown" do expect(@matcher.matches?(lambda{ throw :sym })).to be_true end it "matches if correct Symbol is thrown with an arg" do expect(@matcher.matches?(lambda{ throw :sym, "argument" })).to be_true end it "does not match if no Symbol is thrown" do expect(@matcher.matches?(lambda{ })).to be_false end it "does not match if correct Symbol is thrown" do expect(@matcher.matches?(lambda{ throw :other_sym })).to be_false end it "provides a failure message when no Symbol is thrown" do @matcher.matches?(lambda{}) expect(@matcher.failure_message_for_should).to eq "expected :sym to be thrown, got nothing" end it "provides a failure message when wrong Symbol is thrown" do @matcher.matches?(lambda{ throw :other_sym }) expect(@matcher.failure_message_for_should).to eq "expected :sym to be thrown, got :other_sym" end it "provides a negative failure message" do @matcher.matches?(lambda{ throw :sym }) expect(@matcher.failure_message_for_should_not).to eq "expected :sym not to be thrown, got :sym" end it "only matches NameErrors raised by uncaught throws" do expect { expect(@matcher.matches?(lambda{ sym })).to be_false }.to raise_error(NameError) end end describe "with a symbol and an arg" do before(:each) { @matcher = throw_symbol(:sym, "a") } it "matches if correct Symbol and args are thrown" do expect(@matcher.matches?(lambda{ throw :sym, "a" })).to be_true end it "does not match if nothing is thrown" do expect(@matcher.matches?(lambda{ })).to be_false end it "does not match if other Symbol is thrown" do expect(@matcher.matches?(lambda{ throw :other_sym, "a" })).to be_false end it "does not match if no arg is thrown" do expect(@matcher.matches?(lambda{ throw :sym })).to be_false end it "does not match if wrong arg is thrown" do expect(@matcher.matches?(lambda{ throw :sym, "b" })).to be_false end it "provides a failure message when no Symbol is thrown" do @matcher.matches?(lambda{}) expect(@matcher.failure_message_for_should).to eq %q[expected :sym with "a" to be thrown, got nothing] end it "provides a failure message when wrong Symbol is thrown" do @matcher.matches?(lambda{ throw :other_sym }) expect(@matcher.failure_message_for_should).to eq %q[expected :sym with "a" to be thrown, got :other_sym] end it "provides a failure message when wrong arg is thrown" do @matcher.matches?(lambda{ throw :sym, "b" }) expect(@matcher.failure_message_for_should).to eq %q[expected :sym with "a" to be thrown, got :sym with "b"] end it "provides a failure message when no arg is thrown" do @matcher.matches?(lambda{ throw :sym }) expect(@matcher.failure_message_for_should).to eq %q[expected :sym with "a" to be thrown, got :sym with no argument] end it "provides a negative failure message" do @matcher.matches?(lambda{ throw :sym }) expect(@matcher.failure_message_for_should_not).to eq %q[expected :sym with "a" not to be thrown, got :sym with no argument] end it "only matches NameErrors raised by uncaught throws" do expect { expect(@matcher.matches?(lambda{ sym })).to be_false }.to raise_error(NameError) end it "raises other errors" do expect { @matcher.matches?(lambda { raise "Boom" }) }.to raise_error(/Boom/) end end end end rspec-expectations-2.14.2/spec/rspec/matchers/include_matcher_integration_spec.rb0000644000004100000410000000203112203207642030406 0ustar www-datawww-datarequire 'spec_helper' module RSpec module Matchers describe "include() interaction with built-in matchers" do it "works with be_within(delta).of(expected)" do expect([10, 20, 30]).to include( be_within(5).of(24) ) expect([10, 20, 30]).not_to include( be_within(3).of(24) ) end it "works with be_instance_of(klass)" do expect(["foo", 123, {:foo => "bar"}]).to include( be_instance_of(Hash) ) expect(["foo", 123, {:foo => "bar"}]).not_to include( be_instance_of(Range) ) end it "works with be_kind_of(klass)" do class StringSubclass < String; end class NotHashSubclass; end expect([StringSubclass.new("baz")]).to include( be_kind_of(String) ) expect([NotHashSubclass.new]).not_to include( be_kind_of(Hash) ) end it "works with be_[some predicate]" do expect([double("actual", :happy? => true)]).to include( be_happy ) expect([double("actual", :happy? => false)]).not_to include( be_happy ) end end end end rspec-expectations-2.14.2/spec/support/0000755000004100000410000000000012203207642020114 5ustar www-datawww-datarspec-expectations-2.14.2/spec/support/classes.rb0000644000004100000410000000312212203207642022074 0ustar www-datawww-data# various classes used by the specs module RSpec module Expectations module Helper class CollectionWithSizeMethod def initialize; @list = []; end def size; @list.size; end def push(item); @list.push(item); end end class CollectionWithLengthMethod def initialize; @list = []; end def length; @list.size; end def push(item); @list.push(item); end end class CollectionWithCountMethod def initialize; @list = []; end def count; @list.count; end def push(item); @list.push(item); end end class CollectionOwner attr_reader :items_in_collection_with_size_method, :items_in_collection_with_length_method, :items_in_collection_with_count_method def initialize @items_in_collection_with_size_method = CollectionWithSizeMethod.new @items_in_collection_with_length_method = CollectionWithLengthMethod.new @items_in_collection_with_count_method = CollectionWithCountMethod.new end def add_to_collection_with_size_method(item) @items_in_collection_with_size_method.push(item) end def add_to_collection_with_length_method(item) @items_in_collection_with_length_method.push(item) end def add_to_collection_with_count_method(item) @items_in_collection_with_count_method.push(item) end def items_for(arg) return [1, 2, 3] if arg == 'a' [1] end def items @items_in_collection_with_size_method end end end end end rspec-expectations-2.14.2/spec/support/in_sub_process.rb0000644000004100000410000000142312203207642023456 0ustar www-datawww-datamodule InSubProcess if RUBY_PLATFORM == 'java' def in_sub_process pending "This spec requires forking to work properly, " + "and JRuby does not support forking" end else # Useful as a way to isolate a global change to a subprocess. def in_sub_process readme, writeme = IO.pipe pid = Process.fork do value = nil begin yield rescue => e value = e end writeme.write Marshal.dump(value) readme.close writeme.close exit! # prevent at_exit hooks from running (e.g. minitest) end writeme.close Process.waitpid(pid) if exception = Marshal.load(readme.read) raise exception end readme.close end end end rspec-expectations-2.14.2/spec/support/ruby_version.rb0000644000004100000410000000016112203207642023165 0ustar www-datawww-datamodule RSpec module Ruby class << self def version RUBY_VERSION end end end end rspec-expectations-2.14.2/spec/support/shared_examples.rb0000644000004100000410000000063512203207642023611 0ustar www-datawww-datashared_examples_for "an RSpec matcher" do |options| let(:valid_value) { options.fetch(:valid_value) } let(:invalid_value) { options.fetch(:invalid_value) } it 'matches a valid value when using #== so it can be composed' do expect(matcher).to eq(valid_value) end it 'does not match an invalid value when using #== so it can be composed' do expect(matcher).not_to eq(invalid_value) end end rspec-expectations-2.14.2/spec/support/matchers.rb0000644000004100000410000000077012203207642022253 0ustar www-datawww-dataRSpec::Matchers.define :include_method do |expected| match do |actual| actual.map { |m| m.to_s }.include?(expected.to_s) end end module RSpec module Matchers def fail raise_error(RSpec::Expectations::ExpectationNotMetError) end def fail_with(message) raise_error(RSpec::Expectations::ExpectationNotMetError, message) end def fail_matching(message) raise_error(RSpec::Expectations::ExpectationNotMetError, /#{Regexp.escape(message)}/) end end end rspec-expectations-2.14.2/.document0000644000004100000410000000006112203207642017262 0ustar www-datawww-datalib/**/*.rb - README.md License.txt Changelog.md rspec-expectations-2.14.2/metadata.yml0000644000004100000410000002604112203207642017754 0ustar www-datawww-data--- !ruby/object:Gem::Specification name: rspec-expectations version: !ruby/object:Gem::Version prerelease: version: 2.14.2 platform: ruby authors: - Steven Baker - David Chelimsky autorequire: bindir: bin cert_chain: [] date: 2013-08-15 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency version_requirements: !ruby/object:Gem::Requirement requirements: - - ! '>=' - !ruby/object:Gem::Version version: 1.1.3 - - < - !ruby/object:Gem::Version version: '2.0' none: false prerelease: false name: diff-lcs requirement: !ruby/object:Gem::Requirement requirements: - - ! '>=' - !ruby/object:Gem::Version version: 1.1.3 - - < - !ruby/object:Gem::Version version: '2.0' none: false type: :runtime - !ruby/object:Gem::Dependency version_requirements: !ruby/object:Gem::Requirement requirements: - - ~> - !ruby/object:Gem::Version version: 10.0.0 none: false prerelease: false name: rake requirement: !ruby/object:Gem::Requirement requirements: - - ~> - !ruby/object:Gem::Version version: 10.0.0 none: false type: :development - !ruby/object:Gem::Dependency version_requirements: !ruby/object:Gem::Requirement requirements: - - ~> - !ruby/object:Gem::Version version: 1.1.9 none: false prerelease: false name: cucumber requirement: !ruby/object:Gem::Requirement requirements: - - ~> - !ruby/object:Gem::Version version: 1.1.9 none: false type: :development - !ruby/object:Gem::Dependency version_requirements: !ruby/object:Gem::Requirement requirements: - - ~> - !ruby/object:Gem::Version version: '0.5' none: false prerelease: false name: aruba requirement: !ruby/object:Gem::Requirement requirements: - - ~> - !ruby/object:Gem::Version version: '0.5' none: false type: :development description: rspec expectations (should[_not] and matchers) email: rspec-users@rubyforge.org executables: [] extensions: [] extra_rdoc_files: [] files: - lib/rspec-expectations.rb - lib/rspec/expectations.rb - lib/rspec/expectations/deprecation.rb - lib/rspec/expectations/differ.rb - lib/rspec/expectations/errors.rb - lib/rspec/expectations/expectation_target.rb - lib/rspec/expectations/extensions.rb - lib/rspec/expectations/extensions/array.rb - lib/rspec/expectations/extensions/object.rb - lib/rspec/expectations/fail_with.rb - lib/rspec/expectations/handler.rb - lib/rspec/expectations/syntax.rb - lib/rspec/expectations/version.rb - lib/rspec/matchers.rb - lib/rspec/matchers/be_close.rb - lib/rspec/matchers/built_in.rb - lib/rspec/matchers/built_in/base_matcher.rb - lib/rspec/matchers/built_in/be.rb - lib/rspec/matchers/built_in/be_instance_of.rb - lib/rspec/matchers/built_in/be_kind_of.rb - lib/rspec/matchers/built_in/be_within.rb - lib/rspec/matchers/built_in/change.rb - lib/rspec/matchers/built_in/cover.rb - lib/rspec/matchers/built_in/eq.rb - lib/rspec/matchers/built_in/eql.rb - lib/rspec/matchers/built_in/equal.rb - lib/rspec/matchers/built_in/exist.rb - lib/rspec/matchers/built_in/has.rb - lib/rspec/matchers/built_in/have.rb - lib/rspec/matchers/built_in/include.rb - lib/rspec/matchers/built_in/match.rb - lib/rspec/matchers/built_in/match_array.rb - lib/rspec/matchers/built_in/raise_error.rb - lib/rspec/matchers/built_in/respond_to.rb - lib/rspec/matchers/built_in/satisfy.rb - lib/rspec/matchers/built_in/start_and_end_with.rb - lib/rspec/matchers/built_in/throw_symbol.rb - lib/rspec/matchers/built_in/yield.rb - lib/rspec/matchers/compatibility.rb - lib/rspec/matchers/configuration.rb - lib/rspec/matchers/dsl.rb - lib/rspec/matchers/extensions/instance_eval_with_args.rb - lib/rspec/matchers/generated_descriptions.rb - lib/rspec/matchers/matcher.rb - lib/rspec/matchers/method_missing.rb - lib/rspec/matchers/operator_matcher.rb - lib/rspec/matchers/pretty.rb - lib/rspec/matchers/test_unit_integration.rb - README.md - License.txt - Changelog.md - .yardopts - .document - features/README.md - features/Upgrade.md - features/built_in_matchers/README.md - features/built_in_matchers/be.feature - features/built_in_matchers/be_within.feature - features/built_in_matchers/cover.feature - features/built_in_matchers/end_with.feature - features/built_in_matchers/equality.feature - features/built_in_matchers/exist.feature - features/built_in_matchers/expect_change.feature - features/built_in_matchers/expect_error.feature - features/built_in_matchers/have.feature - features/built_in_matchers/include.feature - features/built_in_matchers/match.feature - features/built_in_matchers/operators.feature - features/built_in_matchers/predicates.feature - features/built_in_matchers/respond_to.feature - features/built_in_matchers/satisfy.feature - features/built_in_matchers/start_with.feature - features/built_in_matchers/throw_symbol.feature - features/built_in_matchers/types.feature - features/built_in_matchers/yield.feature - features/custom_matchers/access_running_example.feature - features/custom_matchers/define_diffable_matcher.feature - features/custom_matchers/define_matcher.feature - features/custom_matchers/define_matcher_outside_rspec.feature - features/custom_matchers/define_matcher_with_fluent_interface.feature - features/customized_message.feature - features/diffing.feature - features/implicit_docstrings.feature - features/step_definitions/additional_cli_steps.rb - features/support/env.rb - features/support/rubinius.rb - features/syntax_configuration.feature - features/test_frameworks/test_unit.feature - spec/rspec/expectations/differ_spec.rb - spec/rspec/expectations/expectation_target_spec.rb - spec/rspec/expectations/extensions/kernel_spec.rb - spec/rspec/expectations/fail_with_spec.rb - spec/rspec/expectations/handler_spec.rb - spec/rspec/expectations/syntax_spec.rb - spec/rspec/matchers/base_matcher_spec.rb - spec/rspec/matchers/be_close_spec.rb - spec/rspec/matchers/be_instance_of_spec.rb - spec/rspec/matchers/be_kind_of_spec.rb - spec/rspec/matchers/be_spec.rb - spec/rspec/matchers/be_within_spec.rb - spec/rspec/matchers/change_spec.rb - spec/rspec/matchers/configuration_spec.rb - spec/rspec/matchers/cover_spec.rb - spec/rspec/matchers/description_generation_spec.rb - spec/rspec/matchers/dsl_spec.rb - spec/rspec/matchers/eq_spec.rb - spec/rspec/matchers/eql_spec.rb - spec/rspec/matchers/equal_spec.rb - spec/rspec/matchers/exist_spec.rb - spec/rspec/matchers/has_spec.rb - spec/rspec/matchers/have_spec.rb - spec/rspec/matchers/include_matcher_integration_spec.rb - spec/rspec/matchers/include_spec.rb - spec/rspec/matchers/match_array_spec.rb - spec/rspec/matchers/match_spec.rb - spec/rspec/matchers/matcher_spec.rb - spec/rspec/matchers/matchers_spec.rb - spec/rspec/matchers/method_missing_spec.rb - spec/rspec/matchers/operator_matcher_spec.rb - spec/rspec/matchers/raise_error_spec.rb - spec/rspec/matchers/respond_to_spec.rb - spec/rspec/matchers/satisfy_spec.rb - spec/rspec/matchers/start_with_end_with_spec.rb - spec/rspec/matchers/throw_symbol_spec.rb - spec/rspec/matchers/yield_spec.rb - spec/spec_helper.rb - spec/support/classes.rb - spec/support/in_sub_process.rb - spec/support/matchers.rb - spec/support/ruby_version.rb - spec/support/shared_examples.rb homepage: http://github.com/rspec/rspec-expectations licenses: - MIT post_install_message: rdoc_options: - --charset=UTF-8 require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' segments: - 0 hash: 2244321019137391681 none: false required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' segments: - 0 hash: 2244321019137391681 none: false requirements: [] rubyforge_project: rspec rubygems_version: 1.8.24 signing_key: specification_version: 3 summary: rspec-expectations-2.14.2 test_files: - features/README.md - features/Upgrade.md - features/built_in_matchers/README.md - features/built_in_matchers/be.feature - features/built_in_matchers/be_within.feature - features/built_in_matchers/cover.feature - features/built_in_matchers/end_with.feature - features/built_in_matchers/equality.feature - features/built_in_matchers/exist.feature - features/built_in_matchers/expect_change.feature - features/built_in_matchers/expect_error.feature - features/built_in_matchers/have.feature - features/built_in_matchers/include.feature - features/built_in_matchers/match.feature - features/built_in_matchers/operators.feature - features/built_in_matchers/predicates.feature - features/built_in_matchers/respond_to.feature - features/built_in_matchers/satisfy.feature - features/built_in_matchers/start_with.feature - features/built_in_matchers/throw_symbol.feature - features/built_in_matchers/types.feature - features/built_in_matchers/yield.feature - features/custom_matchers/access_running_example.feature - features/custom_matchers/define_diffable_matcher.feature - features/custom_matchers/define_matcher.feature - features/custom_matchers/define_matcher_outside_rspec.feature - features/custom_matchers/define_matcher_with_fluent_interface.feature - features/customized_message.feature - features/diffing.feature - features/implicit_docstrings.feature - features/step_definitions/additional_cli_steps.rb - features/support/env.rb - features/support/rubinius.rb - features/syntax_configuration.feature - features/test_frameworks/test_unit.feature - spec/rspec/expectations/differ_spec.rb - spec/rspec/expectations/expectation_target_spec.rb - spec/rspec/expectations/extensions/kernel_spec.rb - spec/rspec/expectations/fail_with_spec.rb - spec/rspec/expectations/handler_spec.rb - spec/rspec/expectations/syntax_spec.rb - spec/rspec/matchers/base_matcher_spec.rb - spec/rspec/matchers/be_close_spec.rb - spec/rspec/matchers/be_instance_of_spec.rb - spec/rspec/matchers/be_kind_of_spec.rb - spec/rspec/matchers/be_spec.rb - spec/rspec/matchers/be_within_spec.rb - spec/rspec/matchers/change_spec.rb - spec/rspec/matchers/configuration_spec.rb - spec/rspec/matchers/cover_spec.rb - spec/rspec/matchers/description_generation_spec.rb - spec/rspec/matchers/dsl_spec.rb - spec/rspec/matchers/eq_spec.rb - spec/rspec/matchers/eql_spec.rb - spec/rspec/matchers/equal_spec.rb - spec/rspec/matchers/exist_spec.rb - spec/rspec/matchers/has_spec.rb - spec/rspec/matchers/have_spec.rb - spec/rspec/matchers/include_matcher_integration_spec.rb - spec/rspec/matchers/include_spec.rb - spec/rspec/matchers/match_array_spec.rb - spec/rspec/matchers/match_spec.rb - spec/rspec/matchers/matcher_spec.rb - spec/rspec/matchers/matchers_spec.rb - spec/rspec/matchers/method_missing_spec.rb - spec/rspec/matchers/operator_matcher_spec.rb - spec/rspec/matchers/raise_error_spec.rb - spec/rspec/matchers/respond_to_spec.rb - spec/rspec/matchers/satisfy_spec.rb - spec/rspec/matchers/start_with_end_with_spec.rb - spec/rspec/matchers/throw_symbol_spec.rb - spec/rspec/matchers/yield_spec.rb - spec/spec_helper.rb - spec/support/classes.rb - spec/support/in_sub_process.rb - spec/support/matchers.rb - spec/support/ruby_version.rb - spec/support/shared_examples.rb has_rdoc: rspec-expectations-2.14.2/.yardopts0000644000004100000410000000011512203207642017311 0ustar www-datawww-data--exclude features --no-private --markup markdown - Changelog.md License.txt rspec-expectations-2.14.2/lib/0000755000004100000410000000000012203207642016214 5ustar www-datawww-datarspec-expectations-2.14.2/lib/rspec-expectations.rb0000644000004100000410000000003512203207642022357 0ustar www-datawww-datarequire "rspec/expectations" rspec-expectations-2.14.2/lib/rspec/0000755000004100000410000000000012203207642017330 5ustar www-datawww-datarspec-expectations-2.14.2/lib/rspec/expectations/0000755000004100000410000000000012203207642022036 5ustar www-datawww-datarspec-expectations-2.14.2/lib/rspec/expectations/handler.rb0000644000004100000410000000450212203207642024001 0ustar www-datawww-datamodule RSpec module Expectations class ExpectationHandler def self.check_message(msg) unless msg.nil? || msg.respond_to?(:to_str) || msg.respond_to?(:call) ::Kernel.warn [ "WARNING: ignoring the provided expectation message argument (", msg.inspect, ") since it is not a string or a proc." ].join end end end class PositiveExpectationHandler < ExpectationHandler def self.handle_matcher(actual, matcher, message=nil, &block) check_message(message) ::RSpec::Matchers.last_should = :should ::RSpec::Matchers.last_matcher = matcher return ::RSpec::Matchers::BuiltIn::PositiveOperatorMatcher.new(actual) if matcher.nil? match = matcher.matches?(actual, &block) return match if match message = message.call if message.respond_to?(:call) message ||= matcher.respond_to?(:failure_message_for_should) ? matcher.failure_message_for_should : matcher.failure_message if matcher.respond_to?(:diffable?) && matcher.diffable? ::RSpec::Expectations.fail_with message, matcher.expected, matcher.actual else ::RSpec::Expectations.fail_with message end end end class NegativeExpectationHandler < ExpectationHandler def self.handle_matcher(actual, matcher, message=nil, &block) check_message(message) ::RSpec::Matchers.last_should = :should_not ::RSpec::Matchers.last_matcher = matcher return ::RSpec::Matchers::BuiltIn::NegativeOperatorMatcher.new(actual) if matcher.nil? match = matcher.respond_to?(:does_not_match?) ? !matcher.does_not_match?(actual, &block) : matcher.matches?(actual, &block) return match unless match message = message.call if message.respond_to?(:call) message ||= matcher.respond_to?(:failure_message_for_should_not) ? matcher.failure_message_for_should_not : matcher.negative_failure_message if matcher.respond_to?(:diffable?) && matcher.diffable? ::RSpec::Expectations.fail_with message, matcher.expected, matcher.actual else ::RSpec::Expectations.fail_with message end end end end end rspec-expectations-2.14.2/lib/rspec/expectations/version.rb0000644000004100000410000000016012203207642024045 0ustar www-datawww-datamodule RSpec module Expectations # @private module Version STRING = '2.14.2' end end end rspec-expectations-2.14.2/lib/rspec/expectations/errors.rb0000644000004100000410000000036512203207642023703 0ustar www-datawww-datamodule RSpec module Expectations if defined?(Test::Unit::AssertionFailedError) class ExpectationNotMetError < Test::Unit::AssertionFailedError; end else class ExpectationNotMetError < ::StandardError; end end end end rspec-expectations-2.14.2/lib/rspec/expectations/deprecation.rb0000644000004100000410000000076012203207642024663 0ustar www-datawww-datamodule RSpec module Expectations module Deprecation # @private # # Used internally to print deprecation warnings def deprecate(deprecated, options={}) message = "DEPRECATION: #{deprecated} is deprecated." message << " Use #{options[:replacement]} instead." if options[:replacement] message << " Called from #{caller(0)[2]}." warn message end end end extend(Expectations::Deprecation) unless respond_to?(:deprecate) end rspec-expectations-2.14.2/lib/rspec/expectations/expectation_target.rb0000644000004100000410000000525712203207642026265 0ustar www-datawww-datamodule RSpec module Expectations # Wraps the target of an expectation. # @example # expect(something) # => ExpectationTarget wrapping something # # # used with `to` # expect(actual).to eq(3) # # # with `not_to` # expect(actual).not_to eq(3) class ExpectationTarget class << self attr_accessor :deprecated_should_enabled alias deprecated_should_enabled? deprecated_should_enabled end # @api private def initialize(target) @target = target end # Runs the given expectation, passing if `matcher` returns true. # @example # expect(value).to eq(5) # expect { perform }.to raise_error # @param [Matcher] # matcher # @param [String] message optional message to display when the expectation fails # @return [Boolean] true if the expectation succeeds (else raises) # @see RSpec::Matchers def to(matcher=nil, message=nil, &block) prevent_operator_matchers(:to, matcher) RSpec::Expectations::PositiveExpectationHandler.handle_matcher(@target, matcher, message, &block) end # Runs the given expectation, passing if `matcher` returns false. # @example # expect(value).not_to eq(5) # @param [Matcher] # matcher # @param [String] message optional message to display when the expectation fails # @return [Boolean] false if the negative expectation succeeds (else raises) # @see RSpec::Matchers def not_to(matcher=nil, message=nil, &block) prevent_operator_matchers(:not_to, matcher) RSpec::Expectations::NegativeExpectationHandler.handle_matcher(@target, matcher, message, &block) end alias to_not not_to def self.enable_deprecated_should return if deprecated_should_enabled? def should(*args) RSpec.deprecate "`expect { }.should`", :replacement => "`expect { }.to`" @target.should(*args) end def should_not(*args) RSpec.deprecate "`expect { }.should_not`", :replacement => "`expect { }.not_to`" @target.should_not(*args) end self.deprecated_should_enabled = true end def self.disable_deprecated_should return unless deprecated_should_enabled? remove_method :should remove_method :should_not self.deprecated_should_enabled = false end private def prevent_operator_matchers(verb, matcher) return if matcher raise ArgumentError, "The expect syntax does not support operator matchers, " + "so you must pass a matcher to `##{verb}`." end end end end rspec-expectations-2.14.2/lib/rspec/expectations/syntax.rb0000644000004100000410000001273312203207642023717 0ustar www-datawww-datamodule RSpec module Expectations # @api private # Provides methods for enabling and disabling the available # syntaxes provided by rspec-expectations. module Syntax extend self # @method should # Passes if `matcher` returns true. Available on every `Object`. # @example # actual.should eq expected # actual.should match /expression/ # @param [Matcher] # matcher # @param [String] message optional message to display when the expectation fails # @return [Boolean] true if the expectation succeeds (else raises) # @see RSpec::Matchers # @method should_not # Passes if `matcher` returns false. Available on every `Object`. # @example # actual.should_not eq expected # @param [Matcher] # matcher # @param [String] message optional message to display when the expectation fails # @return [Boolean] false if the negative expectation succeeds (else raises) # @see RSpec::Matchers # @method expect # Supports `expect(actual).to matcher` syntax by wrapping `actual` in an # `ExpectationTarget`. # @example # expect(actual).to eq(expected) # expect(actual).not_to eq(expected) # @return [ExpectationTarget] # @see ExpectationTarget#to # @see ExpectationTarget#not_to # @api private # Determines where we add `should` and `should_not`. def default_should_host @default_should_host ||= ::Object.ancestors.last end # @api private # Enables the `should` syntax. def enable_should(syntax_host = default_should_host) return if should_enabled?(syntax_host) syntax_host.module_eval do def should(matcher=nil, message=nil, &block) ::RSpec::Expectations::PositiveExpectationHandler.handle_matcher(self, matcher, message, &block) end def should_not(matcher=nil, message=nil, &block) ::RSpec::Expectations::NegativeExpectationHandler.handle_matcher(self, matcher, message, &block) end end ::RSpec::Expectations::ExpectationTarget.enable_deprecated_should if expect_enabled? end # @api private # Disables the `should` syntax. def disable_should(syntax_host = default_should_host) return unless should_enabled?(syntax_host) syntax_host.module_eval do undef should undef should_not end ::RSpec::Expectations::ExpectationTarget.disable_deprecated_should end # @api private # Enables the `expect` syntax. def enable_expect(syntax_host = ::RSpec::Matchers) return if expect_enabled?(syntax_host) syntax_host.module_eval do def expect(*target, &target_block) target << target_block if block_given? raise ArgumentError.new("You must pass an argument or a block to #expect but not both.") unless target.size == 1 ::RSpec::Expectations::ExpectationTarget.new(target.first) end end ::RSpec::Expectations::ExpectationTarget.enable_deprecated_should if should_enabled? end # @api private # Disables the `expect` syntax. def disable_expect(syntax_host = ::RSpec::Matchers) return unless expect_enabled?(syntax_host) syntax_host.module_eval do undef expect end ::RSpec::Expectations::ExpectationTarget.disable_deprecated_should end # @api private # Indicates whether or not the `should` syntax is enabled. def should_enabled?(syntax_host = default_should_host) syntax_host.method_defined?(:should) end # @api private # Indicates whether or not the `expect` syntax is enabled. def expect_enabled?(syntax_host = ::RSpec::Matchers) syntax_host.method_defined?(:expect) end # @api private # Generates a positive expectation expression. def positive_expression(target_expression, matcher_expression) expression_generator.positive_expression(target_expression, matcher_expression) end # @api private # Generates a negative expectation expression. def negative_expression(target_expression, matcher_expression) expression_generator.negative_expression(target_expression, matcher_expression) end # @api private # Selects which expression generator to use based on the configured syntax. def expression_generator if expect_enabled? ExpectExpressionGenerator else ShouldExpressionGenerator end end # @api private # Generates expectation expressions for the `should` syntax. module ShouldExpressionGenerator def self.positive_expression(target_expression, matcher_expression) "#{target_expression}.should #{matcher_expression}" end def self.negative_expression(target_expression, matcher_expression) "#{target_expression}.should_not #{matcher_expression}" end end # @api private # Generates expectation expressions for the `expect` syntax. module ExpectExpressionGenerator def self.positive_expression(target_expression, matcher_expression) "expect(#{target_expression}).to #{matcher_expression}" end def self.negative_expression(target_expression, matcher_expression) "expect(#{target_expression}).not_to #{matcher_expression}" end end end end end rspec-expectations-2.14.2/lib/rspec/expectations/differ.rb0000644000004100000410000001020312203207642023616 0ustar www-datawww-datarequire 'diff/lcs' require 'diff/lcs/hunk' require 'pp' module RSpec module Expectations class Differ # This is snagged from diff/lcs/ldiff.rb (which is a commandline tool) def diff_as_string(input_data_new, input_data_old) output = matching_encoding("", input_data_old) data_old = input_data_old.split(matching_encoding("\n", input_data_old)).map! { |e| e.chomp } data_new = input_data_new.split(matching_encoding("\n", input_data_new)).map! { |e| e.chomp } diffs = Diff::LCS.diff(data_old, data_new) return output if diffs.empty? oldhunk = hunk = nil file_length_difference = 0 diffs.each do |piece| begin hunk = Diff::LCS::Hunk.new( data_old, data_new, piece, context_lines, file_length_difference ) file_length_difference = hunk.file_length_difference next unless oldhunk # Hunks may overlap, which is why we need to be careful when our # diff includes lines of context. Otherwise, we might print # redundant lines. if (context_lines > 0) and hunk.overlaps?(oldhunk) if hunk.respond_to?(:merge) # diff-lcs 1.2.x hunk.merge(oldhunk) else # diff-lcs 1.1.3 hunk.unshift(oldhunk) end else output << matching_encoding(oldhunk.diff(format).to_s, output) end ensure oldhunk = hunk output << matching_encoding("\n", output) end end #Handle the last remaining hunk output << matching_encoding(oldhunk.diff(format).to_s,output) output << matching_encoding("\n",output) color_diff output rescue Encoding::CompatibilityError if input_data_new.encoding != input_data_old.encoding "Could not produce a diff because the encoding of the actual string (#{input_data_old.encoding}) "+ "differs from the encoding of the expected string (#{input_data_new.encoding})" else "Could not produce a diff because of the encoding of the string (#{input_data_old.encoding})" end end def diff_as_object(actual, expected) actual_as_string = object_to_string(actual) expected_as_string = object_to_string(expected) if diff = diff_as_string(actual_as_string, expected_as_string) color_diff diff end end protected def format :unified end def context_lines 3 end def color(text, color_code) "\e[#{color_code}m#{text}\e[0m" end def red(text) color(text, 31) end def green(text) color(text, 32) end def blue(text) color(text, 34) end def color_diff(diff) return diff unless RSpec::Matchers.configuration.color? diff.lines.map { |line| case line[0].chr when "+" green line when "-" red line when "@" line[1].chr == "@" ? blue(line) : line else line end }.join end def object_to_string(object) case object when Hash object.keys.sort_by { |k| k.to_s }.map do |key| pp_key = PP.singleline_pp(key, "") pp_value = PP.singleline_pp(object[key], "") # on 1.9.3 PP seems to minimise to US-ASCII, ensure we're matching source encoding # # note, PP is used to ensure the ordering of the internal values of key/value e.g. # <# a: b: c:> not <# c: a: b:> matching_encoding("#{pp_key} => #{pp_value}", key) end.join(",\n") when String object =~ /\n/ ? object : object.inspect else PP.pp(object,"") end end if String.method_defined?(:encoding) def matching_encoding(string, source) string.encode(source.encoding) end else def matching_encoding(string, source) string end end end end end rspec-expectations-2.14.2/lib/rspec/expectations/extensions.rb0000644000004100000410000000013512203207642024561 0ustar www-datawww-datarequire 'rspec/expectations/extensions/array' require 'rspec/expectations/extensions/object' rspec-expectations-2.14.2/lib/rspec/expectations/fail_with.rb0000644000004100000410000000427212203207642024336 0ustar www-datawww-datamodule RSpec module Expectations class << self # @private def differ @differ ||= Differ.new end # Raises an RSpec::Expectations::ExpectationNotMetError with message. # @param [String] message # @param [Object] expected # @param [Object] actual # # Adds a diff to the failure message when `expected` and `actual` are # both present. def fail_with(message, expected=nil, actual=nil) if !message raise ArgumentError, "Failure message is nil. Does your matcher define the " + "appropriate failure_message_for_* method to return a string?" end if actual && expected if all_strings?(actual, expected) if any_multiline_strings?(actual, expected) message << "\nDiff:" << differ.diff_as_string(coerce_to_string(actual), coerce_to_string(expected)) end elsif no_procs?(actual, expected) && no_numbers?(actual, expected) message << "\nDiff:" << differ.diff_as_object(actual, expected) end end raise(RSpec::Expectations::ExpectationNotMetError.new(message)) end private def no_procs?(*args) args.flatten.none? {|a| Proc === a} end def all_strings?(*args) args.flatten.all? {|a| String === a} end def any_multiline_strings?(*args) all_strings?(*args) && args.flatten.any? { |a| multiline?(a) } end def no_numbers?(*args) args.flatten.none? {|a| Numeric === a} end def coerce_to_string(string_or_array) return string_or_array unless Array === string_or_array diffably_stringify(string_or_array).join("\n") end def diffably_stringify(array) array.map do |entry| if Array === entry entry.inspect else entry.to_s.gsub("\n", "\\n") end end end if String.method_defined?(:encoding) def multiline?(string) string.include?("\n".encode(string.encoding)) end else def multiline?(string) string.include?("\n") end end end end end rspec-expectations-2.14.2/lib/rspec/expectations/extensions/0000755000004100000410000000000012203207642024235 5ustar www-datawww-datarspec-expectations-2.14.2/lib/rspec/expectations/extensions/array.rb0000644000004100000410000000032212203207642025675 0ustar www-datawww-data# @private class Array unless public_instance_methods.map {|m| m.to_s}.include?('none?') # Supports +none?+ on early patch levels of Ruby 1.8.6 def none?(&block) !any?(&block) end end end rspec-expectations-2.14.2/lib/rspec/expectations/extensions/object.rb0000644000004100000410000000132712203207642026033 0ustar www-datawww-datamodule RSpec module Expectations module DeprecatedConstants # Displays deprecation warning when it captures Rspec and Spec. Otherwise # delegates to super. def const_missing(name) case name when :Rspec, :Spec RSpec.deprecate(name.to_s, :replacement => "RSpec") RSpec else begin super rescue Exception => e e.backtrace.reject! {|l| l =~ Regexp.compile(__FILE__) } raise e end end end end # @deprecated (no replacement) def differ=(ignore) RSpec.deprecate("RSpec::Expectations.differ=(differ)") end end end extend RSpec::Expectations::DeprecatedConstants rspec-expectations-2.14.2/lib/rspec/expectations.rb0000644000004100000410000000331212203207642022362 0ustar www-datawww-datarequire 'rspec/expectations/extensions' require 'rspec/matchers' require 'rspec/expectations/expectation_target' require 'rspec/matchers/configuration' require 'rspec/expectations/fail_with' require 'rspec/expectations/errors' require 'rspec/expectations/deprecation' require 'rspec/expectations/handler' require 'rspec/expectations/version' require 'rspec/expectations/differ' module RSpec # RSpec::Expectations adds two instance methods to every object: # # should(matcher=nil) # should_not(matcher=nil) # # Both methods take an optional matcher object (See # [RSpec::Matchers](../RSpec/Matchers)). When `should` is invoked with a # matcher, it turns around and calls `matcher.matches?(self)`. For example, # in the expression: # # order.total.should eq(Money.new(5.55, :USD)) # # the `should` method invokes the equivalent of `eq.matches?(order.total)`. If # `matches?` returns true, the expectation is met and execution continues. If # `false`, then the spec fails with the message returned by # `eq.failure_message_for_should`. # # Given the expression: # # order.entries.should_not include(entry) # # the `should_not` method invokes the equivalent of # `include.matches?(order.entries)`, but it interprets `false` as success, and # `true` as a failure, using the message generated by # `eq.failure_message_for_should_not`. # # rspec-expectations ships with a standard set of useful matchers, and writing # your own matchers is quite simple. # # See [RSpec::Matchers](../RSpec/Matchers) for more information about the # built-in matchers that ship with rspec-expectations, and how to write your # own custom matchers. module Expectations end end rspec-expectations-2.14.2/lib/rspec/matchers/0000755000004100000410000000000012203207642021136 5ustar www-datawww-datarspec-expectations-2.14.2/lib/rspec/matchers/test_unit_integration.rb0000644000004100000410000000076712203207642026116 0ustar www-datawww-data# Include Matchers for other test frameworks. Note that MiniTest _must_ # come before TU because on ruby 1.9, T::U::TC is a subclass of MT::U::TC # and a 1.9 bug can lead to infinite recursion from the `super` call in our # method_missing hook. See this gist for more info: # https://gist.github.com/845896 if defined?(MiniTest::Unit::TestCase) MiniTest::Unit::TestCase.send(:include, RSpec::Matchers) end if defined?(Test::Unit::TestCase) Test::Unit::TestCase.send(:include, RSpec::Matchers) end rspec-expectations-2.14.2/lib/rspec/matchers/matcher.rb0000644000004100000410000002374112203207642023115 0ustar www-datawww-datarequire 'set' module RSpec module Matchers module DSL # Provides the context in which the block passed to RSpec::Matchers.define # will be evaluated. class Matcher include RSpec::Matchers::Extensions::InstanceEvalWithArgs include RSpec::Matchers::Pretty include RSpec::Matchers attr_reader :expected, :actual, :rescued_exception attr_accessor :matcher_execution_context # @api private def initialize(name, &declarations) @name = name @declarations = declarations @actual = nil @diffable = false @expected_exception, @rescued_exception = nil, nil @match_for_should_not_block = nil @messages = {} end PERSISTENT_INSTANCE_VARIABLES = [ :@name, :@declarations, :@diffable, :@match_block, :@match_for_should_not_block, :@expected_exception ].to_set # @api private def for_expected(*expected) @expected = expected dup.instance_eval do instance_variables.map {|ivar| ivar.intern}.each do |ivar| instance_variable_set(ivar, nil) unless (PERSISTENT_INSTANCE_VARIABLES + [:@expected]).include?(ivar) end @messages = {} making_declared_methods_public do instance_eval_with_args(*@expected, &@declarations) end self end end # @api private # Used internally by +should+ and +should_not+. def matches?(actual) @actual = actual if @expected_exception begin instance_eval_with_args(actual, &@match_block) true rescue @expected_exception => @rescued_exception false end else begin instance_eval_with_args(actual, &@match_block) rescue RSpec::Expectations::ExpectationNotMetError false end end end # Stores the block that is used to determine whether this matcher passes # or fails. The block should return a boolean value. When the matcher is # passed to `should` and the block returns `true`, then the expectation # passes. Similarly, when the matcher is passed to `should_not` and the # block returns `false`, then the expectation passes. # # Use `match_for_should` when used in conjuntion with # `match_for_should_not`. # # @example # # RSpec::Matchers.define :be_even do # match do |actual| # actual.even? # end # end # # 4.should be_even # passes # 3.should_not be_even # passes # 3.should be_even # fails # 4.should_not be_even # fails # # @yield [Object] actual the actual value (or receiver of should) def match(&block) @match_block = block end alias_method :match_for_should, :match # Use this to define the block for a negative expectation (`should_not`) # when the positive and negative forms require different handling. This # is rarely necessary, but can be helpful, for example, when specifying # asynchronous processes that require different timeouts. # # @yield [Object] actual the actual value (or receiver of should) def match_for_should_not(&block) @match_for_should_not_block = block end # Use this instead of `match` when the block will raise an exception # rather than returning false to indicate a failure. # # @example # # RSpec::Matchers.define :accept_as_valid do |candidate_address| # match_unless_raises ValidationException do |validator| # validator.validate(candidate_address) # end # end # # email_validator.should accept_as_valid("person@company.com") def match_unless_raises(exception=Exception, &block) @expected_exception = exception match(&block) end # Customize the failure messsage to use when this matcher is invoked with # `should`. Only use this when the message generated by default doesn't # suit your needs. # # @example # # RSpec::Matchers.define :have_strength do |expected| # match { ... } # # failure_message_for_should do |actual| # "Expected strength of #{expected}, but had #{actual.strength}" # end # end # # @yield [Object] actual the actual object def failure_message_for_should(&block) cache_or_call_cached(:failure_message_for_should, &block) end # Customize the failure messsage to use when this matcher is invoked with # `should_not`. Only use this when the message generated by default # doesn't suit your needs. # # @example # # RSpec::Matchers.define :have_strength do |expected| # match { ... } # # failure_message_for_should_not do |actual| # "Expected not to have strength of #{expected}, but did" # end # end # # @yield [Object] actual the actual object # @yield [Object] actual the actual object def failure_message_for_should_not(&block) cache_or_call_cached(:failure_message_for_should_not, &block) end # Customize the description to use for one-liners. Only use this when # the description generated by default doesn't suit your needs. # # @example # # RSpec::Matchers.define :qualify_for do |expected| # match { ... } # # description do # "qualify for #{expected}" # end # end def description(&block) cache_or_call_cached(:description, &block) end # Tells the matcher to diff the actual and expected values in the failure # message. def diffable @diffable = true end # Convenience for defining methods on this matcher to create a fluent # interface. The trick about fluent interfaces is that each method must # return self in order to chain methods together. `chain` handles that # for you. # # @example # # RSpec::Matchers.define :have_errors_on do |key| # chain :with do |message| # @message = message # end # # match do |actual| # actual.errors[key] == @message # end # end # # minor.should have_errors_on(:age).with("Not old enough to participate") def chain(method, &block) define_method method do |*args| block.call(*args) self end end # @api private # Used internally by objects returns by +should+ and +should_not+. def diffable? @diffable end # @api private # Used internally by +should_not+ def does_not_match?(actual) @actual = actual @match_for_should_not_block ? instance_eval_with_args(actual, &@match_for_should_not_block) : !matches?(actual) end def respond_to?(method, include_private=false) super || matcher_execution_context.respond_to?(method, include_private) end private def method_missing(method, *args, &block) if matcher_execution_context.respond_to?(method) matcher_execution_context.__send__ method, *args, &block else super(method, *args, &block) end end def include(*args) singleton_class.__send__(:include, *args) end def define_method(name, &block) singleton_class.__send__(:define_method, name, &block) end def making_declared_methods_public # Our home-grown instance_exec in ruby 1.8.6 results in any methods # declared in the block eval'd by instance_exec in the block to which we # are yielding here are scoped private. This is NOT the case for Ruby # 1.8.7 or 1.9. # # Also, due some crazy scoping that I don't understand, these methods # are actually available in the specs (something about the matcher being # defined in the scope of RSpec::Matchers or within an example), so not # doing the following will not cause specs to fail, but they *will* # cause features to fail and that will make users unhappy. So don't. orig_private_methods = private_methods yield (private_methods - orig_private_methods).each {|m| singleton_class.__send__ :public, m} end def cache_or_call_cached(key, &block) block ? cache(key, &block) : call_cached(key) end def cache(key, &block) @messages[key] = block end def call_cached(key) if @messages.has_key?(key) @messages[key].arity == 1 ? @messages[key].call(@actual) : @messages[key].call else __send__("default_#{key}") end end def default_description "#{name_to_sentence}#{expected_to_sentence}" end def default_failure_message_for_should "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}" end def default_failure_message_for_should_not "expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}" end unless method_defined?(:singleton_class) def singleton_class class << self; self; end end end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/configuration.rb0000644000004100000410000000640412203207642024336 0ustar www-datawww-datarequire 'rspec/expectations/syntax' module RSpec module Matchers # Provides configuration options for rspec-expectations. class Configuration # Configures the supported syntax. # @param [Array, Symbol] values the syntaxes to enable # @example # RSpec.configure do |rspec| # rspec.expect_with :rspec do |c| # c.syntax = :should # # or # c.syntax = :expect # # or # c.syntax = [:should, :expect] # end # end def syntax=(values) if Array(values).include?(:expect) Expectations::Syntax.enable_expect else Expectations::Syntax.disable_expect end if Array(values).include?(:should) Expectations::Syntax.enable_should else Expectations::Syntax.disable_should end end # The list of configured syntaxes. # @return [Array] the list of configured syntaxes. def syntax syntaxes = [] syntaxes << :should if Expectations::Syntax.should_enabled? syntaxes << :expect if Expectations::Syntax.expect_enabled? syntaxes end # color config for expectations # fallback if rspec core not available if ::RSpec.respond_to?(:configuration) def color? ::RSpec.configuration.color_enabled? end else attr_writer :color def color? @color end end # Adds `should` and `should_not` to the given classes # or modules. This can be used to ensure `should` works # properly on things like proxy objects (particular # `Delegator`-subclassed objects on 1.8). # # @param [Array] modules the list of classes or modules # to add `should` and `should_not` to. def add_should_and_should_not_to(*modules) modules.each do |mod| Expectations::Syntax.enable_should(mod) end end # Sets or gets the backtrace formatter. The backtrace formatter should # implement `#format_backtrace(Array)`. This is used # to format backtraces of errors handled by the `raise_error` # matcher. # # If you are using rspec-core, rspec-core's backtrace formatting # will be used (including respecting the presence or absence of # the `--backtrace` option). # # @overload backtrace_formatter # @return [#format_backtrace] the backtrace formatter # @overload backtrace_formatter= # @param value [#format_backtrace] sets the backtrace formatter attr_writer :backtrace_formatter def backtrace_formatter @backtrace_formatter ||= if defined?(::RSpec::Core::BacktraceFormatter) ::RSpec::Core::BacktraceFormatter else NullBacktraceFormatter end end # @api private NullBacktraceFormatter = Module.new do def self.format_backtrace(backtrace) backtrace end end end # The configuration object # @return [RSpec::Matchers::Configuration] the configuration object def self.configuration @configuration ||= Configuration.new end # set default syntax configuration.syntax = [:expect, :should] end end rspec-expectations-2.14.2/lib/rspec/matchers/be_close.rb0000644000004100000410000000042212203207642023234 0ustar www-datawww-datamodule RSpec module Matchers # @deprecated use +be_within+ instead. def be_close(expected, delta) RSpec.deprecate("be_close(#{expected}, #{delta})", :replacement => "be_within(#{delta}).of(#{expected})") be_within(delta).of(expected) end end end rspec-expectations-2.14.2/lib/rspec/matchers/method_missing.rb0000644000004100000410000000046312203207642024477 0ustar www-datawww-datamodule RSpec module Matchers private def method_missing(method, *args, &block) return Matchers::BuiltIn::BePredicate.new(method, *args, &block) if method.to_s =~ /^be_/ return Matchers::BuiltIn::Has.new(method, *args, &block) if method.to_s =~ /^have_/ super end end end rspec-expectations-2.14.2/lib/rspec/matchers/pretty.rb0000644000004100000410000000324712203207642023020 0ustar www-datawww-datamodule RSpec module Matchers module Pretty def split_words(sym) sym.to_s.gsub(/_/,' ') end def to_sentence(words) return "" unless words words = Array(words).map { |w| to_word(w) } case words.length when 0 "" when 1 " #{words[0]}" when 2 " #{words[0]} and #{words[1]}" else " #{words[0...-1].join(', ')}, and #{words[-1]}" end end def _pretty_print(array) result = "" array.each_with_index do |item, index| if index < (array.length - 2) result << "#{item.inspect}, " elsif index < (array.length - 1) result << "#{item.inspect} and " else result << "#{item.inspect}" end end result end def to_word(item) is_matcher_with_description?(item) ? item.description : item.inspect end def name_to_sentence split_words(name) end def expected_to_sentence to_sentence(@expected) if defined?(@expected) end def name defined?(@name) ? @name : underscore(self.class.name.split("::").last) end # Borrowed from ActiveSupport def underscore(camel_cased_word) word = camel_cased_word.to_s.dup word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end private def is_matcher_with_description?(object) RSpec::Matchers.is_a_matcher?(object) && object.respond_to?(:description) end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in.rb0000644000004100000410000000411712203207642023273 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn require 'rspec/matchers/built_in/base_matcher' autoload :BeAnInstanceOf, 'rspec/matchers/built_in/be_instance_of' autoload :Be, 'rspec/matchers/built_in/be' autoload :BeTrue, 'rspec/matchers/built_in/be' autoload :BeFalse, 'rspec/matchers/built_in/be' autoload :BeNil, 'rspec/matchers/built_in/be' autoload :BeComparedTo, 'rspec/matchers/built_in/be' autoload :BePredicate, 'rspec/matchers/built_in/be' autoload :BeAKindOf, 'rspec/matchers/built_in/be_kind_of' autoload :BeWithin, 'rspec/matchers/built_in/be_within' autoload :Change, 'rspec/matchers/built_in/change' autoload :Cover, 'rspec/matchers/built_in/cover' if (1..2).respond_to?(:cover?) autoload :Eq, 'rspec/matchers/built_in/eq' autoload :Eql, 'rspec/matchers/built_in/eql' autoload :Equal, 'rspec/matchers/built_in/equal' autoload :Exist, 'rspec/matchers/built_in/exist' autoload :Has, 'rspec/matchers/built_in/has' autoload :Have, 'rspec/matchers/built_in/have' autoload :Include, 'rspec/matchers/built_in/include' autoload :Match, 'rspec/matchers/built_in/match' autoload :MatchArray, 'rspec/matchers/built_in/match_array' autoload :RaiseError, 'rspec/matchers/built_in/raise_error' autoload :RespondTo, 'rspec/matchers/built_in/respond_to' autoload :StartWith, 'rspec/matchers/built_in/start_and_end_with' autoload :EndWith, 'rspec/matchers/built_in/start_and_end_with' autoload :Satisfy, 'rspec/matchers/built_in/satisfy' autoload :ThrowSymbol, 'rspec/matchers/built_in/throw_symbol' autoload :YieldControl, 'rspec/matchers/built_in/yield' autoload :YieldWithArgs, 'rspec/matchers/built_in/yield' autoload :YieldWithNoArgs, 'rspec/matchers/built_in/yield' autoload :YieldSuccessiveArgs, 'rspec/matchers/built_in/yield' end end end rspec-expectations-2.14.2/lib/rspec/matchers/operator_matcher.rb0000644000004100000410000000704612203207642025030 0ustar www-datawww-datamodule RSpec module Matchers class OperatorMatcher class << self def registry @registry ||= {} end def register(klass, operator, matcher) registry[klass] ||= {} registry[klass][operator] = matcher end def unregister(klass, operator) registry[klass] && registry[klass].delete(operator) end def get(klass, operator) klass.ancestors.each { |ancestor| matcher = registry[ancestor] && registry[ancestor][operator] return matcher if matcher } nil end end def initialize(actual) @actual = actual end def self.use_custom_matcher_or_delegate(operator) define_method(operator) do |expected| if uses_generic_implementation_of?(operator) && matcher = OperatorMatcher.get(@actual.class, operator) @actual.__send__(::RSpec::Matchers.last_should, matcher.new(expected)) else eval_match(@actual, operator, expected) end end negative_operator = operator.sub(/^=/, '!') if negative_operator != operator && respond_to?(negative_operator) define_method(negative_operator) do |expected| opposite_should = ::RSpec::Matchers.last_should == :should ? :should_not : :should raise "RSpec does not support `#{::RSpec::Matchers.last_should} #{negative_operator} expected`. " + "Use `#{opposite_should} #{operator} expected` instead." end end end ['==', '===', '=~', '>', '>=', '<', '<='].each do |operator| use_custom_matcher_or_delegate operator end def fail_with_message(message) RSpec::Expectations.fail_with(message, @expected, @actual) end def description "#{@operator} #{@expected.inspect}" end private if Method.method_defined?(:owner) # 1.8.6 lacks Method#owner :-( def uses_generic_implementation_of?(op) @actual.method(op).owner == ::Kernel end else def uses_generic_implementation_of?(op) # This is a bit of a hack, but: # # {}.method(:=~).to_s # => "#" # # In the absence of Method#owner, this is the best we # can do to see if the method comes from Kernel. @actual.method(op).to_s.include?('(Kernel)') end end def eval_match(actual, operator, expected) ::RSpec::Matchers.last_matcher = self @operator, @expected = operator, expected __delegate_operator(actual, operator, expected) end end module BuiltIn class PositiveOperatorMatcher < OperatorMatcher def __delegate_operator(actual, operator, expected) if actual.__send__(operator, expected) true elsif ['==','===', '=~'].include?(operator) fail_with_message("expected: #{expected.inspect}\n got: #{actual.inspect} (using #{operator})") else fail_with_message("expected: #{operator} #{expected.inspect}\n got: #{operator.gsub(/./, ' ')} #{actual.inspect}") end end end class NegativeOperatorMatcher < OperatorMatcher def __delegate_operator(actual, operator, expected) return false unless actual.__send__(operator, expected) return fail_with_message("expected not: #{operator} #{expected.inspect}\n got: #{operator.gsub(/./, ' ')} #{actual.inspect}") end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/dsl.rb0000644000004100000410000000121112203207642022240 0ustar www-datawww-datamodule RSpec module Matchers module DSL # Defines a custom matcher. # @see RSpec::Matchers def define(name, &declarations) matcher_template = RSpec::Matchers::DSL::Matcher.new(name, &declarations) define_method name do |*expected| matcher = matcher_template.for_expected(*expected) matcher.matcher_execution_context = @matcher_execution_context ||= self matcher end end alias_method :matcher, :define if RSpec.respond_to?(:configure) RSpec.configure {|c| c.extend self} end end end end RSpec::Matchers.extend RSpec::Matchers::DSL rspec-expectations-2.14.2/lib/rspec/matchers/built_in/0000755000004100000410000000000012203207642022743 5ustar www-datawww-datarspec-expectations-2.14.2/lib/rspec/matchers/built_in/be_kind_of.rb0000644000004100000410000000030512203207642025345 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class BeAKindOf < BaseMatcher def match(expected, actual) actual.kind_of? expected end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/throw_symbol.rb0000644000004100000410000000543312203207642026025 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class ThrowSymbol def initialize(expected_symbol = nil, expected_arg=nil) @expected_symbol = expected_symbol @expected_arg = expected_arg @caught_symbol = @caught_arg = nil end def matches?(given_proc) begin if @expected_symbol.nil? given_proc.call else @caught_arg = catch :proc_did_not_throw_anything do catch @expected_symbol do given_proc.call throw :proc_did_not_throw_anything, :nothing_thrown end end if @caught_arg == :nothing_thrown @caught_arg = nil else @caught_symbol = @expected_symbol end end # Ruby 1.8 uses NameError with `symbol' # Ruby 1.9 uses ArgumentError with :symbol rescue NameError, ArgumentError => e unless e.message =~ /uncaught throw (`|\:)([a-zA-Z0-9_]*)(')?/ other_exception = e raise end @caught_symbol = $2.to_sym rescue => other_exception raise ensure unless other_exception if @expected_symbol.nil? return !@caught_symbol.nil? else if @expected_arg.nil? return @caught_symbol == @expected_symbol else return (@caught_symbol == @expected_symbol) & (@caught_arg == @expected_arg) end end end end end alias == matches? def failure_message_for_should "expected #{expected} to be thrown, got #{caught}" end def failure_message_for_should_not "expected #{expected('no Symbol')}#{' not' if @expected_symbol} to be thrown, got #{caught}" end def description "throw #{expected}" end private def expected(symbol_desc = 'a Symbol') throw_description(@expected_symbol || symbol_desc, @expected_arg) end def caught throw_description(@caught_symbol || 'nothing', @caught_arg) end def throw_description(symbol, arg) symbol_description = symbol.is_a?(String) ? symbol : symbol.inspect arg_description = if arg " with #{arg.inspect}" elsif @expected_arg && @caught_symbol == @expected_symbol " with no argument" else "" end symbol_description + arg_description end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/respond_to.rb0000644000004100000410000000375712203207642025460 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class RespondTo def initialize(*names) @names = names @expected_arity = nil end def matches?(actual) find_failing_method_names(actual, :reject).empty? end alias == matches? def does_not_match?(actual) find_failing_method_names(actual, :select).empty? end def failure_message_for_should "expected #{@actual.inspect} to respond to #{@failing_method_names.collect {|name| name.inspect }.join(', ')}#{with_arity}" end def failure_message_for_should_not failure_message_for_should.sub(/to respond to/, 'not to respond to') end def description "respond to #{pp_names}#{with_arity}" end def with(n) @expected_arity = n self end def argument self end alias :arguments :argument private def find_failing_method_names(actual, filter_method) @actual = actual @failing_method_names = @names.__send__(filter_method) do |name| @actual.respond_to?(name) && matches_arity?(actual, name) end end def matches_arity?(actual, name) return true unless @expected_arity actual_arity = actual.method(name).arity if actual_arity < 0 # ~ inverts the one's complement and gives us the number of required args ~actual_arity <= @expected_arity else actual_arity == @expected_arity end end def with_arity @expected_arity.nil?? "" : " with #{@expected_arity} argument#{@expected_arity == 1 ? '' : 's'}" end def pp_names # Ruby 1.9 returns the same thing for array.to_s as array.inspect, so just use array.inspect here @names.length == 1 ? "##{@names.first}" : @names.inspect end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/be_within.rb0000644000004100000410000000251612203207642025244 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class BeWithin def initialize(delta) @delta = delta end def matches?(actual) @actual = actual raise needs_expected unless defined? @expected raise needs_subtractable unless @actual.respond_to? :- (@actual - @expected).abs <= @tolerance end alias == matches? def of(expected) @expected = expected @tolerance = @delta @unit = '' self end def percent_of(expected) @expected = expected @tolerance = @delta * @expected.abs / 100.0 @unit = '%' self end def failure_message_for_should "expected #{@actual} to #{description}" end def failure_message_for_should_not "expected #{@actual} not to #{description}" end def description "be within #{@delta}#{@unit} of #{@expected}" end private def needs_subtractable ArgumentError.new "The actual value (#{@actual.inspect}) must respond to `-`" end def needs_expected ArgumentError.new "You must set an expected value using #of: be_within(#{@delta}).of(expected_value)" end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/has.rb0000644000004100000410000000226312203207642024046 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class Has def initialize(expected, *args) @expected, @args = expected, args end def matches?(actual) actual.__send__(predicate(@expected), *@args) end alias == matches? def failure_message_for_should "expected ##{predicate(@expected)}#{failure_message_args_description} to return true, got false" end def failure_message_for_should_not "expected ##{predicate(@expected)}#{failure_message_args_description} to return false, got true" end def description [method_description(@expected), args_description].compact.join(' ') end private def predicate(sym) "#{sym.to_s.sub("have_","has_")}?".to_sym end def method_description(method) method.to_s.gsub('_', ' ') end def args_description return nil if @args.empty? @args.map { |arg| arg.inspect }.join(', ') end def failure_message_args_description desc = args_description "(#{desc})" if desc end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/yield.rb0000644000004100000410000002007412203207642024401 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class YieldProbe def self.probe(block) probe = new assert_valid_expect_block!(block) block.call(probe) probe.assert_used! probe end attr_accessor :num_yields, :yielded_args def initialize @used = false self.num_yields, self.yielded_args = 0, [] end def to_proc @used = true probe = self Proc.new do |*args| probe.num_yields += 1 probe.yielded_args << args end end def single_yield_args yielded_args.first end def yielded_once?(matcher_name) case num_yields when 1 then true when 0 then false else raise "The #{matcher_name} matcher is not designed to be used with a " + "method that yields multiple times. Use the yield_successive_args " + "matcher for that case." end end def successive_yield_args yielded_args.map do |arg_array| arg_array.size == 1 ? arg_array.first : arg_array end end def assert_used! return if @used raise "You must pass the argument yielded to your expect block on " + "to the method-under-test as a block. It acts as a probe that " + "allows the matcher to detect whether or not the method-under-test " + "yields, and, if so, how many times, and what the yielded arguments " + "are." end def self.assert_valid_expect_block!(block) return if block.arity == 1 raise "Your expect block must accept an argument to be used with this " + "matcher. Pass the argument as a block on to the method you are testing." end end class YieldControl < BaseMatcher def initialize @expectation_type = nil @expected_yields_count = nil end def matches?(block) probe = YieldProbe.probe(block) if @expectation_type probe.num_yields.send(@expectation_type, @expected_yields_count) else probe.yielded_once?(:yield_control) end end def once exactly(1) self end def twice exactly(2) self end def exactly(number) set_expected_yields_count(:==, number) self end def at_most(number) set_expected_yields_count(:<=, number) self end def at_least(number) set_expected_yields_count(:>=, number) self end def times self end def failure_message_for_should 'expected given block to yield control'.tap do |failure_message| failure_message << relativity_failure_message end end def failure_message_for_should_not 'expected given block not to yield control'.tap do |failure_message| failure_message << relativity_failure_message end end private def set_expected_yields_count(relativity, n) @expectation_type = relativity @expected_yields_count = case n when Numeric then n when :once then 1 when :twice then 2 end end def relativity_failure_message return '' unless @expected_yields_count " #{human_readable_expecation_type}#{human_readable_count}" end def human_readable_expecation_type case @expectation_type when :<= then 'at most ' when :>= then 'at least ' else '' end end def human_readable_count case @expected_yields_count when 1 then "once" when 2 then "twice" else "#{@expected_yields_count} times" end end end class YieldWithNoArgs < BaseMatcher def matches?(block) @probe = YieldProbe.probe(block) @probe.yielded_once?(:yield_with_no_args) && @probe.single_yield_args.empty? end def failure_message_for_should "expected given block to yield with no arguments, but #{failure_reason}" end def failure_message_for_should_not "expected given block not to yield with no arguments, but did" end private def failure_reason if @probe.num_yields.zero? "did not yield" else "yielded with arguments: #{@probe.single_yield_args.inspect}" end end end class YieldWithArgs def initialize(*args) @expected = args end def matches?(block) @probe = YieldProbe.probe(block) @actual = @probe.single_yield_args @probe.yielded_once?(:yield_with_args) && args_match? end alias == matches? def failure_message_for_should "expected given block to yield with arguments, but #{positive_failure_reason}" end def failure_message_for_should_not "expected given block not to yield with arguments, but #{negative_failure_reason}" end def description desc = "yield with args" desc << "(" + @expected.map { |e| e.inspect }.join(", ") + ")" unless @expected.empty? desc end private def positive_failure_reason if @probe.num_yields.zero? "did not yield" else @positive_args_failure end end def negative_failure_reason if all_args_match? "yielded with expected arguments" + "\nexpected not: #{@expected.inspect}" + "\n got: #{@actual.inspect} (compared using === and ==)" else "did" end end def args_match? if @expected.empty? # expect {...}.to yield_with_args @positive_args_failure = "yielded with no arguments" if @actual.empty? return !@actual.empty? end unless match = all_args_match? @positive_args_failure = "yielded with unexpected arguments" + "\nexpected: #{@expected.inspect}" + "\n got: #{@actual.inspect} (compared using === and ==)" end match end def all_args_match? return false if @expected.size != @actual.size @expected.zip(@actual).all? do |expected, actual| expected === actual || actual == expected end end end class YieldSuccessiveArgs def initialize(*args) @expected = args end def matches?(block) @probe = YieldProbe.probe(block) @actual = @probe.successive_yield_args args_match? end alias == matches? def failure_message_for_should "expected given block to yield successively with arguments, but yielded with unexpected arguments" + "\nexpected: #{@expected.inspect}" + "\n got: #{@actual.inspect} (compared using === and ==)" end def failure_message_for_should_not "expected given block not to yield successively with arguments, but yielded with expected arguments" + "\nexpected not: #{@expected.inspect}" + "\n got: #{@actual.inspect} (compared using === and ==)" end def description desc = "yield successive args" desc << "(" + @expected.map { |e| e.inspect }.join(", ") + ")" desc end private def args_match? return false if @expected.size != @actual.size @expected.zip(@actual).all? do |expected, actual| expected === actual || actual == expected end end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/change.rb0000644000004100000410000001016312203207642024516 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class Change def initialize(receiver=nil, message=nil, &block) @message = message @value_proc = block || lambda {receiver.__send__(message)} @expected_after = @expected_before = @minimum = @maximum = @expected_delta = nil @eval_before = @eval_after = false end def matches?(event_proc) raise_block_syntax_error if block_given? @actual_before = evaluate_value_proc event_proc.call @actual_after = evaluate_value_proc (!change_expected? || changed?) && matches_before? && matches_after? && matches_expected_delta? && matches_min? && matches_max? end alias == matches? def raise_block_syntax_error raise SyntaxError.new(<<-MESSAGE) block passed to should or should_not change must use {} instead of do/end MESSAGE end def evaluate_value_proc case val = @value_proc.call when Enumerable, String val.dup else val end end def failure_message_for_should if @eval_before && !expected_matches_actual?(@expected_before, @actual_before) "#{message} should have initially been #{@expected_before.inspect}, but was #{@actual_before.inspect}" elsif @eval_after && !expected_matches_actual?(@expected_after, @actual_after) "#{message} should have been changed to #{failure_message_for_expected_after}, but is now #{@actual_after.inspect}" elsif @expected_delta "#{message} should have been changed by #{@expected_delta.inspect}, but was changed by #{actual_delta.inspect}" elsif @minimum "#{message} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}" elsif @maximum "#{message} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}" else "#{message} should have changed, but is still #{@actual_before.inspect}" end end def actual_delta @actual_after - @actual_before end def failure_message_for_should_not "#{message} should not have changed, but did change from #{@actual_before.inspect} to #{@actual_after.inspect}" end def by(expected_delta) @expected_delta = expected_delta self end def by_at_least(minimum) @minimum = minimum self end def by_at_most(maximum) @maximum = maximum self end def to(to) @eval_after = true @expected_after = to self end def from (before) @eval_before = true @expected_before = before self end def description "change ##{message}" end private def failure_message_for_expected_after if RSpec::Matchers.is_a_matcher?(@expected_after) @expected_after.description else @expected_after.inspect end end def message @message || "result" end def change_expected? @expected_delta != 0 end def changed? @actual_before != @actual_after end def matches_before? @eval_before ? expected_matches_actual?(@expected_before, @actual_before) : true end def matches_after? @eval_after ? expected_matches_actual?(@expected_after, @actual_after) : true end def matches_expected_delta? @expected_delta ? (@actual_before + @expected_delta == @actual_after) : true end def matches_min? @minimum ? (@actual_after - @actual_before >= @minimum) : true end def matches_max? @maximum ? (@actual_after - @actual_before <= @maximum) : true end def expected_matches_actual?(expected, actual) expected === actual || actual == expected end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/be.rb0000644000004100000410000001153212203207642023660 0ustar www-datawww-datarequire 'rspec/matchers/dsl' module RSpec module Matchers module BuiltIn class BeTrue < BaseMatcher def match(_, actual) !!actual end def failure_message_for_should "expected: true value\n got: #{actual.inspect}" end def failure_message_for_should_not "expected: non-true value\n got: #{actual.inspect}" end end class BeFalse < BaseMatcher def match(_, actual) !actual end def failure_message_for_should "expected: false value\n got: #{actual.inspect}" end def failure_message_for_should_not "expected: non-false value\n got: #{actual.inspect}" end end class BeNil < BaseMatcher def match(_, actual) actual.nil? end def failure_message_for_should "expected: nil\n got: #{actual.inspect}" end def failure_message_for_should_not "expected: not nil\n got: nil" end end module BeHelpers private def args_to_s @args.empty? ? "" : parenthesize(inspected_args.join(', ')) end def parenthesize(string) "(#{string})" end def inspected_args @args.collect{|a| a.inspect} end def expected_to_sentence split_words(@expected) end def args_to_sentence to_sentence(@args) end end class Be < BaseMatcher include BeHelpers def initialize(*args, &block) @args = args end def match(_, actual) !!actual end def failure_message_for_should "expected #{@actual.inspect} to evaluate to true" end def failure_message_for_should_not "expected #{@actual.inspect} to evaluate to false" end [:==, :<, :<=, :>=, :>, :===, :=~].each do |operator| define_method operator do |operand| BeComparedTo.new(operand, operator) end end end class BeComparedTo < Be def initialize(operand, operator) @expected, @operator = operand, operator @args = [] end def matches?(actual) @actual = actual @actual.__send__ @operator, @expected end def failure_message_for_should "expected: #{@operator} #{@expected.inspect}\n got: #{@operator.to_s.gsub(/./, ' ')} #{@actual.inspect}" end def failure_message_for_should_not message = <<-MESSAGE `#{negative_expectation_expression}` not only FAILED, it is a bit confusing. MESSAGE raise message << ([:===,:==].include?(@operator) ? "It might be more clearly expressed without the \"be\"?" : "It might be more clearly expressed in the positive?") end def description "be #{@operator} #{expected_to_sentence}#{args_to_sentence}" end def negative_expectation_expression Expectations::Syntax.negative_expression("actual", "be #{@operator} #{@expected}") end end class BePredicate < BaseMatcher include BeHelpers def initialize(*args, &block) @expected = parse_expected(args.shift) @args = args @block = block end def matches?(actual) @actual = actual begin return @result = actual.__send__(predicate, *@args, &@block) rescue NameError => predicate_missing_error "this needs to be here or rcov will not count this branch even though it's executed in a code example" end begin return @result = actual.__send__(present_tense_predicate, *@args, &@block) rescue NameError raise predicate_missing_error end end alias === matches? def failure_message_for_should "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}" end def failure_message_for_should_not "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}" end def description "#{prefix_to_sentence}#{expected_to_sentence}#{args_to_sentence}" end private def predicate "#{@expected}?".to_sym end def present_tense_predicate "#{@expected}s?".to_sym end def parse_expected(expected) @prefix, expected = prefix_and_expected(expected) expected end def prefix_and_expected(symbol) symbol.to_s =~ /^(be_(an?_)?)(.*)/ return $1, $3 end def prefix_to_sentence split_words(@prefix) end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/base_matcher.rb0000644000004100000410000000347712203207642025720 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn # @api private # # Used _internally_ as a base class for matchers that ship with # rspec-expectations. # # ### Warning: # # This class is for internal use, and subject to change without notice. We # strongly recommend that you do not base your custom matchers on this # class. If/when this changes, we will announce it and remove this warning. class BaseMatcher include RSpec::Matchers::Pretty attr_reader :actual, :expected, :rescued_exception def initialize(expected = nil) @expected = expected end def matches?(actual) @actual = actual match(expected, actual) end def match_unless_raises(*exceptions) exceptions.unshift Exception if exceptions.empty? begin yield true rescue *exceptions => @rescued_exception false end end def failure_message_for_should assert_ivars :@actual, :@expected "expected #{@actual.inspect} to #{name_to_sentence}#{expected_to_sentence}" end def failure_message_for_should_not assert_ivars :@actual, :@expected "expected #{@actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}" end def description expected ? "#{name_to_sentence} #{@expected.inspect}" : name_to_sentence end def diffable? false end def ==(other) matches?(other) end private def assert_ivars *ivars raise "#{self.class.name} needs to supply #{to_sentence ivars}" unless ivars.all? { |v| instance_variables.map(&:intern).include? v } end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/eql.rb0000644000004100000410000000105212203207642024047 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class Eql < BaseMatcher def match(expected, actual) actual.eql? expected end def failure_message_for_should "\nexpected: #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using eql?)\n" end def failure_message_for_should_not "\nexpected: value != #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using eql?)\n" end def diffable? true end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/include.rb0000644000004100000410000000341012203207642024711 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class Include < BaseMatcher def initialize(*expected) @expected = expected end def matches?(actual) @actual = actual perform_match(:all?, :all?, @actual, @expected) end def does_not_match?(actual) @actual = actual perform_match(:none?, :any?, @actual, @expected) end def description "include#{expected_to_sentence}" end def diffable? # Matchers do not diff well, since diff uses their inspect # output, which includes their instance variables and such. @expected.none? { |e| RSpec::Matchers.is_a_matcher?(e) } end private def perform_match(predicate, hash_predicate, actuals, expecteds) expecteds.__send__(predicate) do |expected| if comparing_hash_values?(actuals, expected) expected.__send__(hash_predicate) { |k,v| actuals.has_key?(k) && actuals[k] == v } elsif comparing_hash_keys?(actuals, expected) actuals.has_key?(expected) elsif comparing_with_matcher?(actual, expected) actual.any? { |value| expected.matches?(value) } else actuals.include?(expected) end end end def comparing_hash_keys?(actual, expected) actual.is_a?(Hash) && !expected.is_a?(Hash) end def comparing_hash_values?(actual, expected) actual.is_a?(Hash) && expected.is_a?(Hash) end def comparing_with_matcher?(actual, expected) actual.is_a?(Array) && RSpec::Matchers.is_a_matcher?(expected) end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/match.rb0000644000004100000410000000027712203207642024372 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class Match < BaseMatcher def match(expected, actual) actual.match expected end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/have.rb0000644000004100000410000001051312203207642024213 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class Have QUERY_METHODS = [:size, :length, :count].freeze def initialize(expected, relativity=:exactly) @expected = case expected when :no then 0 when String then expected.to_i else expected end @relativity = relativity @actual = @collection_name = @plural_collection_name = nil end def relativities @relativities ||= { :exactly => "", :at_least => "at least ", :at_most => "at most " } end def matches?(collection_or_owner) collection = determine_collection(collection_or_owner) case collection when enumerator_class for query_method in QUERY_METHODS next unless collection.respond_to?(query_method) @actual = collection.__send__(query_method) break unless @actual.nil? end raise not_a_collection if @actual.nil? else query_method = determine_query_method(collection) raise not_a_collection unless query_method @actual = collection.__send__(query_method) end case @relativity when :at_least then @actual >= @expected when :at_most then @actual <= @expected else @actual == @expected end end alias == matches? def determine_collection(collection_or_owner) if collection_or_owner.respond_to?(@collection_name) collection_or_owner.__send__(@collection_name, *@args, &@block) elsif (@plural_collection_name && collection_or_owner.respond_to?(@plural_collection_name)) collection_or_owner.__send__(@plural_collection_name, *@args, &@block) elsif determine_query_method(collection_or_owner) collection_or_owner else collection_or_owner.__send__(@collection_name, *@args, &@block) end end def determine_query_method(collection) QUERY_METHODS.detect {|m| collection.respond_to?(m)} end def not_a_collection "expected #{@collection_name} to be a collection but it does not respond to #length, #size or #count" end def failure_message_for_should "expected #{relative_expectation} #{@collection_name}, got #{@actual}" end def failure_message_for_should_not if @relativity == :exactly return "expected target not to have #{@expected} #{@collection_name}, got #{@actual}" elsif @relativity == :at_most return <<-EOF Isn't life confusing enough? Instead of having to figure out the meaning of this: #{Expectations::Syntax.negative_expression("actual", "have_at_most(#{@expected}).#{@collection_name}")} We recommend that you use this instead: #{Expectations::Syntax.positive_expression("actual", "have_at_least(#{@expected + 1}).#{@collection_name}")} EOF elsif @relativity == :at_least return <<-EOF Isn't life confusing enough? Instead of having to figure out the meaning of this: #{Expectations::Syntax.negative_expression("actual", "have_at_least(#{@expected}).#{@collection_name}")} We recommend that you use this instead: #{Expectations::Syntax.positive_expression("actual", "have_at_most(#{@expected - 1}).#{@collection_name}")} EOF end end def description "have #{relative_expectation} #{@collection_name}" end def respond_to?(m) @expected.respond_to?(m) || super end private def method_missing(method, *args, &block) @collection_name = method if inflector = (defined?(ActiveSupport::Inflector) && ActiveSupport::Inflector.respond_to?(:pluralize) ? ActiveSupport::Inflector : (defined?(Inflector) ? Inflector : nil)) @plural_collection_name = inflector.pluralize(method.to_s) end @args = args @block = block self end def relative_expectation "#{relativities[@relativity]}#{@expected}" end def enumerator_class RUBY_VERSION < '1.9' ? Enumerable::Enumerator : Enumerator end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/match_array.rb0000644000004100000410000000316012203207642025562 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class MatchArray < BaseMatcher def match(expected, actual) return false unless actual.respond_to? :to_ary @extra_items = difference_between_arrays(actual, expected) @missing_items = difference_between_arrays(expected, actual) @extra_items.empty? & @missing_items.empty? end def failure_message_for_should if actual.respond_to? :to_ary message = "expected collection contained: #{safe_sort(expected).inspect}\n" message += "actual collection contained: #{safe_sort(actual).inspect}\n" message += "the missing elements were: #{safe_sort(@missing_items).inspect}\n" unless @missing_items.empty? message += "the extra elements were: #{safe_sort(@extra_items).inspect}\n" unless @extra_items.empty? else message = "expected an array, actual collection was #{actual.inspect}" end message end def failure_message_for_should_not "Matcher does not support should_not" end def description "contain exactly #{_pretty_print(expected)}" end private def safe_sort(array) array.sort rescue array end def difference_between_arrays(array_1, array_2) difference = array_1.to_ary.dup array_2.to_ary.each do |element| if index = difference.index(element) difference.delete_at(index) end end difference end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/equal.rb0000644000004100000410000000201312203207642024373 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class Equal < BaseMatcher def match(expected, actual) actual.equal? expected end def failure_message_for_should return <<-MESSAGE expected #{inspect_object(expected)} got #{inspect_object(actual)} Compared using equal?, which compares object identity, but expected and actual are not the same object. Use `#{eq_expression}` if you don't care about object identity in this example. MESSAGE end def failure_message_for_should_not return <<-MESSAGE expected not #{inspect_object(actual)} got #{inspect_object(expected)} Compared using equal?, which compares object identity. MESSAGE end def diffable?; true; end private def inspect_object(o) "#<#{o.class}:#{o.object_id}> => #{o.inspect}" end def eq_expression Expectations::Syntax.positive_expression("actual", "eq(expected)") end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/exist.rb0000644000004100000410000000155212203207642024427 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class Exist < BaseMatcher def initialize(*expected) @expected = expected end def matches?(actual) @actual = actual predicates = [:exist?, :exists?].select { |p| @actual.respond_to?(p) } existence_values = predicates.map { |p| @actual.__send__(p, *@expected) } uniq_truthy_values = existence_values.map { |v| !!v }.uniq case uniq_truthy_values.size when 0; raise NoMethodError.new("#{@actual.inspect} does not respond to either #exist? or #exists?") when 1; existence_values.first else raise "#exist? and #exists? returned different values:\n\n" + " exist?: #{existence_values.first}\n" + "exists?: #{existence_values.last}" end end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/cover.rb0000644000004100000410000000065212203207642024411 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class Cover < BaseMatcher def initialize(*expected) @expected = expected end def matches?(range) @actual = range @expected.all? { |e| range.cover?(e) } end def does_not_match?(range) @actual = range expected.none? { |e| range.cover?(e) } end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/satisfy.rb0000644000004100000410000000113612203207642024753 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class Satisfy def initialize(&block) @block = block end def matches?(actual, &block) @block = block if block @actual = actual @block.call(actual) end alias == matches? def failure_message_for_should "expected #{@actual} to satisfy block" end def failure_message_for_should_not "expected #{@actual} not to satisfy block" end def description "satisfy block" end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/eq.rb0000644000004100000410000000102412203207642023672 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class Eq < BaseMatcher def match(expected, actual) actual == expected end def failure_message_for_should "\nexpected: #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using ==)\n" end def failure_message_for_should_not "\nexpected: value != #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using ==)\n" end def diffable?; true; end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/raise_error.rb0000644000004100000410000000777312203207642025622 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class RaiseError def initialize(expected_error_or_message=Exception, expected_message=nil, &block) @block = block @actual_error = nil case expected_error_or_message when String, Regexp @expected_error, @expected_message = Exception, expected_error_or_message else @expected_error, @expected_message = expected_error_or_message, expected_message end end def matches?(given_proc, negative_expectation = false) if negative_expectation && (expecting_specific_exception? || @expected_message) what_to_deprecate = if expecting_specific_exception? && @expected_message "`expect { }.not_to raise_error(SpecificErrorClass, message)`" elsif expecting_specific_exception? "`expect { }.not_to raise_error(SpecificErrorClass)`" elsif @expected_message "`expect { }.not_to raise_error(message)`" end RSpec.deprecate(what_to_deprecate, :replacement => "`expect { }.not_to raise_error()`") end @raised_expected_error = false @with_expected_message = false @eval_block = false @eval_block_passed = false unless given_proc.respond_to?(:call) ::Kernel.warn "`raise_error` was called with non-proc object #{given_proc.inspect}" return false end begin given_proc.call rescue Exception => @actual_error if @actual_error == @expected_error || @expected_error === @actual_error @raised_expected_error = true @with_expected_message = verify_message end end unless negative_expectation eval_block if @raised_expected_error && @with_expected_message && @block end ensure return (@raised_expected_error & @with_expected_message) ? (@eval_block ? @eval_block_passed : true) : false end alias == matches? def does_not_match?(given_proc) !matches?(given_proc, :negative_expectation) end def eval_block @eval_block = true begin @block[@actual_error] @eval_block_passed = true rescue Exception => err @actual_error = err end end def verify_message case @expected_message when nil true when Regexp @expected_message =~ @actual_error.message else @expected_message == @actual_error.message end end def failure_message_for_should @eval_block ? @actual_error.message : "expected #{expected_error}#{given_error}" end def failure_message_for_should_not "expected no #{expected_error}#{given_error}" end def description "raise #{expected_error}" end private def expected_error case @expected_message when nil @expected_error.inspect when Regexp "#{@expected_error} with message matching #{@expected_message.inspect}" else "#{@expected_error} with #{@expected_message.inspect}" end end def format_backtrace(backtrace) formatter = Matchers.configuration.backtrace_formatter formatter.format_backtrace(backtrace) end def given_error return " but nothing was raised" unless @actual_error backtrace = format_backtrace(@actual_error.backtrace) [ ", got #{@actual_error.inspect} with backtrace:", *backtrace ].join("\n # ") end def expecting_specific_exception? @expected_error != Exception end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/start_and_end_with.rb0000644000004100000410000000303012203207642027124 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class StartAndEndWith < BaseMatcher def initialize(*expected) @expected = expected.length == 1 ? expected.first : expected end def matches?(actual) @actual = actual.respond_to?(:[]) ? actual : (raise ArgumentError.new("#{actual.inspect} does not respond to :[]")) begin @expected.respond_to?(:length) ? subset_matches?(@expected, @actual) : element_matches?(@expected, @actual) rescue ArgumentError raise ArgumentError.new("#{actual.inspect} does not have ordered elements") end end def failure_message_for_should "expected #{@actual.inspect} to #{self.class.name.split('::').last.sub(/With/,'').downcase} with #{@expected.inspect}" end def failure_message_for_should_not "expected #{@actual.inspect} not to #{self.class.name.split('::').last.sub(/With/,'').downcase} with #{@expected.inspect}" end end class StartWith < StartAndEndWith def subset_matches?(expected, actual) actual[0, expected.length] == expected end def element_matches?(expected, actual) @actual[0] == @expected end end class EndWith < StartAndEndWith def subset_matches?(expected, actual) actual[-expected.length, expected.length] == expected end def element_matches?(expected, actual) actual[-1] == expected end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/built_in/be_instance_of.rb0000644000004100000410000000043512203207642026230 0ustar www-datawww-datamodule RSpec module Matchers module BuiltIn class BeAnInstanceOf < BaseMatcher def match(expected, actual) actual.instance_of? expected end def description "be an instance of #{expected}" end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/extensions/0000755000004100000410000000000012203207642023335 5ustar www-datawww-datarspec-expectations-2.14.2/lib/rspec/matchers/extensions/instance_eval_with_args.rb0000644000004100000410000000273212203207642030550 0ustar www-datawww-datamodule RSpec module Matchers module Extensions module InstanceEvalWithArgs # based on Bounded Spec InstanceExec (Mauricio Fernandez) # http://eigenclass.org/hiki/bounded+space+instance_exec # - uses singleton_class instead of global InstanceExecHelper module # - this keeps it scoped to classes/modules that include this module # - only necessary for ruby 1.8.6 def instance_eval_with_args(*args, &block) return instance_exec(*args, &block) if respond_to?(:instance_exec) # If there are no args and the block doesn't expect any, there's no # need to fake instance_exec with our hack below. # Notes: # * lambda { }.arity # => -1 # * lambda { || }.arity # => 0 # * lambda { |*a| }.arity # -1 return instance_eval(&block) if block.arity < 1 && args.empty? singleton_class = (class << self; self; end) begin orig_critical, Thread.critical = Thread.critical, true n = 0 n += 1 while respond_to?(method_name="__instance_exec#{n}") singleton_class.module_eval{ define_method(method_name, &block) } ensure Thread.critical = orig_critical end begin return __send__(method_name, *args) ensure singleton_class.module_eval{ remove_method(method_name) } rescue nil end end end end end end rspec-expectations-2.14.2/lib/rspec/matchers/compatibility.rb0000644000004100000410000000110712203207642024333 0ustar www-datawww-dataRSpec::Matchers.constants.each do |c| if Class === (klass = RSpec::Matchers.const_get(c)) if klass.public_instance_methods.any? {|m| ['failure_message_for_should',:failure_message_for_should].include?(m)} klass.class_eval do alias_method :failure_message, :failure_message_for_should end end if klass.public_instance_methods.any? {|m| ['failure_message_for_should_not',:failure_message_for_should_not].include?(m)} klass.class_eval do alias_method :negative_failure_message, :failure_message_for_should_not end end end end rspec-expectations-2.14.2/lib/rspec/matchers/generated_descriptions.rb0000644000004100000410000000154712203207642026216 0ustar www-datawww-datamodule RSpec module Matchers class << self attr_accessor :last_matcher, :last_should end def self.clear_generated_description self.last_matcher = nil self.last_should = nil end def self.generated_description return nil if last_should.nil? "#{last_should.to_s.gsub('_',' ')} #{last_description}" end private def self.last_description last_matcher.respond_to?(:description) ? last_matcher.description : <<-MESSAGE When you call a matcher in an example without a String, like this: specify { object.should matcher } or this: it { should matcher } RSpec expects the matcher to have a #description method. You should either add a String to the example this matcher is being used in, or give it a description method. Then you won't have to suffer this lengthy warning again. MESSAGE end end end rspec-expectations-2.14.2/lib/rspec/matchers.rb0000644000004100000410000005710212203207642021470 0ustar www-datawww-datarequire 'rspec/matchers/extensions/instance_eval_with_args' require 'rspec/matchers/pretty' require 'rspec/matchers/built_in' require 'rspec/matchers/matcher' require 'rspec/matchers/operator_matcher' require 'rspec/matchers/be_close' require 'rspec/matchers/generated_descriptions' require 'rspec/matchers/method_missing' require 'rspec/matchers/compatibility' require 'rspec/matchers/dsl' require 'rspec/matchers/test_unit_integration' module RSpec # RSpec::Matchers provides a number of useful matchers we use to define # expectations. A matcher is any object that responds to the following: # # matches?(actual) # failure_message_for_should # # These methods are also part of the matcher protocol, but are optional: # # does_not_match?(actual) # failure_message_for_should_not # description # # ## Predicates # # In addition to matchers that are defined explicitly, RSpec will create # custom matchers on the fly for any arbitrary predicate, giving your specs a # much more natural language feel. # # A Ruby predicate is a method that ends with a "?" and returns true or false. # Common examples are `empty?`, `nil?`, and `instance_of?`. # # All you need to do is write `expect(..).to be_` followed by the predicate # without the question mark, and RSpec will figure it out from there. # For example: # # expect([]).to be_empty # => [].empty?() | passes # expect([]).not_to be_empty # => [].empty?() | fails # # In addtion to prefixing the predicate matchers with "be_", you can also use "be_a_" # and "be_an_", making your specs read much more naturally: # # expect("a string").to be_an_instance_of(String) # =>"a string".instance_of?(String) # passes # # expect(3).to be_a_kind_of(Fixnum) # => 3.kind_of?(Numeric) | passes # expect(3).to be_a_kind_of(Numeric) # => 3.kind_of?(Numeric) | passes # expect(3).to be_an_instance_of(Fixnum) # => 3.instance_of?(Fixnum) | passes # expect(3).not_to be_an_instance_of(Numeric) # => 3.instance_of?(Numeric) | fails # # RSpec will also create custom matchers for predicates like `has_key?`. To # use this feature, just state that the object should have_key(:key) and RSpec will # call has_key?(:key) on the target. For example: # # expect(:a => "A").to have_key(:a) # expect(:a => "A").to have_key(:b) # fails # # You can use this feature to invoke any predicate that begins with "has_", whether it is # part of the Ruby libraries (like `Hash#has_key?`) or a method you wrote on your own class. # # ## Custom Matchers # # When you find that none of the stock matchers provide a natural feeling # expectation, you can very easily write your own using RSpec's matcher DSL # or writing one from scratch. # # ### Matcher DSL # # Imagine that you are writing a game in which players can be in various # zones on a virtual board. To specify that bob should be in zone 4, you # could say: # # expect(bob.current_zone).to eql(Zone.new("4")) # # But you might find it more expressive to say: # # expect(bob).to be_in_zone("4") # # and/or # # expect(bob).not_to be_in_zone("3") # # You can create such a matcher like so: # # RSpec::Matchers.define :be_in_zone do |zone| # match do |player| # player.in_zone?(zone) # end # end # # This will generate a be_in_zone method that returns a matcher # with logical default messages for failures. You can override the failure # messages and the generated description as follows: # # RSpec::Matchers.define :be_in_zone do |zone| # match do |player| # player.in_zone?(zone) # end # # failure_message_for_should do |player| # # generate and return the appropriate string. # end # # failure_message_for_should_not do |player| # # generate and return the appropriate string. # end # # description do # # generate and return the appropriate string. # end # end # # Each of the message-generation methods has access to the block arguments # passed to the create method (in this case, zone). The # failure message methods (failure_message_for_should and # failure_message_for_should_not) are passed the actual value (the # receiver of expect(..) or expect(..).not_to). # # ### Custom Matcher from scratch # # You could also write a custom matcher from scratch, as follows: # # class BeInZone # def initialize(expected) # @expected = expected # end # # def matches?(target) # @target = target # @target.current_zone.eql?(Zone.new(@expected)) # end # # def failure_message_for_should # "expected #{@target.inspect} to be in Zone #{@expected}" # end # # def failure_message_for_should_not # "expected #{@target.inspect} not to be in Zone #{@expected}" # end # end # # ... and a method like this: # # def be_in_zone(expected) # BeInZone.new(expected) # end # # And then expose the method to your specs. This is normally done # by including the method and the class in a module, which is then # included in your spec: # # module CustomGameMatchers # class BeInZone # # ... # end # # def be_in_zone(expected) # # ... # end # end # # describe "Player behaviour" do # include CustomGameMatchers # # ... # end # # or you can include in globally in a spec_helper.rb file required # from your spec file(s): # # RSpec::configure do |config| # config.include(CustomGameMatchers) # end module Matchers # @api private def self.is_a_matcher?(obj) return true if ::RSpec::Matchers::BuiltIn::BaseMatcher === obj return false if obj.respond_to?(:i_respond_to_everything_so_im_not_really_a_matcher) return false unless obj.respond_to?(:matches?) obj.respond_to?(:failure_message_for_should) || obj.respond_to?(:failure_message) end # Passes if actual is truthy (anything but false or nil) def be_true BuiltIn::BeTrue.new end # Passes if actual is falsy (false or nil) def be_false BuiltIn::BeFalse.new end # Passes if actual is nil def be_nil BuiltIn::BeNil.new end # @example # expect(actual).to be_true # expect(actual).to be_false # expect(actual).to be_nil # expect(actual).to be_[arbitrary_predicate](*args) # expect(actual).not_to be_nil # expect(actual).not_to be_[arbitrary_predicate](*args) # # Given true, false, or nil, will pass if actual value is true, false or # nil (respectively). Given no args means the caller should satisfy an if # condition (to be or not to be). # # Predicates are any Ruby method that ends in a "?" and returns true or # false. Given be_ followed by arbitrary_predicate (without the "?"), # RSpec will match convert that into a query against the target object. # # The arbitrary_predicate feature will handle any predicate prefixed with # "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of) or "be_" # (e.g. be_empty), letting you choose the prefix that best suits the # predicate. def be(*args) args.empty? ? Matchers::BuiltIn::Be.new : equal(*args) end # passes if target.kind_of?(klass) def be_a(klass) be_a_kind_of(klass) end alias_method :be_an, :be_a # Passes if actual.instance_of?(expected) # # @example # # expect(5).to be_an_instance_of(Fixnum) # expect(5).not_to be_an_instance_of(Numeric) # expect(5).not_to be_an_instance_of(Float) def be_an_instance_of(expected) BuiltIn::BeAnInstanceOf.new(expected) end alias_method :be_instance_of, :be_an_instance_of # Passes if actual.kind_of?(expected) # # @example # # expect(5).to be_a_kind_of(Fixnum) # expect(5).to be_a_kind_of(Numeric) # expect(5).not_to be_a_kind_of(Float) def be_a_kind_of(expected) BuiltIn::BeAKindOf.new(expected) end alias_method :be_kind_of, :be_a_kind_of # Passes if actual == expected +/- delta # # @example # # expect(result).to be_within(0.5).of(3.0) # expect(result).not_to be_within(0.5).of(3.0) def be_within(delta) BuiltIn::BeWithin.new(delta) end # Applied to a proc, specifies that its execution will cause some value to # change. # # @param [Object] receiver # @param [Symbol] message the message to send the receiver # # You can either pass receiver and message, or a block, # but not both. # # When passing a block, it must use the { ... } format, not # do/end, as { ... } binds to the +change+ method, whereas do/end # would errantly bind to the +expect(..)+ or +expect(..).not_to+ method. # # @example # # expect { # team.add_player(player) # }.to change(roster, :count) # # expect { # team.add_player(player) # }.to change(roster, :count).by(1) # # expect { # team.add_player(player) # }.to change(roster, :count).by_at_least(1) # # expect { # team.add_player(player) # }.to change(roster, :count).by_at_most(1) # # string = "string" # expect { # string.reverse! # }.to change { string }.from("string").to("gnirts") # # string = "string" # expect { # string # }.not_to change { string } # # expect { # person.happy_birthday # }.to change(person, :birthday).from(32).to(33) # # expect { # employee.develop_great_new_social_networking_app # }.to change(employee, :title).from("Mail Clerk").to("CEO") # # expect { # doctor.leave_office # }.to change(doctor, :sign).from(/is in/).to(/is out/) # # user = User.new(:type => "admin") # expect { # user.symbolize_type # }.to change(user, :type).from(String).to(Symbol) # # == Notes # # Evaluates receiver.message or block before and after it # evaluates the block passed to expect. # # expect( ... ).not_to change only supports the form with no subsequent # calls to by, by_at_least, by_at_most, # to or from. def change(receiver=nil, message=nil, &block) BuiltIn::Change.new(receiver, message, &block) end # Passes if actual covers expected. This works for # Ranges. You can also pass in multiple args # and it will only pass if all args are found in Range. # # @example # expect(1..10).to cover(5) # expect(1..10).to cover(4, 6) # expect(1..10).to cover(4, 6, 11) # fails # expect(1..10).not_to cover(11) # expect(1..10).not_to cover(5) # fails # # ### Warning:: Ruby >= 1.9 only def cover(*values) BuiltIn::Cover.new(*values) end if (1..2).respond_to?(:cover?) # Matches if the actual value ends with the expected value(s). In the case # of a string, matches against the last `expected.length` characters of the # actual string. In the case of an array, matches against the last # `expected.length` elements of the actual array. # # @example # # expect("this string").to end_with "string" # expect([0, 1, 2, 3, 4]).to end_with 4 # expect([0, 2, 3, 4, 4]).to end_with 3, 4 def end_with(*expected) BuiltIn::EndWith.new(*expected) end # Passes if actual == expected. # # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more # information about equality in Ruby. # # @example # # expect(5).to eq(5) # expect(5).not_to eq(3) def eq(expected) BuiltIn::Eq.new(expected) end # Passes if +actual.eql?(expected)+ # # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more # information about equality in Ruby. # # @example # # expect(5).to eql(5) # expect(5).not_to eql(3) def eql(expected) BuiltIn::Eql.new(expected) end # Passes if actual.equal?(expected) (object identity). # # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more # information about equality in Ruby. # # @example # # expect(5).to equal(5) # Fixnums are equal # expect("5").not_to equal("5") # Strings that look the same are not the same object def equal(expected) BuiltIn::Equal.new(expected) end # Passes if `actual.exist?` or `actual.exists?` # # @example # expect(File).to exist("path/to/file") def exist(*args) BuiltIn::Exist.new(*args) end # Passes if receiver is a collection with the submitted number of items OR # if the receiver OWNS a collection with the submitted number of items. # # If the receiver OWNS the collection, you must use the name of the # collection. So if a `Team` instance has a collection named `#players`, # you must use that name to set the expectation. # # If the receiver IS the collection, you can use any name you like for # `named_collection`. We'd recommend using either "elements", "members", or # "items" as these are all standard ways of describing the things IN a # collection. # # This also works for Strings, letting you set expectations about their # lengths. # # @example # # # Passes if team.players.size == 11 # expect(team).to have(11).players # # # Passes if [1,2,3].length == 3 # expect([1,2,3]).to have(3).items #"items" is pure sugar # # # Passes if ['a', 'b', 'c'].count == 3 # expect([1,2,3]).to have(3).items #"items" is pure sugar # # # Passes if "this string".length == 11 # expect("this string").to have(11).characters #"characters" is pure sugar def have(n) BuiltIn::Have.new(n) end alias :have_exactly :have # Exactly like have() with >=. # # @example # expect("this").to have_at_least(3).letters # # ### Warning: # # `expect(..).not_to have_at_least` is not supported def have_at_least(n) BuiltIn::Have.new(n, :at_least) end # Exactly like have() with <=. # # @example # expect("this").to have_at_most(4).letters # # ### Warning: # # `expect(..).not_to have_at_most` is not supported def have_at_most(n) BuiltIn::Have.new(n, :at_most) end # Passes if actual includes expected. This works for # collections and Strings. You can also pass in multiple args # and it will only pass if all args are found in collection. # # @example # # expect([1,2,3]).to include(3) # expect([1,2,3]).to include(2,3) # expect([1,2,3]).to include(2,3,4) # fails # expect([1,2,3]).not_to include(4) # expect("spread").to include("read") # expect("spread").not_to include("red") def include(*expected) BuiltIn::Include.new(*expected) end # Given a Regexp or String, passes if actual.match(pattern) # # @example # # expect(email).to match(/^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) # expect(email).to match("@example.com") # expect(zipcode).to match_regex(/\A\d{5}(-\d{4})?\z/) # expect(zipcode).to match_regex("90210") # # @note Due to Ruby's method dispatch mechanism, using the `#match` matcher # within a custom matcher defined via the matcher DSL # (`RSpec::Matcher.define`) will result Ruby calling the wrong `#match` # method and raising an `ArgumentError`. Instead, use the aliased # `#match_regex` method. def match(expected) BuiltIn::Match.new(expected) end alias_method :match_regex, :match # With no args, matches if any error is raised. # With a named error, matches only if that specific error is raised. # With a named error and messsage specified as a String, matches only if both match. # With a named error and messsage specified as a Regexp, matches only if both match. # Pass an optional block to perform extra verifications on the exception matched # # @example # # expect { do_something_risky }.to raise_error # expect { do_something_risky }.to raise_error(PoorRiskDecisionError) # expect { do_something_risky }.to raise_error(PoorRiskDecisionError) { |error| expect(error.data).to eq 42 } # expect { do_something_risky }.to raise_error(PoorRiskDecisionError, "that was too risky") # expect { do_something_risky }.to raise_error(PoorRiskDecisionError, /oo ri/) # # expect { do_something_risky }.not_to raise_error # expect { do_something_risky }.not_to raise_error(PoorRiskDecisionError) # expect { do_something_risky }.not_to raise_error(PoorRiskDecisionError, "that was too risky") # expect { do_something_risky }.not_to raise_error(PoorRiskDecisionError, /oo ri/) def raise_error(error=Exception, message=nil, &block) BuiltIn::RaiseError.new(error, message, &block) end alias_method :raise_exception, :raise_error # Matches if the target object responds to all of the names # provided. Names can be Strings or Symbols. # # @example # # expect("string").to respond_to(:length) # def respond_to(*names) BuiltIn::RespondTo.new(*names) end # Passes if the submitted block returns true. Yields target to the # block. # # Generally speaking, this should be thought of as a last resort when # you can't find any other way to specify the behaviour you wish to # specify. # # If you do find yourself in such a situation, you could always write # a custom matcher, which would likely make your specs more expressive. # # @example # # expect(5).to satisfy { |n| n > 3 } def satisfy(&block) BuiltIn::Satisfy.new(&block) end # Matches if the actual value starts with the expected value(s). In the # case of a string, matches against the first `expected.length` characters # of the actual string. In the case of an array, matches against the first # `expected.length` elements of the actual array. # # @example # # expect("this string").to start_with "this s" # expect([0, 1, 2, 3, 4]).to start_with 0 # expect([0, 2, 3, 4, 4]).to start_with 0, 1 def start_with(*expected) BuiltIn::StartWith.new(*expected) end # Given no argument, matches if a proc throws any Symbol. # # Given a Symbol, matches if the given proc throws the specified Symbol. # # Given a Symbol and an arg, matches if the given proc throws the # specified Symbol with the specified arg. # # @example # # expect { do_something_risky }.to throw_symbol # expect { do_something_risky }.to throw_symbol(:that_was_risky) # expect { do_something_risky }.to throw_symbol(:that_was_risky, 'culprit') # # expect { do_something_risky }.not_to throw_symbol # expect { do_something_risky }.not_to throw_symbol(:that_was_risky) # expect { do_something_risky }.not_to throw_symbol(:that_was_risky, 'culprit') def throw_symbol(expected_symbol=nil, expected_arg=nil) BuiltIn::ThrowSymbol.new(expected_symbol, expected_arg) end # Passes if the method called in the expect block yields, regardless # of whether or not arguments are yielded. # # @example # # expect { |b| 5.tap(&b) }.to yield_control # expect { |b| "a".to_sym(&b) }.not_to yield_control # # @note Your expect block must accept a parameter and pass it on to # the method-under-test as a block. # @note This matcher is not designed for use with methods that yield # multiple times. def yield_control BuiltIn::YieldControl.new end # Passes if the method called in the expect block yields with # no arguments. Fails if it does not yield, or yields with arguments. # # @example # # 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| "a".to_sym(&b) }.not_to yield_with_no_args # because it does not yield # # @note Your expect block must accept a parameter and pass it on to # the method-under-test as a block. # @note This matcher is not designed for use with methods that yield # multiple times. def yield_with_no_args BuiltIn::YieldWithNoArgs.new end # Given no arguments, matches if the method called in the expect # block yields with arguments (regardless of what they are or how # many there are). # # Given arguments, matches if the method called in the expect block # yields with arguments that match the given arguments. # # Argument matching is done using `===` (the case match operator) # and `==`. If the expected and actual arguments match with either # operator, the matcher will pass. # # @example # # expect { |b| 5.tap(&b) }.to yield_with_args # because #tap yields an arg # expect { |b| 5.tap(&b) }.to yield_with_args(5) # because 5 == 5 # expect { |b| 5.tap(&b) }.to yield_with_args(Fixnum) # because Fixnum === 5 # expect { |b| File.open("f.txt", &b) }.to yield_with_args(/txt/) # because /txt/ === "f.txt" # # expect { |b| User.transaction(&b) }.not_to yield_with_args # because it yields no args # expect { |b| 5.tap(&b) }.not_to yield_with_args(1, 2, 3) # # @note Your expect block must accept a parameter and pass it on to # the method-under-test as a block. # @note This matcher is not designed for use with methods that yield # multiple times. def yield_with_args(*args) BuiltIn::YieldWithArgs.new(*args) end # Designed for use with methods that repeatedly yield (such as # iterators). Passes if the method called in the expect block yields # multiple times with arguments matching those given. # # Argument matching is done using `===` (the case match operator) # and `==`. If the expected and actual arguments match with either # operator, the matcher will pass. # # @example # # expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3) # expect { |b| { :a => 1, :b => 2 }.each(&b) }.to yield_successive_args([:a, 1], [:b, 2]) # expect { |b| [1, 2, 3].each(&b) }.not_to yield_successive_args(1, 2) # # @note Your expect block must accept a parameter and pass it on to # the method-under-test as a block. def yield_successive_args(*args) BuiltIn::YieldSuccessiveArgs.new(*args) end # Passes if actual contains all of the expected regardless of order. # This works for collections. Pass in multiple args and it will only # pass if all args are found in collection. # # @note This is also available using the `=~` operator with `should`, # but `=~` is not supported with `expect`. # # @note This matcher only supports positive expectations. # expect(..).not_to match_array(other_array) is not supported. # # @example # # expect([1,2,3]).to match_array([1,2,3]) # expect([1,2,3]).to match_array([1,3,2]) def match_array(array) BuiltIn::MatchArray.new(array) end OperatorMatcher.register(Enumerable, '=~', BuiltIn::MatchArray) end end