activesupport 3.1.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activesupport might be problematic. Click here for more details.

Files changed (276) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +798 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +13 -7
  5. data/lib/active_support/array_inquirer.rb +44 -0
  6. data/lib/active_support/backtrace_cleaner.rb +38 -34
  7. data/lib/active_support/benchmarkable.rb +17 -28
  8. data/lib/active_support/cache/file_store.rb +85 -70
  9. data/lib/active_support/cache/mem_cache_store.rb +75 -66
  10. data/lib/active_support/cache/memory_store.rb +31 -23
  11. data/lib/active_support/cache/null_store.rb +41 -0
  12. data/lib/active_support/cache/strategy/local_cache.rb +73 -70
  13. data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
  14. data/lib/active_support/cache.rb +360 -294
  15. data/lib/active_support/callbacks.rb +563 -393
  16. data/lib/active_support/concern.rb +42 -34
  17. data/lib/active_support/concurrency/latch.rb +19 -0
  18. data/lib/active_support/concurrency/share_lock.rb +186 -0
  19. data/lib/active_support/configurable.rb +70 -12
  20. data/lib/active_support/core_ext/array/access.rb +53 -9
  21. data/lib/active_support/core_ext/array/conversions.rb +109 -62
  22. data/lib/active_support/core_ext/array/extract_options.rb +2 -2
  23. data/lib/active_support/core_ext/array/grouping.rb +39 -32
  24. data/lib/active_support/core_ext/array/inquiry.rb +17 -0
  25. data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
  26. data/lib/active_support/core_ext/array/wrap.rb +16 -18
  27. data/lib/active_support/core_ext/array.rb +2 -2
  28. data/lib/active_support/core_ext/benchmark.rb +7 -0
  29. data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -36
  30. data/lib/active_support/core_ext/class/attribute.rb +47 -34
  31. data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -79
  32. data/lib/active_support/core_ext/class/subclasses.rb +12 -7
  33. data/lib/active_support/core_ext/class.rb +0 -3
  34. data/lib/active_support/core_ext/date/blank.rb +12 -0
  35. data/lib/active_support/core_ext/date/calculations.rb +57 -167
  36. data/lib/active_support/core_ext/date/conversions.rb +31 -42
  37. data/lib/active_support/core_ext/date/zones.rb +2 -10
  38. data/lib/active_support/core_ext/date.rb +5 -0
  39. data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
  40. data/lib/active_support/core_ext/date_and_time/compatibility.rb +18 -0
  41. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  42. data/lib/active_support/core_ext/date_time/acts_like.rb +1 -0
  43. data/lib/active_support/core_ext/date_time/blank.rb +12 -0
  44. data/lib/active_support/core_ext/date_time/calculations.rb +132 -65
  45. data/lib/active_support/core_ext/date_time/compatibility.rb +5 -0
  46. data/lib/active_support/core_ext/date_time/conversions.rb +36 -34
  47. data/lib/active_support/core_ext/date_time.rb +5 -0
  48. data/lib/active_support/core_ext/digest/uuid.rb +51 -0
  49. data/lib/active_support/core_ext/enumerable.rb +81 -74
  50. data/lib/active_support/core_ext/file/atomic.rb +53 -26
  51. data/lib/active_support/core_ext/file.rb +0 -1
  52. data/lib/active_support/core_ext/hash/compact.rb +20 -0
  53. data/lib/active_support/core_ext/hash/conversions.rb +175 -70
  54. data/lib/active_support/core_ext/hash/deep_merge.rb +30 -8
  55. data/lib/active_support/core_ext/hash/except.rb +11 -12
  56. data/lib/active_support/core_ext/hash/indifferent_access.rb +7 -8
  57. data/lib/active_support/core_ext/hash/keys.rb +147 -24
  58. data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
  59. data/lib/active_support/core_ext/hash/slice.rb +22 -14
  60. data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
  61. data/lib/active_support/core_ext/hash.rb +2 -2
  62. data/lib/active_support/core_ext/integer/inflections.rb +13 -1
  63. data/lib/active_support/core_ext/integer/multiple.rb +4 -0
  64. data/lib/active_support/core_ext/integer/time.rb +12 -22
  65. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -2
  66. data/lib/active_support/core_ext/kernel/concern.rb +12 -0
  67. data/lib/active_support/core_ext/kernel/debugger.rb +2 -15
  68. data/lib/active_support/core_ext/kernel/reporting.rb +12 -62
  69. data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
  70. data/lib/active_support/core_ext/kernel.rb +2 -3
  71. data/lib/active_support/core_ext/load_error.rb +14 -7
  72. data/lib/active_support/core_ext/marshal.rb +22 -0
  73. data/lib/active_support/core_ext/module/aliasing.rb +16 -12
  74. data/lib/active_support/core_ext/module/anonymous.rb +12 -8
  75. data/lib/active_support/core_ext/module/attr_internal.rb +2 -5
  76. data/lib/active_support/core_ext/module/attribute_accessors.rb +165 -13
  77. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
  78. data/lib/active_support/core_ext/module/concerning.rb +135 -0
  79. data/lib/active_support/core_ext/module/delegation.rb +141 -68
  80. data/lib/active_support/core_ext/module/deprecation.rb +17 -3
  81. data/lib/active_support/core_ext/module/introspection.rb +9 -31
  82. data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
  83. data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
  84. data/lib/active_support/core_ext/module/reachable.rb +1 -3
  85. data/lib/active_support/core_ext/module/remove_method.rb +24 -5
  86. data/lib/active_support/core_ext/module.rb +3 -3
  87. data/lib/active_support/core_ext/name_error.rb +15 -2
  88. data/lib/active_support/core_ext/numeric/bytes.rb +20 -0
  89. data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
  90. data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
  91. data/lib/active_support/core_ext/numeric/time.rb +31 -36
  92. data/lib/active_support/core_ext/numeric.rb +2 -0
  93. data/lib/active_support/core_ext/object/acts_like.rb +4 -4
  94. data/lib/active_support/core_ext/object/blank.rb +52 -18
  95. data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
  96. data/lib/active_support/core_ext/object/duplicable.rb +12 -20
  97. data/lib/active_support/core_ext/object/inclusion.rb +13 -1
  98. data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
  99. data/lib/active_support/core_ext/object/json.rb +205 -0
  100. data/lib/active_support/core_ext/object/to_param.rb +1 -55
  101. data/lib/active_support/core_ext/object/to_query.rb +66 -9
  102. data/lib/active_support/core_ext/object/try.rb +124 -33
  103. data/lib/active_support/core_ext/object/with_options.rb +37 -11
  104. data/lib/active_support/core_ext/object.rb +2 -1
  105. data/lib/active_support/core_ext/range/conversions.rb +17 -7
  106. data/lib/active_support/core_ext/range/each.rb +21 -0
  107. data/lib/active_support/core_ext/range/include_range.rb +20 -18
  108. data/lib/active_support/core_ext/range/overlaps.rb +1 -1
  109. data/lib/active_support/core_ext/range.rb +1 -2
  110. data/lib/active_support/core_ext/securerandom.rb +23 -0
  111. data/lib/active_support/core_ext/string/access.rb +95 -90
  112. data/lib/active_support/core_ext/string/behavior.rb +1 -1
  113. data/lib/active_support/core_ext/string/conversions.rb +41 -38
  114. data/lib/active_support/core_ext/string/exclude.rb +6 -1
  115. data/lib/active_support/core_ext/string/filters.rb +70 -17
  116. data/lib/active_support/core_ext/string/indent.rb +43 -0
  117. data/lib/active_support/core_ext/string/inflections.rb +139 -59
  118. data/lib/active_support/core_ext/string/inquiry.rb +2 -2
  119. data/lib/active_support/core_ext/string/multibyte.rb +46 -65
  120. data/lib/active_support/core_ext/string/output_safety.rb +153 -56
  121. data/lib/active_support/core_ext/string/strip.rb +3 -6
  122. data/lib/active_support/core_ext/string/zones.rb +14 -0
  123. data/lib/active_support/core_ext/string.rb +2 -3
  124. data/lib/active_support/core_ext/struct.rb +3 -0
  125. data/lib/active_support/core_ext/time/calculations.rb +173 -173
  126. data/lib/active_support/core_ext/time/compatibility.rb +5 -0
  127. data/lib/active_support/core_ext/time/conversions.rb +33 -29
  128. data/lib/active_support/core_ext/time/marshal.rb +2 -56
  129. data/lib/active_support/core_ext/time/zones.rb +57 -32
  130. data/lib/active_support/core_ext/time.rb +5 -0
  131. data/lib/active_support/core_ext/uri.rb +13 -19
  132. data/lib/active_support/core_ext.rb +3 -2
  133. data/lib/active_support/dependencies/autoload.rb +47 -20
  134. data/lib/active_support/dependencies/interlock.rb +51 -0
  135. data/lib/active_support/dependencies.rb +315 -265
  136. data/lib/active_support/deprecation/behaviors.rb +71 -30
  137. data/lib/active_support/deprecation/instance_delegator.rb +24 -0
  138. data/lib/active_support/deprecation/method_wrappers.rb +59 -18
  139. data/lib/active_support/deprecation/proxy_wrappers.rb +82 -14
  140. data/lib/active_support/deprecation/reporting.rb +61 -14
  141. data/lib/active_support/deprecation.rb +38 -13
  142. data/lib/active_support/descendants_tracker.rb +34 -19
  143. data/lib/active_support/duration/iso8601_parser.rb +122 -0
  144. data/lib/active_support/duration/iso8601_serializer.rb +51 -0
  145. data/lib/active_support/duration.rb +85 -14
  146. data/lib/active_support/evented_file_update_checker.rb +194 -0
  147. data/lib/active_support/execution_wrapper.rb +117 -0
  148. data/lib/active_support/executor.rb +6 -0
  149. data/lib/active_support/file_update_checker.rb +138 -17
  150. data/lib/active_support/gem_version.rb +15 -0
  151. data/lib/active_support/gzip.rb +11 -5
  152. data/lib/active_support/hash_with_indifferent_access.rb +199 -49
  153. data/lib/active_support/i18n.rb +6 -2
  154. data/lib/active_support/i18n_railtie.rb +40 -21
  155. data/lib/active_support/inflections.rb +22 -13
  156. data/lib/active_support/inflector/inflections.rb +175 -144
  157. data/lib/active_support/inflector/methods.rb +328 -91
  158. data/lib/active_support/inflector/transliterate.rb +51 -37
  159. data/lib/active_support/json/decoding.rb +31 -22
  160. data/lib/active_support/json/encoding.rb +88 -248
  161. data/lib/active_support/key_generator.rb +71 -0
  162. data/lib/active_support/lazy_load_hooks.rb +27 -25
  163. data/lib/active_support/locale/en.yml +102 -3
  164. data/lib/active_support/log_subscriber/test_helper.rb +24 -21
  165. data/lib/active_support/log_subscriber.rb +36 -49
  166. data/lib/active_support/logger.rb +106 -0
  167. data/lib/active_support/logger_silence.rb +28 -0
  168. data/lib/active_support/logger_thread_safe_level.rb +31 -0
  169. data/lib/active_support/message_encryptor.rb +72 -36
  170. data/lib/active_support/message_verifier.rb +96 -24
  171. data/lib/active_support/multibyte/chars.rb +88 -333
  172. data/lib/active_support/multibyte/unicode.rb +156 -136
  173. data/lib/active_support/multibyte.rb +5 -28
  174. data/lib/active_support/notifications/fanout.rb +115 -19
  175. data/lib/active_support/notifications/instrumenter.rb +52 -15
  176. data/lib/active_support/notifications.rb +168 -33
  177. data/lib/active_support/number_helper/number_converter.rb +182 -0
  178. data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
  179. data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
  180. data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
  181. data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
  182. data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
  183. data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
  184. data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
  185. data/lib/active_support/number_helper.rb +368 -0
  186. data/lib/active_support/option_merger.rb +1 -1
  187. data/lib/active_support/ordered_hash.rb +18 -183
  188. data/lib/active_support/ordered_options.rb +44 -24
  189. data/lib/active_support/per_thread_registry.rb +58 -0
  190. data/lib/active_support/proxy_object.rb +13 -0
  191. data/lib/active_support/rails.rb +27 -0
  192. data/lib/active_support/railtie.rb +25 -34
  193. data/lib/active_support/reloader.rb +129 -0
  194. data/lib/active_support/rescuable.rb +98 -48
  195. data/lib/active_support/security_utils.rb +27 -0
  196. data/lib/active_support/string_inquirer.rb +14 -9
  197. data/lib/active_support/subscriber.rb +120 -0
  198. data/lib/active_support/tagged_logging.rb +78 -0
  199. data/lib/active_support/test_case.rb +69 -17
  200. data/lib/active_support/testing/assertions.rb +43 -41
  201. data/lib/active_support/testing/autorun.rb +12 -0
  202. data/lib/active_support/testing/constant_lookup.rb +50 -0
  203. data/lib/active_support/testing/declarative.rb +7 -21
  204. data/lib/active_support/testing/deprecation.rb +14 -33
  205. data/lib/active_support/testing/file_fixtures.rb +34 -0
  206. data/lib/active_support/testing/isolation.rb +53 -95
  207. data/lib/active_support/testing/method_call_assertions.rb +41 -0
  208. data/lib/active_support/testing/setup_and_teardown.rb +21 -82
  209. data/lib/active_support/testing/stream.rb +42 -0
  210. data/lib/active_support/testing/tagged_logging.rb +25 -0
  211. data/lib/active_support/testing/time_helpers.rb +134 -0
  212. data/lib/active_support/time.rb +6 -23
  213. data/lib/active_support/time_with_zone.rb +239 -92
  214. data/lib/active_support/values/time_zone.rb +236 -160
  215. data/lib/active_support/values/unicode_tables.dat +0 -0
  216. data/lib/active_support/version.rb +5 -7
  217. data/lib/active_support/xml_mini/jdom.rb +19 -13
  218. data/lib/active_support/xml_mini/libxml.rb +3 -4
  219. data/lib/active_support/xml_mini/libxmlsax.rb +2 -3
  220. data/lib/active_support/xml_mini/nokogiri.rb +3 -4
  221. data/lib/active_support/xml_mini/nokogirisax.rb +2 -3
  222. data/lib/active_support/xml_mini/rexml.rb +8 -10
  223. data/lib/active_support/xml_mini.rb +66 -34
  224. data/lib/active_support.rb +40 -23
  225. metadata +185 -134
  226. data/CHANGELOG +0 -1534
  227. data/lib/active_support/base64.rb +0 -42
  228. data/lib/active_support/basic_object.rb +0 -21
  229. data/lib/active_support/buffered_logger.rb +0 -137
  230. data/lib/active_support/cache/compressed_mem_cache_store.rb +0 -13
  231. data/lib/active_support/cache/synchronized_memory_store.rb +0 -11
  232. data/lib/active_support/core_ext/array/random_access.rb +0 -30
  233. data/lib/active_support/core_ext/array/uniq_by.rb +0 -16
  234. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -44
  235. data/lib/active_support/core_ext/class/inheritable_attributes.rb +0 -178
  236. data/lib/active_support/core_ext/date/freeze.rb +0 -31
  237. data/lib/active_support/core_ext/date_time/zones.rb +0 -21
  238. data/lib/active_support/core_ext/exception.rb +0 -3
  239. data/lib/active_support/core_ext/file/path.rb +0 -5
  240. data/lib/active_support/core_ext/float/rounding.rb +0 -19
  241. data/lib/active_support/core_ext/float.rb +0 -1
  242. data/lib/active_support/core_ext/hash/deep_dup.rb +0 -11
  243. data/lib/active_support/core_ext/hash/diff.rb +0 -13
  244. data/lib/active_support/core_ext/kernel/requires.rb +0 -28
  245. data/lib/active_support/core_ext/logger.rb +0 -81
  246. data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +0 -31
  247. data/lib/active_support/core_ext/module/method_names.rb +0 -14
  248. data/lib/active_support/core_ext/module/synchronization.rb +0 -43
  249. data/lib/active_support/core_ext/object/to_json.rb +0 -19
  250. data/lib/active_support/core_ext/proc.rb +0 -14
  251. data/lib/active_support/core_ext/process/daemon.rb +0 -23
  252. data/lib/active_support/core_ext/process.rb +0 -1
  253. data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
  254. data/lib/active_support/core_ext/range/cover.rb +0 -3
  255. data/lib/active_support/core_ext/rexml.rb +0 -46
  256. data/lib/active_support/core_ext/string/encoding.rb +0 -11
  257. data/lib/active_support/core_ext/string/interpolation.rb +0 -2
  258. data/lib/active_support/core_ext/string/xchar.rb +0 -18
  259. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
  260. data/lib/active_support/file_watcher.rb +0 -36
  261. data/lib/active_support/json/variable.rb +0 -9
  262. data/lib/active_support/memoizable.rb +0 -105
  263. data/lib/active_support/multibyte/exceptions.rb +0 -8
  264. data/lib/active_support/multibyte/utils.rb +0 -60
  265. data/lib/active_support/ruby/shim.rb +0 -22
  266. data/lib/active_support/secure_random.rb +0 -6
  267. data/lib/active_support/testing/mochaing.rb +0 -7
  268. data/lib/active_support/testing/pending.rb +0 -52
  269. data/lib/active_support/testing/performance/jruby.rb +0 -115
  270. data/lib/active_support/testing/performance/rubinius.rb +0 -113
  271. data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
  272. data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
  273. data/lib/active_support/testing/performance/ruby.rb +0 -152
  274. data/lib/active_support/testing/performance.rb +0 -317
  275. data/lib/active_support/time/autoload.rb +0 -5
  276. data/lib/active_support/whiny_nil.rb +0 -60
