motion_blender-support 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.yardopts +1 -0
- data/Gemfile +4 -0
- data/HACKS.md +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +359 -0
- data/Rakefile +14 -0
- data/app/app_delegate.rb +5 -0
- data/examples/Inflector/.gitignore +16 -0
- data/examples/Inflector/Gemfile +5 -0
- data/examples/Inflector/Rakefile +13 -0
- data/examples/Inflector/app/app_delegate.rb +8 -0
- data/examples/Inflector/app/inflections.rb +5 -0
- data/examples/Inflector/app/inflector_view_controller.rb +109 -0
- data/examples/Inflector/app/words_view_controller.rb +101 -0
- data/examples/Inflector/resources/Default-568h@2x.png +0 -0
- data/examples/Inflector/spec/main_spec.rb +9 -0
- data/lib/motion-support/callbacks.rb +8 -0
- data/lib/motion-support/concern.rb +4 -0
- data/lib/motion-support/core_ext/array.rb +10 -0
- data/lib/motion-support/core_ext/class.rb +5 -0
- data/lib/motion-support/core_ext/hash.rb +13 -0
- data/lib/motion-support/core_ext/integer.rb +6 -0
- data/lib/motion-support/core_ext/module.rb +11 -0
- data/lib/motion-support/core_ext/numeric.rb +6 -0
- data/lib/motion-support/core_ext/object.rb +12 -0
- data/lib/motion-support/core_ext/range.rb +5 -0
- data/lib/motion-support/core_ext/string.rb +13 -0
- data/lib/motion-support/core_ext/time.rb +16 -0
- data/lib/motion-support/core_ext.rb +13 -0
- data/lib/motion-support/inflector.rb +8 -0
- data/lib/motion-support.rb +81 -0
- data/motion/_stdlib/array.rb +13 -0
- data/motion/_stdlib/cgi.rb +22 -0
- data/motion/_stdlib/date.rb +81 -0
- data/motion/_stdlib/enumerable.rb +9 -0
- data/motion/_stdlib/time.rb +19 -0
- data/motion/callbacks.rb +511 -0
- data/motion/concern.rb +122 -0
- data/motion/core_ext/array/access.rb +28 -0
- data/motion/core_ext/array/conversions.rb +86 -0
- data/motion/core_ext/array/extract_options.rb +11 -0
- data/motion/core_ext/array/grouping.rb +99 -0
- data/motion/core_ext/array/prepend_and_append.rb +7 -0
- data/motion/core_ext/array/wrap.rb +45 -0
- data/motion/core_ext/array.rb +19 -0
- data/motion/core_ext/class/attribute.rb +119 -0
- data/motion/core_ext/class/attribute_accessors.rb +168 -0
- data/motion/core_ext/date/acts_like.rb +8 -0
- data/motion/core_ext/date/calculations.rb +117 -0
- data/motion/core_ext/date/conversions.rb +56 -0
- data/motion/core_ext/date_and_time/calculations.rb +232 -0
- data/motion/core_ext/enumerable.rb +90 -0
- data/motion/core_ext/hash/deep_delete_if.rb +23 -0
- data/motion/core_ext/hash/deep_merge.rb +27 -0
- data/motion/core_ext/hash/except.rb +15 -0
- data/motion/core_ext/hash/indifferent_access.rb +19 -0
- data/motion/core_ext/hash/keys.rb +150 -0
- data/motion/core_ext/hash/reverse_merge.rb +22 -0
- data/motion/core_ext/hash/slice.rb +40 -0
- data/motion/core_ext/integer/inflections.rb +27 -0
- data/motion/core_ext/integer/multiple.rb +10 -0
- data/motion/core_ext/integer/time.rb +41 -0
- data/motion/core_ext/kernel/singleton_class.rb +6 -0
- data/motion/core_ext/metaclass.rb +8 -0
- data/motion/core_ext/module/aliasing.rb +69 -0
- data/motion/core_ext/module/anonymous.rb +19 -0
- data/motion/core_ext/module/attr_internal.rb +38 -0
- data/motion/core_ext/module/attribute_accessors.rb +64 -0
- data/motion/core_ext/module/delegation.rb +175 -0
- data/motion/core_ext/module/introspection.rb +60 -0
- data/motion/core_ext/module/reachable.rb +5 -0
- data/motion/core_ext/module/remove_method.rb +12 -0
- data/motion/core_ext/ns_dictionary.rb +14 -0
- data/motion/core_ext/ns_string.rb +14 -0
- data/motion/core_ext/numeric/bytes.rb +44 -0
- data/motion/core_ext/numeric/conversions.rb +49 -0
- data/motion/core_ext/numeric/time.rb +75 -0
- data/motion/core_ext/object/acts_like.rb +10 -0
- data/motion/core_ext/object/blank.rb +105 -0
- data/motion/core_ext/object/deep_dup.rb +44 -0
- data/motion/core_ext/object/duplicable.rb +87 -0
- data/motion/core_ext/object/inclusion.rb +15 -0
- data/motion/core_ext/object/instance_variables.rb +28 -0
- data/motion/core_ext/object/to_param.rb +58 -0
- data/motion/core_ext/object/to_query.rb +26 -0
- data/motion/core_ext/object/try.rb +78 -0
- data/motion/core_ext/range/include_range.rb +23 -0
- data/motion/core_ext/range/overlaps.rb +8 -0
- data/motion/core_ext/regexp.rb +5 -0
- data/motion/core_ext/string/access.rb +104 -0
- data/motion/core_ext/string/behavior.rb +6 -0
- data/motion/core_ext/string/exclude.rb +11 -0
- data/motion/core_ext/string/filters.rb +55 -0
- data/motion/core_ext/string/indent.rb +43 -0
- data/motion/core_ext/string/inflections.rb +178 -0
- data/motion/core_ext/string/starts_ends_with.rb +4 -0
- data/motion/core_ext/string/strip.rb +24 -0
- data/motion/core_ext/time/acts_like.rb +8 -0
- data/motion/core_ext/time/calculations.rb +215 -0
- data/motion/core_ext/time/conversions.rb +52 -0
- data/motion/descendants_tracker.rb +50 -0
- data/motion/duration.rb +104 -0
- data/motion/hash_with_indifferent_access.rb +253 -0
- data/motion/inflections.rb +67 -0
- data/motion/inflector/inflections.rb +203 -0
- data/motion/inflector/methods.rb +321 -0
- data/motion/logger.rb +47 -0
- data/motion/number_helper.rb +54 -0
- data/motion/version.rb +3 -0
- data/motion_blender-support.gemspec +21 -0
- data/spec/motion-support/_helpers/constantize_test_cases.rb +75 -0
- data/spec/motion-support/_helpers/inflector_test_cases.rb +270 -0
- data/spec/motion-support/callback_spec.rb +702 -0
- data/spec/motion-support/concern_spec.rb +93 -0
- data/spec/motion-support/core_ext/array/access_spec.rb +29 -0
- data/spec/motion-support/core_ext/array/conversion_spec.rb +60 -0
- data/spec/motion-support/core_ext/array/extract_options_spec.rb +15 -0
- data/spec/motion-support/core_ext/array/grouping_spec.rb +85 -0
- data/spec/motion-support/core_ext/array/prepend_and_append_spec.rb +25 -0
- data/spec/motion-support/core_ext/array/wrap_spec.rb +19 -0
- data/spec/motion-support/core_ext/array_spec.rb +16 -0
- data/spec/motion-support/core_ext/class/attribute_accessor_spec.rb +127 -0
- data/spec/motion-support/core_ext/class/attribute_spec.rb +92 -0
- data/spec/motion-support/core_ext/date/acts_like_spec.rb +11 -0
- data/spec/motion-support/core_ext/date/calculation_spec.rb +186 -0
- data/spec/motion-support/core_ext/date/conversion_spec.rb +18 -0
- data/spec/motion-support/core_ext/date_and_time/calculation_spec.rb +336 -0
- data/spec/motion-support/core_ext/enumerable_spec.rb +130 -0
- data/spec/motion-support/core_ext/hash/deep_delete_if_spec.rb +19 -0
- data/spec/motion-support/core_ext/hash/deep_merge_spec.rb +32 -0
- data/spec/motion-support/core_ext/hash/except_spec.rb +43 -0
- data/spec/motion-support/core_ext/hash/key_spec.rb +236 -0
- data/spec/motion-support/core_ext/hash/reverse_merge_spec.rb +26 -0
- data/spec/motion-support/core_ext/hash/slice_spec.rb +61 -0
- data/spec/motion-support/core_ext/integer/inflection_spec.rb +23 -0
- data/spec/motion-support/core_ext/integer/multiple_spec.rb +19 -0
- data/spec/motion-support/core_ext/kernel/singleton_class_spec.rb +9 -0
- data/spec/motion-support/core_ext/metaclass_spec.rb +9 -0
- data/spec/motion-support/core_ext/module/aliasing_spec.rb +143 -0
- data/spec/motion-support/core_ext/module/anonymous_spec.rb +29 -0
- data/spec/motion-support/core_ext/module/attr_internal_spec.rb +104 -0
- data/spec/motion-support/core_ext/module/attribute_accessor_spec.rb +86 -0
- data/spec/motion-support/core_ext/module/delegation_spec.rb +136 -0
- data/spec/motion-support/core_ext/module/introspection_spec.rb +70 -0
- data/spec/motion-support/core_ext/module/reachable_spec.rb +61 -0
- data/spec/motion-support/core_ext/module/remove_method_spec.rb +25 -0
- data/spec/motion-support/core_ext/numeric/bytes_spec.rb +43 -0
- data/spec/motion-support/core_ext/numeric/conversions_spec.rb +40 -0
- data/spec/motion-support/core_ext/object/acts_like_spec.rb +21 -0
- data/spec/motion-support/core_ext/object/blank_spec.rb +54 -0
- data/spec/motion-support/core_ext/object/deep_dup_spec.rb +54 -0
- data/spec/motion-support/core_ext/object/duplicable_spec.rb +31 -0
- data/spec/motion-support/core_ext/object/inclusion_spec.rb +34 -0
- data/spec/motion-support/core_ext/object/instance_variable_spec.rb +19 -0
- data/spec/motion-support/core_ext/object/to_param_spec.rb +75 -0
- data/spec/motion-support/core_ext/object/to_query_spec.rb +37 -0
- data/spec/motion-support/core_ext/object/try_spec.rb +92 -0
- data/spec/motion-support/core_ext/range/include_range_spec.rb +31 -0
- data/spec/motion-support/core_ext/range/overlap_spec.rb +43 -0
- data/spec/motion-support/core_ext/regexp_spec.rb +7 -0
- data/spec/motion-support/core_ext/string/access_spec.rb +53 -0
- data/spec/motion-support/core_ext/string/behavior_spec.rb +7 -0
- data/spec/motion-support/core_ext/string/exclude_spec.rb +8 -0
- data/spec/motion-support/core_ext/string/filter_spec.rb +49 -0
- data/spec/motion-support/core_ext/string/indent_spec.rb +56 -0
- data/spec/motion-support/core_ext/string/inflection_spec.rb +142 -0
- data/spec/motion-support/core_ext/string/starts_end_with_spec.rb +14 -0
- data/spec/motion-support/core_ext/string/strip_spec.rb +34 -0
- data/spec/motion-support/core_ext/string_spec.rb +88 -0
- data/spec/motion-support/core_ext/time/acts_like_spec.rb +11 -0
- data/spec/motion-support/core_ext/time/calculation_spec.rb +201 -0
- data/spec/motion-support/core_ext/time/conversion_spec.rb +53 -0
- data/spec/motion-support/descendants_tracker_spec.rb +58 -0
- data/spec/motion-support/duration_spec.rb +107 -0
- data/spec/motion-support/hash_with_indifferent_access_spec.rb +605 -0
- data/spec/motion-support/inflector_spec.rb +504 -0
- data/spec/motion-support/ns_dictionary_spec.rb +89 -0
- data/spec/motion-support/ns_string_spec.rb +182 -0
- data/spec/motion-support/number_helper_spec.rb +55 -0
- metadata +352 -0
@@ -0,0 +1,186 @@
|
|
1
|
+
describe "Date" do
|
2
|
+
describe "calculations" do
|
3
|
+
describe "yesterday" do
|
4
|
+
it "should calculate yesterday's date" do
|
5
|
+
Date.new(1982,10,15).yesterday.should == Date.new(1982,10,14)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should construct yesterday's date" do
|
9
|
+
Date.yesterday.should == Date.current - 1
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "tomorrow" do
|
14
|
+
it "should calculate tomorrow's date" do
|
15
|
+
Date.new(1982,10,4).tomorrow.should == Date.new(1982,10,5)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should construct tomorrow's date" do
|
19
|
+
Date.tomorrow.should == Date.current + 1
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "change" do
|
24
|
+
it "should change correctly" do
|
25
|
+
Date.new(2005,2,11).change(:day => 21).should == Date.new(2005,2,21)
|
26
|
+
Date.new(2005,2,11).change(:year => 2007, :month => 5).should == Date.new(2007,5,11)
|
27
|
+
Date.new(2005,2,22).change(:year => 2006).should == Date.new(2006,2,22)
|
28
|
+
Date.new(2005,2,22).change(:month => 6).should == Date.new(2005,6,22)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "sunday" do
|
33
|
+
it "should calculate correctly" do
|
34
|
+
Date.new(2008,3,02).sunday.should == Date.new(2008,3,2)
|
35
|
+
Date.new(2008,2,29).sunday.should == Date.new(2008,3,2)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "beginning_of_week" do
|
40
|
+
it "should calculate correctly" do
|
41
|
+
Date.new(1982,10,15).beginning_of_week.should == Date.new(1982,10,11)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "end_of_week" do
|
46
|
+
it "should calculate correctly" do
|
47
|
+
Date.new(1982,10,4).end_of_week.should == Date.new(1982,10,10)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "end_of_year" do
|
52
|
+
it "should calculate correctly" do
|
53
|
+
Date.new(2008,2,22).end_of_year.should == Date.new(2008,12,31)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "end_of_month" do
|
58
|
+
it "should calculate correctly" do
|
59
|
+
Date.new(2005,3,20).end_of_month.should == Date.new(2005,3,31)
|
60
|
+
Date.new(2005,2,20).end_of_month.should == Date.new(2005,2,28)
|
61
|
+
Date.new(2005,4,20).end_of_month.should == Date.new(2005,4,30)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "prev_year" do
|
66
|
+
it "should calculate correctly" do
|
67
|
+
Date.new(1983,10,14).prev_year.should == Date.new(1982,10,14)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should work with leap years" do
|
71
|
+
Date.new(2000,2,29).prev_year.should == Date.new(1999,2,28)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "last_year" do
|
76
|
+
it "should calculate correctly" do
|
77
|
+
Date.new(2005,6,5).last_year.should == Date.new(2004,6,5)
|
78
|
+
Date.new(1983,10,14).last_year.should == Date.new(1982,10,14)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should work with leap years" do
|
82
|
+
Date.new(2000,2,29).last_year.should == Date.new(1999,2,28)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "next_year" do
|
87
|
+
it "should calculate correctly" do
|
88
|
+
Date.new(1981,10,10).next_year.should == Date.new(1982,10,10)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should work with leap years" do
|
92
|
+
Date.new(2000,2,29).next_year.should == Date.new(2001,2,28)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "advance" do
|
97
|
+
it "should calculate correctly" do
|
98
|
+
Date.new(2005,2,28).advance(:years => 1).should == Date.new(2006,2,28)
|
99
|
+
Date.new(2005,2,28).advance(:months => 4).should == Date.new(2005,6,28)
|
100
|
+
Date.new(2005,2,28).advance(:weeks => 3).should == Date.new(2005,3,21)
|
101
|
+
Date.new(2005,2,28).advance(:days => 5).should == Date.new(2005,3,5)
|
102
|
+
Date.new(2005,2,28).advance(:years => 7, :months => 7).should == Date.new(2012,9,28)
|
103
|
+
Date.new(2005,2,28).advance(:years => 7, :months => 19, :days => 5).should == Date.new(2013,10,3)
|
104
|
+
Date.new(2005,2,28).advance(:years => 7, :months => 19, :weeks => 2, :days => 5).should == Date.new(2013,10,17)
|
105
|
+
Date.new(2004,2,29).advance(:years => 1).should == Date.new(2005,2,28)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should advance years before days" do
|
109
|
+
Date.new(2011, 2, 28).advance(:years => 1, :days => 1).should == Date.new(2012, 2, 29)
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should advance months before days" do
|
113
|
+
Date.new(2010, 2, 28).advance(:months => 1, :days => 1).should == Date.new(2010, 3, 29)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should not change passed option hash" do
|
117
|
+
options = { :years => 3, :months => 11, :days => 2 }
|
118
|
+
Date.new(2005,2,28).advance(options)
|
119
|
+
options.should == { :years => 3, :months => 11, :days => 2 }
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe "last_week" do
|
124
|
+
it "should calculate correctly" do
|
125
|
+
Date.new(2005,5,17).last_week.should == Date.new(2005,5,9)
|
126
|
+
Date.new(2007,1,7).last_week.should == Date.new(2006,12,25)
|
127
|
+
Date.new(2010,2,19).last_week(:friday).should == Date.new(2010,2,12)
|
128
|
+
Date.new(2010,2,19).last_week(:saturday).should == Date.new(2010,2,13)
|
129
|
+
Date.new(2010,3,4).last_week(:saturday).should == Date.new(2010,2,27)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe "last_month" do
|
134
|
+
it "should calculate correctly on the 31st" do
|
135
|
+
Date.new(2004, 3, 31).last_month.should == Date.new(2004, 2, 29)
|
136
|
+
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "last_quarter" do
|
141
|
+
it "should calculate correctly on the 31st" do
|
142
|
+
Date.new(2004, 5, 31).last_quarter.should == Date.new(2004, 2, 29)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
describe "since" do
|
147
|
+
it "should calculate correctly" do
|
148
|
+
Date.new(2005,2,21).since(45).should == Time.local(2005,2,21,0,0,45)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
describe "ago" do
|
153
|
+
it "should calculate correctly" do
|
154
|
+
Date.new(2005,2,21).ago(45).should == Time.local(2005,2,20,23,59,15)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe "beginning_of_day" do
|
159
|
+
it "should calculate correctly" do
|
160
|
+
Date.new(2005,2,21).beginning_of_day.should == Time.local(2005,2,21,0,0,0)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
describe "end_of_day" do
|
165
|
+
it "should calculate correctly" do
|
166
|
+
Date.new(2005,2,21).end_of_day.should == Time.local(2005,2,21,23,59,59,Rational(999999999, 1000))
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
describe "past?" do
|
171
|
+
it "should calculate correctly" do
|
172
|
+
Date.yesterday.should.be.past
|
173
|
+
Date.today.last_week.should.be.past
|
174
|
+
Date.tomorrow.should.not.be.past
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
describe "future?" do
|
179
|
+
it "should calculate correctly" do
|
180
|
+
Date.tomorrow.should.be.future
|
181
|
+
Date.today.next_week.should.be.future
|
182
|
+
Date.yesterday.should.not.be.future
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
describe "date" do
|
2
|
+
describe "conversions" do
|
3
|
+
it "should convert date to string" do
|
4
|
+
date = Date.new(2005, 2, 21)
|
5
|
+
date.to_s.should == "2005-2-21"
|
6
|
+
date.to_s(:short).should == "21 Feb"
|
7
|
+
date.to_s(:long).should == "February 21, 2005"
|
8
|
+
date.to_s(:long_ordinal).should == "February 21st, 2005"
|
9
|
+
date.to_s(:db).should == "2005-02-21"
|
10
|
+
date.to_s(:rfc822).should == "21 Feb 2005"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should make inspect readable" do
|
14
|
+
Date.new(2005, 2, 21).readable_inspect.should == "Mon, 21 Feb 2005"
|
15
|
+
Date.new(2005, 2, 21).readable_inspect.should == Date.new(2005, 2, 21).inspect
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,336 @@
|
|
1
|
+
class Date
|
2
|
+
def self.for_spec(year, month, day, hour, minute, second, usec)
|
3
|
+
new(year, month, day)
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
class Time
|
8
|
+
def self.for_spec(year, month, day, hour, minute, second, usec)
|
9
|
+
local(year, month, day, hour, minute, second, usec)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
[Date, Time].each do |klass|
|
14
|
+
describe klass do
|
15
|
+
@klass = klass
|
16
|
+
def date_or_time(year, month, day, hour, minute, second, usec = nil)
|
17
|
+
@klass.for_spec(year, month, day, hour, minute, second, usec)
|
18
|
+
end
|
19
|
+
|
20
|
+
def with_bw_default(bw = :monday)
|
21
|
+
old_bw = Date.beginning_of_week
|
22
|
+
Date.beginning_of_week = bw
|
23
|
+
yield
|
24
|
+
ensure
|
25
|
+
Date.beginning_of_week = old_bw
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "yesterday" do
|
29
|
+
it "should be calculated correctly" do
|
30
|
+
date_or_time(2005,2,22,10,10,10).yesterday.should == date_or_time(2005,2,21,10,10,10)
|
31
|
+
date_or_time(2005,3,2,10,10,10).yesterday.yesterday.should == date_or_time(2005,2,28,10,10,10)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "tomorrow" do
|
36
|
+
it "should be calculated correctly" do
|
37
|
+
date_or_time(2005,2,22,10,10,10).tomorrow.should == date_or_time(2005,2,23,10,10,10)
|
38
|
+
date_or_time(2005,2,28,10,10,10).tomorrow.tomorrow.should == date_or_time(2005,3,2,10,10,10)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "days_ago" do
|
43
|
+
it "should be calculated correctly" do
|
44
|
+
date_or_time(2005,6,5,10,10,10).days_ago(1).should == date_or_time(2005,6,4,10,10,10)
|
45
|
+
date_or_time(2005,6,5,10,10,10).days_ago(5).should == date_or_time(2005,5,31,10,10,10)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "days_since" do
|
50
|
+
it "should be calculated correctly" do
|
51
|
+
date_or_time(2005,6,5,10,10,10).days_since(1).should == date_or_time(2005,6,6,10,10,10)
|
52
|
+
date_or_time(2004,12,31,10,10,10).days_since(1).should == date_or_time(2005,1,1,10,10,10)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "weeks_ago" do
|
57
|
+
it "should be calculated correctly" do
|
58
|
+
date_or_time(2005,6,5,10,10,10).weeks_ago(1).should == date_or_time(2005,5,29,10,10,10)
|
59
|
+
date_or_time(2005,6,5,10,10,10).weeks_ago(5).should == date_or_time(2005,5,1,10,10,10)
|
60
|
+
date_or_time(2005,6,5,10,10,10).weeks_ago(6).should == date_or_time(2005,4,24,10,10,10)
|
61
|
+
date_or_time(2005,6,5,10,10,10).weeks_ago(14).should == date_or_time(2005,2,27,10,10,10)
|
62
|
+
date_or_time(2005,1,1,10,10,10).weeks_ago(1).should == date_or_time(2004,12,25,10,10,10)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "weeks_since" do
|
67
|
+
it "should be calculated correctly" do
|
68
|
+
date_or_time(2005,7,7,10,10,10).weeks_since(1).should == date_or_time(2005,7,14,10,10,10)
|
69
|
+
date_or_time(2005,7,7,10,10,10).weeks_since(1).should == date_or_time(2005,7,14,10,10,10)
|
70
|
+
date_or_time(2005,6,27,10,10,10).weeks_since(1).should == date_or_time(2005,7,4,10,10,10)
|
71
|
+
date_or_time(2004,12,28,10,10,10).weeks_since(1).should == date_or_time(2005,1,4,10,10,10)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "months_ago" do
|
76
|
+
it "should be calculated correctly" do
|
77
|
+
date_or_time(2005,6,5,10,10,10).months_ago(1).should == date_or_time(2005,5,5,10,10,10)
|
78
|
+
date_or_time(2005,6,5,10,10,10).months_ago(7).should == date_or_time(2004,11,5,10,10,10)
|
79
|
+
date_or_time(2005,6,5,10,10,10).months_ago(6).should == date_or_time(2004,12,5,10,10,10)
|
80
|
+
date_or_time(2005,6,5,10,10,10).months_ago(12).should == date_or_time(2004,6,5,10,10,10)
|
81
|
+
date_or_time(2005,6,5,10,10,10).months_ago(24).should == date_or_time(2003,6,5,10,10,10)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "months_since" do
|
86
|
+
it "should be calculated correctly" do
|
87
|
+
date_or_time(2005,6,5,10,10,10).months_since(1).should == date_or_time(2005,7,5,10,10,10)
|
88
|
+
date_or_time(2005,12,5,10,10,10).months_since(1).should == date_or_time(2006,1,5,10,10,10)
|
89
|
+
date_or_time(2005,6,5,10,10,10).months_since(6).should == date_or_time(2005,12,5,10,10,10)
|
90
|
+
date_or_time(2005,12,5,10,10,10).months_since(6).should == date_or_time(2006,6,5,10,10,10)
|
91
|
+
date_or_time(2005,6,5,10,10,10).months_since(7).should == date_or_time(2006,1,5,10,10,10)
|
92
|
+
date_or_time(2005,6,5,10,10,10).months_since(12).should == date_or_time(2006,6,5,10,10,10)
|
93
|
+
date_or_time(2005,6,5,10,10,10).months_since(24).should == date_or_time(2007,6,5,10,10,10)
|
94
|
+
date_or_time(2005,3,31,10,10,10).months_since(1).should == date_or_time(2005,4,30,10,10,10)
|
95
|
+
date_or_time(2005,1,29,10,10,10).months_since(1).should == date_or_time(2005,2,28,10,10,10)
|
96
|
+
date_or_time(2005,1,30,10,10,10).months_since(1).should == date_or_time(2005,2,28,10,10,10)
|
97
|
+
date_or_time(2005,1,31,10,10,10).months_since(1).should == date_or_time(2005,2,28,10,10,10)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "years_ago" do
|
102
|
+
it "should be calculated correctly" do
|
103
|
+
date_or_time(2005,6,5,10,10,10).years_ago(1).should == date_or_time(2004,6,5,10,10,10)
|
104
|
+
date_or_time(2005,6,5,10,10,10).years_ago(7).should == date_or_time(1998,6,5,10,10,10)
|
105
|
+
# 1 year ago from leap day
|
106
|
+
date_or_time(2004,2,29,10,10,10).years_ago(1).should == date_or_time(2003,2,28,10,10,10)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe "years_since" do
|
111
|
+
it "should be calculated correctly" do
|
112
|
+
date_or_time(2005,6,5,10,10,10).years_since(1).should == date_or_time(2006,6,5,10,10,10)
|
113
|
+
date_or_time(2005,6,5,10,10,10).years_since(7).should == date_or_time(2012,6,5,10,10,10)
|
114
|
+
# 1 year since leap day
|
115
|
+
date_or_time(2004,2,29,10,10,10).years_since(1).should == date_or_time(2005,2,28,10,10,10)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "beginning_of_month" do
|
120
|
+
it "should be calculated correctly" do
|
121
|
+
date_or_time(2005,2,22,10,10,10).beginning_of_month.should == date_or_time(2005,2,1,0,0,0)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe "beginning_of_quarter" do
|
126
|
+
it "should be calculated correctly" do
|
127
|
+
date_or_time(2005,2,15,10,10,10).beginning_of_quarter.should == date_or_time(2005,1,1,0,0,0)
|
128
|
+
date_or_time(2005,1,1,0,0,0).beginning_of_quarter.should == date_or_time(2005,1,1,0,0,0)
|
129
|
+
date_or_time(2005,12,31,10,10,10).beginning_of_quarter.should == date_or_time(2005,10,1,0,0,0)
|
130
|
+
date_or_time(2005,6,30,23,59,59).beginning_of_quarter.should == date_or_time(2005,4,1,0,0,0)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe "end_of_quarter" do
|
135
|
+
it "should be calculated correctly" do
|
136
|
+
date_or_time(2007,2,15,10,10,10).end_of_quarter.should == date_or_time(2007,3,31,23,59,59,Rational(999999999, 1000))
|
137
|
+
date_or_time(2007,3,31,0,0,0).end_of_quarter.should == date_or_time(2007,3,31,23,59,59,Rational(999999999, 1000))
|
138
|
+
date_or_time(2007,12,21,10,10,10).end_of_quarter.should == date_or_time(2007,12,31,23,59,59,Rational(999999999, 1000))
|
139
|
+
date_or_time(2007,4,1,0,0,0).end_of_quarter.should == date_or_time(2007,6,30,23,59,59,Rational(999999999, 1000))
|
140
|
+
date_or_time(2008,5,31,0,0,0).end_of_quarter.should == date_or_time(2008,6,30,23,59,59,Rational(999999999, 1000))
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe "beginning_of_year" do
|
145
|
+
it "should be calculated correctly" do
|
146
|
+
date_or_time(2005,2,22,10,10,10).beginning_of_year.should == date_or_time(2005,1,1,0,0,0)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "next_week" do
|
151
|
+
it "should be calculated correctly" do
|
152
|
+
date_or_time(2005,2,22,15,15,10).next_week.should == date_or_time(2005,2,28,0,0,0)
|
153
|
+
date_or_time(2005,2,22,15,15,10).next_week(:friday).should == date_or_time(2005,3,4,0,0,0)
|
154
|
+
date_or_time(2006,10,23,0,0,0).next_week.should == date_or_time(2006,10,30,0,0,0)
|
155
|
+
date_or_time(2006,10,23,0,0,0).next_week(:wednesday).should == date_or_time(2006,11,1,0,0,0)
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should be calculated correctly with default beginning of week" do
|
159
|
+
# calling with_bw_default produces LocalJumpError, even though a block is given
|
160
|
+
begin
|
161
|
+
old_bw = Date.beginning_of_week
|
162
|
+
Date.beginning_of_week = :tuesday
|
163
|
+
|
164
|
+
date_or_time(2012,3,21,0,0,0).next_week(:wednesday).should == date_or_time(2012,3,28,0,0,0)
|
165
|
+
date_or_time(2012,3,21,0,0,0).next_week(:saturday).should == date_or_time(2012,3,31,0,0,0)
|
166
|
+
date_or_time(2012,3,21,0,0,0).next_week(:tuesday).should == date_or_time(2012,3,27,0,0,0)
|
167
|
+
date_or_time(2012,3,21,0,0,0).next_week(:monday).should == date_or_time(2012,4,02,0,0,0)
|
168
|
+
ensure
|
169
|
+
Date.beginning_of_week = old_bw
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
describe "next_month" do
|
175
|
+
it "should be calculated correctly" do
|
176
|
+
date_or_time(2005,8,31,15,15,10).next_month.should == date_or_time(2005,9,30,15,15,10)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
describe "next_quarter" do
|
181
|
+
it "should be calculated correctly" do
|
182
|
+
date_or_time(2005,8,31,15,15,10).next_quarter.should == date_or_time(2005,11,30,15,15,10)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe "next_year" do
|
187
|
+
it "should be calculated correctly" do
|
188
|
+
date_or_time(2005,6,5,10,10,10).next_year.should == date_or_time(2006,6,5,10,10,10)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
describe "prev_week" do
|
193
|
+
it "should be calculated correctly" do
|
194
|
+
date_or_time(2005,3,1,15,15,10).prev_week.should == date_or_time(2005,2,21,0,0,0)
|
195
|
+
date_or_time(2005,3,1,15,15,10).prev_week(:tuesday).should == date_or_time(2005,2,22,0,0,0)
|
196
|
+
date_or_time(2005,3,1,15,15,10).prev_week(:friday).should == date_or_time(2005,2,25,0,0,0)
|
197
|
+
date_or_time(2006,11,6,0,0,0).prev_week.should == date_or_time(2006,10,30,0,0,0)
|
198
|
+
date_or_time(2006,11,23,0,0,0).prev_week(:wednesday).should == date_or_time(2006,11,15,0,0,0)
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should be calculated correctly with default beginning of week" do
|
202
|
+
begin
|
203
|
+
old_bw = Date.beginning_of_week
|
204
|
+
Date.beginning_of_week = :tuesday
|
205
|
+
|
206
|
+
date_or_time(2012,3,21,0,0,0).prev_week(:wednesday).should == date_or_time(2012,3,14,0,0,0)
|
207
|
+
date_or_time(2012,3,21,0,0,0).prev_week(:saturday).should == date_or_time(2012,3,17,0,0,0)
|
208
|
+
date_or_time(2012,3,21,0,0,0).prev_week(:tuesday).should == date_or_time(2012,3,13,0,0,0)
|
209
|
+
date_or_time(2012,3,21,0,0,0).prev_week(:monday).should == date_or_time(2012,3,19,0,0,0)
|
210
|
+
ensure
|
211
|
+
Date.beginning_of_week = old_bw
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe "prev_month" do
|
217
|
+
it "should be calculated correctly" do
|
218
|
+
date_or_time(2004,3,31,10,10,10).prev_month.should == date_or_time(2004,2,29,10,10,10)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
describe "prev_quarter" do
|
223
|
+
it "should be calculated correctly" do
|
224
|
+
date_or_time(2004,5,31,10,10,10).prev_quarter.should == date_or_time(2004,2,29,10,10,10)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
describe "prev_year" do
|
229
|
+
it "should be calculated correctly" do
|
230
|
+
date_or_time(2005,6,5,10,10,10).prev_year.should == date_or_time(2004,6,5,10,10,10)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
describe "days_to_week_start" do
|
235
|
+
it "should be calculated correctly" do
|
236
|
+
date_or_time(2011,11,01,0,0,0).days_to_week_start(:tuesday).should == 0
|
237
|
+
date_or_time(2011,11,02,0,0,0).days_to_week_start(:tuesday).should == 1
|
238
|
+
date_or_time(2011,11,03,0,0,0).days_to_week_start(:tuesday).should == 2
|
239
|
+
date_or_time(2011,11,04,0,0,0).days_to_week_start(:tuesday).should == 3
|
240
|
+
date_or_time(2011,11,05,0,0,0).days_to_week_start(:tuesday).should == 4
|
241
|
+
date_or_time(2011,11,06,0,0,0).days_to_week_start(:tuesday).should == 5
|
242
|
+
date_or_time(2011,11,07,0,0,0).days_to_week_start(:tuesday).should == 6
|
243
|
+
|
244
|
+
date_or_time(2011,11,03,0,0,0).days_to_week_start(:monday).should == 3
|
245
|
+
date_or_time(2011,11,04,0,0,0).days_to_week_start(:tuesday).should == 3
|
246
|
+
date_or_time(2011,11,05,0,0,0).days_to_week_start(:wednesday).should == 3
|
247
|
+
date_or_time(2011,11,06,0,0,0).days_to_week_start(:thursday).should == 3
|
248
|
+
date_or_time(2011,11,07,0,0,0).days_to_week_start(:friday).should == 3
|
249
|
+
date_or_time(2011,11,8,0,0,0).days_to_week_start(:saturday).should == 3
|
250
|
+
date_or_time(2011,11,9,0,0,0).days_to_week_start(:sunday).should == 3
|
251
|
+
end
|
252
|
+
|
253
|
+
it "should be calculated correctly with default beginning of week" do
|
254
|
+
begin
|
255
|
+
old_bw = Date.beginning_of_week
|
256
|
+
Date.beginning_of_week = :friday
|
257
|
+
|
258
|
+
date_or_time(2012,03,8,0,0,0).days_to_week_start.should == 6
|
259
|
+
date_or_time(2012,03,7,0,0,0).days_to_week_start.should == 5
|
260
|
+
date_or_time(2012,03,6,0,0,0).days_to_week_start.should == 4
|
261
|
+
date_or_time(2012,03,5,0,0,0).days_to_week_start.should == 3
|
262
|
+
date_or_time(2012,03,4,0,0,0).days_to_week_start.should == 2
|
263
|
+
date_or_time(2012,03,3,0,0,0).days_to_week_start.should == 1
|
264
|
+
date_or_time(2012,03,2,0,0,0).days_to_week_start.should == 0
|
265
|
+
ensure
|
266
|
+
Date.beginning_of_week = old_bw
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
describe "beginning_of_week" do
|
272
|
+
it "should be calculated correctly" do
|
273
|
+
date_or_time(2005,2,4,10,10,10).beginning_of_week.should == date_or_time(2005,1,31,0,0,0)
|
274
|
+
date_or_time(2005,11,28,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
|
275
|
+
date_or_time(2005,11,29,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
|
276
|
+
date_or_time(2005,11,30,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
|
277
|
+
date_or_time(2005,12,01,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
|
278
|
+
date_or_time(2005,12,02,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
|
279
|
+
date_or_time(2005,12,03,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
|
280
|
+
date_or_time(2005,12,04,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
describe "end_of_week" do
|
285
|
+
it "should be calculated correctly" do
|
286
|
+
date_or_time(2007,12,31,10,10,10).end_of_week.should == date_or_time(2008,1,6,23,59,59,Rational(999999999, 1000))
|
287
|
+
date_or_time(2007,8,27,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
|
288
|
+
date_or_time(2007,8,28,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
|
289
|
+
date_or_time(2007,8,29,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
|
290
|
+
date_or_time(2007,8,30,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
|
291
|
+
date_or_time(2007,8,31,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
|
292
|
+
date_or_time(2007,9,01,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
|
293
|
+
date_or_time(2007,9,02,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
describe "end_of_month" do
|
298
|
+
it "should be calculated correctly" do
|
299
|
+
date_or_time(2005,3,20,10,10,10).end_of_month.should == date_or_time(2005,3,31,23,59,59,Rational(999999999, 1000))
|
300
|
+
date_or_time(2005,2,20,10,10,10).end_of_month.should == date_or_time(2005,2,28,23,59,59,Rational(999999999, 1000))
|
301
|
+
date_or_time(2005,4,20,10,10,10).end_of_month.should == date_or_time(2005,4,30,23,59,59,Rational(999999999, 1000))
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
describe "end_of_year" do
|
306
|
+
it "should be calculated correctly" do
|
307
|
+
date_or_time(2007,2,22,10,10,10).end_of_year.should == date_or_time(2007,12,31,23,59,59,Rational(999999999, 1000))
|
308
|
+
date_or_time(2007,12,31,10,10,10).end_of_year.should == date_or_time(2007,12,31,23,59,59,Rational(999999999, 1000))
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
describe "weekdays" do
|
313
|
+
it "should return monday with default beginning of week" do
|
314
|
+
begin
|
315
|
+
old_bw = Date.beginning_of_week
|
316
|
+
Date.beginning_of_week = :saturday
|
317
|
+
|
318
|
+
date_or_time(2012,9,18,0,0,0).monday.should == date_or_time(2012,9,17,0,0,0)
|
319
|
+
ensure
|
320
|
+
Date.beginning_of_week = old_bw
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
it "should return sunday with default beginning of week" do
|
325
|
+
begin
|
326
|
+
old_bw = Date.beginning_of_week
|
327
|
+
Date.beginning_of_week = :wednesday
|
328
|
+
|
329
|
+
date_or_time(2012,9,19,0,0,0).sunday.should == date_or_time(2012,9,23,23,59,59, Rational(999999999, 1000))
|
330
|
+
ensure
|
331
|
+
Date.beginning_of_week = old_bw
|
332
|
+
end
|
333
|
+
end
|
334
|
+
end
|
335
|
+
end
|
336
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
describe "enumerable" do
|
2
|
+
describe "collect_with_index" do
|
3
|
+
describe "on Array" do
|
4
|
+
it "should return an empty array on an empty array" do
|
5
|
+
[].collect_with_index { |x, i| x }.should == []
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should yield an index for each element" do
|
9
|
+
[1, 2, 3].collect_with_index { |x, i| [i, x] }.should == [[0, 1], [1, 2], [2, 3]]
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should apply the block to each element" do
|
13
|
+
[1, 2, 3].collect_with_index { |x, i| x * i }.should == [0, 2, 6]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "on Hash" do
|
18
|
+
it "should return an empty array on an empty hash" do
|
19
|
+
{}.collect_with_index { |x, i| x }.should == []
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should apply the block to each element" do
|
23
|
+
result = {:foo => 1, :bar => 2, :baz => 3}.collect_with_index { |p, i| [p.first, p.last, i] }
|
24
|
+
result.should == [[:foo, 1, 0], [:bar, 2, 1], [:baz, 3, 2]]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "sum" do
|
30
|
+
it "should return the sum of an integer array" do
|
31
|
+
[5, 15, 10].sum.should == 30
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should concatenate strings in a string array" do
|
35
|
+
['foo', 'bar'].sum.should == "foobar"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should concatenate arrays" do
|
39
|
+
[[1, 2], [3, 1, 5]].sum.should == [1, 2, 3, 1, 5]
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should yield a block to each element before summing if given" do
|
43
|
+
[1, 2, 3].sum { |p| p * 2 }.should == 12
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should allow a custom result for an empty list" do
|
47
|
+
[].sum("empty") { |i| i.amount }.should == "empty"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "index_by" do
|
52
|
+
class Person < Struct.new(:first_name, :last_name)
|
53
|
+
end
|
54
|
+
|
55
|
+
before do
|
56
|
+
@peter = Person.new('Peter', 'Griffin')
|
57
|
+
@homer = Person.new('Homer', 'Simpson')
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should create hash with keys given by block result" do
|
61
|
+
[@peter, @homer].index_by { |x| x.first_name }.should == { 'Peter' => @peter, 'Homer' => @homer }
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should create enumerator if no block is given" do
|
65
|
+
[@peter, @homer].index_by.should.is_a Enumerator
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "many?" do
|
70
|
+
it "should return false for an empty enumerable" do
|
71
|
+
[].many?.should == false
|
72
|
+
{}.many?.should == false
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should return false for an enumerable with only one element" do
|
76
|
+
[1].many?.should == false
|
77
|
+
{:foo => :bar}.many?.should == false
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should return true for an enumerable with more than one element" do
|
81
|
+
[1, 2].many?.should == true
|
82
|
+
[1, 2, 3].many?.should == true
|
83
|
+
{:foo => :bar, :baz => :bingo}.many?.should == true
|
84
|
+
{:a => :b, :c => :d, :e => :f}.many?.should == true
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should return false if there are less than two elements satisfying the block if given" do
|
88
|
+
[1].many? { |x| x * 10 > 100 }.should == false
|
89
|
+
[1, 2, 3, 4, 5].many? { |x| x >= 5 }.should == false
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should return true if there is more than one element satisfying the block if given" do
|
93
|
+
[1, 2, 3, 4, 5].many? { |x| x < 4 }.should == true
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "exclude?" do
|
98
|
+
it "should return true if the element is not in the array" do
|
99
|
+
[1, 2, 3].exclude?(4).should == true
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should return false if the element is in the array" do
|
103
|
+
[1, 2, 3].exclude?(2).should == false
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should always return true for an empty array" do
|
107
|
+
[].exclude?(rand).should == true
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "Range" do
|
113
|
+
describe "sum" do
|
114
|
+
it "should return the sum of integers in an inclusive range" do
|
115
|
+
(3..5).sum.should == 12
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should return the sum of integers in an exclusive range" do
|
119
|
+
(3...5).sum.should == 7
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should return zero for an empty range" do
|
123
|
+
(1...1).sum.should == 0
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should return custom identity for an empty range if given" do
|
127
|
+
(1...1).sum("empty").should == "empty"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
describe "hash" do
|
2
|
+
before do
|
3
|
+
@hash = { :a => 1, :b => [ { :c => 2, :d => 3 }, :b ], :d => { :f => 4 } }
|
4
|
+
end
|
5
|
+
|
6
|
+
describe "deep_delete_if" do
|
7
|
+
it "should delete a top level key recursively" do
|
8
|
+
@hash.deep_delete_if { |k,v| k == :a }.should == { :b => [ { :c => 2, :d => 3 }, :b ], :d => { :f => 4 } }
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should delete a key within an array recursively" do
|
12
|
+
@hash.deep_delete_if { |k,v| v == 2 }.should == { :a => 1, :b => [ { :d => 3 }, :b ], :d => { :f => 4 } }
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should delete a key within a hash recursively" do
|
16
|
+
@hash.deep_delete_if { |k,v| v == 4 }.should == { :a => 1, :b => [ { :c => 2, :d => 3 }, :b ], :d => {} }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|