summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancis Luong (Franco) <franco@definefunk.com>2017-08-30 07:49:23 -0400
committerGitHub <noreply@github.com>2017-08-30 07:49:23 -0400
commit335e3433869581fb3e1ece78999aed473c3ea989 (patch)
treeded60f7e337a409363703bf988a03fba616df6b6
parent6f0efd8d852adc10bea044d38603000c6acd0d2d (diff)
parentc534ea04cb6b69d10878b3049d494f06f411fc6b (diff)
downloadipaddress-335e3433869581fb3e1ece78999aed473c3ea989.tar.gz
Merge branch 'master' into improve-remove_warningsimprove-remove_warnings
-rw-r--r--lib/ipaddress/ipv4.rb2
-rw-r--r--lib/ipaddress/ipv6.rb2
-rw-r--r--test/ipaddress/ipv4_test.rb9
-rw-r--r--test/ipaddress/ipv6_test.rb7
4 files changed, 19 insertions, 1 deletions
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb
index a123e71..7051678 100644
--- a/lib/ipaddress/ipv4.rb
+++ b/lib/ipaddress/ipv4.rb
@@ -61,6 +61,7 @@ module IPAddress;
# IPAddress::IPv4.new "10.0.0.1/255.0.0.0"
#
def initialize(str)
+ raise ArgumentError, "Nil IP" unless str
ip, netmask = str.split("/")
# Check the ip and remove white space
@@ -509,6 +510,7 @@ module IPAddress;
# #=> ["10.100.100.1/8","10.100.100.1/16","172.16.0.1/16"]
#
def <=>(oth)
+ return nil unless oth.is_a?(self.class)
return prefix <=> oth.prefix if to_u32 == oth.to_u32
to_u32 <=> oth.to_u32
end
diff --git a/lib/ipaddress/ipv6.rb b/lib/ipaddress/ipv6.rb
index 6ae651d..3e506ac 100644
--- a/lib/ipaddress/ipv6.rb
+++ b/lib/ipaddress/ipv6.rb
@@ -87,6 +87,7 @@ module IPAddress;
# ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
#
def initialize(str)
+ raise ArgumentError, "Nil IP" unless str
ip, netmask = str.split("/")
if str =~ /:.+\./
@@ -494,6 +495,7 @@ module IPAddress;
# #=> ["2001:db8:1::1/64","2001:db8:1::1/65","2001:db8:2::1/64"]
#
def <=>(oth)
+ return nil unless oth.is_a?(self.class)
return prefix <=> oth.prefix if to_u128 == oth.to_u128
to_u128 <=> oth.to_u128
end
diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb
index a58e410..56a78d0 100644
--- a/test/ipaddress/ipv4_test.rb
+++ b/test/ipaddress/ipv4_test.rb
@@ -92,7 +92,8 @@ class IPv4Test < Minitest::Test
@invalid_ipv4.each do |i|
assert_raises(ArgumentError) {@klass.new(i)}
end
- assert_raises(ArgumentError) {@klass.new("10.0.0.0/asd")}
+ assert_raises (ArgumentError) {@klass.new(nil)}
+ assert_raises (ArgumentError) {@klass.new("10.0.0.0/asd")}
end
def test_initialize_without_prefix
@@ -372,6 +373,12 @@ class IPv4Test < Minitest::Test
ip3 = @klass.new("10.0.0.0/8")
arr = ["10.0.0.0/8","10.0.0.0/16","10.0.0.0/24"]
assert_equal arr, [ip1,ip2,ip3].sort.map{|s| s.to_string}
+ # compare with alien thing
+ ip1 = @klass.new('127.0.0.1')
+ ip2 = IPAddress::IPv6.new('::1')
+ not_ip = String
+ assert_equal nil, ip1 <=> ip2
+ assert_equal nil, ip1 <=> not_ip
end
def test_method_minus
diff --git a/test/ipaddress/ipv6_test.rb b/test/ipaddress/ipv6_test.rb
index 7966ad0..b43c9de 100644
--- a/test/ipaddress/ipv6_test.rb
+++ b/test/ipaddress/ipv6_test.rb
@@ -56,6 +56,7 @@ class IPv6Test < Minitest::Test
end
assert_equal 64, @ip.prefix
+ assert_raises(ArgumentError) {@klass.new nil }
assert_raises(ArgumentError) {
@klass.new "::10.1.1.1"
}
@@ -258,6 +259,12 @@ class IPv6Test < Minitest::Test
arr = ["2001:db8:1::1/64","2001:db8:1::1/65",
"2001:db8:1::2/64","2001:db8:2::1/64"]
assert_equal arr, [ip1,ip2,ip3,ip4].sort.map{|s| s.to_string}
+ # compare with alien thing
+ ip1 = @klass.new('::1')
+ ip2 = IPAddress::IPv4.new('127.0.0.1')
+ not_ip = String
+ assert_equal nil, ip1 <=> ip2
+ assert_equal nil, ip1 <=> not_ip
end
def test_classmethod_expand