add regexp to cmp matcher

i.e. `123 should { cmp /2+/ }`
This commit is contained in:
Dominik Richter 2016-04-19 22:09:05 -04:00
parent bc8cebde73
commit 9b199c9223
2 changed files with 12 additions and 1 deletions

View file

@ -70,6 +70,14 @@ Unlike ``eq``, cmp is a matcher for less-restrictive comparisons. It will try to
its('users') { should cmp ['root'] }
end
* Single-value arrays of strings may also be compared to a regex
.. code-block:: ruby
describe auditd_conf do
its('log_format') { should cmp /raw/i }
end
* Improved printing of octal comparisons
.. code-block:: ruby

View file

@ -240,13 +240,16 @@ RSpec::Matchers.define :cmp do |first_expected|
end
def octal?(value)
return false unless value.is_a?(String)
!(value =~ /\A0+\d+\Z/).nil?
end
def try_match(actual, op, expected) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def try_match(actual, op, expected) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
# if actual and expected are strings
if expected.is_a?(String) && actual.is_a?(String)
return actual.casecmp(expected) == 0 if op == :==
elsif expected.is_a?(Regexp) && actual.is_a?(String)
return !actual.match(expected).nil?
elsif expected.is_a?(String) && integer?(expected) && actual.is_a?(Integer)
return actual.method(op).call(expected.to_i)
elsif expected.is_a?(Integer) && integer?(actual)