@@ -3,109 +3,48 @@ require 'active_support/callbacks'
3
3
 
4
4
  module ActiveSupport
5
5
  module Testing
6
+ # Adds support for +setup+ and +teardown+ callbacks.
7
+ # These callbacks serve as a replacement to overwriting the
8
+ # <tt>#setup</tt> and <tt>#teardown</tt> methods of your TestCase.
9
+ #
10
+ # class ExampleTest < ActiveSupport::TestCase
11
+ # setup do
12
+ # # ...
13
+ # end
14
+ #
15
+ # teardown do
16
+ # # ...
17
+ # end
18
+ # end
6
19
  module SetupAndTeardown
7
20
  extend ActiveSupport::Concern
8
21
 
9
22
  included do
10
23
  include ActiveSupport::Callbacks
11
24
  define_callbacks :setup, :teardown
12
-
13
- if defined?(MiniTest::Assertions) && TestCase < MiniTest::Assertions
14
- include ForMiniTest
15
- else
16
- include ForClassicTestUnit
17
- end
18
25
  end
19
26
 
20
27
  module ClassMethods
28
+ # Add a callback, which runs before <tt>TestCase#setup</tt>.
21
29
  def setup(*args, &block)
22
30
  set_callback(:setup, :before, *args, &block)
23
31
  end
