diff --git a/lib/matchers/matchers.rb b/lib/matchers/matchers.rb index b408aa224..7414d7c3b 100644 --- a/lib/matchers/matchers.rb +++ b/lib/matchers/matchers.rb @@ -240,14 +240,21 @@ RSpec::Matchers.define :cmp do |expected| false end + def octal?(value) + return true if value =~ /\A0+\d+\Z/ + false + end + match do |actual| # if actual and expected are strings - if actual.is_a?(String) && expected.is_a?(String) + if expected.is_a?(String) && actual.is_a?(String) actual.casecmp(expected) == 0 elsif expected.is_a?(Integer) && integer?(actual) expected == actual.to_i elsif expected.is_a?(Float) && float?(actual) expected == actual.to_f + elsif octal?(expected) && actual.is_a?(Integer) + expected.to_i(8) == actual # fallback to equal else actual == expected @@ -255,10 +262,12 @@ RSpec::Matchers.define :cmp do |expected| end failure_message do |actual| + actual = '0' + actual.to_s(8) if octal?(expected) "\nexpected: #{expected}\n got: #{actual}\n\n(compared using `cmp` matcher)\n" end failure_message_when_negated do |actual| + actual = '0' + actual.to_s(8) if octal?(expected) "\nexpected: value != #{expected}\n got: #{actual}\n\n(compared using `cmp` matcher)\n" end end diff --git a/test/integration/test/integration/default/file_spec.rb b/test/integration/test/integration/default/file_spec.rb index 1d6438069..f4fc26f40 100644 --- a/test/integration/test/integration/default/file_spec.rb +++ b/test/integration/test/integration/default/file_spec.rb @@ -40,6 +40,8 @@ if os.unix? # it { should have_mode } its('mode') { should eq 00765 } it { should be_mode 00765 } + its('mode') { should cmp '0765' } + its('mode') { should_not cmp '0777' } it { should be_readable } it { should be_readable.by('owner') }