File: matchers.rb

package info (click to toggle)
ruby-rspec 3.5.0c3e0m0s0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,312 kB
  • ctags: 4,788
  • sloc: ruby: 62,572; sh: 785; makefile: 100
file content (43 lines) | stat: -rw-r--r-- 1,076 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require 'rspec/matchers/fail_matchers'

RSpec::Matchers.define :include_method do |expected|
  match do |actual|
    actual.map { |m| m.to_s }.include?(expected.to_s)
  end
end

RSpec::Matchers.define :custom_include do |*args|
  match { |actual| expect(actual).to include(*args) }
end

RSpec::Matchers.define :be_a_clone_of do |expected|
  match do |actual|
    !actual.equal?(expected) &&
        actual.class.equal?(expected.class) &&
        state_of(actual) == state_of(expected)
  end

  def state_of(object)
    ivar_names = object.instance_variables
    Hash[ ivar_names.map { |n| [n, object.instance_variable_get(n)] } ]
  end
end

RSpec::Matchers.define :have_string_length do |expected|
  match do |actual|
    @actual = actual
    string_length == expected
  end

  def string_length
    @string_length ||= @actual.length
  end
end


RSpec.configure do |config|
  config.include RSpec::Matchers::FailMatchers
end

RSpec::Matchers.define_negated_matcher :a_string_excluding, :a_string_including
RSpec::Matchers.define_negated_matcher :a_string_not_matching, :match