24
32
 
33
+ # Add a callback, which runs after <tt>TestCase#teardown</tt>.
25
34
  def teardown(*args, &block)
26
35
  set_callback(:teardown, :after, *args, &block)
27
36
  end
28
37
  end
29
38
 
30
- module ForMiniTest
31
- def run(runner)
32
- result = '.'
33
- begin
34
- run_callbacks :setup do
35
- result = super
36
- end
37
- rescue Exception => e
38
- result = runner.puke(self.class, method_name, e)
39
- ensure
40
- begin
41
- run_callbacks :teardown
42
- rescue Exception => e
43
- result = runner.puke(self.class, method_name, e)
44
- end
45
- end
46
- result
47
- end
39
+ def before_setup # :nodoc:
40
+ super
41
+ run_callbacks :setup
48
42
  end
49
43
 
50
- module ForClassicTestUnit
51
- # For compatibility with Ruby < 1.8.6
52
- PASSTHROUGH_EXCEPTIONS = Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS rescue [NoMemoryError, SignalException, Interrupt, SystemExit]
53
-
54
- # This redefinition is unfortunate but test/unit shows us no alternative.
55
- # Doubly unfortunate: hax to support Mocha's hax.
56
- def run(result)
57
- return if @method_name.to_s == "default_test"
58
-
59
- mocha_counter = retrieve_mocha_counter(result)
60
- yield(Test::Unit::TestCase::STARTED, name)
61
- @_result = result
62
-
63
- begin
64
- begin
65
- run_callbacks :setup do
66
- setup
67
- __send__(@method_name)
68
- mocha_verify(mocha_counter) if mocha_counter
69
- end
70
- rescue Mocha::ExpectationError => e
71
- add_failure(e.message, e.backtrace)
72
- rescue Test::Unit::AssertionFailedError => e
73
- add_failure(e.message, e.backtrace)
74
- rescue Exception => e
75
- raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
76
- add_error(e)
77
- ensure
78
- begin
79
- teardown
80
- run_callbacks :teardown
81
- rescue Test::Unit::AssertionFailedError => e
82
- add_failure(e.message, e.backtrace)
83
- rescue Exception => e
84
- raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
85
- add_error(e)
86
- end
87
- end
88
- ensure
89
- mocha_teardown if mocha_counter
90
- end
91
-
92
- result.add_run
93
- yield(Test::Unit::TestCase::FINISHED, name)
94
- end
95
-
96
- protected
97
-
98
- def retrieve_mocha_counter(result) #:nodoc:
99
- if respond_to?(:mocha_verify) # using mocha
100
- if defined?(Mocha::TestCaseAdapter::AssertionCounter)
101
- Mocha::TestCaseAdapter::AssertionCounter.new(result)
102
- else
103
- Mocha::Integration::TestUnit::AssertionCounter.new(result)
104
- end
105
- end
106
- end
44
+ def after_teardown # :nodoc:
45
+ run_callbacks :teardown
46
+ super
107
47
  end
