mirror of
https://github.com/inspec/inspec
synced 2024-11-10 23:24:18 +00:00
add regexp to cmp matcher
i.e. `123 should { cmp /2+/ }`
This commit is contained in:
parent
bc8cebde73
commit
9b199c9223
2 changed files with 12 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue