Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#include? is more liberal about compared types #251

Closed
wants to merge 12 commits into from
Closed

#include? is more liberal about compared types #251

wants to merge 12 commits into from

Conversation

ryanbreed
Copy link

The #include? method now can accept Strings, IPAddr, and Nexpose::IPRange objects when testing for inclusion.

Description

Comparisons have been delegated to private methods #include_iprange? and include_ipaddr? (which passes through to include_iprange?). Care has been taken to preserve old behavior and trap runtime exceptions for compared strings that cannot be coerced into a compatible type. The method will raise an exception if an attempt is made to compare with an incompatible type.

Motivation and Context

This enhancement will behave more correctly in more obvious ways for comparisons to commonly used data types. IPRange has also been extracted from site.rb into its own separate file.

  1. Fixes 240
  2. Supersedes PR 249 - see discussion there for further context.

How Has This Been Tested?

  • Existing unit tests pass

  • New unit tests have been extracted to spec/nexpose/ip_range_spec.rb:

    Finished in 0.05846 seconds (files took 0.52899 seconds to load)
    93 examples, 0 failures

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist:

  • [ X ] I have updated the documentation accordingly (if changes are required).
  • [ X ] I have added tests to cover my changes.
  • [ X ] All new and existing tests passed.

end

context 'when IPRange spans multiple addresses' do
let( :iprange ) { Nexpose::IPRange.new('192.168.1.64','192.168.1.95') }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space inside parentheses detected.
Space missing after comma.

it_behaves_like 'uncovered address', Nexpose::IPRange.new(below_subject, equivalent)
it_behaves_like 'uncovered address', Nexpose::IPRange.new(equivalent, above_subject)
it_behaves_like 'uncovered address', Nexpose::IPRange.new(below_subject, above_subject)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

end

context 'when IPRange spans multiple addresses' do
let( :iprange ) { Nexpose::IPRange.new('192.168.1.64','192.168.1.95') }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space inside parentheses detected.
Space missing after comma.

end

context 'when IPRange contains a single address' do
let( :iprange ) { Nexpose::IPRange.new('192.168.1.81') }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space inside parentheses detected.
Unnecessary spacing detected.

it_behaves_like 'uncovered address', Nexpose::IPRange.new(below_subject, equivalent)
it_behaves_like 'uncovered address', Nexpose::IPRange.new(equivalent, above_subject)
it_behaves_like 'uncovered address', Nexpose::IPRange.new(below_subject, above_subject)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.


shared_examples_for 'incompatible type' do |other|
it 'raises an ArgumentError' do
expect{ iprange.include?(other) }.to raise_error( ArgumentError, /incompatible type/)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space missing to the left of {.
Space inside parentheses detected.

end

context 'when IPRange contains a single address' do
let( :iprange ) { Nexpose::IPRange.new('192.168.1.81') }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space inside parentheses detected.
Unnecessary spacing detected.

end

it 'emits a warning to stderr' do
expect{ iprange.include?(unusable_string) }.to output(/could not coerce/).to_stderr

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space missing to the left of {.


shared_examples_for 'incompatible type' do |other|
it 'raises an ArgumentError' do
expect{ iprange.include?(other) }.to raise_error( ArgumentError, /incompatible type/)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space missing to the left of {.
Space inside parentheses detected.

end

it 'traps exceptions from IPAddr.initialize' do
expect{ iprange.include?(unusable_string) }.not_to raise_error

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space missing to the left of {.

end

it 'emits a warning to stderr' do
expect{ iprange.include?(unusable_string) }.to output(/could not coerce/).to_stderr

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space missing to the left of {.

end
end
describe '#include?' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at block body beginning.

end

it 'traps exceptions from IPAddr.initialize' do
expect{ iprange.include?(unusable_string) }.not_to raise_error

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space missing to the left of {.

@@ -0,0 +1,267 @@
require 'spec_helper'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing frozen string literal comment.

end
end
describe '#include?' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at block body beginning.

end
end
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final newline missing.

@@ -0,0 +1,267 @@
require 'spec_helper'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing frozen string literal comment.

(ip_from <= other_from) && (other_from <= ip_to)
else
ip_from = IPAddr.new(self.from)
ip_to = IPAddr.new(self.to)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant self detected.

end
end
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final newline missing.

other_from = IPAddr.new(other.from)
(ip_from <= other_from) && (other_from <= ip_to)
else
ip_from = IPAddr.new(self.from)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant self detected.

(ip_from <= other_from) && (other_from <= ip_to)
else
ip_from = IPAddr.new(self.from)
ip_to = IPAddr.new(self.to)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant self detected.

false
elsif (other.to==nil) && (self.to!=nil)
ip_from = IPAddr.new(self.from)
ip_to = IPAddr.new(self.to)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant self detected.

other_from = IPAddr.new(other.from)
(ip_from <= other_from) && (other_from <= ip_to)
else
ip_from = IPAddr.new(self.from)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant self detected.

elsif (other.to!=nil) && (self.to==nil)
false
elsif (other.to==nil) && (self.to!=nil)
ip_from = IPAddr.new(self.from)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant self detected.

false
elsif (other.to==nil) && (self.to!=nil)
ip_from = IPAddr.new(self.from)
ip_to = IPAddr.new(self.to)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant self detected.

eql?(other)
elsif (other.to!=nil) && (self.to==nil)
false
elsif (other.to==nil) && (self.to!=nil)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer the use of the nil? predicate.
Surrounding space missing for operator ==.
Redundant self detected.
Prefer !expression.nil? over expression != nil.
Surrounding space missing for operator !=.

elsif (other.to!=nil) && (self.to==nil)
false
elsif (other.to==nil) && (self.to!=nil)
ip_from = IPAddr.new(self.from)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant self detected.

def include_iprange?(other)
if (other.to==nil) && (self.to==nil)
eql?(other)
elsif (other.to!=nil) && (self.to==nil)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer !expression.nil? over expression != nil.
Surrounding space missing for operator !=.
Redundant self detected.
Prefer the use of the nil? predicate.
Surrounding space missing for operator ==.

eql?(other)
elsif (other.to!=nil) && (self.to==nil)
false
elsif (other.to==nil) && (self.to!=nil)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer the use of the nil? predicate.
Surrounding space missing for operator ==.
Redundant self detected.
Prefer !expression.nil? over expression != nil.
Surrounding space missing for operator !=.

def include_iprange?(other)
if (other.to==nil) && (self.to==nil)
eql?(other)
elsif (other.to!=nil) && (self.to==nil)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer !expression.nil? over expression != nil.
Surrounding space missing for operator !=.
Redundant self detected.
Prefer the use of the nil? predicate.
Surrounding space missing for operator ==.

end

def include_iprange?(other)
if (other.to==nil) && (self.to==nil)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer the use of the nil? predicate.
Surrounding space missing for operator ==.
Redundant self detected.

end

def include_iprange?(other)
if (other.to==nil) && (self.to==nil)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer the use of the nil? predicate.
Surrounding space missing for operator ==.
Redundant self detected.

@ryanbreed
Copy link
Author

finished simplifying/consolidating unit tests, but it looks like i've got some style cleanup to do.

I'll close this one and resubmit as a single squashed commit.

@ryanbreed ryanbreed closed this Oct 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IPRange include? function error
2 participants