108
-
109
48
  end
110
49
  end
111
50
  end
@@ -0,0 +1,42 @@
1
+ module ActiveSupport
2
+ module Testing
3
+ module Stream #:nodoc:
4
+ private
5
+
6
+ def silence_stream(stream)
7
+ old_stream = stream.dup
8
+ stream.reopen(IO::NULL)
9
+ stream.sync = true
10
+ yield
11
+ ensure
12
+ stream.reopen(old_stream)
13
+ old_stream.close
14
+ end
15
+
16
+ def quietly
17
+ silence_stream(STDOUT) do
18
+ silence_stream(STDERR) do
19
+ yield
20
+ end
21
+ end
22
+ end
23
+
24
+ def capture(stream)
25
+ stream = stream.to_s
26
+ captured_stream = Tempfile.new(stream)
27
+ stream_io = eval("$#{stream}")
28
+ origin_stream = stream_io.dup
29
+ stream_io.reopen(captured_stream)
30
+
31
+ yield
32
+
33
+ stream_io.rewind
34
+ return captured_stream.read
35
+ ensure
36
+ captured_stream.close
37
+ captured_stream.unlink
38
+ stream_io.reopen(origin_stream)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,25 @@
1
+ module ActiveSupport
2
+ module Testing
3
+ # Logs a "PostsControllerTest: test name" heading before each test to
4
+ # make test.log easier to search and follow along with.
5
+ module TaggedLogging #:nodoc:
6
+ attr_writer :tagged_logger
7
+
8
+ def before_setup
9
+ if tagged_logger && tagged_logger.info?
10
+ heading = "#{self.class}: #{name}"
11
+ divider = '-' * heading.size
12
+ tagged_logger.info divider
13
+ tagged_logger.info heading
14
+ tagged_logger.info divider
15
+ end
16
+ super
17
+ end
18
+
19
+ private
20
+ def tagged_logger
21
+ @tagged_logger ||= (defined?(Rails.logger) && Rails.logger)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,134 @@
1
+ module ActiveSupport
2
+ module Testing
3
+ class SimpleStubs # :nodoc:
4
+ Stub = Struct.new(:object, :method_name, :original_method)
5
+
6
+ def initialize
7
+ @stubs = {}
8
+ end
9
+
10
+ def stub_object(object, method_name, return_value)
11
+ key = [object.object_id, method_name]
12
+
13
+ if stub = @stubs[key]
14
+ unstub_object(stub)
15
+ end
16
+
17
+ new_name = "__simple_stub__#{method_name}"
18
+
19
+ @stubs[key] = Stub.new(object, method_name, new_name)
20
+
21
+ object.singleton_class.send :alias_method, new_name, method_name
22
+ object.define_singleton_method(method_name) { return_value }
23
+ end
24
+
25
+ def unstub_all!
26
+ @stubs.each_value do |stub|
27
+ unstub_object(stub)
28
+ end
29
+ @stubs = {}
30
+ end
31
+
32
+ private
33
+
34
+ def unstub_object(stub)
35
+ singleton_class = stub.object.singleton_class
36
+ singleton_class.send :undef_method, stub.method_name
37
+ singleton_class.send :alias_method, stub.method_name, stub.original_method
38
+ singleton_class.send :undef_method, stub.original_method
39
+ end
40
+ end
41
+
42
+ # Contains helpers that help you test passage of time.
43
+ module TimeHelpers
44
+ # Changes current time to the time in the future or in the past by a given time difference by
45
+ # stubbing +Time.now+, +Date.today+, and +DateTime.now+.
46
+ #
47
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
48
+ # travel 1.day
49
+ # Time.current # => Sun, 10 Nov 2013 15:34:49 EST -05:00
50
+ # Date.current # => Sun, 10 Nov 2013
51
+ # DateTime.current # => Sun, 10 Nov 2013 15:34:49 -0500
52
+ #
53
+ # This method also accepts a block, which will return the current time back to its original
54
+ # state at the end of the block:
55
+ #
56
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
57
+ # travel 1.day do
58
+ # User.create.created_at # => Sun, 10 Nov 2013 15:34:49 EST -05:00
59
+ # end
60
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
61
+ def travel(duration, &block)
62
+ travel_to Time.now + duration, &block
63
+ end
64
+
65
+ # Changes current time to the given time by stubbing +Time.now+,
66
+ # +Date.today+, and +DateTime.now+ to return the time or date passed into this method.
67
+ #
68
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
69
+ # travel_to Time.new(2004, 11, 24, 01, 04, 44)
70
+ # Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
71
+ # Date.current # => Wed, 24 Nov 2004
72
+ # DateTime.current # => Wed, 24 Nov 2004 01:04:44 -0500
73
+ #
74
+ # Dates are taken as their timestamp at the beginning of the day in the
75
+ # application time zone. <tt>Time.current</tt> returns said timestamp,
76
+ # and <tt>Time.now</tt> its equivalent in the system time zone. Similarly,
77
+ # <tt>Date.current</tt> returns a date equal to the argument, and
78
+ # <tt>Date.today</tt> the date according to <tt>Time.now</tt>, which may
79
+ # be different. (Note that you rarely want to deal with <tt>Time.now</tt>,
80
+ # or <tt>Date.today</tt>, in order to honor the application time zone
81
+ # please always use <tt>Time.current</tt> and <tt>Date.current</tt>.)
82
+ #
83
+ # Note that the usec for the time passed will be set to 0 to prevent rounding
84
+ # errors with external services, like MySQL (which will round instead of floor,
85
+ # leading to off-by-one-second errors).
86
+ #
87
+ # This method also accepts a block, which will return the current time back to its original
88
+ # state at the end of the block:
89
+ #
90
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
91
+ # travel_to Time.new(2004, 11, 24, 01, 04, 44) do
92
+ # Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
93
+ # end
94
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
95
+ def travel_to(date_or_time)
96
+ if date_or_time.is_a?(Date) && !date_or_time.is_a?(DateTime)
97
+ now = date_or_time.midnight.to_time
98
+ else
99
+ now = date_or_time.to_time.change(usec: 0)
100
+ end
101
+
102
+ simple_stubs.stub_object(Time, :now, now)
103
+ simple_stubs.stub_object(Date, :today, now.to_date)
104
+ simple_stubs.stub_object(DateTime, :now, now.to_datetime)
105
+
106
+ if block_given?
107
+ begin
108
+ yield
109
+ ensure
110
+ travel_back
111
+ end
112
+ end
113
+ end
114
+
115
+ # Returns the current time back to its original state, by removing the stubs added by
116
+ # `travel` and `travel_to`.
117
+ #
118
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
119
+ # travel_to Time.new(2004, 11, 24, 01, 04, 44)
120
+ # Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
121
+ # travel_back
122
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
123
+ def travel_back
124
+ simple_stubs.unstub_all!
125
+ end
126
+
127
+ private
128
+
129
+ def simple_stubs
130
+ @simple_stubs ||= SimpleStubs.new
131
+ end
132
+ end
133
+ end
134
+ end
@@ -1,35 +1,18 @@
1
- require 'active_support'
2
-
3
1
  module ActiveSupport
