activesupport 5.0.7.1

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 (236) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1013 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +39 -0
  5. data/lib/active_support.rb +99 -0
  6. data/lib/active_support/all.rb +3 -0
  7. data/lib/active_support/array_inquirer.rb +44 -0
  8. data/lib/active_support/backtrace_cleaner.rb +103 -0
  9. data/lib/active_support/benchmarkable.rb +49 -0
  10. data/lib/active_support/builder.rb +6 -0
  11. data/lib/active_support/cache.rb +701 -0
  12. data/lib/active_support/cache/file_store.rb +204 -0
  13. data/lib/active_support/cache/mem_cache_store.rb +207 -0
  14. data/lib/active_support/cache/memory_store.rb +167 -0
  15. data/lib/active_support/cache/null_store.rb +41 -0
  16. data/lib/active_support/cache/strategy/local_cache.rb +172 -0
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
  18. data/lib/active_support/callbacks.rb +791 -0
  19. data/lib/active_support/concern.rb +142 -0
  20. data/lib/active_support/concurrency/latch.rb +26 -0
  21. data/lib/active_support/concurrency/share_lock.rb +226 -0
  22. data/lib/active_support/configurable.rb +148 -0
  23. data/lib/active_support/core_ext.rb +4 -0
  24. data/lib/active_support/core_ext/array.rb +7 -0
  25. data/lib/active_support/core_ext/array/access.rb +90 -0
  26. data/lib/active_support/core_ext/array/conversions.rb +211 -0
  27. data/lib/active_support/core_ext/array/extract_options.rb +29 -0
  28. data/lib/active_support/core_ext/array/grouping.rb +107 -0
  29. data/lib/active_support/core_ext/array/inquiry.rb +17 -0
  30. data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
  31. data/lib/active_support/core_ext/array/wrap.rb +46 -0
  32. data/lib/active_support/core_ext/benchmark.rb +14 -0
  33. data/lib/active_support/core_ext/big_decimal.rb +1 -0
  34. data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
  35. data/lib/active_support/core_ext/class.rb +2 -0
  36. data/lib/active_support/core_ext/class/attribute.rb +128 -0
  37. data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -0
  38. data/lib/active_support/core_ext/class/subclasses.rb +41 -0
  39. data/lib/active_support/core_ext/date.rb +5 -0
  40. data/lib/active_support/core_ext/date/acts_like.rb +8 -0
  41. data/lib/active_support/core_ext/date/blank.rb +12 -0
  42. data/lib/active_support/core_ext/date/calculations.rb +143 -0
  43. data/lib/active_support/core_ext/date/conversions.rb +95 -0
  44. data/lib/active_support/core_ext/date/zones.rb +6 -0
  45. data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
  46. data/lib/active_support/core_ext/date_and_time/compatibility.rb +14 -0
  47. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  48. data/lib/active_support/core_ext/date_time.rb +5 -0
  49. data/lib/active_support/core_ext/date_time/acts_like.rb +14 -0
  50. data/lib/active_support/core_ext/date_time/blank.rb +12 -0
  51. data/lib/active_support/core_ext/date_time/calculations.rb +199 -0
  52. data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
  53. data/lib/active_support/core_ext/date_time/conversions.rb +105 -0
  54. data/lib/active_support/core_ext/digest/uuid.rb +51 -0
  55. data/lib/active_support/core_ext/enumerable.rb +146 -0
  56. data/lib/active_support/core_ext/file.rb +1 -0
  57. data/lib/active_support/core_ext/file/atomic.rb +68 -0
  58. data/lib/active_support/core_ext/hash.rb +9 -0
  59. data/lib/active_support/core_ext/hash/compact.rb +24 -0
  60. data/lib/active_support/core_ext/hash/conversions.rb +262 -0
  61. data/lib/active_support/core_ext/hash/deep_merge.rb +38 -0
  62. data/lib/active_support/core_ext/hash/except.rb +22 -0
  63. data/lib/active_support/core_ext/hash/indifferent_access.rb +23 -0
  64. data/lib/active_support/core_ext/hash/keys.rb +170 -0
  65. data/lib/active_support/core_ext/hash/reverse_merge.rb +22 -0
  66. data/lib/active_support/core_ext/hash/slice.rb +48 -0
  67. data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
  68. data/lib/active_support/core_ext/integer.rb +3 -0
  69. data/lib/active_support/core_ext/integer/inflections.rb +29 -0
  70. data/lib/active_support/core_ext/integer/multiple.rb +10 -0
  71. data/lib/active_support/core_ext/integer/time.rb +29 -0
  72. data/lib/active_support/core_ext/kernel.rb +4 -0
  73. data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
  74. data/lib/active_support/core_ext/kernel/concern.rb +12 -0
  75. data/lib/active_support/core_ext/kernel/debugger.rb +3 -0
  76. data/lib/active_support/core_ext/kernel/reporting.rb +43 -0
  77. data/lib/active_support/core_ext/kernel/singleton_class.rb +6 -0
  78. data/lib/active_support/core_ext/load_error.rb +31 -0
  79. data/lib/active_support/core_ext/marshal.rb +22 -0
  80. data/lib/active_support/core_ext/module.rb +12 -0
  81. data/lib/active_support/core_ext/module/aliasing.rb +74 -0
  82. data/lib/active_support/core_ext/module/anonymous.rb +28 -0
  83. data/lib/active_support/core_ext/module/attr_internal.rb +36 -0
  84. data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
  85. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
  86. data/lib/active_support/core_ext/module/concerning.rb +135 -0
  87. data/lib/active_support/core_ext/module/delegation.rb +216 -0
  88. data/lib/active_support/core_ext/module/deprecation.rb +23 -0
  89. data/lib/active_support/core_ext/module/introspection.rb +68 -0
  90. data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
  91. data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
  92. data/lib/active_support/core_ext/module/reachable.rb +8 -0
  93. data/lib/active_support/core_ext/module/remove_method.rb +35 -0
  94. data/lib/active_support/core_ext/name_error.rb +31 -0
  95. data/lib/active_support/core_ext/numeric.rb +4 -0
  96. data/lib/active_support/core_ext/numeric/bytes.rb +64 -0
  97. data/lib/active_support/core_ext/numeric/conversions.rb +144 -0
  98. data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
  99. data/lib/active_support/core_ext/numeric/time.rb +74 -0
  100. data/lib/active_support/core_ext/object.rb +14 -0
  101. data/lib/active_support/core_ext/object/acts_like.rb +10 -0
  102. data/lib/active_support/core_ext/object/blank.rb +143 -0
  103. data/lib/active_support/core_ext/object/conversions.rb +4 -0
  104. data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
  105. data/lib/active_support/core_ext/object/duplicable.rb +124 -0
  106. data/lib/active_support/core_ext/object/inclusion.rb +27 -0
  107. data/lib/active_support/core_ext/object/instance_variables.rb +28 -0
  108. data/lib/active_support/core_ext/object/json.rb +205 -0
  109. data/lib/active_support/core_ext/object/to_param.rb +1 -0
  110. data/lib/active_support/core_ext/object/to_query.rb +84 -0
  111. data/lib/active_support/core_ext/object/try.rb +146 -0
  112. data/lib/active_support/core_ext/object/with_options.rb +69 -0
  113. data/lib/active_support/core_ext/range.rb +4 -0
  114. data/lib/active_support/core_ext/range/conversions.rb +31 -0
  115. data/lib/active_support/core_ext/range/each.rb +21 -0
  116. data/lib/active_support/core_ext/range/include_range.rb +23 -0
  117. data/lib/active_support/core_ext/range/overlaps.rb +8 -0
  118. data/lib/active_support/core_ext/regexp.rb +5 -0
  119. data/lib/active_support/core_ext/securerandom.rb +23 -0
  120. data/lib/active_support/core_ext/string.rb +13 -0
  121. data/lib/active_support/core_ext/string/access.rb +104 -0
  122. data/lib/active_support/core_ext/string/behavior.rb +6 -0
  123. data/lib/active_support/core_ext/string/conversions.rb +57 -0
  124. data/lib/active_support/core_ext/string/exclude.rb +11 -0
  125. data/lib/active_support/core_ext/string/filters.rb +102 -0
  126. data/lib/active_support/core_ext/string/indent.rb +43 -0
  127. data/lib/active_support/core_ext/string/inflections.rb +244 -0
  128. data/lib/active_support/core_ext/string/inquiry.rb +13 -0
  129. data/lib/active_support/core_ext/string/multibyte.rb +53 -0
  130. data/lib/active_support/core_ext/string/output_safety.rb +260 -0
  131. data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
  132. data/lib/active_support/core_ext/string/strip.rb +23 -0
  133. data/lib/active_support/core_ext/string/zones.rb +14 -0
  134. data/lib/active_support/core_ext/struct.rb +3 -0
  135. data/lib/active_support/core_ext/time.rb +5 -0
  136. data/lib/active_support/core_ext/time/acts_like.rb +8 -0
  137. data/lib/active_support/core_ext/time/calculations.rb +290 -0
  138. data/lib/active_support/core_ext/time/compatibility.rb +14 -0
  139. data/lib/active_support/core_ext/time/conversions.rb +67 -0
  140. data/lib/active_support/core_ext/time/marshal.rb +3 -0
  141. data/lib/active_support/core_ext/time/zones.rb +111 -0
  142. data/lib/active_support/core_ext/uri.rb +24 -0
  143. data/lib/active_support/dependencies.rb +755 -0
  144. data/lib/active_support/dependencies/autoload.rb +77 -0
  145. data/lib/active_support/dependencies/interlock.rb +55 -0
  146. data/lib/active_support/deprecation.rb +43 -0
  147. data/lib/active_support/deprecation/behaviors.rb +90 -0
  148. data/lib/active_support/deprecation/instance_delegator.rb +37 -0
  149. data/lib/active_support/deprecation/method_wrappers.rb +70 -0
  150. data/lib/active_support/deprecation/proxy_wrappers.rb +149 -0
  151. data/lib/active_support/deprecation/reporting.rb +112 -0
  152. data/lib/active_support/descendants_tracker.rb +60 -0
  153. data/lib/active_support/duration.rb +235 -0
  154. data/lib/active_support/duration/iso8601_parser.rb +122 -0
  155. data/lib/active_support/duration/iso8601_serializer.rb +51 -0
  156. data/lib/active_support/evented_file_update_checker.rb +199 -0
  157. data/lib/active_support/execution_wrapper.rb +126 -0
  158. data/lib/active_support/executor.rb +6 -0
  159. data/lib/active_support/file_update_checker.rb +157 -0
  160. data/lib/active_support/gem_version.rb +15 -0
  161. data/lib/active_support/gzip.rb +36 -0
  162. data/lib/active_support/hash_with_indifferent_access.rb +329 -0
  163. data/lib/active_support/i18n.rb +13 -0
  164. data/lib/active_support/i18n_railtie.rb +115 -0
  165. data/lib/active_support/inflections.rb +70 -0
  166. data/lib/active_support/inflector.rb +7 -0
  167. data/lib/active_support/inflector/inflections.rb +242 -0
  168. data/lib/active_support/inflector/methods.rb +390 -0
  169. data/lib/active_support/inflector/transliterate.rb +112 -0
  170. data/lib/active_support/json.rb +2 -0
  171. data/lib/active_support/json/decoding.rb +74 -0
  172. data/lib/active_support/json/encoding.rb +127 -0
  173. data/lib/active_support/key_generator.rb +71 -0
  174. data/lib/active_support/lazy_load_hooks.rb +76 -0
  175. data/lib/active_support/locale/en.yml +135 -0
  176. data/lib/active_support/log_subscriber.rb +109 -0
  177. data/lib/active_support/log_subscriber/test_helper.rb +104 -0
  178. data/lib/active_support/logger.rb +106 -0
  179. data/lib/active_support/logger_silence.rb +28 -0
  180. data/lib/active_support/logger_thread_safe_level.rb +31 -0
  181. data/lib/active_support/message_encryptor.rb +114 -0
  182. data/lib/active_support/message_verifier.rb +134 -0
  183. data/lib/active_support/multibyte.rb +21 -0
  184. data/lib/active_support/multibyte/chars.rb +231 -0
  185. data/lib/active_support/multibyte/unicode.rb +413 -0
  186. data/lib/active_support/notifications.rb +212 -0
  187. data/lib/active_support/notifications/fanout.rb +157 -0
  188. data/lib/active_support/notifications/instrumenter.rb +91 -0
  189. data/lib/active_support/number_helper.rb +368 -0
  190. data/lib/active_support/number_helper/number_converter.rb +182 -0
  191. data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
  192. data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
  193. data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
  194. data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
  195. data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
  196. data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
  197. data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
  198. data/lib/active_support/option_merger.rb +25 -0
  199. data/lib/active_support/ordered_hash.rb +48 -0
  200. data/lib/active_support/ordered_options.rb +81 -0
  201. data/lib/active_support/per_thread_registry.rb +58 -0
  202. data/lib/active_support/proxy_object.rb +13 -0
  203. data/lib/active_support/rails.rb +27 -0
  204. data/lib/active_support/railtie.rb +51 -0
  205. data/lib/active_support/reloader.rb +129 -0
  206. data/lib/active_support/rescuable.rb +173 -0
  207. data/lib/active_support/security_utils.rb +27 -0
  208. data/lib/active_support/string_inquirer.rb +26 -0
  209. data/lib/active_support/subscriber.rb +120 -0
  210. data/lib/active_support/tagged_logging.rb +77 -0
  211. data/lib/active_support/test_case.rb +88 -0
  212. data/lib/active_support/testing/assertions.rb +99 -0
  213. data/lib/active_support/testing/autorun.rb +5 -0
  214. data/lib/active_support/testing/constant_lookup.rb +50 -0
  215. data/lib/active_support/testing/declarative.rb +26 -0
  216. data/lib/active_support/testing/deprecation.rb +36 -0
  217. data/lib/active_support/testing/file_fixtures.rb +34 -0
  218. data/lib/active_support/testing/isolation.rb +115 -0
  219. data/lib/active_support/testing/method_call_assertions.rb +41 -0
  220. data/lib/active_support/testing/setup_and_teardown.rb +50 -0
  221. data/lib/active_support/testing/stream.rb +42 -0
  222. data/lib/active_support/testing/tagged_logging.rb +25 -0
  223. data/lib/active_support/testing/time_helpers.rb +136 -0
  224. data/lib/active_support/time.rb +18 -0
  225. data/lib/active_support/time_with_zone.rb +511 -0
  226. data/lib/active_support/values/time_zone.rb +484 -0
  227. data/lib/active_support/values/unicode_tables.dat +0 -0
  228. data/lib/active_support/version.rb +8 -0
  229. data/lib/active_support/xml_mini.rb +209 -0
  230. data/lib/active_support/xml_mini/jdom.rb +181 -0
  231. data/lib/active_support/xml_mini/libxml.rb +77 -0
  232. data/lib/active_support/xml_mini/libxmlsax.rb +82 -0
  233. data/lib/active_support/xml_mini/nokogiri.rb +81 -0
  234. data/lib/active_support/xml_mini/nokogirisax.rb +85 -0
  235. data/lib/active_support/xml_mini/rexml.rb +128 -0
  236. metadata +350 -0
