Sha256: 2d10b0eb984852f4f4abc71d59988868149a3791ea823233449bc8b54d22bb54

Contents?: true

Size: 1.63 KB

Versions: 87

Compression:

Stored size: 1.63 KB

Contents

require 'spec_helper'

describe Acfs::Model::Attributes::DateTime do
  let(:model) { Class.new.tap { |c| c.send :include, Acfs::Model }}
  let(:params) { {} }
  subject { Acfs::Model::Attributes::DateTime.new params }

  describe 'cast' do
    it 'should return same object, if obj is already of DateTime class' do
      date_time = DateTime.now
      retval = subject.cast(date_time)
      expect(retval).to be == date_time
    end

    it 'should return parsed object, if obj is of Time class' do
      time = Time.now
      retval = subject.cast(time)
      expect(retval).to be == DateTime.iso8601(time.iso8601)
    end

    it 'should return parsed object, if obj is of Date class' do
      date = Date.today
      retval = subject.cast(date)
      expect(retval).to be == DateTime.iso8601(date.iso8601)
    end

    it 'should return parsed object, if obj is of String class in ISO-8601 format' do
      date_time_string = DateTime.now.iso8601
      retval = subject.cast(date_time_string)
      expect(retval).to be == DateTime.iso8601(date_time_string)
    end

    it 'should raise an error if obj is of String class not in valid ISO-8601 format' do
      malformed_string = 'qwe123'
      expect {
        subject.cast(malformed_string)
      }.to raise_error ArgumentError
    end

    it 'should raise an error if obj is of wrong class (Fixnum)' do
      fixnum = 12
      expect {
        subject.cast(fixnum)
      }.to raise_error TypeError
    end

    context 'with allow_nil option' do
      let(:params) { {allow_nil: true} }

      it 'should accept empty string as nil' do
        expect(subject.cast('')).to eq nil
      end
    end
  end
end

Version data entries

87 entries across 87 versions & 1 rubygems

Version Path
acfs-0.30.0.1.b260 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.30.0 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.29.1.1.b259 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.29.1.1.b257 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.29.1 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.29.0.1.b256 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.29.0.1.b254 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.29.0 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.28.0.1.b253 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.28.0.1.b249 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.28.0 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.27.0.1.b248 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.27.0.1.b244 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.27.0 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.26.0.1.b243 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.26.0.1.b242 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.26.0 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.25.0.1.b232 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.25.0.1.b229 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.25.0.1.b228 spec/acfs/model/attributes/date_time_spec.rb