4
2
  autoload :Duration, 'active_support/duration'
5
3
  autoload :TimeWithZone, 'active_support/time_with_zone'
6
4
  autoload :TimeZone, 'active_support/values/time_zone'
7
-
8
- on_load_all do
9
- [Duration, TimeWithZone, TimeZone]
10
- end
11
5
  end
12
6
 
13
7
  require 'date'
14
8
  require 'time'
15
9
 
16
- require 'active_support/core_ext/time/publicize_conversion_methods'
17
- require 'active_support/core_ext/time/marshal'
18
- require 'active_support/core_ext/time/acts_like'
19
- require 'active_support/core_ext/time/calculations'
20
- require 'active_support/core_ext/time/conversions'
21
- require 'active_support/core_ext/time/zones'
22
-
23
- require 'active_support/core_ext/date/acts_like'
24
- require 'active_support/core_ext/date/freeze'
25
- require 'active_support/core_ext/date/calculations'
26
- require 'active_support/core_ext/date/conversions'
27
- require 'active_support/core_ext/date/zones'
28
-
29
- require 'active_support/core_ext/date_time/acts_like'
30
- require 'active_support/core_ext/date_time/calculations'
31
- require 'active_support/core_ext/date_time/conversions'
32
- require 'active_support/core_ext/date_time/zones'
10
+ require 'active_support/core_ext/time'
11
+ require 'active_support/core_ext/date'
12
+ require 'active_support/core_ext/date_time'
33
13
 
34
14
  require 'active_support/core_ext/integer/time'
35
15
  require 'active_support/core_ext/numeric/time'
16
+
17
+ require 'active_support/core_ext/string/conversions'
18
+ require 'active_support/core_ext/string/zones'