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.25.0.1.b227 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.25.0.1.b226 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.25.0.1.b225 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.25.0.1.b223 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.25.0.1.b221 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.25.0.1.b217 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.25.0 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.24.0.1.b216 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.24.0.1.b214 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.24.0.1.b212 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.24.0.b210 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.24.0 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.23.2.b209 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.23.2.b208 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.23.2.b206 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.23.2.b205 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.23.2.b204 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.23.2.b203 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.23.2.b200 spec/acfs/model/attributes/date_time_spec.rb
acfs-0.23.2.b199 spec/acfs/model/attributes/date_time_spec.rb