motion_blender-support 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.yardopts +1 -0
  4. data/Gemfile +4 -0
  5. data/HACKS.md +7 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +359 -0
  8. data/Rakefile +14 -0
  9. data/app/app_delegate.rb +5 -0
  10. data/examples/Inflector/.gitignore +16 -0
  11. data/examples/Inflector/Gemfile +5 -0
  12. data/examples/Inflector/Rakefile +13 -0
  13. data/examples/Inflector/app/app_delegate.rb +8 -0
  14. data/examples/Inflector/app/inflections.rb +5 -0
  15. data/examples/Inflector/app/inflector_view_controller.rb +109 -0
  16. data/examples/Inflector/app/words_view_controller.rb +101 -0
  17. data/examples/Inflector/resources/Default-568h@2x.png +0 -0
  18. data/examples/Inflector/spec/main_spec.rb +9 -0
  19. data/lib/motion-support/callbacks.rb +8 -0
  20. data/lib/motion-support/concern.rb +4 -0
  21. data/lib/motion-support/core_ext/array.rb +10 -0
  22. data/lib/motion-support/core_ext/class.rb +5 -0
  23. data/lib/motion-support/core_ext/hash.rb +13 -0
  24. data/lib/motion-support/core_ext/integer.rb +6 -0
  25. data/lib/motion-support/core_ext/module.rb +11 -0
  26. data/lib/motion-support/core_ext/numeric.rb +6 -0
  27. data/lib/motion-support/core_ext/object.rb +12 -0
  28. data/lib/motion-support/core_ext/range.rb +5 -0
  29. data/lib/motion-support/core_ext/string.rb +13 -0
  30. data/lib/motion-support/core_ext/time.rb +16 -0
  31. data/lib/motion-support/core_ext.rb +13 -0
  32. data/lib/motion-support/inflector.rb +8 -0
  33. data/lib/motion-support.rb +81 -0
  34. data/motion/_stdlib/array.rb +13 -0
  35. data/motion/_stdlib/cgi.rb +22 -0
  36. data/motion/_stdlib/date.rb +81 -0
  37. data/motion/_stdlib/enumerable.rb +9 -0
  38. data/motion/_stdlib/time.rb +19 -0
  39. data/motion/callbacks.rb +511 -0
  40. data/motion/concern.rb +122 -0
  41. data/motion/core_ext/array/access.rb +28 -0
  42. data/motion/core_ext/array/conversions.rb +86 -0
  43. data/motion/core_ext/array/extract_options.rb +11 -0
  44. data/motion/core_ext/array/grouping.rb +99 -0
  45. data/motion/core_ext/array/prepend_and_append.rb +7 -0
  46. data/motion/core_ext/array/wrap.rb +45 -0
  47. data/motion/core_ext/array.rb +19 -0
  48. data/motion/core_ext/class/attribute.rb +119 -0
  49. data/motion/core_ext/class/attribute_accessors.rb +168 -0
  50. data/motion/core_ext/date/acts_like.rb +8 -0
  51. data/motion/core_ext/date/calculations.rb +117 -0
  52. data/motion/core_ext/date/conversions.rb +56 -0
  53. data/motion/core_ext/date_and_time/calculations.rb +232 -0
  54. data/motion/core_ext/enumerable.rb +90 -0
  55. data/motion/core_ext/hash/deep_delete_if.rb +23 -0
  56. data/motion/core_ext/hash/deep_merge.rb +27 -0
  57. data/motion/core_ext/hash/except.rb +15 -0
  58. data/motion/core_ext/hash/indifferent_access.rb +19 -0
  59. data/motion/core_ext/hash/keys.rb +150 -0
  60. data/motion/core_ext/hash/reverse_merge.rb +22 -0
  61. data/motion/core_ext/hash/slice.rb +40 -0
  62. data/motion/core_ext/integer/inflections.rb +27 -0
  63. data/motion/core_ext/integer/multiple.rb +10 -0
  64. data/motion/core_ext/integer/time.rb +41 -0
  65. data/motion/core_ext/kernel/singleton_class.rb +6 -0
  66. data/motion/core_ext/metaclass.rb +8 -0
  67. data/motion/core_ext/module/aliasing.rb +69 -0
  68. data/motion/core_ext/module/anonymous.rb +19 -0
  69. data/motion/core_ext/module/attr_internal.rb +38 -0
  70. data/motion/core_ext/module/attribute_accessors.rb +64 -0
  71. data/motion/core_ext/module/delegation.rb +175 -0
  72. data/motion/core_ext/module/introspection.rb +60 -0
  73. data/motion/core_ext/module/reachable.rb +5 -0
  74. data/motion/core_ext/module/remove_method.rb +12 -0
  75. data/motion/core_ext/ns_dictionary.rb +14 -0
  76. data/motion/core_ext/ns_string.rb +14 -0
  77. data/motion/core_ext/numeric/bytes.rb +44 -0
  78. data/motion/core_ext/numeric/conversions.rb +49 -0
  79. data/motion/core_ext/numeric/time.rb +75 -0
  80. data/motion/core_ext/object/acts_like.rb +10 -0
  81. data/motion/core_ext/object/blank.rb +105 -0
  82. data/motion/core_ext/object/deep_dup.rb +44 -0
  83. data/motion/core_ext/object/duplicable.rb +87 -0
  84. data/motion/core_ext/object/inclusion.rb +15 -0
  85. data/motion/core_ext/object/instance_variables.rb +28 -0
  86. data/motion/core_ext/object/to_param.rb +58 -0
  87. data/motion/core_ext/object/to_query.rb +26 -0
  88. data/motion/core_ext/object/try.rb +78 -0
  89. data/motion/core_ext/range/include_range.rb +23 -0
  90. data/motion/core_ext/range/overlaps.rb +8 -0
  91. data/motion/core_ext/regexp.rb +5 -0
  92. data/motion/core_ext/string/access.rb +104 -0
  93. data/motion/core_ext/string/behavior.rb +6 -0
  94. data/motion/core_ext/string/exclude.rb +11 -0
  95. data/motion/core_ext/string/filters.rb +55 -0
  96. data/motion/core_ext/string/indent.rb +43 -0
  97. data/motion/core_ext/string/inflections.rb +178 -0
  98. data/motion/core_ext/string/starts_ends_with.rb +4 -0
  99. data/motion/core_ext/string/strip.rb +24 -0
  100. data/motion/core_ext/time/acts_like.rb +8 -0
  101. data/motion/core_ext/time/calculations.rb +215 -0
  102. data/motion/core_ext/time/conversions.rb +52 -0
  103. data/motion/descendants_tracker.rb +50 -0
  104. data/motion/duration.rb +104 -0
  105. data/motion/hash_with_indifferent_access.rb +253 -0
  106. data/motion/inflections.rb +67 -0
  107. data/motion/inflector/inflections.rb +203 -0
  108. data/motion/inflector/methods.rb +321 -0
  109. data/motion/logger.rb +47 -0
  110. data/motion/number_helper.rb +54 -0
  111. data/motion/version.rb +3 -0
  112. data/motion_blender-support.gemspec +21 -0
  113. data/spec/motion-support/_helpers/constantize_test_cases.rb +75 -0
  114. data/spec/motion-support/_helpers/inflector_test_cases.rb +270 -0
  115. data/spec/motion-support/callback_spec.rb +702 -0
  116. data/spec/motion-support/concern_spec.rb +93 -0
  117. data/spec/motion-support/core_ext/array/access_spec.rb +29 -0
  118. data/spec/motion-support/core_ext/array/conversion_spec.rb +60 -0
  119. data/spec/motion-support/core_ext/array/extract_options_spec.rb +15 -0
  120. data/spec/motion-support/core_ext/array/grouping_spec.rb +85 -0
  121. data/spec/motion-support/core_ext/array/prepend_and_append_spec.rb +25 -0
  122. data/spec/motion-support/core_ext/array/wrap_spec.rb +19 -0
  123. data/spec/motion-support/core_ext/array_spec.rb +16 -0
  124. data/spec/motion-support/core_ext/class/attribute_accessor_spec.rb +127 -0
  125. data/spec/motion-support/core_ext/class/attribute_spec.rb +92 -0
  126. data/spec/motion-support/core_ext/date/acts_like_spec.rb +11 -0
  127. data/spec/motion-support/core_ext/date/calculation_spec.rb +186 -0
  128. data/spec/motion-support/core_ext/date/conversion_spec.rb +18 -0
  129. data/spec/motion-support/core_ext/date_and_time/calculation_spec.rb +336 -0
  130. data/spec/motion-support/core_ext/enumerable_spec.rb +130 -0
  131. data/spec/motion-support/core_ext/hash/deep_delete_if_spec.rb +19 -0
  132. data/spec/motion-support/core_ext/hash/deep_merge_spec.rb +32 -0
  133. data/spec/motion-support/core_ext/hash/except_spec.rb +43 -0
  134. data/spec/motion-support/core_ext/hash/key_spec.rb +236 -0
  135. data/spec/motion-support/core_ext/hash/reverse_merge_spec.rb +26 -0
  136. data/spec/motion-support/core_ext/hash/slice_spec.rb +61 -0
  137. data/spec/motion-support/core_ext/integer/inflection_spec.rb +23 -0
  138. data/spec/motion-support/core_ext/integer/multiple_spec.rb +19 -0
  139. data/spec/motion-support/core_ext/kernel/singleton_class_spec.rb +9 -0
  140. data/spec/motion-support/core_ext/metaclass_spec.rb +9 -0
  141. data/spec/motion-support/core_ext/module/aliasing_spec.rb +143 -0
  142. data/spec/motion-support/core_ext/module/anonymous_spec.rb +29 -0
  143. data/spec/motion-support/core_ext/module/attr_internal_spec.rb +104 -0
  144. data/spec/motion-support/core_ext/module/attribute_accessor_spec.rb +86 -0
  145. data/spec/motion-support/core_ext/module/delegation_spec.rb +136 -0
  146. data/spec/motion-support/core_ext/module/introspection_spec.rb +70 -0
  147. data/spec/motion-support/core_ext/module/reachable_spec.rb +61 -0
  148. data/spec/motion-support/core_ext/module/remove_method_spec.rb +25 -0
  149. data/spec/motion-support/core_ext/numeric/bytes_spec.rb +43 -0
  150. data/spec/motion-support/core_ext/numeric/conversions_spec.rb +40 -0
  151. data/spec/motion-support/core_ext/object/acts_like_spec.rb +21 -0
  152. data/spec/motion-support/core_ext/object/blank_spec.rb +54 -0
  153. data/spec/motion-support/core_ext/object/deep_dup_spec.rb +54 -0
  154. data/spec/motion-support/core_ext/object/duplicable_spec.rb +31 -0
  155. data/spec/motion-support/core_ext/object/inclusion_spec.rb +34 -0
  156. data/spec/motion-support/core_ext/object/instance_variable_spec.rb +19 -0
  157. data/spec/motion-support/core_ext/object/to_param_spec.rb +75 -0
  158. data/spec/motion-support/core_ext/object/to_query_spec.rb +37 -0
  159. data/spec/motion-support/core_ext/object/try_spec.rb +92 -0
  160. data/spec/motion-support/core_ext/range/include_range_spec.rb +31 -0
  161. data/spec/motion-support/core_ext/range/overlap_spec.rb +43 -0
  162. data/spec/motion-support/core_ext/regexp_spec.rb +7 -0
  163. data/spec/motion-support/core_ext/string/access_spec.rb +53 -0
  164. data/spec/motion-support/core_ext/string/behavior_spec.rb +7 -0
  165. data/spec/motion-support/core_ext/string/exclude_spec.rb +8 -0
  166. data/spec/motion-support/core_ext/string/filter_spec.rb +49 -0
  167. data/spec/motion-support/core_ext/string/indent_spec.rb +56 -0
  168. data/spec/motion-support/core_ext/string/inflection_spec.rb +142 -0
  169. data/spec/motion-support/core_ext/string/starts_end_with_spec.rb +14 -0
  170. data/spec/motion-support/core_ext/string/strip_spec.rb +34 -0
  171. data/spec/motion-support/core_ext/string_spec.rb +88 -0
  172. data/spec/motion-support/core_ext/time/acts_like_spec.rb +11 -0
  173. data/spec/motion-support/core_ext/time/calculation_spec.rb +201 -0
  174. data/spec/motion-support/core_ext/time/conversion_spec.rb +53 -0
  175. data/spec/motion-support/descendants_tracker_spec.rb +58 -0
  176. data/spec/motion-support/duration_spec.rb +107 -0
  177. data/spec/motion-support/hash_with_indifferent_access_spec.rb +605 -0
  178. data/spec/motion-support/inflector_spec.rb +504 -0
  179. data/spec/motion-support/ns_dictionary_spec.rb +89 -0
  180. data/spec/motion-support/ns_string_spec.rb +182 -0
  181. data/spec/motion-support/number_helper_spec.rb +55 -0
  182. metadata +352 -0
