motion-support 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. data/.gitignore +3 -0
  2. data/Gemfile +1 -1
  3. data/README.md +231 -2
  4. data/Rakefile +3 -3
  5. data/app/app_delegate.rb +0 -2
  6. data/examples/Inflector/.gitignore +16 -0
  7. data/examples/Inflector/Gemfile +5 -0
  8. data/examples/Inflector/Rakefile +13 -0
  9. data/examples/Inflector/app/app_delegate.rb +8 -0
  10. data/examples/Inflector/app/inflections.rb +5 -0
  11. data/examples/Inflector/app/inflector_view_controller.rb +120 -0
  12. data/examples/Inflector/app/words_view_controller.rb +101 -0
  13. data/examples/Inflector/resources/Default-568h@2x.png +0 -0
  14. data/examples/Inflector/spec/main_spec.rb +9 -0
  15. data/lib/motion-support/core_ext/array.rb +13 -0
  16. data/lib/motion-support/core_ext/class.rb +8 -0
  17. data/lib/motion-support/core_ext/hash.rb +16 -0
  18. data/lib/motion-support/core_ext/integer.rb +9 -0
  19. data/lib/motion-support/core_ext/module.rb +14 -0
  20. data/lib/motion-support/core_ext/numeric.rb +8 -0
  21. data/lib/motion-support/core_ext/object.rb +14 -0
  22. data/lib/motion-support/core_ext/range.rb +8 -0
  23. data/lib/motion-support/core_ext/string.rb +14 -0
  24. data/lib/motion-support/core_ext/time.rb +19 -0
  25. data/lib/motion-support/core_ext.rb +3 -0
  26. data/lib/motion-support/inflector.rb +12 -156
  27. data/lib/motion-support.rb +5 -5
  28. data/motion/_stdlib/cgi.rb +22 -0
  29. data/motion/_stdlib/date.rb +77 -0
  30. data/motion/_stdlib/time.rb +19 -0
  31. data/motion/core_ext/array/access.rb +28 -0
  32. data/motion/core_ext/array/conversions.rb +86 -0
  33. data/motion/core_ext/array/extract_options.rb +11 -0
  34. data/motion/core_ext/array/grouping.rb +99 -0
  35. data/motion/core_ext/array/prepend_and_append.rb +7 -0
  36. data/motion/core_ext/array/wrap.rb +45 -0
  37. data/{lib/motion-support → motion/core_ext}/array.rb +0 -4
  38. data/motion/core_ext/class/attribute.rb +119 -0
  39. data/motion/core_ext/class/attribute_accessors.rb +168 -0
  40. data/motion/core_ext/date/acts_like.rb +8 -0
  41. data/motion/core_ext/date/calculations.rb +117 -0
  42. data/motion/core_ext/date/conversions.rb +56 -0
  43. data/motion/core_ext/date_and_time/calculations.rb +232 -0
  44. data/motion/core_ext/enumerable.rb +90 -0
  45. data/motion/core_ext/hash/deep_merge.rb +27 -0
  46. data/motion/core_ext/hash/except.rb +15 -0
  47. data/motion/core_ext/hash/indifferent_access.rb +19 -0
  48. data/motion/core_ext/hash/keys.rb +138 -0
  49. data/motion/core_ext/hash/reverse_merge.rb +22 -0
  50. data/motion/core_ext/hash/slice.rb +40 -0
  51. data/motion/core_ext/integer/inflections.rb +27 -0
  52. data/motion/core_ext/integer/multiple.rb +10 -0
  53. data/motion/core_ext/integer/time.rb +41 -0
  54. data/{lib/motion-support → motion/core_ext}/metaclass.rb +0 -0
  55. data/motion/core_ext/module/aliasing.rb +69 -0
  56. data/motion/core_ext/module/anonymous.rb +19 -0
  57. data/motion/core_ext/module/attr_internal.rb +38 -0
  58. data/motion/core_ext/module/attribute_accessors.rb +64 -0
  59. data/motion/core_ext/module/delegation.rb +175 -0
  60. data/motion/core_ext/module/introspection.rb +60 -0
  61. data/motion/core_ext/module/reachable.rb +5 -0
  62. data/motion/core_ext/module/remove_method.rb +12 -0
  63. data/motion/core_ext/ns_dictionary.rb +11 -0
  64. data/motion/core_ext/numeric/bytes.rb +44 -0
  65. data/motion/core_ext/numeric/conversions.rb +7 -0
  66. data/motion/core_ext/numeric/time.rb +75 -0
  67. data/motion/core_ext/object/acts_like.rb +10 -0
  68. data/motion/core_ext/object/blank.rb +105 -0
  69. data/motion/core_ext/object/deep_dup.rb +44 -0
  70. data/motion/core_ext/object/duplicable.rb +83 -0
  71. data/motion/core_ext/object/instance_variables.rb +28 -0
  72. data/motion/core_ext/object/to_param.rb +58 -0
  73. data/motion/core_ext/object/to_query.rb +26 -0
  74. data/motion/core_ext/object/try.rb +78 -0
  75. data/motion/core_ext/range/include_range.rb +23 -0
  76. data/motion/core_ext/range/overlaps.rb +8 -0
  77. data/motion/core_ext/regexp.rb +5 -0
  78. data/motion/core_ext/string/access.rb +104 -0
  79. data/motion/core_ext/string/behavior.rb +6 -0
  80. data/motion/core_ext/string/exclude.rb +11 -0
  81. data/motion/core_ext/string/filters.rb +55 -0
  82. data/motion/core_ext/string/indent.rb +43 -0
  83. data/motion/core_ext/string/inflections.rb +195 -0
  84. data/motion/core_ext/string/starts_ends_with.rb +4 -0
  85. data/motion/core_ext/string/strip.rb +24 -0
  86. data/motion/core_ext/time/acts_like.rb +8 -0
  87. data/motion/core_ext/time/calculations.rb +215 -0
  88. data/motion/core_ext/time/conversions.rb +52 -0
  89. data/motion/duration.rb +104 -0
  90. data/motion/hash_with_indifferent_access.rb +251 -0
  91. data/motion/inflections.rb +67 -0
  92. data/motion/inflector/inflections.rb +203 -0
  93. data/motion/inflector/methods.rb +321 -0
  94. data/{lib/motion-support → motion}/logger.rb +0 -0
  95. data/{lib/motion-support → motion}/version.rb +1 -1
  96. data/motion-support.gemspec +2 -2
  97. data/spec/motion-support/_helpers/constantize_test_cases.rb +75 -0
  98. data/spec/motion-support/_helpers/inflector_test_cases.rb +313 -0
  99. data/spec/motion-support/core_ext/array/access_spec.rb +29 -0
  100. data/spec/motion-support/core_ext/array/conversion_spec.rb +60 -0
  101. data/spec/motion-support/core_ext/array/extract_options_spec.rb +15 -0
  102. data/spec/motion-support/core_ext/array/grouping_spec.rb +85 -0
  103. data/spec/motion-support/core_ext/array/prepend_and_append_spec.rb +25 -0
  104. data/spec/motion-support/core_ext/array/wrap_spec.rb +19 -0
  105. data/spec/motion-support/{array_spec.rb → core_ext/array_spec.rb} +0 -5
  106. data/spec/motion-support/core_ext/class/attribute_accessor_spec.rb +127 -0
  107. data/spec/motion-support/core_ext/class/attribute_spec.rb +92 -0
  108. data/spec/motion-support/core_ext/date/acts_like_spec.rb +11 -0
  109. data/spec/motion-support/core_ext/date/calculation_spec.rb +186 -0
  110. data/spec/motion-support/core_ext/date/conversion_spec.rb +18 -0
  111. data/spec/motion-support/core_ext/date_and_time/calculation_spec.rb +336 -0
  112. data/spec/motion-support/core_ext/enumerable_spec.rb +130 -0
  113. data/spec/motion-support/core_ext/hash/deep_merge_spec.rb +32 -0
  114. data/spec/motion-support/core_ext/hash/except_spec.rb +43 -0
  115. data/spec/motion-support/core_ext/hash/key_spec.rb +230 -0
  116. data/spec/motion-support/core_ext/hash/reverse_merge_spec.rb +26 -0
  117. data/spec/motion-support/core_ext/hash/slice_spec.rb +61 -0
  118. data/spec/motion-support/core_ext/integer/inflection_spec.rb +23 -0
  119. data/spec/motion-support/core_ext/integer/multiple_spec.rb +19 -0
  120. data/spec/motion-support/{metaclass_spec.rb → core_ext/metaclass_spec.rb} +0 -0
  121. data/spec/motion-support/core_ext/module/aliasing_spec.rb +143 -0
  122. data/spec/motion-support/core_ext/module/anonymous_spec.rb +29 -0
  123. data/spec/motion-support/core_ext/module/attr_internal_spec.rb +104 -0
  124. data/spec/motion-support/core_ext/module/attribute_accessor_spec.rb +86 -0
  125. data/spec/motion-support/core_ext/module/delegation_spec.rb +136 -0
  126. data/spec/motion-support/core_ext/module/introspection_spec.rb +70 -0
  127. data/spec/motion-support/core_ext/module/reachable_spec.rb +61 -0
  128. data/spec/motion-support/core_ext/module/remove_method_spec.rb +25 -0
  129. data/spec/motion-support/core_ext/numeric/bytes_spec.rb +43 -0
  130. data/spec/motion-support/core_ext/object/acts_like_spec.rb +21 -0
  131. data/spec/motion-support/core_ext/object/blank_spec.rb +54 -0
  132. data/spec/motion-support/core_ext/object/deep_dup_spec.rb +54 -0
  133. data/spec/motion-support/core_ext/object/duplicable_spec.rb +31 -0
  134. data/spec/motion-support/core_ext/object/instance_variable_spec.rb +19 -0
  135. data/spec/motion-support/core_ext/object/to_param_spec.rb +75 -0
  136. data/spec/motion-support/core_ext/object/to_query_spec.rb +37 -0
  137. data/spec/motion-support/core_ext/object/try_spec.rb +92 -0
  138. data/spec/motion-support/core_ext/range/include_range_spec.rb +31 -0
  139. data/spec/motion-support/core_ext/range/overlap_spec.rb +43 -0
  140. data/spec/motion-support/core_ext/regexp_spec.rb +7 -0
  141. data/spec/motion-support/core_ext/string/access_spec.rb +53 -0
  142. data/spec/motion-support/core_ext/string/behavior_spec.rb +7 -0
  143. data/spec/motion-support/core_ext/string/exclude_spec.rb +8 -0
  144. data/spec/motion-support/core_ext/string/filter_spec.rb +48 -0
  145. data/spec/motion-support/core_ext/string/indent_spec.rb +56 -0
  146. data/spec/motion-support/core_ext/string/inflection_spec.rb +142 -0
  147. data/spec/motion-support/core_ext/string/starts_end_with_spec.rb +14 -0
  148. data/spec/motion-support/core_ext/string/strip_spec.rb +34 -0
  149. data/spec/motion-support/core_ext/string_spec.rb +88 -0
  150. data/spec/motion-support/core_ext/time/acts_like_spec.rb +11 -0
  151. data/spec/motion-support/core_ext/time/calculation_spec.rb +201 -0
  152. data/spec/motion-support/core_ext/time/conversion_spec.rb +54 -0
  153. data/spec/motion-support/duration_spec.rb +107 -0
  154. data/spec/motion-support/hash_with_indifferent_access_spec.rb +605 -0
  155. data/spec/motion-support/inflector_spec.rb +474 -35
  156. data/spec/motion-support/ns_dictionary_spec.rb +29 -0
  157. metadata +212 -35
  158. data/lib/motion-support/cattr_accessor.rb +0 -19
  159. data/lib/motion-support/class_inheritable_accessor.rb +0 -23
  160. data/lib/motion-support/class_inheritable_array.rb +0 -29
  161. data/lib/motion-support/hash.rb +0 -31
  162. data/lib/motion-support/nilclass.rb +0 -5
  163. data/lib/motion-support/object.rb +0 -17
  164. data/lib/motion-support/string.rb +0 -71
  165. data/spec/motion-support/cattr_accessor_spec.rb +0 -49
  166. data/spec/motion-support/class_inheritable_accessor_spec.rb +0 -49
  167. data/spec/motion-support/class_inheritable_array_spec.rb +0 -61
  168. data/spec/motion-support/hash_spec.rb +0 -31
  169. data/spec/motion-support/nilclass_spec.rb +0 -5
  170. data/spec/motion-support/object_spec.rb +0 -43
  171. data/spec/motion-support/string_spec.rb +0 -145
