activesupport 3.2.22.5 → 4.0.0.beta1

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 (214) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +325 -136
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +4 -2
  5. data/lib/active_support.rb +8 -21
  6. data/lib/active_support/backtrace_cleaner.rb +33 -25
  7. data/lib/active_support/basic_object.rb +7 -17
  8. data/lib/active_support/benchmarkable.rb +19 -15
  9. data/lib/active_support/buffered_logger.rb +9 -113
  10. data/lib/active_support/cache.rb +203 -171
  11. data/lib/active_support/cache/file_store.rb +12 -12
  12. data/lib/active_support/cache/mem_cache_store.rb +24 -30
  13. data/lib/active_support/cache/memory_store.rb +2 -0
  14. data/lib/active_support/callbacks.rb +195 -247
  15. data/lib/active_support/concern.rb +16 -23
  16. data/lib/active_support/concurrency/latch.rb +27 -0
  17. data/lib/active_support/configurable.rb +69 -12
  18. data/lib/active_support/core_ext.rb +1 -0
  19. data/lib/active_support/core_ext/array.rb +0 -1
  20. data/lib/active_support/core_ext/array/access.rb +17 -9
  21. data/lib/active_support/core_ext/array/conversions.rb +113 -55
  22. data/lib/active_support/core_ext/array/extract_options.rb +2 -2
  23. data/lib/active_support/core_ext/array/grouping.rb +21 -22
  24. data/lib/active_support/core_ext/array/uniq_by.rb +12 -9
  25. data/lib/active_support/core_ext/array/wrap.rb +11 -14
  26. data/lib/active_support/core_ext/big_decimal/conversions.rb +7 -24
  27. data/lib/active_support/core_ext/class/attribute.rb +12 -8
  28. data/lib/active_support/core_ext/class/attribute_accessors.rb +14 -12
  29. data/lib/active_support/core_ext/class/delegating_attributes.rb +15 -19
  30. data/lib/active_support/core_ext/class/subclasses.rb +11 -5
  31. data/lib/active_support/core_ext/date.rb +6 -0
  32. data/lib/active_support/core_ext/date/calculations.rb +34 -188
  33. data/lib/active_support/core_ext/date/conversions.rb +16 -38
  34. data/lib/active_support/core_ext/date/infinite_comparable.rb +5 -0
  35. data/lib/active_support/core_ext/date/zones.rb +25 -2
  36. data/lib/active_support/core_ext/date_and_time/calculations.rb +232 -0
  37. data/lib/active_support/core_ext/date_time.rb +5 -0
  38. data/lib/active_support/core_ext/date_time/acts_like.rb +0 -1
  39. data/lib/active_support/core_ext/date_time/calculations.rb +73 -65
  40. data/lib/active_support/core_ext/date_time/conversions.rb +21 -33
  41. data/lib/active_support/core_ext/date_time/infinite_comparable.rb +5 -0
  42. data/lib/active_support/core_ext/date_time/zones.rb +11 -8
  43. data/lib/active_support/core_ext/enumerable.rb +26 -73
  44. data/lib/active_support/core_ext/file.rb +0 -1
  45. data/lib/active_support/core_ext/file/atomic.rb +27 -11
  46. data/lib/active_support/core_ext/hash.rb +0 -1
  47. data/lib/active_support/core_ext/hash/conversions.rb +145 -79
  48. data/lib/active_support/core_ext/hash/deep_merge.rb +14 -8
  49. data/lib/active_support/core_ext/hash/diff.rb +5 -4
  50. data/lib/active_support/core_ext/hash/except.rb +1 -9
  51. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -5
  52. data/lib/active_support/core_ext/hash/keys.rb +108 -24
  53. data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
  54. data/lib/active_support/core_ext/hash/slice.rb +12 -12
  55. data/lib/active_support/core_ext/infinite_comparable.rb +35 -0
  56. data/lib/active_support/core_ext/integer/inflections.rb +13 -1
  57. data/lib/active_support/core_ext/integer/time.rb +17 -12
  58. data/lib/active_support/core_ext/kernel/debugger.rb +2 -2
  59. data/lib/active_support/core_ext/kernel/reporting.rb +36 -22
  60. data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
  61. data/lib/active_support/core_ext/load_error.rb +7 -5
  62. data/lib/active_support/core_ext/logger.rb +7 -23
  63. data/lib/active_support/core_ext/marshal.rb +19 -0
  64. data/lib/active_support/core_ext/module.rb +1 -3
  65. data/lib/active_support/core_ext/module/aliasing.rb +8 -9
  66. data/lib/active_support/core_ext/module/anonymous.rb +2 -7
  67. data/lib/active_support/core_ext/module/attr_internal.rb +0 -1
  68. data/lib/active_support/core_ext/module/attribute_accessors.rb +12 -10
  69. data/lib/active_support/core_ext/module/delegation.rb +57 -40
  70. data/lib/active_support/core_ext/module/deprecation.rb +19 -3
  71. data/lib/active_support/core_ext/module/introspection.rb +17 -27
  72. data/lib/active_support/core_ext/module/qualified_const.rb +8 -20
  73. data/lib/active_support/core_ext/module/remove_method.rb +1 -5
  74. data/lib/active_support/core_ext/numeric.rb +2 -0
  75. data/lib/active_support/core_ext/numeric/conversions.rb +135 -0
  76. data/lib/active_support/core_ext/numeric/infinite_comparable.rb +9 -0
  77. data/lib/active_support/core_ext/numeric/time.rb +6 -6
  78. data/lib/active_support/core_ext/object.rb +1 -0
  79. data/lib/active_support/core_ext/object/acts_like.rb +4 -4
  80. data/lib/active_support/core_ext/object/blank.rb +7 -23
  81. data/lib/active_support/core_ext/object/deep_dup.rb +46 -0
  82. data/lib/active_support/core_ext/object/duplicable.rb +1 -30
  83. data/lib/active_support/core_ext/object/inclusion.rb +6 -6
  84. data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
  85. data/lib/active_support/core_ext/object/to_json.rb +8 -0
  86. data/lib/active_support/core_ext/object/to_param.rb +5 -2
  87. data/lib/active_support/core_ext/object/try.rb +46 -25
  88. data/lib/active_support/core_ext/object/with_options.rb +7 -8
  89. data/lib/active_support/core_ext/proc.rb +3 -0
  90. data/lib/active_support/core_ext/range.rb +0 -2
  91. data/lib/active_support/core_ext/range/conversions.rb +0 -2
  92. data/lib/active_support/core_ext/range/include_range.rb +1 -1
  93. data/lib/active_support/core_ext/range/overlaps.rb +1 -1
  94. data/lib/active_support/core_ext/string.rb +2 -2
  95. data/lib/active_support/core_ext/string/access.rb +95 -90
  96. data/lib/active_support/core_ext/string/conversions.rb +29 -38
  97. data/lib/active_support/core_ext/string/encoding.rb +6 -9
  98. data/lib/active_support/core_ext/string/filters.rb +24 -18
  99. data/lib/active_support/core_ext/string/indent.rb +43 -0
  100. data/lib/active_support/core_ext/string/inflections.rb +70 -60
  101. data/lib/active_support/core_ext/string/inquiry.rb +2 -2
  102. data/lib/active_support/core_ext/string/multibyte.rb +41 -64
  103. data/lib/active_support/core_ext/string/output_safety.rb +59 -51
  104. data/lib/active_support/core_ext/string/zones.rb +13 -0
  105. data/lib/active_support/core_ext/struct.rb +6 -0
  106. data/lib/active_support/core_ext/thread.rb +74 -0
  107. data/lib/active_support/core_ext/time.rb +6 -0
  108. data/lib/active_support/core_ext/time/calculations.rb +105 -193
  109. data/lib/active_support/core_ext/time/conversions.rb +27 -51
  110. data/lib/active_support/core_ext/time/infinite_comparable.rb +5 -0
  111. data/lib/active_support/core_ext/time/marshal.rb +0 -27
  112. data/lib/active_support/core_ext/time/zones.rb +27 -17
  113. data/lib/active_support/core_ext/uri.rb +13 -17
  114. data/lib/active_support/dependencies.rb +160 -141
  115. data/lib/active_support/dependencies/autoload.rb +47 -20
  116. data/lib/active_support/deprecation.rb +39 -14
  117. data/lib/active_support/deprecation/behaviors.rb +44 -30
  118. data/lib/active_support/deprecation/instance_delegator.rb +24 -0
  119. data/lib/active_support/deprecation/method_wrappers.rb +33 -18
  120. data/lib/active_support/deprecation/proxy_wrappers.rb +58 -13
  121. data/lib/active_support/deprecation/reporting.rb +40 -11
  122. data/lib/active_support/descendants_tracker.rb +34 -19
  123. data/lib/active_support/duration.rb +6 -8
  124. data/lib/active_support/file_update_checker.rb +63 -47
  125. data/lib/active_support/gzip.rb +11 -5
  126. data/lib/active_support/hash_with_indifferent_access.rb +112 -37
  127. data/lib/active_support/i18n.rb +4 -0
  128. data/lib/active_support/i18n_railtie.rb +5 -22
  129. data/lib/active_support/inflections.rb +14 -12
  130. data/lib/active_support/inflector/inflections.rb +108 -71
  131. data/lib/active_support/inflector/methods.rb +181 -160
  132. data/lib/active_support/inflector/transliterate.rb +16 -17
  133. data/lib/active_support/json/decoding.rb +18 -17
  134. data/lib/active_support/json/encoding.rb +93 -39
  135. data/lib/active_support/json/variable.rb +10 -1
  136. data/lib/active_support/key_generator.rb +75 -0
  137. data/lib/active_support/lazy_load_hooks.rb +21 -19
  138. data/lib/active_support/locale/en.yml +100 -3
  139. data/lib/active_support/log_subscriber.rb +56 -36
  140. data/lib/active_support/log_subscriber/test_helper.rb +18 -15
  141. data/lib/active_support/logger.rb +57 -0
  142. data/lib/active_support/logger_silence.rb +24 -0
  143. data/lib/active_support/message_encryptor.rb +32 -29
  144. data/lib/active_support/message_verifier.rb +8 -14
  145. data/lib/active_support/multibyte.rb +5 -28
  146. data/lib/active_support/multibyte/chars.rb +80 -333
  147. data/lib/active_support/multibyte/unicode.rb +74 -64
  148. data/lib/active_support/notifications.rb +57 -25
  149. data/lib/active_support/notifications/fanout.rb +105 -18
  150. data/lib/active_support/notifications/instrumenter.rb +32 -13
  151. data/lib/active_support/number_helper.rb +636 -0
  152. data/lib/active_support/ordered_hash.rb +8 -190
  153. data/lib/active_support/ordered_options.rb +21 -23
  154. data/lib/active_support/proxy_object.rb +13 -0
  155. data/lib/active_support/rails.rb +27 -0
  156. data/lib/active_support/railtie.rb +12 -32
  157. data/lib/active_support/rescuable.rb +9 -4
  158. data/lib/active_support/string_inquirer.rb +13 -8
  159. data/lib/active_support/tagged_logging.rb +51 -73
  160. data/lib/active_support/test_case.rb +46 -17
  161. data/lib/active_support/testing/assertions.rb +56 -26
  162. data/lib/active_support/testing/autorun.rb +5 -0
  163. data/lib/active_support/testing/constant_lookup.rb +52 -0
  164. data/lib/active_support/testing/declarative.rb +1 -1
  165. data/lib/active_support/testing/deprecation.rb +0 -19
  166. data/lib/active_support/testing/isolation.rb +25 -58
  167. data/lib/active_support/testing/pending.rb +5 -43
  168. data/lib/active_support/testing/setup_and_teardown.rb +6 -92
  169. data/lib/active_support/testing/tagged_logging.rb +25 -0
  170. data/lib/active_support/time.rb +6 -21
  171. data/lib/active_support/time_with_zone.rb +78 -43
  172. data/lib/active_support/values/time_zone.rb +77 -58
  173. data/lib/active_support/values/unicode_tables.dat +0 -0
  174. data/lib/active_support/version.rb +4 -4
  175. data/lib/active_support/xml_mini.rb +35 -17
  176. data/lib/active_support/xml_mini/jdom.rb +9 -17
  177. data/lib/active_support/xml_mini/libxml.rb +1 -2
  178. data/lib/active_support/xml_mini/libxmlsax.rb +1 -2
  179. data/lib/active_support/xml_mini/nokogiri.rb +1 -2
  180. data/lib/active_support/xml_mini/nokogirisax.rb +1 -2
  181. data/lib/active_support/xml_mini/rexml.rb +6 -8
  182. metadata +107 -77
  183. data/lib/active_support/base64.rb +0 -54
  184. data/lib/active_support/core_ext/array/random_access.rb +0 -30
  185. data/lib/active_support/core_ext/date/freeze.rb +0 -33
  186. data/lib/active_support/core_ext/exception.rb +0 -3
  187. data/lib/active_support/core_ext/file/path.rb +0 -5
  188. data/lib/active_support/core_ext/float.rb +0 -1
  189. data/lib/active_support/core_ext/float/rounding.rb +0 -19
  190. data/lib/active_support/core_ext/hash/deep_dup.rb +0 -18
  191. data/lib/active_support/core_ext/io.rb +0 -15
  192. data/lib/active_support/core_ext/module/method_names.rb +0 -14
  193. data/lib/active_support/core_ext/module/synchronization.rb +0 -45
  194. data/lib/active_support/core_ext/process.rb +0 -1
  195. data/lib/active_support/core_ext/process/daemon.rb +0 -23
  196. data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
  197. data/lib/active_support/core_ext/range/cover.rb +0 -3
  198. data/lib/active_support/core_ext/rexml.rb +0 -46
  199. data/lib/active_support/core_ext/string/interpolation.rb +0 -2
  200. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
  201. data/lib/active_support/memoizable.rb +0 -116
  202. data/lib/active_support/multibyte/exceptions.rb +0 -8
  203. data/lib/active_support/multibyte/utils.rb +0 -60
  204. data/lib/active_support/ruby/shim.rb +0 -22
  205. data/lib/active_support/security_utils.rb +0 -27
  206. data/lib/active_support/testing/mochaing.rb +0 -7
  207. data/lib/active_support/testing/performance.rb +0 -317
  208. data/lib/active_support/testing/performance/jruby.rb +0 -115
  209. data/lib/active_support/testing/performance/rubinius.rb +0 -113
  210. data/lib/active_support/testing/performance/ruby.rb +0 -152
  211. data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
  212. data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
  213. data/lib/active_support/time/autoload.rb +0 -5
  214. data/lib/active_support/whiny_nil.rb +0 -24
