Sha256: 9b1e153ff9459d3500b48664c76d7276a4e624eaee730145e018f9511362e637

Contents?: true

Size: 1.68 KB

Versions: 9

Compression:

Stored size: 1.68 KB

Contents

require 'pathname'
require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'

describe DataMapper::Types::IPAddress do

  before(:each) do
    @ip_str = "81.20.130.1"
    @ip = IPAddr.new(@ip_str)
  end

  describe ".dump" do
    it "should return the IP address as a string" do
      DataMapper::Types::IPAddress.dump(@ip, :property).should == @ip_str
    end

    it "should return nil if the string is nil" do
      DataMapper::Types::IPAddress.dump(nil, :property).should be_nil
    end

    it "should return an empty IP address if the string is empty" do
      DataMapper::Types::IPAddress.dump("", :property).should == ""
    end
  end

  describe ".load" do
    it "should return the IP address string as IPAddr" do
      DataMapper::Types::IPAddress.load(@ip_str, :property).should == @ip
    end

    it "should return nil if given nil" do
      DataMapper::Types::IPAddress.load(nil, :property).should be_nil
    end

    it "should return an empty IP address if given an empty string" do
      DataMapper::Types::IPAddress.load("", :property).should == IPAddr.new("0.0.0.0")
    end

    it 'should raise an ArgumentError if given something else' do
      lambda {
        DataMapper::Types::IPAddress.load([], :property)
      }.should raise_error(ArgumentError, '+value+ must be nil or a String')
    end
  end

  describe '.typecast' do
    it 'should do nothing if an IpAddr is provided' do
      DataMapper::Types::IPAddress.typecast(@ip, :property).should == @ip
    end

    it 'should defer to .load if a string is provided' do
      DataMapper::Types::IPAddress.should_receive(:load).with(@ip_str, :property)
      DataMapper::Types::IPAddress.typecast(@ip_str, :property)
    end
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dm-types-0.9.11 spec/unit/ip_address_spec.rb
dm-types-0.9.10 spec/unit/ip_address_spec.rb
dm-types-0.9.3 spec/unit/ip_address_spec.rb
dm-types-0.9.6 spec/unit/ip_address_spec.rb
dm-types-0.9.4 spec/unit/ip_address_spec.rb
dm-types-0.9.5 spec/unit/ip_address_spec.rb
dm-types-0.9.7 spec/unit/ip_address_spec.rb
dm-types-0.9.8 spec/unit/ip_address_spec.rb
dm-types-0.9.9 spec/unit/ip_address_spec.rb