@@ -0,0 +1,99 @@
1
+ require 'active_support/core_ext/object/blank'
2
+
3
+ module ActiveSupport
4
+ module Testing
5
+ module Assertions
6
+ # Asserts that an expression is not truthy. Passes if <tt>object</tt> is
7
+ # +nil+ or +false+. "Truthy" means "considered true in a conditional"
8
+ # like <tt>if foo</tt>.
9
+ #
10
+ # assert_not nil # => true
11
+ # assert_not false # => true
12
+ # assert_not 'foo' # => Expected "foo" to be nil or false
13
+ #
14
+ # An error message can be specified.
15
+ #
16
+ # assert_not foo, 'foo should be false'
17
+ def assert_not(object, message = nil)
18
+ message ||= "Expected #{mu_pp(object)} to be nil or false"
19
+ assert !object, message
20
+ end
21
+
22
+ # Test numeric difference between the return value of an expression as a
23
+ # result of what is evaluated in the yielded block.
24
+ #
25
+ # assert_difference 'Article.count' do
26
+ # post :create, params: { article: {...} }
27
+ # end
28
+ #
29
+ # An arbitrary expression is passed in and evaluated.
30
+ #
31
+ # assert_difference 'Article.last.comments(:reload).size' do
32
+ # post :create, params: { comment: {...} }
33
+ # end
34
+ #
35
+ # An arbitrary positive or negative difference can be specified.
36
+ # The default is <tt>1</tt>.
37
+ #
38
+ # assert_difference 'Article.count', -1 do
39
+ # post :delete, params: { id: ... }
40
+ # end
41
+ #
42
+ # An array of expressions can also be passed in and evaluated.
43
+ #
44
+ # assert_difference [ 'Article.count', 'Post.count' ], 2 do
45
+ # post :create, params: { article: {...} }
46
+ # end
47
+ #
48
+ # A lambda or a list of lambdas can be passed in and evaluated:
49
+ #
50
+ # assert_difference ->{ Article.count }, 2 do
51
+ # post :create, params: { article: {...} }
52
+ # end
53
+ #
54
+ # assert_difference [->{ Article.count }, ->{ Post.count }], 2 do
55
+ # post :create, params: { article: {...} }
56
+ # end
57
+ #
58
+ # An error message can be specified.
59
+ #
60
+ # assert_difference 'Article.count', -1, 'An Article should be destroyed' do
61
+ # post :delete, params: { id: ... }
62
+ # end
63
+ def assert_difference(expression, difference = 1, message = nil, &block)
64
+ expressions = Array(expression)
65
+
66
+ exps = expressions.map { |e|
67
+ e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
68
+ }
69
+ before = exps.map(&:call)
70
+
71
+ retval = yield
72
+
73
+ expressions.zip(exps).each_with_index do |(code, e), i|
74
+ error = "#{code.inspect} didn't change by #{difference}"
75
+ error = "#{message}.\n#{error}" if message
76
+ assert_equal(before[i] + difference, e.call, error)
77
+ end
78
+
79
+ retval
80
+ end
81
+
82
+ # Assertion that the numeric result of evaluating an expression is not
83
+ # changed before and after invoking the passed in block.
84
+ #
85
+ # assert_no_difference 'Article.count' do
86
+ # post :create, params: { article: invalid_attributes }
87
+ # end
88
+ #
89
+ # An error message can be specified.
90
+ #
91
+ # assert_no_difference 'Article.count', 'An Article should not be created' do
92
+ # post :create, params: { article: invalid_attributes }
93
+ # end
94
+ def assert_no_difference(expression, message = nil, &block)
95
+ assert_difference expression, 0, message, &block
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,5 @@
1
+ gem 'minitest'
2
+
3
+ require 'minitest'
4
+
5
+ Minitest.autorun
@@ -0,0 +1,50 @@
1
+ require "active_support/concern"
2
+ require "active_support/inflector"
3
+
4
+ module ActiveSupport
5
+ module Testing
6
+ # Resolves a constant from a minitest spec name.
7
+ #
8
+ # Given the following spec-style test:
9
+ #
10
+ # describe WidgetsController, :index do
11
+ # describe "authenticated user" do
12
+ # describe "returns widgets" do
13
+ # it "has a controller that exists" do
14
+ # assert_kind_of WidgetsController, @controller
15
+ # end
16
+ # end
17
+ # end
18
+ # end
19
+ #
20
+ # The test will have the following name:
21
+ #
22
+ # "WidgetsController::index::authenticated user::returns widgets"
23
+ #
24
+ # The constant WidgetsController can be resolved from the name.
25
+ # The following code will resolve the constant:
26
+ #
27
+ # controller = determine_constant_from_test_name(name) do |constant|
28
+ # Class === constant && constant < ::ActionController::Metal
29
+ # end
30
+ module ConstantLookup
31
+ extend ::ActiveSupport::Concern
32
+
33
+ module ClassMethods # :nodoc:
34
+ def determine_constant_from_test_name(test_name)
35
+ names = test_name.split "::"
36
+ while names.size > 0 do
37
+ names.last.sub!(/Test$/, "")
38
+ begin
39
+ constant = names.join("::").safe_constantize
40
+ break(constant) if yield(constant)
41
+ ensure
42
+ names.pop
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,26 @@
1
+ module ActiveSupport
2
+ module Testing
3
+ module Declarative
4
+ unless defined?(Spec)
5
+ # Helper to define a test method using a String. Under the hood, it replaces
6
+ # spaces with underscores and defines the test method.
7
+ #
8
+ # test "verify something" do
9
+ # ...
10
+ # end
11
+ def test(name, &block)
12
+ test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
13
+ defined = method_defined? test_name
14
+ raise "#{test_name} is already defined in #{self}" if defined
15
+ if block_given?
16
+ define_method(test_name, &block)
17
+ else
18
+ define_method(test_name) do
19
+ flunk "No implementation provided for #{name}"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ require 'active_support/deprecation'
2
+
3
+ module ActiveSupport
4
+ module Testing
5
+ module Deprecation #:nodoc:
6
+ def assert_deprecated(match = nil, deprecator = nil, &block)
7
+ result, warnings = collect_deprecations(deprecator, &block)
8
+ assert !warnings.empty?, "Expected a deprecation warning within the block but received none"
9
+ if match
10
+ match = Regexp.new(Regexp.escape(match)) unless match.is_a?(Regexp)
11
+ assert warnings.any? { |w| w =~ match }, "No deprecation warning matched #{match}: #{warnings.join(', ')}"
12
+ end
13
+ result
14
+ end
15
+
16
+ def assert_not_deprecated(deprecator = nil, &block)
17
+ result, deprecations = collect_deprecations(deprecator, &block)
18
+ assert deprecations.empty?, "Expected no deprecation warning within the block but received #{deprecations.size}: \n #{deprecations * "\n "}"
19
+ result
20
+ end
21
+
22
+ def collect_deprecations(deprecator = nil)
23
+ deprecator ||= ActiveSupport::Deprecation
24
+ old_behavior = deprecator.behavior
25
+ deprecations = []
26
+ deprecator.behavior = Proc.new do |message, callstack|
27
+ deprecations << message
28
+ end
29
+ result = yield
30
+ [result, deprecations]
31
+ ensure
32
+ deprecator.behavior = old_behavior
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,34 @@
1
+ module ActiveSupport
2
+ module Testing
3
+ # Adds simple access to sample files called file fixtures.
4
+ # File fixtures are normal files stored in
5
+ # <tt>ActiveSupport::TestCase.file_fixture_path</tt>.
6
+ #
7
+ # File fixtures are represented as +Pathname+ objects.
8
+ # This makes it easy to extract specific information:
9
+ #
10
+ # file_fixture("example.txt").read # get the file's content
11
+ # file_fixture("example.mp3").size # get the file size
12
+ module FileFixtures
13
+ extend ActiveSupport::Concern
14
+
15
+ included do
16
+ class_attribute :file_fixture_path, instance_writer: false
17
+ end
18
+
19
+ # Returns a +Pathname+ to the fixture file named +fixture_name+.
20
+ #
21
+ # Raises +ArgumentError+ if +fixture_name+ can't be found.
22
+ def file_fixture(fixture_name)
23
+ path = Pathname.new(File.join(file_fixture_path, fixture_name))
24
+
25
+ if path.exist?
26
+ path
27
+ else
28
+ msg = "the directory '%s' does not contain a file named '%s'"
29
+ raise ArgumentError, msg % [file_fixture_path, fixture_name]
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,115 @@
1
+ module ActiveSupport
2
+ module Testing
3
+ module Isolation
4
+ require 'thread'
5
+
6
+ def self.included(klass) #:nodoc:
7
+ klass.class_eval do
8
+ parallelize_me!
9
+ end
10
+ end
11
+
12
+ def self.forking_env?
13
+ !ENV["NO_FORK"] && Process.respond_to?(:fork)
14
+ end
15
+
16
+ @@class_setup_mutex = Mutex.new
17
+
18
+ def _run_class_setup # class setup method should only happen in parent
19
+ @@class_setup_mutex.synchronize do
20
+ unless defined?(@@ran_class_setup) || ENV['ISOLATION_TEST']
21
+ self.class.setup if self.class.respond_to?(:setup)
22
+ @@ran_class_setup = true
23
+ end
24
+ end
25
+ end
26
+
27
+ def run
28
+ serialized = run_in_isolation do
29
+ super
30
+ end
31
+
32
+ Marshal.load(serialized)
33
+ end
34
+
35
+ module Forking
36
+ def run_in_isolation(&blk)
37
+ read, write = IO.pipe
38
+ read.binmode
39
+ write.binmode
40
+
41
+ pid = fork do
42
+ read.close
43
+ yield
44
+ begin
45
+ if error?
46
+ failures.map! { |e|
47
+ begin
48
+ Marshal.dump e
49
+ e
50
+ rescue TypeError
51
+ ex = Exception.new e.message
52
+ ex.set_backtrace e.backtrace
53
+ Minitest::UnexpectedError.new ex
54
+ end
55
+ }
56
+ end
57
+ result = Marshal.dump(self.dup)
58
+ end
59
+
60
+ write.puts [result].pack("m")
61
+ exit!
62
+ end
63
+
64
+ write.close
65
+ result = read.read
66
+ Process.wait2(pid)
67
+ return result.unpack("m")[0]
68
+ end
69
+ end
70
+
71
+ module Subprocess
72
+ ORIG_ARGV = ARGV.dup unless defined?(ORIG_ARGV)
73
+
74
+ # Crazy H4X to get this working in windows / jruby with
75
+ # no forking.
76
+ def run_in_isolation(&blk)
77
+ require "tempfile"
78
+
79
+ if ENV["ISOLATION_TEST"]
80
+ yield
81
+ File.open(ENV["ISOLATION_OUTPUT"], "w") do |file|
82
+ file.puts [Marshal.dump(self.dup)].pack("m")
83
+ end
84
+ exit!
85
+ else
86
+ Tempfile.open("isolation") do |tmpfile|
87
+ env = {
88
+ 'ISOLATION_TEST' => self.class.name,
89
+ 'ISOLATION_OUTPUT' => tmpfile.path
90
+ }
91
+
92
+ load_paths = $-I.map {|p| "-I\"#{File.expand_path(p)}\"" }.join(" ")
93
+ orig_args = ORIG_ARGV.join(" ")
94
+ test_opts = "-n#{self.class.name}##{self.name}"
95
+ command = "#{Gem.ruby} #{load_paths} #{$0} '#{orig_args}' #{test_opts}"
96
+
97
+ # IO.popen lets us pass env in a cross-platform way
98
+ child = IO.popen(env, command)
99
+
100
+ begin
101
+ Process.wait(child.pid)
102
+ rescue Errno::ECHILD # The child process may exit before we wait
103
+ nil
104
+ end
105
+
106
+ return tmpfile.read.unpack("m")[0]
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ include forking_env? ? Forking : Subprocess
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,41 @@
1
+ require 'minitest/mock'
2
+
3
+ module ActiveSupport
4
+ module Testing
5
+ module MethodCallAssertions # :nodoc:
6
+ private
7
+ def assert_called(object, method_name, message = nil, times: 1, returns: nil)
8
+ times_called = 0
9
+
10
+ object.stub(method_name, proc { times_called += 1; returns }) { yield }
11
+
12
+ error = "Expected #{method_name} to be called #{times} times, " \
13
+ "but was called #{times_called} times"
14
+ error = "#{message}.\n#{error}" if message
15
+ assert_equal times, times_called, error
16
+ end
17
+
18
+ def assert_called_with(object, method_name, args = [], returns: nil)
19
+ mock = Minitest::Mock.new
20
+
21
+ if args.all? { |arg| arg.is_a?(Array) }
22
+ args.each { |arg| mock.expect(:call, returns, arg) }
23
+ else
24
+ mock.expect(:call, returns, args)
25
+ end
26
+
27
+ object.stub(method_name, mock) { yield }
28
+
29
+ mock.verify
30
+ end
31
+
32
+ def assert_not_called(object, method_name, message = nil, &block)
33
+ assert_called(object, method_name, message, times: 0, &block)
34
+ end
35
+
36
+ def stub_any_instance(klass, instance: klass.new)
37
+ klass.stub(:new, instance) { yield instance }
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,50 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/callbacks'
3
+
4
+ module ActiveSupport
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
19
+ module SetupAndTeardown
20
+ extend ActiveSupport::Concern
21
+
22
+ included do
23
+ include ActiveSupport::Callbacks
24
+ define_callbacks :setup, :teardown
25
+ end
26
+
27
+ module ClassMethods
28
+ # Add a callback, which runs before <tt>TestCase#setup</tt>.
29
+ def setup(*args, &block)
30
+ set_callback(:setup, :before, *args, &block)
31
+ end
32
+
33
+ # Add a callback, which runs after <tt>TestCase#teardown</tt>.
34
+ def teardown(*args, &block)
35
+ set_callback(:teardown, :after, *args, &block)
36
+ end
37
+ end
38
+
39
+ def before_setup # :nodoc:
40
+ super
41
+ run_callbacks :setup
42
+ end
43
+
44
+ def after_teardown # :nodoc:
45
+ run_callbacks :teardown
46
+ super
47
+ end
48
+ end
49
+ end
50
+ end