Skip to content
This repository
tree: 79e8a956b1
Eric Hutzelman
file 115 lines (94 sloc) 3.054 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
require 'spec_helper'

module Foo
  class Bar; end
end

describe HappyMapper::Item do

  describe "new instance" do
    before do
      @item = HappyMapper::Item.new(:foo, String, :tag => 'foobar')
    end

    it "should accept a name" do
      @item.name.should == 'foo'
    end

    it 'should accept a type' do
      @item.type.should == String
    end

    it 'should accept :tag as an option' do
      @item.tag.should == 'foobar'
    end

    it "should have a method_name" do
      @item.method_name.should == 'foo'
    end
  end

  describe "#constant" do
    it "should just use type if constant" do
      item = HappyMapper::Item.new(:foo, String)
      item.constant.should == String
    end

    it "should convert string type to constant" do
      item = HappyMapper::Item.new(:foo, 'String')
      item.constant.should == String
    end

    it "should convert string with :: to constant" do
      item = HappyMapper::Item.new(:foo, 'Foo::Bar')
      item.constant.should == Foo::Bar
    end
  end

  describe "#method_name" do
    it "should convert dashes to underscores" do
      item = HappyMapper::Item.new(:'foo-bar', String, :tag => 'foobar')
      item.method_name.should == 'foo_bar'
    end
  end

  describe "#xpath" do
    it "should default to tag" do
      item = HappyMapper::Item.new(:foo, String, :tag => 'foobar')
      item.xpath.should == 'foobar'
    end

    it "should prepend with .// if options[:deep] true" do
      item = HappyMapper::Item.new(:foo, String, :tag => 'foobar', :deep => true)
      item.xpath.should == './/foobar'
    end

    it "should prepend namespace if namespace exists" do
      item = HappyMapper::Item.new(:foo, String, :tag => 'foobar')
      item.namespace = 'http://example.com'
      item.xpath.should == 'happymapper:foobar'
    end
  end

  describe "typecasting" do
    it "should work with Strings" do
      item = HappyMapper::Item.new(:foo, String)
      [21, '21'].each do |a|
        item.typecast(a).should == '21'
      end
    end

    it "should work with Integers" do
      item = HappyMapper::Item.new(:foo, Integer)
      [21, 21.0, '21'].each do |a|
        item.typecast(a).should == 21
      end
    end

    it "should work with Floats" do
      item = HappyMapper::Item.new(:foo, Float)
      [21, 21.0, '21'].each do |a|
        item.typecast(a).should == 21.0
      end
    end

    it "should work with Times" do
      item = HappyMapper::Item.new(:foo, Time)
      item.typecast('2000-01-01 01:01:01.123456').should == Time.local(2000, 1, 1, 1, 1, 1, 123456)
    end

    it "should work with Dates" do
      item = HappyMapper::Item.new(:foo, Date)
      item.typecast('2000-01-01').should == Date.new(2000, 1, 1)
    end

    it "should work with DateTimes" do
      item = HappyMapper::Item.new(:foo, DateTime)
      item.typecast('2000-01-01 00:00:00').should == DateTime.new(2000, 1, 1, 0, 0, 0)
    end

    it "should work with Boolean" do
      item = HappyMapper::Item.new(:foo, Boolean)
      item.typecast('false').should == false
    end
  end
end
Something went wrong with that request. Please try again.