@@ -1,97 +1,75 @@
1
1
  require 'active_support/core_ext/object/blank'
2
- require 'active_support/deprecation'
3
2
  require 'logger'
3
+ require 'active_support/logger'
4
4
 
5
5
  module ActiveSupport
6
- # Wraps any standard Logger class to provide tagging capabilities. Examples:
6
+ # Wraps any standard Logger object to provide tagging capabilities.
7
7
  #
8
- # Logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
9
- # Logger.tagged("BCX") { Logger.info "Stuff" } # Logs "[BCX] Stuff"
10
- # Logger.tagged("BCX", "Jason") { Logger.info "Stuff" } # Logs "[BCX] [Jason] Stuff"
11
- # Logger.tagged("BCX") { Logger.tagged("Jason") { Logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff"
8
+ # logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
9
+ # logger.tagged('BCX') { logger.info 'Stuff' } # Logs "[BCX] Stuff"
10
+ # logger.tagged('BCX', "Jason") { logger.info 'Stuff' } # Logs "[BCX] [Jason] Stuff"
11
+ # logger.tagged('BCX') { logger.tagged('Jason') { logger.info 'Stuff' } } # Logs "[BCX] [Jason] Stuff"
12
12
  #
13
- # This is used by the default Rails.logger as configured by Railties to make it easy to stamp log lines
14
- # with subdomains, request ids, and anything else to aid debugging of multi-user production applications.
15
- class TaggedLogging
16
- def initialize(logger)
17
- @logger = logger
18
- end
19
-
20
- def tagged(*tags)
21
- new_tags = push_tags(*tags)
22
- yield self
23
- ensure
24
- pop_tags(new_tags.size)
25
- end
13
+ # This is used by the default Rails.logger as configured by Railties to make
14
+ # it easy to stamp log lines with subdomains, request ids, and anything else
15
+ # to aid debugging of multi-user production applications.
16
+ module TaggedLogging
17
+ module Formatter # :nodoc:
18
+ # This method is invoked when a log event occurs.
19
+ def call(severity, timestamp, progname, msg)
20
+ super(severity, timestamp, progname, "#{tags_text}#{msg}")
21
+ end
26
22
 