@@ -0,0 +1,104 @@
1
+ describe "module" do
2
+ describe "attr_internal" do
3
+ before do
4
+ @target = Class.new
5
+ @instance = @target.new
6
+ end
7
+
8
+ describe "reader" do
9
+ it "should define reader" do
10
+ lambda { @target.attr_internal_reader :foo }.should.not.raise
11
+ end
12
+
13
+ describe "defined" do
14
+ before do
15
+ @target.attr_internal_reader :foo
16
+ end
17
+
18
+ it "should not have instance variable at first" do
19
+ @instance.instance_variable_defined?('@_foo').should.not.be.true
20
+ end
21
+
22
+ it "should not define writer" do
23
+ lambda { @instance.foo = 1 }.should.raise NoMethodError
24
+ end
25
+
26
+ it "should return value of internal reader" do
27
+ @instance.instance_variable_set('@_foo', 1)
28
+ lambda { @instance.foo.should == 1 }.should.not.raise
29
+ end
30
+ end
31
+ end
32
+
33
+ describe "writer" do
34
+ it "should define writer" do
35
+ lambda { @target.attr_internal_writer :foo }.should.not.raise
36
+ end
37
+
38
+ describe "defined" do
39
+ before do
40
+ @target.attr_internal_writer :foo
41
+ end
42
+
43
+ it "should not have instance variable at first" do
44
+ @instance.instance_variable_defined?('@_foo').should.not.be.true
45
+ end
46
+
47
+ it "should set instance variable" do
48
+ lambda { @instance.foo = 1 }.should.not.raise
49
+ @instance.instance_variable_defined?('@_foo').should.be.true
50
+ @instance.instance_variable_get('@_foo').should == 1
51
+ end
52
+
53
+ it "should not define reader" do
54
+ lambda { @instance.foo }.should.raise NoMethodError
55
+ end
56
+ end
57
+ end
58
+
59
+ describe "accessor" do
60
+ it "should define accessor" do
61
+ lambda { @target.attr_internal :foo }.should.not.raise
62
+ end
63
+
64
+ describe "defined" do
65
+ before do
66
+ @target.attr_internal :foo
67
+ end
68
+
69
+ it "should not have instance variable at first" do
70
+ @instance.instance_variable_defined?('@_foo').should.not.be.true
71
+ end
72
+
73
+ it "should set instance variable" do
74
+ lambda { @instance.foo = 1 }.should.not.raise
75
+ @instance.instance_variable_defined?('@_foo').should.be.true
76
+ @instance.instance_variable_get('@_foo').should == 1
77
+ end
78
+
79
+ it "should read from instance variable" do
80
+ @instance.instance_variable_set('@_foo', 1)
81
+ lambda { @instance.foo }.should.not.raise
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "naming format" do
87
+ it "should allow custom naming format for instance variable" do
88
+ begin
89
+ Module.attr_internal_naming_format.should == '@_%s'
90
+ lambda { Module.attr_internal_naming_format = '@abc%sdef' }.should.not.raise
91
+ @target.attr_internal :foo
92
+
93
+ @instance.instance_variable_defined?('@_foo').should.be.false
94
+ @instance.instance_variable_defined?('@abcfoodef').should.be.false
95
+ lambda { @instance.foo = 1 }.should.not.raise
96
+ @instance.instance_variable_defined?('@_foo').should.not.be.true
97
+ @instance.instance_variable_defined?('@abcfoodef').should.be.true
98
+ ensure
99
+ Module.attr_internal_naming_format = '@_%s'
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,86 @@
1
+ describe "module" do
2
+ describe "attribute accessors" do
3
+ before do
4
+ m = @module = Module.new
5
+ @module.instance_eval do
6
+ mattr_accessor :foo
7
+ mattr_accessor :bar, :instance_writer => false
8
+ mattr_reader :shaq, :instance_reader => false
9
+ mattr_accessor :camp, :instance_accessor => false
10
+ end
11
+ @class = Class.new
12
+ @class.instance_eval { include m }
13
+ @object = @class.new
14
+ end
15
+
16
+ describe "reader" do
17
+ it "should return nil by default" do
18
+ @module.foo.should.be.nil
19
+ end
20
+ end
21
+
22
+ describe "writer" do
23
+ it "should set value" do
24
+ @module.foo = :test
25
+ @module.foo.should == :test
26
+ end
27
+
28
+ it "should set value through instance writer" do
29
+ @object.foo = :bar
30
+ @object.foo.should == :bar
31
+ end
32
+
33
+ it "should set instance reader's value through module's writer" do
34
+ @module.foo = :test
35
+ @object.foo.should == :test
36
+ end
37
+
38
+ it "should set module reader's value through instances's writer" do
39
+ @object.foo = :bar
40
+ @module.foo.should == :bar
41
+ end
42
+ end
43
+
44
+ describe "instance_writer => false" do
45
+ it "should not create instance writer" do
46
+ @module.should.respond_to :foo
47
+ @module.should.respond_to :foo=
48
+ @object.should.respond_to :bar
49
+ @object.should.not.respond_to :bar=
50
+ end
51
+ end
52
+
53
+ describe "instance_reader => false" do
54
+ it "should not create instance reader" do
55
+ @module.should.respond_to :shaq
56
+ @object.should.not.respond_to :shaq
57
+ end
58
+ end
59
+
60
+ describe "instance_accessor => false" do
61
+ it "should not create reader or writer" do
62
+ @module.should.respond_to :camp
63
+ @object.should.not.respond_to :camp
64
+ @object.should.not.respond_to :camp=
65
+ end
66
+ end
67
+ end
68
+
69
+ describe "invalid attribute accessors" do
70
+ it "should raise NameError when creating an invalid reader" do
71
+ lambda do
72
+ Class.new do
73
+ mattr_reader "invalid attribute name"
74
+ end
75
+ end.should.raise NameError
76
+ end
77
+
78
+ it "should raise NameError when creating an invalid writer" do
79
+ lambda do
80
+ Class.new do
81
+ mattr_writer "invalid attribute name"
82
+ end
83
+ end.should.raise NameError
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,136 @@
1
+ module ModuleDelegation
2
+ class Delegator
3
+ def receive
4
+ "result"
5
+ end
6
+ end
7
+
8
+ class InstanceVariable
9
+ def initialize
10
+ @instance_variable = Delegator.new
11
+ end
12
+ delegate :receive, :to => '@instance_variable'
13
+ end
14
+
15
+ class ClassVariable
16
+ def initialize
17
+ @@class_variable = Delegator.new
18
+ end
19
+ delegate :receive, :to => '@@class_variable'
20
+ end
21
+
22
+ class RelativeConstant
23
+ Receiver = Delegator.new
24
+ delegate :receive, :to => 'Receiver'
25
+ end
26
+
27
+ class AbsoluteConstant
28
+ Receiver = Delegator.new
29
+ delegate :receive, :to => 'ModuleDelegation::AbsoluteConstant::Receiver'
30
+ end
31
+
32
+ class Method
33
+ def method
34
+ Delegator.new
35
+ end
36
+ delegate :receive, :to => :method
37
+ end
38
+
39
+ class MethodChain
40
+ def first
41
+ self
42
+ end
43
+
44
+ def second
45
+ Delegator.new
46
+ end
47
+ delegate :receive, :to => 'first.second'
48
+ end
49
+
50
+ class ConstantSingleton
51
+ class Nested
52
+ def self.method
53
+ Delegator.new
54
+ end
55
+ end
56
+ delegate :receive, :to => 'Nested.method'
57
+ end
58
+
59
+ class AllowNil
60
+ delegate :receive, :to => '@undefined', :allow_nil => true
61
+ end
62
+
63
+ class DisallowNil
64
+ delegate :receive, :to => '@undefined'
65
+ end
66
+
67
+ class NonexistentMethod
68
+ delegate :receive, :to => :nonexistent_method
69
+ end
70
+
71
+ class Prefix
72
+ def method
73
+ Delegator.new
74
+ end
75
+ delegate :receive, :to => :method, :prefix => 'foo'
76
+ end
77
+
78
+ class AutoPrefix
79
+ def method
80
+ Delegator.new
81
+ end
82
+ delegate :receive, :to => :method, :prefix => true
83
+ end
84
+ end
85
+
86
+ describe "module" do
87
+ describe "delegation" do
88
+ it "should delegate to instance variable" do
89
+ ModuleDelegation::InstanceVariable.new.receive.should == "result"
90
+ end
91
+
92
+ it "should delegate to class variable" do
93
+ ModuleDelegation::ClassVariable.new.receive.should == "result"
94
+ end
95
+
96
+ it "should delegate to relative constant" do
97
+ ModuleDelegation::RelativeConstant.new.receive.should == "result"
98
+ end
99
+
100
+ it "should delegate to absolute constant" do
101
+ ModuleDelegation::AbsoluteConstant.new.receive.should == "result"
102
+ end
103
+
104
+ it "should delegate to method" do
105
+ ModuleDelegation::Method.new.receive.should == "result"
106
+ end
107
+
108
+ it "should delegate to method chain" do
109
+ ModuleDelegation::MethodChain.new.receive.should == "result"
110
+ end
111
+
112
+ it "should delegate to constant's singleton method" do
113
+ ModuleDelegation::ConstantSingleton.new.receive.should == "result"
114
+ end
115
+
116
+ it "should delegate to nil without raising an error when allowed" do
117
+ ModuleDelegation::AllowNil.new.receive.should.be.nil
118
+ end
119
+
120
+ it "should raise an error when delegating to nil and not allowed" do
121
+ lambda { ModuleDelegation::DisallowNil.new.receive }.should.raise
122
+ end
123
+
124
+ it "should raise an error when delegating to non-existent method" do
125
+ lambda { ModuleDelegation::NonexistentMethod.new.receive }.should.raise NoMethodError
126
+ end
127
+
128
+ it "should delegate with prefix" do
129
+ ModuleDelegation::Prefix.new.foo_receive.should == "result"
130
+ end
131
+
132
+ it "should delegate to method with automatic prefix" do
133
+ ModuleDelegation::AutoPrefix.new.method_receive.should == "result"
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,70 @@
1
+ module ModuleIntrospection
2
+ module Foo
3
+ module Bar
4
+ end
5
+ end
6
+ module Baz
7
+ end
8
+ end
9
+ ModuleIntrospectionAlias = ModuleIntrospection::Foo
10
+
11
+ describe "module" do
12
+ describe "introspection" do
13
+ describe "parent_name" do
14
+ it "should return nil for top-level module" do
15
+ ModuleIntrospection.parent_name.should.be.nil
16
+ end
17
+
18
+ it "should return direct parent for nested module" do
19
+ ModuleIntrospection::Foo.parent_name.should == "ModuleIntrospection"
20
+ end
21
+
22
+ it "should return nil for anonymous module" do
23
+ Module.new.parent_name.should.be.nil
24
+ end
25
+
26
+ it "should return original parent name for module alias" do
27
+ ModuleIntrospectionAlias.parent_name.should == "ModuleIntrospection"
28
+ end
29
+ end
30
+
31
+ describe "parent" do
32
+ it "should return Object for top-level module" do
33
+ ModuleIntrospection.parent.should == Object
34
+ end
35
+
36
+ it "should return direct parent for nested module" do
37
+ ModuleIntrospection::Foo.parent.should == ModuleIntrospection
38
+ end
39
+
40
+ it "should return nil for anonymous module" do
41
+ Module.new.parent.should == Object
42
+ end
43
+
44
+ it "should return original parent for module alias" do
45
+ ModuleIntrospectionAlias.parent.should == ModuleIntrospection
46
+ end
47
+ end
48
+
49
+ describe "parents" do
50
+ it "should return Object for top-level module" do
51
+ ModuleIntrospection.parents.should == [Object]
52
+ end
53
+
54
+ it "should return parents for nested module" do
55
+ ModuleIntrospection::Foo::Bar.parents.should == [ModuleIntrospection::Foo, ModuleIntrospection, Object]
56
+ end
57
+
58
+ it "should return original parents for module alias" do
59
+ ModuleIntrospectionAlias.parents.should == [ModuleIntrospection, Object]
60
+ end
61
+ end
62
+
63
+ describe "local_constants" do
64
+ it "should return all direct constants within the module as symbols" do
65
+ ModuleIntrospection.local_constants.should.include :Foo
66
+ ModuleIntrospection.local_constants.should.include :Baz
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,61 @@
1
+ describe "Module" do
2
+ describe "reachable?" do
3
+ it "should be false for an anonymous module" do
4
+ Module.new.should.not.be.reachable
5
+ end
6
+
7
+ it "should be false for an anonymous class" do
8
+ Class.new.should.not.be.reachable
9
+ end
10
+
11
+ it "should be true for a named module" do
12
+ Kernel.should.be.reachable
13
+ end
14
+
15
+ it "should be true for a named class" do
16
+ Object.should.be.reachable
17
+ end
18
+
19
+ it "should be false for a named class whose constant has gone" do
20
+ class C; end
21
+ c = C
22
+
23
+ Object.send(:remove_const, :C)
24
+
25
+ c.should.not.be.reachable
26
+ end
27
+
28
+ it "should be false for a named module whose constant has gone" do
29
+ module M; end
30
+ m = M
31
+
32
+ Object.send(:remove_const, :M)
33
+
34
+ m.should.not.be.reachable
35
+ end
36
+
37
+ it "should be false for a named class whose constant was redefined" do
38
+ class C; end
39
+ c = C
40
+
41
+ Object.send(:remove_const, :C)
42
+
43
+ class C; end
44
+
45
+ C.should.be.reachable
46
+ c.should.not.be.reachable
47
+ end
48
+
49
+ it "should be false for a named module whose constant was redefined" do
50
+ module M; end
51
+ m = M
52
+
53
+ Object.send(:remove_const, :M)
54
+
55
+ module M; end
56
+
57
+ M.should.be.reachable
58
+ m.should.not.be.reachable
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,25 @@
1
+ module RemoveMethodSpec
2
+ class A
3
+ def do_something
4
+ return 1
5
+ end
6
+ end
7
+ end
8
+
9
+ describe "module" do
10
+ describe "remove_method" do
11
+ it "should remove a method from an object" do
12
+ RemoveMethodSpec::A.class_eval {
13
+ self.remove_possible_method(:do_something)
14
+ }
15
+ RemoveMethodSpec::A.new.should.not.respond_to :do_something
16
+ end
17
+
18
+ def test_redefine_method_in_an_object
19
+ RemoveMethodSpec::A.class_eval {
20
+ self.redefine_method(:do_something) { return 100 }
21
+ }
22
+ RemoveMethodSpec::A.new.do_something.should == 100
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,43 @@
1
+ describe "Numeric" do
2
+ describe "bytes" do
3
+ it "should calculate kilobytes" do
4
+ 1.kilobyte.should == 1024.bytes
5
+ 3.kilobytes.should == 3072
6
+ 3.kilobyte.should == 3072
7
+ end
8
+
9
+ it "should calculate megabytes" do
10
+ 1.megabyte.should == 1024.kilobytes
11
+ 3.5.megabytes.should == 3584.0.kilobytes
12
+ 3.megabytes.should == 1024.kilobytes + 2.megabytes
13
+ 512.megabytes.should == 2.gigabytes / 4
14
+ 3.megabytes.should == 3145728
15
+ 3.megabyte.should == 3145728
16
+ end
17
+
18
+ it "should calculate gigabytes" do
19
+ 3.5.gigabytes.should == 3584.0.megabytes
20
+ 10.gigabytes.should == 256.megabytes * 20 + 5.gigabytes
21
+ 3.gigabytes.should == 3221225472
22
+ 3.gigabyte.should == 3221225472
23
+ end
24
+
25
+ it "should calculate terabytes" do
26
+ 1.terabyte.should == 1.kilobyte ** 4
27
+ 3.terabytes.should == 3298534883328
28
+ 3.terabyte.should == 3298534883328
29
+ end
30
+
31
+ it "should calculate petabytes" do
32
+ 1.petabyte.should == 1.kilobyte ** 5
33
+ 3.petabytes.should == 3377699720527872
34
+ 3.petabyte.should == 3377699720527872
35
+ end
36
+
37
+ it "should calculate exabytes" do
38
+ 1.exabyte.should == 1.kilobyte ** 6
39
+ 3.exabytes.should == 3458764513820540928
40
+ 3.exabyte.should == 3458764513820540928
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,40 @@
1
+ describe "Numeric" do
2
+ describe "conversions" do
3
+ describe "phone" do
4
+ it "should format phone number without area code" do
5
+ 5551234.to_s(:phone).should == "555-1234"
6
+ end
7
+
8
+ it "should format long number without area code" do
9
+ 225551212.to_s(:phone).should == "22-555-1212"
10
+ 8005551212.to_s(:phone).should == "800-555-1212"
11
+ end
12
+
13
+ it "should format phone number with area code" do
14
+ 8005551212.to_s(:phone, :area_code => true).should == "(800) 555-1212"
15
+ end
16
+
17
+ it "should format phone number with custom delimiter" do
18
+ 8005551212.to_s(:phone, :delimiter => " ").should == "800 555 1212"
19
+ 5551212.to_s(:phone, :delimiter => '.').should == "555.1212"
20
+ end
21
+
22
+ it "should append extension to phone number" do
23
+ 8005551212.to_s(:phone, :area_code => true, :extension => 123).should == "(800) 555-1212 x 123"
24
+ end
25
+
26
+ it "should not append whitespace as extension to the phone number" do
27
+ 8005551212.to_s(:phone, :extension => " ").should == "800-555-1212"
28
+ end
29
+
30
+ it "should format phone number with country code" do
31
+ 8005551212.to_s(:phone, :country_code => 1).should == "+1-800-555-1212"
32
+ 225551212.to_s(:phone, :country_code => 45).should == "+45-22-555-1212"
33
+ end
34
+
35
+ it "should format phone number with country code and empty delimiter" do
36
+ 8005551212.to_s(:phone, :country_code => 1, :delimiter => '').should == "+18005551212"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,21 @@
1
+ class DuckFoo
2
+ def acts_like_foo?
3
+ true
4
+ end
5
+ end
6
+
7
+ describe "Object" do
8
+ describe "acts_like" do
9
+ it "should not act like anything" do
10
+ object = Object.new
11
+ object.should.not.acts_like(:time)
12
+ object.should.not.acts_like(:date)
13
+ object.should.not.acts_like(:foo)
14
+ end
15
+
16
+ it "should allow subclasses to act like something" do
17
+ object = DuckFoo.new
18
+ object.should.acts_like(:foo)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,54 @@
1
+ class EmptyTrue
2
+ def empty?() true; end
3
+ end
4
+
5
+ class EmptyFalse
6
+ def empty?() false; end
7
+ end
8
+
9
+ BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", ' ', [], {} ]
10
+ NOT = [ EmptyFalse.new, Object.new, true, 0, 1, 'a', [nil], { nil => 0 } ]
11
+
12
+ describe "blank" do
13
+ describe "blank?" do
14
+ BLANK.each do |v|
15
+ it "should treat #{v.inspect} as blank" do
16
+ v.should.be.blank
17
+ end
18
+ end
19
+
20
+ NOT.each do |v|
21
+ it "should treat #{v.inspect} as NOT blank" do
22
+ v.should.not.be.blank
23
+ end
24
+ end
25
+ end
26
+
27
+ describe "present?" do
28
+ BLANK.each do |v|
29
+ it "should treat #{v.inspect} as NOT present" do
30
+ v.should.not.be.present
31
+ end
32
+ end
33
+
34
+ NOT.each do |v|
35
+ it "should treat #{v.inspect} as present" do
36
+ v.should.be.present
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "presence" do
42
+ BLANK.each do |v|
43
+ it "should return nil for #{v.inspect}.presence" do
44
+ v.presence.should.be.nil
45
+ end
46
+ end
47
+
48
+ NOT.each do |v|
49
+ it "should return self for #{v.inspect}.presence" do
50
+ v.presence.should == v
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,54 @@
1
+ describe "deep_dup" do
2
+ describe "array" do
3
+ it "should deep_dup nested array" do
4
+ array = [1, [2, 3]]
5
+ dup = array.deep_dup
6
+ dup[1][2] = 4
7
+ array[1][2].should.be.nil
8
+ dup[1][2].should == 4
9
+ end
10
+
11
+ it "should deep_dup array with hash inside" do
12
+ array = [1, { :a => 2, :b => 3 } ]
13
+ dup = array.deep_dup
14
+ dup[1][:c] = 4
15
+ array[1][:c].should.be.nil
16
+ dup[1][:c].should == 4
17
+ end
18
+ end
19
+
20
+ describe "hash" do
21
+ it "should deep_dup nested hash" do
22
+ hash = { :a => { :b => 'b' } }
23
+ dup = hash.deep_dup
24
+ dup[:a][:c] = 'c'
25
+ hash[:a][:c].should.be.nil
26
+ dup[:a][:c].should == 'c'
27
+ end
28
+
29
+ it "should deep_dup hash with array inside" do
30
+ hash = { :a => [1, 2] }
31
+ dup = hash.deep_dup
32
+ dup[:a][2] = 'c'
33
+ hash[:a][2].should.be.nil
34
+ dup[:a][2].should == 'c'
35
+ end
36
+
37
+ it "should deep_dup hash with init value" do
38
+ zero_hash = Hash.new 0
39
+ hash = { :a => zero_hash }
40
+ dup = hash.deep_dup
41
+ dup[:a][44].should == 0
42
+ end
43
+ end
44
+
45
+ describe "Object" do
46
+ it "should deep_dup object" do
47
+ object = Object.new
48
+ dup = object.deep_dup
49
+ dup.instance_variable_set(:@a, 1)
50
+ object.should.not.be.instance_variable_defined(:@a)
51
+ dup.should.be.instance_variable_defined(:@a)
52
+ end
53
+ end
54
+ end