@@ -0,0 +1,142 @@
1
+ describe "String" do
2
+ describe "Inflections" do
3
+ describe "singularize" do
4
+ InflectorTestCases::SingularToPlural.each do |singular, plural|
5
+ it "should pluralize singular #{singular}" do
6
+ singular.pluralize.should == plural
7
+ end
8
+ end
9
+
10
+ it "should pluralize plural" do
11
+ "plurals".pluralize.should == "plurals"
12
+ end
13
+
14
+ it "should pluralize with number" do
15
+ "blargle".pluralize(0).should == "blargles"
16
+ "blargle".pluralize(1).should == "blargle"
17
+ "blargle".pluralize(2).should == "blargles"
18
+ end
19
+ end
20
+
21
+ describe "singularize" do
22
+ InflectorTestCases::SingularToPlural.each do |singular, plural|
23
+ it "should singularize plural #{plural}" do
24
+ plural.singularize.should == singular
25
+ end
26
+ end
27
+ end
28
+
29
+ describe "titleize" do
30
+ InflectorTestCases::MixtureToTitleCase.each do |before, titleized|
31
+ it "should titleize #{before}" do
32
+ before.titleize.should == titleized
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "camelize" do
38
+ InflectorTestCases::CamelToUnderscore.each do |camel, underscore|
39
+ it "should camelize #{underscore}" do
40
+ underscore.camelize.should == camel
41
+ end
42
+ end
43
+
44
+ it "should lower-camelize" do
45
+ 'Capital'.camelize(:lower).should == 'capital'
46
+ end
47
+ end
48
+
49
+ describe "dasherize" do
50
+ InflectorTestCases::UnderscoresToDashes.each do |underscored, dasherized|
51
+ it "should dasherize #{underscored}" do
52
+ underscored.dasherize.should == dasherized
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "underscore" do
58
+ InflectorTestCases::CamelToUnderscore.each do |camel, underscore|
59
+ it "should underscore #{camel}" do
60
+ camel.underscore.should == underscore
61
+ end
62
+ end
63
+
64
+ it "should underscore acronyms" do
65
+ "HTMLTidy".underscore.should == "html_tidy"
66
+ "HTMLTidyGenerator".underscore.should == "html_tidy_generator"
67
+ end
68
+
69
+ InflectorTestCases::UnderscoreToLowerCamel.each do |underscored, lower_camel|
70
+ it "should lower-camelize #{underscored}" do
71
+ underscored.camelize(:lower).should == lower_camel
72
+ end
73
+ end
74
+ end
75
+
76
+ it "should demodulize" do
77
+ "MyApplication::Billing::Account".demodulize.should == "Account"
78
+ end
79
+
80
+ it "should deconstantize" do
81
+ "MyApplication::Billing::Account".deconstantize.should == "MyApplication::Billing"
82
+ end
83
+
84
+ describe "foreign_key" do
85
+ InflectorTestCases::ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
86
+ it "should build foreign key from #{klass}" do
87
+ klass.foreign_key.should == foreign_key
88
+ end
89
+ end
90
+
91
+ InflectorTestCases::ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
92
+ it "should build foreign key from #{klass} without underscore" do
93
+ klass.foreign_key(false).should == foreign_key
94
+ end
95
+ end
96
+ end
97
+
98
+ describe "tableize" do
99
+ InflectorTestCases::ClassNameToTableName.each do |class_name, table_name|
100
+ it "should tableize #{class_name}" do
101
+ class_name.tableize.should == table_name
102
+ end
103
+ end
104
+ end
105
+
106
+ describe "classify" do
107
+ InflectorTestCases::ClassNameToTableName.each do |class_name, table_name|
108
+ it "should classify #{table_name}" do
109
+ table_name.classify.should == class_name
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "humanize" do
115
+ InflectorTestCases::UnderscoreToHuman.each do |underscore, human|
116
+ it "should humanize #{underscore}" do
117
+ underscore.humanize.should == human
118
+ end
119
+ end
120
+ end
121
+
122
+ describe "constantize" do
123
+ extend ConstantizeTestCases
124
+
125
+ it "should constantize" do
126
+ run_constantize_tests_on do |string|
127
+ string.constantize
128
+ end
129
+ end
130
+ end
131
+
132
+ describe "safe_constantize" do
133
+ extend ConstantizeTestCases
134
+
135
+ it "should safe_constantize" do
136
+ run_safe_constantize_tests_on do |string|
137
+ string.safe_constantize
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,14 @@
1
+ describe "String" do
2
+ describe "starts_with?/ends_with?" do
3
+ it "should have starts/ends_with? alias" do
4
+ s = "hello"
5
+ s.starts_with?('h').should.be.true
6
+ s.starts_with?('hel').should.be.true
7
+ s.starts_with?('el').should.be.false
8
+
9
+ s.ends_with?('o').should.be.true
10
+ s.ends_with?('lo').should.be.true
11
+ s.ends_with?('el').should.be.false
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ describe "String" do
2
+ describe "strip_heredoc" do
3
+ it "should strip heredoc on an empty string" do
4
+ ''.strip_heredoc.should == ''
5
+ end
6
+
7
+ it "should strip heredoc on a string with no lines" do
8
+ 'x'.strip_heredoc.should == 'x'
9
+ ' x'.strip_heredoc.should == 'x'
10
+ end
11
+
12
+ it "should strip heredoc on a heredoc with no margin" do
13
+ "foo\nbar".strip_heredoc.should == "foo\nbar"
14
+ "foo\n bar".strip_heredoc.should == "foo\n bar"
15
+ end
16
+
17
+ it "should strip heredoc on a regular indented heredoc" do
18
+ <<-EOS.strip_heredoc.should == "foo\n bar\nbaz\n"
19
+ foo
20
+ bar
21
+ baz
22
+ EOS
23
+ end
24
+
25
+ it "should strip heredoc on a regular indented heredoc with blank lines" do
26
+ <<-EOS.strip_heredoc.should == "foo\n bar\n\nbaz\n"
27
+ foo
28
+ bar
29
+
30
+ baz
31
+ EOS
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,88 @@
1
+ describe "string" do
2
+ describe "constantize" do
3
+ module Ace
4
+ module Base
5
+ class Case
6
+ class Dice
7
+ end
8
+ end
9
+ class Fase < Case
10
+ end
11
+ end
12
+ class Gas
13
+ include Base
14
+ end
15
+ end
16
+
17
+ class Object
18
+ module AddtlGlobalConstants
19
+ class Case
20
+ class Dice
21
+ end
22
+ end
23
+ end
24
+ include AddtlGlobalConstants
25
+ end
26
+
27
+ it "should lookup nested constant" do
28
+ "Ace::Base::Case".constantize.should == Ace::Base::Case
29
+ end
30
+
31
+ it "should lookup nested absolute constant" do
32
+ "::Ace::Base::Case".constantize.should == Ace::Base::Case
33
+ end
34
+
35
+ it "should lookup nested inherited constant" do
36
+ "Ace::Base::Fase::Dice".constantize.should == Ace::Base::Fase::Dice
37
+ end
38
+
39
+ it "should lookup nested included constant" do
40
+ "Ace::Gas::Case".constantize.should == Ace::Gas::Case
41
+ end
42
+
43
+ it "should lookup nested constant included into Object" do
44
+ "Case::Dice".constantize.should == Case::Dice
45
+ end
46
+
47
+ it "should lookup nested absolute constant included into Object" do
48
+ "Object::Case::Dice".constantize.should == Case::Dice
49
+ end
50
+
51
+ it "should lookup constant" do
52
+ "String".constantize.should == String
53
+ end
54
+
55
+ it "should lookup absolute constant" do
56
+ "::Ace".constantize.should == Ace
57
+ end
58
+
59
+ it "should return Object for empty string" do
60
+ "".constantize.should == Object
61
+ end
62
+
63
+ it "should return Object for double colon" do
64
+ "::".constantize.should == Object
65
+ end
66
+
67
+ it "should raise NameError for unknown constant" do
68
+ lambda { "UnknownClass".constantize }.should.raise NameError
69
+ end
70
+
71
+ it "should raise NameError for unknown nested constant" do
72
+ lambda { "UnknownClass::Ace".constantize }.should.raise NameError
73
+ lambda { "UnknownClass::Ace::Base".constantize }.should.raise NameError
74
+ end
75
+
76
+ it "should raise NameError for invalid string" do
77
+ lambda { "An invalid string".constantize }.should.raise NameError
78
+ lambda { "InvalidClass\n".constantize }.should.raise NameError
79
+ end
80
+
81
+ it "should raise NameError for nested unknown constant in known constant" do
82
+ lambda { "Ace::ConstantizeTestCases".constantize }.should.raise NameError
83
+ lambda { "Ace::Base::ConstantizeTestCases".constantize }.should.raise NameError
84
+ lambda { "Ace::Gas::Base".constantize }.should.raise NameError
85
+ lambda { "Ace::Gas::ConstantizeTestCases".constantize }.should.raise NameError
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,11 @@
1
+ describe "Time" do
2
+ describe "acts_like" do
3
+ it "should act like time" do
4
+ Time.now.should.acts_like(:time)
5
+ end
6
+
7
+ it "should not act like date" do
8
+ Time.now.should.not.acts_like(:date)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,201 @@
1
+ describe "Time" do
2
+ describe "calculations" do
3
+ describe "seconds_since_midnight" do
4
+ it "should calculate correctly" do
5
+ Time.local(2005,1,1,0,0,1).seconds_since_midnight.should == 1
6
+ Time.local(2005,1,1,0,1,0).seconds_since_midnight.should == 60
7
+ Time.local(2005,1,1,1,1,0).seconds_since_midnight.should == 3660
8
+ Time.local(2005,1,1,23,59,59).seconds_since_midnight.should == 86399
9
+ Time.local(2005,1,1,0,1,0,10).seconds_since_midnight.should == 60.00001
10
+ end
11
+ end
12
+
13
+ describe "seconds_until_end_of_day" do
14
+ it "should calculate correctly" do
15
+ Time.local(2005,1,1,23,59,59).seconds_until_end_of_day.should == 0
16
+ Time.local(2005,1,1,23,59,58).seconds_until_end_of_day.should == 1
17
+ Time.local(2005,1,1,23,58,59).seconds_until_end_of_day.should == 60
18
+ Time.local(2005,1,1,22,58,59).seconds_until_end_of_day.should == 3660
19
+ Time.local(2005,1,1,0,0,0).seconds_until_end_of_day.should == 86399
20
+ end
21
+ end
22
+
23
+ describe "beginning_of_day" do
24
+ it "should calculate correctly" do
25
+ Time.local(2005,2,4,10,10,10).beginning_of_day.should == Time.local(2005,2,4,0,0,0)
26
+ end
27
+ end
28
+
29
+ describe "beginning_of_hour" do
30
+ it "should calculate correctly" do
31
+ Time.local(2005,2,4,19,30,10).beginning_of_hour.should == Time.local(2005,2,4,19,0,0)
32
+ end
33
+ end
34
+
35
+ describe "beginning_of_minute" do
36
+ it "should calculate correctly" do
37
+ Time.local(2005,2,4,19,30,10).beginning_of_minute.should == Time.local(2005,2,4,19,30,0)
38
+ end
39
+ end
40
+
41
+ describe "end_of_day" do
42
+ it "should calculate correctly" do
43
+ Time.local(2007,8,12,10,10,10).end_of_day.should == Time.local(2007,8,12,23,59,59,Rational(999999999, 1000))
44
+ end
45
+ end
46
+
47
+ describe "end_of_hour" do
48
+ it "should calculate correctly" do
49
+ Time.local(2005,2,4,19,30,10).end_of_hour.should == Time.local(2005,2,4,19,59,59,Rational(999999999, 1000))
50
+ end
51
+ end
52
+
53
+ describe "end_of_minute" do
54
+ it "should calculate correctly" do
55
+ Time.local(2005,2,4,19,30,10).end_of_minute.should == Time.local(2005,2,4,19,30,59,Rational(999999999, 1000))
56
+ end
57
+ end
58
+
59
+ describe "last_year" do
60
+ it "should calculate correctly" do
61
+ Time.local(2005,6,5,10,0,0).last_year.should == Time.local(2004,6,5,10)
62
+ end
63
+ end
64
+
65
+ describe "ago" do
66
+ it "should calculate correctly" do
67
+ Time.local(2005,2,22,10,10,10).ago(1).should == Time.local(2005,2,22,10,10,9)
68
+ Time.local(2005,2,22,10,10,10).ago(3600).should == Time.local(2005,2,22,9,10,10)
69
+ Time.local(2005,2,22,10,10,10).ago(86400*2).should == Time.local(2005,2,20,10,10,10)
70
+ Time.local(2005,2,22,10,10,10).ago(86400*2 + 3600 + 25).should == Time.local(2005,2,20,9,9,45)
71
+ end
72
+ end
73
+
74
+ describe "since" do
75
+ it "should calculate correctly" do
76
+ Time.local(2005,2,22,10,10,10).since(1).should == Time.local(2005,2,22,10,10,11)
77
+ Time.local(2005,2,22,10,10,10).since(3600).should == Time.local(2005,2,22,11,10,10)
78
+ Time.local(2005,2,22,10,10,10).since(86400*2).should == Time.local(2005,2,24,10,10,10)
79
+ Time.local(2005,2,22,10,10,10).since(86400*2 + 3600 + 25).should == Time.local(2005,2,24,11,10,35)
80
+ end
81
+ end
82
+
83
+ describe "change" do
84
+ it "should calculate correctly" do
85
+ Time.local(2005,2,22,15,15,10).change(:year => 2006).should == Time.local(2006,2,22,15,15,10)
86
+ Time.local(2005,2,22,15,15,10).change(:month => 6).should == Time.local(2005,6,22,15,15,10)
87
+ Time.local(2005,2,22,15,15,10).change(:year => 2012, :month => 9).should == Time.local(2012,9,22,15,15,10)
88
+ Time.local(2005,2,22,15,15,10).change(:hour => 16).should == Time.local(2005,2,22,16)
89
+ Time.local(2005,2,22,15,15,10).change(:hour => 16, :min => 45).should == Time.local(2005,2,22,16,45)
90
+ Time.local(2005,2,22,15,15,10).change(:min => 45).should == Time.local(2005,2,22,15,45)
91
+
92
+ Time.local(2005,1,2,11,22,33,44).change(:hour => 5).should == Time.local(2005,1,2,5,0,0,0)
93
+ Time.local(2005,1,2,11,22,33,44).change(:min => 6).should == Time.local(2005,1,2,11,6,0,0)
94
+ Time.local(2005,1,2,11,22,33,44).change(:sec => 7).should == Time.local(2005,1,2,11,22,7,0)
95
+ Time.local(2005,1,2,11,22,33,44).change(:usec => 8).should == Time.local(2005,1,2,11,22,33,8)
96
+ end
97
+ end
98
+
99
+ describe "advance" do
100
+ it "should calculate correctly" do
101
+ Time.local(2005,2,28,15,15,10).advance(:years => 1).should == Time.local(2006,2,28,15,15,10)
102
+ Time.local(2005,2,28,15,15,10).advance(:months => 4).should == Time.local(2005,6,28,15,15,10)
103
+ Time.local(2005,2,28,15,15,10).advance(:weeks => 3).should == Time.local(2005,3,21,15,15,10)
104
+ Time.local(2005,2,28,15,15,10).advance(:weeks => 3.5).should == Time.local(2005,3,25,3,15,10)
105
+ Time.local(2005,2,28,15,15,10).advance(:days => 5).should == Time.local(2005,3,5,15,15,10)
106
+ Time.local(2005,2,28,15,15,10).advance(:days => 5.5).should == Time.local(2005,3,6,3,15,10)
107
+ Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 7).should == Time.local(2012,9,28,15,15,10)
108
+ Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :days => 5).should == Time.local(2013,10,3,15,15,10)
109
+ Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :weeks => 2, :days => 5).should == Time.local(2013,10,17,15,15,10)
110
+ Time.local(2005,2,28,15,15,10).advance(:years => -3, :months => -2, :days => -1).should == Time.local(2001,12,27,15,15,10)
111
+ Time.local(2004,2,29,15,15,10).advance(:years => 1).should == Time.local(2005,2,28,15,15,10)
112
+ Time.local(2005,2,28,15,15,10).advance(:hours => 5).should == Time.local(2005,2,28,20,15,10)
113
+ Time.local(2005,2,28,15,15,10).advance(:minutes => 7).should == Time.local(2005,2,28,15,22,10)
114
+ Time.local(2005,2,28,15,15,10).advance(:seconds => 9).should == Time.local(2005,2,28,15,15,19)
115
+ Time.local(2005,2,28,15,15,10).advance(:hours => 5, :minutes => 7, :seconds => 9).should == Time.local(2005,2,28,20,22,19)
116
+ Time.local(2005,2,28,15,15,10).advance(:hours => -5, :minutes => -7, :seconds => -9).should == Time.local(2005,2,28,10,8,1)
117
+ Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :weeks => 2, :days => 5, :hours => 5, :minutes => 7, :seconds => 9).should == Time.local(2013,10,17,20,22,19)
118
+ end
119
+
120
+ it "should advance with nsec" do
121
+ t = Time.at(0, Rational(108635108, 1000))
122
+ t.advance(:months => 0).should == t
123
+ end
124
+ end
125
+
126
+ describe "days_in_month" do
127
+ it "should calculate with year" do
128
+ Time.days_in_month(1, 2005).should == 31
129
+
130
+ Time.days_in_month(2, 2005).should == 28
131
+ Time.days_in_month(2, 2004).should == 29
132
+ Time.days_in_month(2, 2000).should == 29
133
+ Time.days_in_month(2, 1900).should == 28
134
+
135
+ Time.days_in_month(3, 2005).should == 31
136
+ Time.days_in_month(4, 2005).should == 30
137
+ Time.days_in_month(5, 2005).should == 31
138
+ Time.days_in_month(6, 2005).should == 30
139
+ Time.days_in_month(7, 2005).should == 31
140
+ Time.days_in_month(8, 2005).should == 31
141
+ Time.days_in_month(9, 2005).should == 30
142
+ Time.days_in_month(10, 2005).should == 31
143
+ Time.days_in_month(11, 2005).should == 30
144
+ Time.days_in_month(12, 2005).should == 31
145
+ end
146
+
147
+ it "should calculate for february in common year" do
148
+ Time.days_in_month(2, 2007).should == 28
149
+ end
150
+
151
+ it "should calculate for february in leap year" do
152
+ Time.days_in_month(2, 2008).should == 29
153
+ end
154
+ end
155
+
156
+ describe "last_month" do
157
+ it "should work on the 31st" do
158
+ Time.local(2004, 3, 31).last_month.should == Time.local(2004, 2, 29)
159
+ end
160
+ end
161
+
162
+ describe "<=>" do
163
+ it "should compare with time" do
164
+ (Time.utc(2000) <=> Time.utc(1999, 12, 31, 23, 59, 59, 999)).should == 1
165
+ (Time.utc(2000) <=> Time.utc(2000, 1, 1, 0, 0, 0)).should == 0
166
+ (Time.utc(2000) <=> Time.utc(2000, 1, 1, 0, 0, 0, 001)).should == -1
167
+ end
168
+ end
169
+
170
+ describe "all_day" do
171
+ it "should calculate correctly" do
172
+ Time.local(2011,6,7,10,10,10).all_day.should == (Time.local(2011,6,7,0,0,0)..Time.local(2011,6,7,23,59,59,Rational(999999999, 1000)))
173
+ end
174
+ end
175
+
176
+ describe "all_week" do
177
+ it "should calculate correctly" do
178
+ Time.local(2011,6,7,10,10,10).all_week.should == (Time.local(2011,6,6,0,0,0)..Time.local(2011,6,12,23,59,59,Rational(999999999, 1000)))
179
+ Time.local(2011,6,7,10,10,10).all_week(:sunday).should == (Time.local(2011,6,5,0,0,0)..Time.local(2011,6,11,23,59,59,Rational(999999999, 1000)))
180
+ end
181
+ end
182
+
183
+ describe "all_month" do
184
+ it "should calculate correctly" do
185
+ Time.local(2011,6,7,10,10,10).all_month.should == (Time.local(2011,6,1,0,0,0)..Time.local(2011,6,30,23,59,59,Rational(999999999, 1000)))
186
+ end
187
+ end
188
+
189
+ describe "all_quarter" do
190
+ it "should calculate correctly" do
191
+ Time.local(2011,6,7,10,10,10).all_quarter.should == (Time.local(2011,4,1,0,0,0)..Time.local(2011,6,30,23,59,59,Rational(999999999, 1000)))
192
+ end
193
+ end
194
+
195
+ describe "all_year" do
196
+ it "should calculate correctly" do
197
+ Time.local(2011,6,7,10,10,10).all_year.should == (Time.local(2011,1,1,0,0,0)..Time.local(2011,12,31,23,59,59,Rational(999999999, 1000)))
198
+ end
199
+ end
200
+ end
201
+ end
@@ -0,0 +1,54 @@
1
+ describe "Time" do
2
+ describe "conversions" do
3
+ describe "to_formatted_s" do
4
+ before do
5
+ @time = Time.utc(2005, 2, 21, 17, 44, 30.12345678901)
6
+ end
7
+
8
+ it "should use default conversion if no parameter is given" do
9
+ @time.to_s.should == @time.to_default_s
10
+ end
11
+
12
+ it "should use default conversion if parameter is unknown" do
13
+ @time.to_s(:doesnt_exist).should == @time.to_default_s
14
+ end
15
+
16
+ it "should convert to db format" do
17
+ @time.to_s(:db).should == "2005-02-21 17:44:30"
18
+ end
19
+
20
+ it "should convert to short format" do
21
+ @time.to_s(:short).should == "21 Feb 17:44"
22
+ end
23
+
24
+ it "should convert to time format" do
25
+ @time.to_s(:time).should == "17:44"
26
+ end
27
+
28
+ it "should convert to number format" do
29
+ @time.to_s(:number).should == "20050221174430"
30
+ end
31
+
32
+ it "should convert to nsec format" do
33
+ @time.to_s(:nsec).should == "200502211744309N"
34
+ # Hmm. Looks like RubyMotion has an issue with nanosecs in string time formatting. It should actually be "20050221174430123456789"
35
+ end
36
+
37
+ it "should convert to long format" do
38
+ @time.to_s(:long).should == "February 21, 2005 17:44"
39
+ end
40
+
41
+ it "should convert to long_ordinal format" do
42
+ @time.to_s(:long_ordinal).should == "February 21st, 2005 17:44"
43
+ end
44
+ end
45
+
46
+ describe "custom date format" do
47
+ it "should convert to custom format" do
48
+ Time::DATE_FORMATS[:custom] = '%Y%m%d%H%M%S'
49
+ Time.local(2005, 2, 21, 14, 30, 0).to_s(:custom).should == '20050221143000'
50
+ Time::DATE_FORMATS.delete(:custom)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,107 @@
1
+ describe "Duration" do
2
+ describe "threequals" do
3
+ it "should be a day" do
4
+ MotionSupport::Duration.should === 1.day
5
+ end
6
+
7
+ it "should not be an int" do
8
+ MotionSupport::Duration.should.not === 1.day.to_i
9
+ end
10
+
11
+ it "should not be a string" do
12
+ MotionSupport::Duration.should.not === 'foo'
13
+ end
14
+ end
15
+
16
+ describe "equals" do
17
+ it "should equal itself" do
18
+ 1.day.should == 1.day
19
+ end
20
+
21
+ it "should equal integer representation" do
22
+ 1.day.should == 1.day.to_i
23
+ end
24
+
25
+ it "should not equal string" do
26
+ 1.day.should.not == 'foo'
27
+ end
28
+ end
29
+
30
+ describe "inspect" do
31
+ it "should convert to string representation" do
32
+ 0.seconds.inspect.should == '0 seconds'
33
+ 1.month.inspect.should == '1 month'
34
+ (1.month + 1.day).inspect.should == '1 month and 1 day'
35
+ (6.months - 2.days).inspect.should == '6 months and -2 days'
36
+ 10.seconds.inspect.should == '10 seconds'
37
+ (10.years + 2.months + 1.day).inspect.should == '10 years, 2 months, and 1 day'
38
+ 1.week.inspect.should == '7 days'
39
+ 1.fortnight.inspect.should == '14 days'
40
+ end
41
+ end
42
+
43
+ describe "arithmetic" do
44
+ it "should not break when subtracting time from itself" do
45
+ lambda { Date.today - Date.today }.should.not.raise
46
+ end
47
+
48
+ it "should add with time" do
49
+ (1.second + 1).should == 1 + 1.second
50
+ end
51
+
52
+ def test_plus_with_time
53
+ assert_equal 1 + 1.second, 1.second + 1, "Duration + Numeric should == Numeric + Duration"
54
+ end
55
+
56
+ end
57
+
58
+ describe "fractions" do
59
+ describe "days" do
60
+ it "should support fractional days" do
61
+ 1.5.weeks.should == (86400 * 7) * 1.5
62
+ 1.7.weeks.should == (86400 * 7) * 1.7
63
+ end
64
+
65
+ it "should support since" do
66
+ t = 2.years.ago
67
+ 1.5.days.since(t).should == 36.hours.since(t)
68
+ end
69
+
70
+ it "should support ago" do
71
+ t = 2.years.ago
72
+ 1.5.days.ago(t).should == 36.hours.ago(t)
73
+ end
74
+ end
75
+
76
+ describe "weeks" do
77
+ it "should support fractional weeks" do
78
+ 1.5.days.should == 86400 * 1.5
79
+ 1.7.days.should == 86400 * 1.7
80
+ end
81
+
82
+ it "should support since" do
83
+ t = 2.years.ago
84
+ 1.5.weeks.since(t).should == (7 * 36).hours.since(t)
85
+ end
86
+
87
+ it "should support ago" do
88
+ t = 2.years.ago
89
+ 1.5.weeks.ago(t).should == (7 * 36).hours.ago(t)
90
+ end
91
+ end
92
+ end
93
+
94
+ describe "delegation" do
95
+ it "should delegate with block" do
96
+ counter = 0
97
+ lambda { 1.minute.times { counter += 1 } }.should.not.raise
98
+ counter.should == 60
99
+ end
100
+ end
101
+
102
+ describe "to_json" do
103
+ it "should convert to json" do
104
+ 2.days.to_json.should == '172800'
105
+ end
106
+ end
107
+ end