27
- def push_tags(*tags)
28
- tags.flatten.reject(&:blank?).tap do |new_tags|
29
- current_tags.concat new_tags
23
+ def tagged(*tags)
24
+ new_tags = push_tags(*tags)
25
+ yield self
26
+ ensure
27
+ pop_tags(new_tags.size)
30
28
  end
31
- end
32
29
 
33
- def pop_tags(size = 1)
34
- current_tags.pop size
35
- end
30
+ def push_tags(*tags)
31
+ tags.flatten.reject(&:blank?).tap do |new_tags|
32
+ current_tags.concat new_tags
33
+ end
34
+ end
36
35
 
37
- def clear_tags!
38
- current_tags.clear
39
- end
36
+ def pop_tags(size = 1)
37
+ current_tags.pop size
38
+ end
40
39
 
41
- def silence(temporary_level = Logger::ERROR, &block)
42
- @logger.silence(temporary_level, &block)
43
- end
44
- deprecate :silence
40
+ def clear_tags!
41
+ current_tags.clear
42
+ end
45
43
 
46
- def add(severity, message = nil, progname = nil, &block)
47
- if message.nil?
48
- if block_given?
49
- message = block.call
50
- else
51
- message = progname
52
- progname = nil #No instance variable for this like Logger
53
- end
44
+ def current_tags
45
+ Thread.current[:activesupport_tagged_logging_tags] ||= []
54
46
  end
