forked from datamapper/dm-types
/
flag_spec.rb
114 lines (91 loc) · 2.96 KB
/
flag_spec.rb
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
require 'spec_helper'
require './spec/fixtures/tshirt'
try_spec do
describe DataMapper::Property::Flag do
describe '.dump' do
before :all do
@flag = DataMapper::TypesFixtures::TShirt.property(
:stuff, DataMapper::Property::Flag[:first, :second, :third, :fourth, :fifth])
@property_klass = DataMapper::Property::Flag
end
it_should_behave_like "A property with flags"
describe 'when argument matches a value in the flag map' do
before :all do
@result = @flag.dump(:first)
end
it 'returns flag bit of value' do
@result.should == 1
end
end
describe 'when argument matches 2nd value in the flag map' do
before :all do
@result = @flag.dump(:second)
end
it 'returns flag bit of value' do
@result.should == 2
end
end
describe 'when argument matches multiple Symbol values in the flag map' do
before :all do
@result = @flag.dump([ :second, :fourth ])
end
it 'builds binary flag from key values of all matches' do
@result.should == 10
end
end
describe 'when argument matches multiple string values in the flag map' do
before :all do
@result = @flag.dump(['first', 'second', 'third', 'fourth', 'fifth'])
end
it 'builds binary flag from key values of all matches' do
@result.should == 31
end
end
describe 'when argument does not match a single value in the flag map' do
before :all do
@result = @flag.dump(:zero)
end
it 'returns zero' do
@result.should == 0
end
end
describe 'when argument contains duplicate flags' do
before :all do
@result = @flag.dump([ :second, :fourth, :second ])
end
it 'behaves the same as if there were no duplicates' do
@result.should == @flag.dump([ :second, :fourth ])
end
end
end
describe '.load' do
before :all do
@flag = DataMapper::TypesFixtures::TShirt.property(:stuff, DataMapper::Property::Flag, :flags => [:uno, :dos, :tres, :cuatro, :cinco])
end
describe 'when argument matches a key in the flag map' do
before :all do
@result = @flag.load(4)
end
it 'returns array with a single matching element' do
@result.should == [ :tres ]
end
end
describe 'when argument matches multiple keys in the flag map' do
before :all do
@result = @flag.load(10)
end
it 'returns array of matching values' do
@result.should == [ :dos, :cuatro ]
end
end
describe 'when argument does not match a single key in the flag map' do
before :all do
@result = @flag.load(nil)
end
it 'returns an empty array' do
@result.should == []
end
end
end
end
end