diff options
author | bluemonk <ceresa@gmail.com> | 2011-03-27 16:16:45 +0200 |
---|---|---|
committer | bluemonk <ceresa@gmail.com> | 2011-03-27 16:16:45 +0200 |
commit | f34b5cdf6ca6c49042d02f09c6dc33f47f1718be (patch) | |
tree | 4b17df09f388739b11a498e389a20a8e68a0ab31 | |
parent | bd5f741440529ca7aed72b0fcf772adc6c229b52 (diff) | |
download | ipaddress-f34b5cdf6ca6c49042d02f09c6dc33f47f1718be.tar.gz |
Added #ipv4? and #ipv6? methods
-rw-r--r-- | lib/ipaddress.rb | 26 | ||||
-rw-r--r-- | lib/ipaddress/ipv4.rb | 24 | ||||
-rw-r--r-- | test/ipaddress/ipv4_test.rb | 8 | ||||
-rw-r--r-- | test/ipaddress/ipv6_test.rb | 38 |
4 files changed, 78 insertions, 18 deletions
diff --git a/lib/ipaddress.rb b/lib/ipaddress.rb index 0c8b331..3627782 100644 --- a/lib/ipaddress.rb +++ b/lib/ipaddress.rb @@ -22,7 +22,7 @@ module IPAddress NAME = "IPAddress" GEM = "ipaddress" AUTHORS = ["Marco Ceresa <ceresa@ieee.org>"] - + # # Parse the argument string to create a new # IPv4, IPv6 or Mapped IP object @@ -50,6 +50,30 @@ module IPAddress end end + # + # True if the object is an IPv4 address + # + # ip = IPAddress("192.168.10.100/24") + # + # ip.ipv4? + # #-> true + # + def ipv4? + self.kind_of? IPAddress::IPv4 + end + + # + # True if the object is an IPv6 address + # + # ip = IPAddress("192.168.10.100/24") + # + # ip.ipv6? + # #-> false + # + def ipv6? + self.kind_of? IPAddress::IPv6 + end + # # Checks if the given string is a valid IP address, # either IPv4 or IPv6 diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb index 04c6ac9..2a0ca65 100644 --- a/lib/ipaddress/ipv4.rb +++ b/lib/ipaddress/ipv4.rb @@ -551,6 +551,30 @@ module IPAddress; def include_all?(*others) others.all? {|oth| include?(oth)} end + + # + # True if the object is an IPv4 address + # + # ip = IPAddress("192.168.10.100/24") + # + # ip.ipv4? + # #-> true + # +# def ipv4? +# true +# end + + # + # True if the object is an IPv6 address + # + # ip = IPAddress("192.168.10.100/24") + # + # ip.ipv6? + # #-> false + # +# def ipv6? +# false +# end # # Checks if an IPv4 address objects belongs diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb index 7d94144..d983543 100644 --- a/test/ipaddress/ipv4_test.rb +++ b/test/ipaddress/ipv4_test.rb @@ -248,6 +248,14 @@ class IPv4Test < Test::Unit::TestCase assert_equal false, ip.include_all?(addr1, @klass.new("13.16.0.0/32")) end + def test_method_ipv4? + assert_equal true, @ip.ipv4? + end + + def test_method_ipv6? + assert_equal false, @ip.ipv6? + end + def test_method_private? assert_equal true, @klass.new("192.168.10.50/24").private? assert_equal true, @klass.new("192.168.10.50/16").private? diff --git a/test/ipaddress/ipv6_test.rb b/test/ipaddress/ipv6_test.rb index e46a6b2..84be76c 100644 --- a/test/ipaddress/ipv6_test.rb +++ b/test/ipaddress/ipv6_test.rb @@ -106,6 +106,14 @@ class IPv6Test < Test::Unit::TestCase end end + def test_method_ipv4? + assert_equal false, @ip.ipv4? + end + + def test_method_ipv6? + assert_equal true, @ip.ipv6? + end + def test_method_network? assert_equal true, @network.network? assert_equal false, @ip.network? @@ -197,22 +205,6 @@ class IPv6Test < Test::Unit::TestCase assert_not_equal compressed, @klass.compress("2001:0db8::cd3") end -# def test_classmethod_create_unpecified -# unspec = @klass.create_unspecified -# assert_equal "::", unspec.address -# assert_equal 128, unspec.prefix -# assert_equal true, unspec.unspecified? -# assert_instance_of @klass, unspec.class -# end - -# def test_classmethod_create_loopback -# loopb = @klass.create_loopback -# assert_equal "::1", loopb.address -# assert_equal 128, loopb.prefix -# assert_equal true, loopb.loopback? -# assert_instance_of @klass, loopb.class -# end - def test_classmethod_parse_data str = " \001\r\270\000\000\000\000\000\b\b\000 \fAz" ip = @klass.parse_data str @@ -231,7 +223,7 @@ class IPv6Test < Test::Unit::TestCase assert_equal @ip.to_s, @klass.parse_hex(@hex,64).to_s end -end # class IPv4Test +end # class IPv6Test class IPv6UnspecifiedTest < Test::Unit::TestCase @@ -259,6 +251,10 @@ class IPv6UnspecifiedTest < Test::Unit::TestCase assert_equal @string, @ip.to_string_uncompressed assert_equal @u128, @ip.to_u128 end + + def test_method_ipv6? + assert_equal true, @ip.ipv6? + end end # class IPv6UnspecifiedTest @@ -289,6 +285,10 @@ class IPv6LoopbackTest < Test::Unit::TestCase assert_equal @string, @ip.to_string_uncompressed assert_equal @u128, @ip.to_u128 end + + def test_method_ipv6? + assert_equal true, @ip.ipv6? + end end # class IPv6LoopbackTest @@ -345,6 +345,10 @@ class IPv6MappedTest < Test::Unit::TestCase assert_equal @u128, @ip.to_u128 end + def test_method_ipv6? + assert_equal true, @ip.ipv6? + end + def test_mapped? assert_equal true, @ip.mapped? end |