55
- @logger.add(severity, "#{tags_text}#{message}", progname)
56
- end
57
47
 
58
- %w( fatal error warn info debug unknown ).each do |severity|
59
- eval <<-EOM, nil, __FILE__, __LINE__ + 1
60
- def #{severity}(progname = nil, &block)
61
- add(Logger::#{severity.upcase}, nil, progname, &block)
48
+ private
49
+ def tags_text
50
+ tags = current_tags
51
+ if tags.any?
52
+ tags.collect { |tag| "[#{tag}] " }.join
53
+ end
62
54
  end
63
- EOM
64
55
  end
65
56
 
66
- def flush
67
- clear_tags!
68
- @logger.flush if @logger.respond_to?(:flush)
57
+ def self.new(logger)
58
+ # Ensure we set a default formatter so we aren't extending nil!
59
+ logger.formatter ||= ActiveSupport::Logger::SimpleFormatter.new
60
+ logger.formatter.extend Formatter
61
+ logger.extend(self)
69
62
  end
70
63
 
71
- def method_missing(method, *args)
72
- @logger.send(method, *args)
73
- end
64
+ delegate :push_tags, :pop_tags, :clear_tags!, to: :formatter
74
65
 
75
- if RUBY_VERSION < '1.9'
76
- def respond_to?(*args)
77
- super || @logger.respond_to?(*args)
78
- end
79
- else
80
- def respond_to_missing?(*args)
81
- @logger.respond_to? *args
82
- end
66
+ def tagged(*tags)
67
+ formatter.tagged(*tags) { yield self }
83
68
  end
84
69
 
85
- private
86
- def tags_text
87
- tags = current_tags
88
- if tags.any?
89
- tags.collect { |tag| "[#{tag}] " }.join
90
- end
91
- end
92
-
93
- def current_tags
94
- Thread.current[:activesupport_tagged_logging_tags] ||= []
95
- end
70
+ def flush
71
+ clear_tags!
72
+ super if defined?(super)
73
+ end
96
74
  end
97
75
  end
@@ -1,38 +1,67 @@
1
- begin
2
- require 'test/unit/testcase'
3
- rescue LoadError => e
4
- raise LoadError, "Please add test-unit gem to your Gemfile: `gem 'test-unit', '~> 3.0'` (#{e.message})", e.backtrace
5
- end
1
+ gem 'minitest' # make sure we get the gem, not stdlib
2
+ require 'minitest/unit'
3
+ require 'active_support/testing/tagged_logging'
6
4
  require 'active_support/testing/setup_and_teardown'
7
5
  require 'active_support/testing/assertions'
8
6
  require 'active_support/testing/deprecation'
9
- require 'active_support/testing/declarative'
10
7
  require 'active_support/testing/pending'
8
+ require 'active_support/testing/declarative'
11
9
  require 'active_support/testing/isolation'
12
- require 'active_support/testing/mochaing'
10
+ require 'active_support/testing/constant_lookup'
13
11
  require 'active_support/core_ext/kernel/reporting'
12
+ require 'active_support/deprecation'
14
13
 
15
- module ActiveSupport
16
- class TestCase < ::Test::Unit::TestCase
17
- if defined?(MiniTest::Assertions) && TestCase < MiniTest::Assertions
18
- Assertion = MiniTest::Assertion
19
- alias_method :method_name, :name if method_defined? :name
20
- alias_method :method_name, :__name__ if method_defined? :__name__
21
- else
22
- Assertion = Test::Unit::AssertionFailedError
14
+ begin
15
+ silence_warnings { require 'mocha/setup' }
16
+ rescue LoadError
17
+ end
23
18
 
24
- undef :default_test
25
- end
19
+ module ActiveSupport
20
+ class TestCase < ::MiniTest::Unit::TestCase
21
+ Assertion = MiniTest::Assertion
22
+ alias_method :method_name, :__name__
26
23
 
27
24
  $tags = {}
28
25
  def self.for_tag(tag)
29
26
  yield if $tags[tag]
30
27
  end
31
28
 
29
+ # FIXME: we have tests that depend on run order, we should fix that and
30
+ # remove this method.
31
+ def self.test_order # :nodoc:
32
+ :sorted
33
+ end
34
+
35
+ include ActiveSupport::Testing::TaggedLogging
32
36
  include ActiveSupport::Testing::SetupAndTeardown
33
37
  include ActiveSupport::Testing::Assertions
34
38
  include ActiveSupport::Testing::Deprecation
35
39
  include ActiveSupport::Testing::Pending
36
40
  extend ActiveSupport::Testing::Declarative
41
+
42
+ # test/unit backwards compatibility methods
43
+ alias :assert_raise :assert_raises
44
+ alias :assert_not_empty :refute_empty
45
+ alias :assert_not_equal :refute_equal
46
+ alias :assert_not_in_delta :refute_in_delta
47
+ alias :assert_not_in_epsilon :refute_in_epsilon
48
+ alias :assert_not_includes :refute_includes
49
+ alias :assert_not_instance_of :refute_instance_of
50
+ alias :assert_not_kind_of :refute_kind_of
51
+ alias :assert_no_match :refute_match
52
+ alias :assert_not_nil :refute_nil
53
+ alias :assert_not_operator :refute_operator
54
+ alias :assert_not_predicate :refute_predicate
55
+ alias :assert_not_respond_to :refute_respond_to
56
+ alias :assert_not_same :refute_same
57
+
58
+ # Fails if the block raises an exception.
59
+ #
60
+ # assert_nothing_raised do
61
+ # ...
62
+ # end
63
+ def assert_nothing_raised(*args)
64
+ yield
65
+ end
37
66
  end
38
67
  end
@@ -1,51 +1,67 @@
1
- require 'active_support/core_ext/array/wrap'
2
1
  require 'active_support/core_ext/object/blank'
3
2
 
4
3
  module ActiveSupport
5
4
  module Testing
6
5
  module Assertions
7
- # Test numeric difference between the return value of an expression as a result of what is evaluated
8
- # in the yielded block.
6
+ # Assert 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' # => 'foo' is not 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.
9
24
  #
10
25
  # assert_difference 'Article.count' do
11
- # post :create, :article => {...}
26
+ # post :create, article: {...}
12
27
  # end
13
28
  #
14
29
  # An arbitrary expression is passed in and evaluated.
15
30
  #
16
31
  # assert_difference 'assigns(:article).comments(:reload).size' do
17
- # post :create, :comment => {...}
32
+ # post :create, comment: {...}
18
33
  # end
19
34
  #
20
- # An arbitrary positive or negative difference can be specified. The default is +1.
35
+ # An arbitrary positive or negative difference can be specified.
36
+ # The default is <tt>1</tt>.
21
37
  #
22
38
  # assert_difference 'Article.count', -1 do
23
- # post :delete, :id => ...
39
+ # post :delete, id: ...
24
40
  # end
25
41
  #
26
42
  # An array of expressions can also be passed in and evaluated.
27
43
  #
28
- # assert_difference [ 'Article.count', 'Post.count' ], +2 do
29
- # post :create, :article => {...}
44
+ # assert_difference [ 'Article.count', 'Post.count' ], 2 do
45
+ # post :create, article: {...}
30
46
  # end
31
47
  #
32
48
  # A lambda or a list of lambdas can be passed in and evaluated:
33
49
  #
34
- # assert_difference lambda { Article.count }, 2 do
35
- # post :create, :article => {...}
50
+ # assert_difference ->{ Article.count }, 2 do
51
+ # post :create, article: {...}
36
52
  # end
37
53
  #
38
54
  # assert_difference [->{ Article.count }, ->{ Post.count }], 2 do
39
- # post :create, :article => {...}
55
+ # post :create, article: {...}
40
56
  # end
41
57
  #
42
- # A error message can be specified.
58
+ # An error message can be specified.
43
59
  #
44
- # assert_difference 'Article.count', -1, "An Article should be destroyed" do
45
- # post :delete, :id => ...
60
+ # assert_difference 'Article.count', -1, 'An Article should be destroyed' do
61
+ # post :delete, id: ...
46
62
  # end
47
63
  def assert_difference(expression, difference = 1, message = nil, &block)
48
- expressions = Array.wrap expression
64
+ expressions = Array(expression)
49
65
 
50
66
  exps = expressions.map { |e|
51
67
  e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
@@ -61,34 +77,48 @@ module ActiveSupport
61
77
  end
62
78
  end
63
79
 
64
- # Assertion that the numeric result of evaluating an expression is not changed before and after
65
- # invoking the passed in block.
80
+ # Assertion that the numeric result of evaluating an expression is not
81
+ # changed before and after invoking the passed in block.
66
82
  #
67
83
  # assert_no_difference 'Article.count' do
68
- # post :create, :article => invalid_attributes
84
+ # post :create, article: invalid_attributes
69
85
  # end
70
86
  #
71
- # A error message can be specified.
87
+ # An error message can be specified.
72
88
  #
73
- # assert_no_difference 'Article.count', "An Article should not be created" do
74
- # post :create, :article => invalid_attributes
89
+ # assert_no_difference 'Article.count', 'An Article should not be created' do
90
+ # post :create, article: invalid_attributes
75
91
  # end
76
92
  def assert_no_difference(expression, message = nil, &block)
77
93
  assert_difference expression, 0, message, &block
78
94
  end
79
95
 
80
- # Test if an expression is blank. Passes if object.blank? is true.
96
+ # Test if an expression is blank. Passes if <tt>object.blank?</tt>
97
+ # is +true+.
81
98
  #
82
- # assert_blank [] # => true
99
+ # assert_blank [] # => true
100
+ # assert_blank [[]] # => [[]] is not blank
101
+ #
102
+ # An error message can be specified.
103
+ #
104
+ # assert_blank [], 'this should be blank'
83
105
  def assert_blank(object, message=nil)
106
+ ActiveSupport::Deprecation.warn('"assert_blank" is deprecated. Please use "assert object.blank?" instead')
84
107
  message ||= "#{object.inspect} is not blank"
85
108
  assert object.blank?, message
86
109
  end
87
110
 
88
- # Test if an expression is not blank. Passes if object.present? is true.
111
+ # Test if an expression is not blank. Passes if <tt>object.present?</tt>
112
+ # is +true+.
113
+ #
114
+ # assert_present({ data: 'x' }) # => true
115
+ # assert_present({}) # => {} is blank
116
+ #
117
+ # An error message can be specified.
89
118
  #
90
- # assert_present {:data => 'x' } # => true
119
+ # assert_present({ data: 'x' }, 'this should not be blank')
91
120
  def assert_present(object, message=nil)
121
+ ActiveSupport::Deprecation.warn('"assert_present" is deprecated. Please use "assert object.present?" instead')
92
122
  message ||= "#{object.inspect} is blank"
93
123
  assert object.present?, message
94
124
  end
@@ -0,0 +1,5 @@
1
+ gem 'minitest'
2
+
3
+ require 'minitest/unit'
4
+
5
+ MiniTest::Unit.autorun
@@ -0,0 +1,52 @@
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("::").constantize
40
+ break(constant) if yield(constant)
41
+ rescue NameError
42
+ # Constant wasn't found, move on
43
+ ensure
44
+ names.pop
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -2,7 +2,7 @@ module ActiveSupport
2
2
  module Testing
3
3
  module Declarative
4
4
 
5
- def self.extended(klass)
5
+ def self.extended(klass) #:nodoc:
6
6
  klass.class_eval do
7
7
 
8
8
  unless method_defined?(:describe)
@@ -34,22 +34,3 @@ module ActiveSupport
34
34
  end
35
35
  end
36
36
  end
37
-
38
- begin
39
- require 'test/unit/error'
40
- rescue LoadError
41
- # Using miniunit, ignore.
42
- else
43
- module Test
44
- module Unit
45
- class Error #:nodoc:
46
- # Silence warnings when reporting test errors.
47
- def message_with_silenced_deprecation
48
- ::ActiveSupport::Deprecation.silence { message_without_silenced_deprecation }
49
- end
50
- alias_method :message_without_silenced_deprecation, :message
51
- alias_method :message, :message_with_silenced_deprecation
52
- end
53
- end
54
- end
55
- end