|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +describe ShallowAttributes::Type::Date do |
| 4 | + let(:type) { ShallowAttributes::Type::Date.new } |
| 5 | + |
| 6 | + describe '#coerce' do |
| 7 | + describe 'when value is Date' do |
| 8 | + it 'returns date object' do |
| 9 | + date = Date.today |
| 10 | + type.coerce(date).class.must_equal Date |
| 11 | + end |
| 12 | + end |
| 13 | + |
| 14 | + describe 'when value is DateTime' do |
| 15 | + it 'returns time object' do |
| 16 | + time = DateTime.now |
| 17 | + type.coerce(time).must_equal time.to_date |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + describe 'when value is Time' do |
| 22 | + it 'returns time object' do |
| 23 | + time = Time.now |
| 24 | + type.coerce(time).must_equal time.to_date |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + describe 'when value is String' do |
| 29 | + it 'returns time object' do |
| 30 | + type.coerce('Thu Nov 29 2001').to_s.must_equal '2001-11-29' |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + describe 'when value is invalid String' do |
| 35 | + it 'returns error' do |
| 36 | + err = -> { type.coerce('') }.must_raise ShallowAttributes::Type::InvalidValueError |
| 37 | + err.message.must_equal %(Invalid value "" for type "Date") |
| 38 | + |
| 39 | + err = -> { type.coerce('asd') }.must_raise ShallowAttributes::Type::InvalidValueError |
| 40 | + err.message.must_equal %(Invalid value "asd" for type "Date") |
| 41 | + |
| 42 | + err = -> { type.coerce('123123') }.must_raise ShallowAttributes::Type::InvalidValueError |
| 43 | + err.message.must_equal %(Invalid value "123123" for type "Date") |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe 'when value is Nil' do |
| 48 | + it 'returns InvalidValueError' do |
| 49 | + err = -> { type.coerce(nil) }.must_raise ShallowAttributes::Type::InvalidValueError |
| 50 | + err.message.must_equal %(Invalid value "" for type "Date") |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + describe 'when value is TrueClass' do |
| 55 | + it 'returns InvalidValueError' do |
| 56 | + err = -> { type.coerce(true) }.must_raise ShallowAttributes::Type::InvalidValueError |
| 57 | + err.message.must_equal %(Invalid value "true" for type "Date") |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + describe 'when value is FalseClass' do |
| 62 | + it 'returns InvalidValueError' do |
| 63 | + err = -> { type.coerce(false) }.must_raise ShallowAttributes::Type::InvalidValueError |
| 64 | + err.message.must_equal %(Invalid value "false" for type "Date") |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + describe 'when value is not allowed' do |
| 69 | + it 'returns error' do |
| 70 | + err = -> { type.coerce([]) }.must_raise ShallowAttributes::Type::InvalidValueError |
| 71 | + err.message.must_equal %(Invalid value "[]" for type "Date") |
| 72 | + |
| 73 | + err = -> { type.coerce({}) }.must_raise ShallowAttributes::Type::InvalidValueError |
| 74 | + err.message.must_equal %(Invalid value "{}" for type "Date") |
| 75 | + |
| 76 | + err = -> { type.coerce(:'1') }.must_raise ShallowAttributes::Type::InvalidValueError |
| 77 | + err.message.must_equal %(Invalid value "1" for type "Date") |
| 78 | + |
| 79 | + err = -> { type.coerce(Class) }.must_raise ShallowAttributes::Type::InvalidValueError |
| 80 | + err.message.must_equal %(Invalid value "Class" for type "Date") |
| 81 | + |
| 82 | + err = -> { type.coerce(Class.new) }.must_raise ShallowAttributes::Type::InvalidValueError |
| 83 | + err.message.must_match 'Invalid value "#<Class:' |
| 84 | + err.message.must_match '" for type "Date"' |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | +end |
0 commit comments