diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-02-14 19:28:05 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-02-14 19:28:05 +0000 |
commit | 6f3c5f67870a625b8be4de6e34e8d0d2f5d2b5e3 (patch) | |
tree | 3da8d75101aabe3c1d90d0582505ec1480c9d885 /activesupport | |
parent | 77bf78b3b78a41d4f2f6e733f5c9c00608c0adba (diff) | |
parent | a1b60696e2b13cbe94d748444cc0da37b190fbb8 (diff) | |
download | rails-6f3c5f67870a625b8be4de6e34e8d0d2f5d2b5e3.tar.gz rails-6f3c5f67870a625b8be4de6e34e8d0d2f5d2b5e3.tar.bz2 rails-6f3c5f67870a625b8be4de6e34e8d0d2f5d2b5e3.zip |
Merge remote branch 'mainstream/master'
Conflicts:
railties/README
railties/guides/source/active_support_core_extensions.textile
railties/guides/source/getting_started.textile
railties/lib/generators/rails/app/templates/README
Diffstat (limited to 'activesupport')
34 files changed, 496 insertions, 347 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 431607f4e0..8ec903e376 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,10 @@ *Rails 3.0 (pending)* +* JSON backend for YAJL. Preferred if available. #2666 [Brian Lopez] + + +*Rails 3.0.0 [beta] (February 4, 2010)* + * Introduce class_attribute to declare inheritable class attributes. Writing an attribute on a subclass behaves just like overriding the superclass reader method. Unifies and replaces most usage of cattr_accessor, class_inheritable_attribute, superclass_delegating_attribute, and extlib_inheritable_attribute. [Jeremy Kemper, Yehuda Katz] * Time#- with a DateTime argument behaves the same as with a Time argument, i.e. returns the difference between self and arg as a Float #3476 [Geoff Buesing] diff --git a/activesupport/Rakefile b/activesupport/Rakefile index 03ab3b2760..357bdca715 100644 --- a/activesupport/Rakefile +++ b/activesupport/Rakefile @@ -49,11 +49,11 @@ Rake::GemPackageTask.new(spec) do |p| p.gem_spec = spec end -desc "Publish the beta gem" -task :pgem => [:package] do - require 'rake/contrib/sshpublisher' - Rake::SshFilePublisher.new("gems.rubyonrails.org", "/u/sites/gems/gems", "pkg", "#{PKG_FILE_NAME}.gem").upload - `ssh gems.rubyonrails.org '/u/sites/gems/gemupdate.sh'` +desc "Release to gemcutter" +task :release => :package do + require 'rake/gemcutter' + Rake::Gemcutter::Tasks.new(spec).define + Rake::Task['gem:push'].invoke end desc "Publish the API documentation" @@ -61,15 +61,3 @@ task :pdoc => [:rdoc] do require 'rake/contrib/sshpublisher' Rake::SshDirPublisher.new("wrath.rubyonrails.org", "public_html/as", "doc").upload end - -desc "Publish the release files to RubyForge." -task :release => [ :package ] do - require 'rubyforge' - require 'rake/contrib/rubyforgepublisher' - - packages = %w( gem tgz zip ).collect{ |ext| "pkg/#{PKG_NAME}-#{PKG_VERSION}.#{ext}" } - - rubyforge = RubyForge.new - rubyforge.login - rubyforge.add_release(PKG_NAME, PKG_NAME, "REL #{PKG_VERSION}", *packages) -end diff --git a/activesupport/activesupport.gemspec b/activesupport/activesupport.gemspec index c366ccfa8e..d0557230aa 100644 --- a/activesupport/activesupport.gemspec +++ b/activesupport/activesupport.gemspec @@ -1,9 +1,10 @@ Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.name = 'activesupport' - s.version = '3.0.0.beta' + s.version = '3.0.0.beta1' s.summary = 'Support and utility classes used by the Rails framework.' s.description = 'Support and utility classes used by the Rails framework.' + s.required_ruby_version = '>= 1.8.7' s.author = 'David Heinemeier Hansson' s.email = 'david@loudthinking.com' diff --git a/activesupport/lib/active_support/core_ext/class.rb b/activesupport/lib/active_support/core_ext/class.rb index 62df7d8b82..f2ca9c7cc9 100644 --- a/activesupport/lib/active_support/core_ext/class.rb +++ b/activesupport/lib/active_support/core_ext/class.rb @@ -1,3 +1,4 @@ require 'active_support/core_ext/class/attribute_accessors' require 'active_support/core_ext/class/inheritable_attributes' require 'active_support/core_ext/class/delegating_attributes' +require 'active_support/core_ext/class/subclasses' diff --git a/activesupport/lib/active_support/core_ext/class/subclasses.rb b/activesupport/lib/active_support/core_ext/class/subclasses.rb new file mode 100644 index 0000000000..bbd8f5aef6 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/class/subclasses.rb @@ -0,0 +1,55 @@ +require 'active_support/core_ext/module/anonymous' +require 'active_support/core_ext/module/reachable' + +class Class #:nodoc: + # Returns an array with the names of the subclasses of +self+ as strings. + # + # Integer.subclasses # => ["Bignum", "Fixnum"] + def subclasses + Class.subclasses_of(self).map { |o| o.to_s } + end + + # Rubinius + if defined?(Class.__subclasses__) + def descendents + subclasses = [] + __subclasses__.each {|k| subclasses << k; subclasses.concat k.descendents } + subclasses + end + else + # MRI + begin + ObjectSpace.each_object(Class.new) {} + + def descendents + subclasses = [] + ObjectSpace.each_object(class << self; self; end) do |k| + subclasses << k unless k == self + end + subclasses + end + # JRuby + rescue StandardError + def descendents + subclasses = [] + ObjectSpace.each_object(Class) do |k| + subclasses << k if k < self + end + subclasses.uniq! + subclasses + end + end + end + + # Exclude this class unless it's a subclass of our supers and is defined. + # We check defined? in case we find a removed class that has yet to be + # garbage collected. This also fails for anonymous classes -- please + # submit a patch if you have a workaround. + def self.subclasses_of(*superclasses) #:nodoc: + subclasses = [] + superclasses.each do |klass| + subclasses.concat klass.descendents.select {|k| k.anonymous? || k.reachable?} + end + subclasses + end +end diff --git a/activesupport/lib/active_support/core_ext/file/atomic.rb b/activesupport/lib/active_support/core_ext/file/atomic.rb index 249fb1362d..49d28e8a34 100644 --- a/activesupport/lib/active_support/core_ext/file/atomic.rb +++ b/activesupport/lib/active_support/core_ext/file/atomic.rb @@ -9,7 +9,7 @@ class File # If your temp directory is not on the same filesystem as the file you're # trying to write, you can provide a different temporary directory. # - # File.atomic_write("/data/something.important", "/data/tmp") do |f| + # File.atomic_write("/data/something.important", "/data/tmp") do |file| # file.write("hello") # end def self.atomic_write(file_name, temp_dir = Dir.tmpdir) diff --git a/activesupport/lib/active_support/core_ext/kernel.rb b/activesupport/lib/active_support/core_ext/kernel.rb index 1922d804bf..c3bed14f63 100644 --- a/activesupport/lib/active_support/core_ext/kernel.rb +++ b/activesupport/lib/active_support/core_ext/kernel.rb @@ -1,4 +1,3 @@ -require 'active_support/core_ext/kernel/daemonizing' require 'active_support/core_ext/kernel/reporting' require 'active_support/core_ext/kernel/agnostics' require 'active_support/core_ext/kernel/requires' diff --git a/activesupport/lib/active_support/core_ext/kernel/daemonizing.rb b/activesupport/lib/active_support/core_ext/kernel/daemonizing.rb deleted file mode 100644 index ed9d1f9bf2..0000000000 --- a/activesupport/lib/active_support/core_ext/kernel/daemonizing.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Kernel - # Turns the current script into a daemon process that detaches from the console. - # It can be shut down with a TERM signal. - def daemonize - Process.daemon - end -end diff --git a/activesupport/lib/active_support/core_ext/kernel/debugger.rb b/activesupport/lib/active_support/core_ext/kernel/debugger.rb index 59e03e3df7..22fcc1910b 100644 --- a/activesupport/lib/active_support/core_ext/kernel/debugger.rb +++ b/activesupport/lib/active_support/core_ext/kernel/debugger.rb @@ -1,6 +1,6 @@ module Kernel unless respond_to?(:debugger) - # Starts a debugging session if ruby-debug has been loaded (call script/server --debugger to do load it). + # Starts a debugging session if ruby-debug has been loaded (call rails server --debugger to do load it). def debugger message = "\n***** Debugger requested, but was not available: Start server with --debugger to enable *****\n" defined?(Rails) ? Rails.logger.info(message) : $stderr.puts(message) diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb index fbe89fe07c..bf272e9e73 100644 --- a/activesupport/lib/active_support/core_ext/module.rb +++ b/activesupport/lib/active_support/core_ext/module.rb @@ -1,10 +1,10 @@ require 'active_support/core_ext/module/aliasing' require 'active_support/core_ext/module/introspection' - -require 'active_support/core_ext/module/inclusion' +require 'active_support/core_ext/module/anonymous' +require 'active_support/core_ext/module/reachable' require 'active_support/core_ext/module/attribute_accessors' require 'active_support/core_ext/module/attr_internal' require 'active_support/core_ext/module/attr_accessor_with_default' require 'active_support/core_ext/module/delegation' -require 'active_support/core_ext/module/loading' require 'active_support/core_ext/module/synchronization' +require 'active_support/core_ext/module/deprecation'
\ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/module/anonymous.rb b/activesupport/lib/active_support/core_ext/module/anonymous.rb new file mode 100644 index 0000000000..df25a09ec9 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module/anonymous.rb @@ -0,0 +1,24 @@ +require 'active_support/core_ext/object/blank' + +class Module + # A module may or may not have a name. + # + # module M; end + # M.name # => "M" + # + # m = Module.new + # m.name # => "" + # + # A module gets a name when it is first assigned to a constant. Either + # via the +module+ or +class+ keyword or by an explicit assignment: + # + # m = Module.new # creates an anonymous module + # M = m # => m gets a name here as a side-effect + # m.name # => "M" + # + def anonymous? + # Uses blank? because the name of an anonymous class is an empty + # string in 1.8, and nil in 1.9. + name.blank? + end +end diff --git a/activesupport/lib/active_support/core_ext/module/inclusion.rb b/activesupport/lib/active_support/core_ext/module/inclusion.rb deleted file mode 100644 index 4f23841645..0000000000 --- a/activesupport/lib/active_support/core_ext/module/inclusion.rb +++ /dev/null @@ -1,30 +0,0 @@ -class Module - # Returns the classes in the current ObjectSpace where this module has been - # mixed in according to Module#included_modules. - # - # module M - # end - # - # module N - # include M - # end - # - # class C - # include M - # end - # - # class D < C - # end - # - # p M.included_in_classes # => [C, D] - # - def included_in_classes - classes = [] - ObjectSpace.each_object(Class) { |k| classes << k if k.included_modules.include?(self) } - - classes.reverse.inject([]) do |unique_classes, klass| - unique_classes << klass unless unique_classes.collect { |k| k.to_s }.include?(klass.to_s) - unique_classes - end - end -end
\ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/module/loading.rb b/activesupport/lib/active_support/core_ext/module/loading.rb deleted file mode 100644 index 43d0578ae6..0000000000 --- a/activesupport/lib/active_support/core_ext/module/loading.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'active_support/core_ext/string/inflections' - -class Module - # Returns String#underscore applied to the module name minus trailing classes. - # - # ActiveRecord.as_load_path # => "active_record" - # ActiveRecord::Associations.as_load_path # => "active_record/associations" - # ActiveRecord::Base.as_load_path # => "active_record" (Base is a class) - # - # The Kernel module gives an empty string by definition. - # - # Kernel.as_load_path # => "" - # Math.as_load_path # => "math" - def as_load_path - if self == Object || self == Kernel - '' - elsif is_a? Class - parent == self ? '' : parent.as_load_path - else - name.split('::').collect do |word| - word.underscore - end * '/' - end - end -end
\ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/module/reachable.rb b/activesupport/lib/active_support/core_ext/module/reachable.rb new file mode 100644 index 0000000000..443d2c3d53 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module/reachable.rb @@ -0,0 +1,10 @@ +require 'active_support/core_ext/module/anonymous' +require 'active_support/core_ext/string/inflections' + +class Module + def reachable? #:nodoc: + !anonymous? && name.constantize.equal?(self) + rescue NameError + false + end +end diff --git a/activesupport/lib/active_support/core_ext/object.rb b/activesupport/lib/active_support/core_ext/object.rb index 08e07a5b24..db2dac1472 100644 --- a/activesupport/lib/active_support/core_ext/object.rb +++ b/activesupport/lib/active_support/core_ext/object.rb @@ -7,3 +7,9 @@ require 'active_support/core_ext/object/conversions' require 'active_support/core_ext/object/instance_variables' require 'active_support/core_ext/object/metaclass' require 'active_support/core_ext/object/misc' +require 'active_support/core_ext/object/extending' + +require 'active_support/core_ext/object/returning' +require 'active_support/core_ext/object/to_param' +require 'active_support/core_ext/object/to_query' +require 'active_support/core_ext/object/with_options' diff --git a/activesupport/lib/active_support/core_ext/object/extending.rb b/activesupport/lib/active_support/core_ext/object/extending.rb new file mode 100644 index 0000000000..c4c37b6a2a --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/extending.rb @@ -0,0 +1,11 @@ +require 'active_support/core_ext/class/subclasses' + +class Object + # Exclude this class unless it's a subclass of our supers and is defined. + # We check defined? in case we find a removed class that has yet to be + # garbage collected. This also fails for anonymous classes -- please + # submit a patch if you have a workaround. + def subclasses_of(*superclasses) #:nodoc: + Class.subclasses_of(*superclasses) + end +end diff --git a/activesupport/lib/active_support/core_ext/string/output_safety.rb b/activesupport/lib/active_support/core_ext/string/output_safety.rb index 3977971e8d..567ba00b0d 100644 --- a/activesupport/lib/active_support/core_ext/string/output_safety.rb +++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb @@ -71,15 +71,12 @@ module ActiveSupport #:nodoc: super(ERB::Util.h(value)) end end + alias << concat def +(other) dup.concat(other) end - def <<(value) - self.concat(value) - end - def html_safe? true end @@ -102,4 +99,4 @@ class String def html_safe ActiveSupport::SafeBuffer.new(self) end -end
\ No newline at end of file +end diff --git a/activesupport/lib/active_support/core_ext/string/xchar.rb b/activesupport/lib/active_support/core_ext/string/xchar.rb index 7183218634..f9a5b4fb64 100644 --- a/activesupport/lib/active_support/core_ext/string/xchar.rb +++ b/activesupport/lib/active_support/core_ext/string/xchar.rb @@ -1,5 +1,5 @@ begin - # See http://bogomips.org/fast_xs/ by Eric Wong. + # See http://fast-xs.rubyforge.org/ by Eric Wong. # Also included with hpricot. require 'fast_xs' rescue LoadError diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 8ded9f8b2d..56de29b730 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -1,8 +1,10 @@ require 'set' require 'thread' +require 'pathname' require 'active_support/core_ext/module/aliasing' require 'active_support/core_ext/module/attribute_accessors' require 'active_support/core_ext/module/introspection' +require 'active_support/core_ext/module/anonymous' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/load_error' require 'active_support/core_ext/name_error' @@ -58,85 +60,116 @@ module ActiveSupport #:nodoc: mattr_accessor :log_activity self.log_activity = false + class WatchStack < Array + def initialize + @mutex = Mutex.new + end + + def self.locked(*methods) + methods.each { |m| class_eval "def #{m}(*) lock { super } end" } + end + + def get(key) + (val = assoc(key)) ? val[1] : [] + end + + locked :concat, :each, :delete_if, :<< + + def new_constants_for(frames) + frames.map do |mod_name, prior_constants| + mod = Inflector.constantize(mod_name) + next unless mod.is_a?(Module) + + new_constants = mod.local_constant_names - prior_constants + get(mod_name).concat(new_constants) + + new_constants.map do |suffix| + ([mod_name, suffix] - ["Object"]).join("::") + end + end.flatten + end + + # Add a set of modules to the watch stack, remembering the initial constants + def add_modules(modules) + list = modules.map do |desc| + name = Dependencies.to_constant_name(desc) + consts = Dependencies.qualified_const_defined?(name) ? + Inflector.constantize(name).local_constant_names : [] + [name, consts] + end + concat(list) + list + end + + def lock + @mutex.synchronize { yield self } + end + end + # An internal stack used to record which constants are loaded by any block. mattr_accessor :constant_watch_stack - self.constant_watch_stack = [] - - mattr_accessor :constant_watch_stack_mutex - self.constant_watch_stack_mutex = Mutex.new + self.constant_watch_stack = WatchStack.new # Module includes this module module ModuleConstMissing #:nodoc: - def self.included(base) #:nodoc: + def self.append_features(base) base.class_eval do - unless defined? const_missing_without_dependencies - alias_method_chain :const_missing, :dependencies - end + # Emulate #exclude via an ivar + return if @_const_missing + @_const_missing = method(:const_missing) + remove_method(:const_missing) end + super end - def self.excluded(base) #:nodoc: + def self.exclude_from(base) base.class_eval do - if defined? const_missing_without_dependencies - undef_method :const_missing - alias_method :const_missing, :const_missing_without_dependencies - undef_method :const_missing_without_dependencies - end + define_method :const_missing, @_const_missing + @_const_missing = nil end end # Use const_missing to autoload associations so we don't have to # require_association when using single-table inheritance. - def const_missing_with_dependencies(class_id) - ActiveSupport::Dependencies.load_missing_constant self, class_id - end + def const_missing(const_name, nesting = nil) + klass_name = name.presence || "Object" + + if !nesting + # We'll assume that the nesting of Foo::Bar is ["Foo::Bar", "Foo"] + # even though it might not be, such as in the case of + # class Foo::Bar; Baz; end + nesting = [] + klass_name.to_s.scan(/::|$/) { nesting.unshift $` } + end - def unloadable(const_desc = self) - super(const_desc) - end - end + # If there are multiple levels of nesting to search under, the top + # level is the one we want to report as the lookup fail. + error = nil - # Class includes this module - module ClassConstMissing #:nodoc: - def const_missing(const_name) - if [Object, Kernel].include?(self) || parent == self - super - else + nesting.each do |namespace| begin - begin - Dependencies.load_missing_constant self, const_name - rescue NameError - parent.send :const_missing, const_name - end + return Dependencies.load_missing_constant namespace.constantize, const_name + rescue NoMethodError then raise rescue NameError => e - # Make sure that the name we are missing is the one that caused the error - parent_qualified_name = Dependencies.qualified_name_for parent, const_name - raise unless e.missing_name? parent_qualified_name - qualified_name = Dependencies.qualified_name_for self, const_name - raise NameError.new("uninitialized constant #{qualified_name}").copy_blame!(e) + error ||= e end end + + # Raise the first error for this set. If this const_missing came from an + # earlier const_missing, this will result in the real error bubbling + # all the way up + raise error + end + + def unloadable(const_desc = self) + super(const_desc) end end # Object includes this module module Loadable #:nodoc: - def self.included(base) #:nodoc: - base.class_eval do - unless defined? load_without_new_constant_marking - alias_method_chain :load, :new_constant_marking - end - end - end - - def self.excluded(base) #:nodoc: - base.class_eval do - if defined? load_without_new_constant_marking - undef_method :load - alias_method :load, :load_without_new_constant_marking - undef_method :load_without_new_constant_marking - end - end + def self.exclude_from(base) + base.class_eval { define_method(:load, Kernel.instance_method(:load)) } end def require_or_load(file_name) @@ -151,26 +184,23 @@ module ActiveSupport #:nodoc: Dependencies.associate_with(file_name) end - def load_with_new_constant_marking(file, *extras) #:nodoc: + def load_dependency(file) if Dependencies.load? - Dependencies.new_constants_in(Object) { load_without_new_constant_marking(file, *extras) } + Dependencies.new_constants_in(Object) { yield }.presence else - load_without_new_constant_marking(file, *extras) + yield end rescue Exception => exception # errors from loading file exception.blame_file! file raise end - def require(file, *extras) #:nodoc: - if Dependencies.load? - Dependencies.new_constants_in(Object) { super } - else - super - end - rescue Exception => exception # errors from required file - exception.blame_file! file - raise + def load(file, *) + load_dependency(file) { super } + end + + def require(file, *) + load_dependency(file) { super } end # Mark the given constant as unloadable. Unloadable constants are removed each @@ -213,16 +243,15 @@ module ActiveSupport #:nodoc: end def hook! - Object.instance_eval { include Loadable } - Module.instance_eval { include ModuleConstMissing } - Class.instance_eval { include ClassConstMissing } - Exception.instance_eval { include Blamable } + Object.class_eval { include Loadable } + Module.class_eval { include ModuleConstMissing } + Exception.class_eval { include Blamable } true end def unhook! - ModuleConstMissing.excluded(Module) - Loadable.excluded(Object) + ModuleConstMissing.exclude_from(Module) + Loadable.exclude_from(Object) true end @@ -292,29 +321,22 @@ module ActiveSupport #:nodoc: # Is the provided constant path defined? def qualified_const_defined?(path) - raise NameError, "#{path.inspect} is not a valid constant name!" unless - /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ path - - names = path.to_s.split('::') - names.shift if names.first.empty? + names = path.sub(/^::/, '').to_s.split('::') - # We can't use defined? because it will invoke const_missing for the parent - # of the name we are checking. names.inject(Object) do |mod, name| - return false unless uninherited_const_defined?(mod, name) + return false unless local_const_defined?(mod, name) mod.const_get name end - return true end if Module.method(:const_defined?).arity == 1 # Does this module define this constant? # Wrapper to accomodate changing Module#const_defined? in Ruby 1.9 - def uninherited_const_defined?(mod, const) + def local_const_defined?(mod, const) mod.const_defined?(const) end else - def uninherited_const_defined?(mod, const) #:nodoc: + def local_const_defined?(mod, const) #:nodoc: mod.const_defined?(const, false) end end @@ -322,29 +344,20 @@ module ActiveSupport #:nodoc: # Given +path+, a filesystem path to a ruby file, return an array of constant # paths which would cause Dependencies to attempt to load this file. def loadable_constants_for_path(path, bases = load_paths) - path = $1 if path =~ /\A(.*)\.rb\Z/ - expanded_path = File.expand_path(path) - - bases.collect do |root| - expanded_root = File.expand_path(root) - next unless %r{\A#{Regexp.escape(expanded_root)}(/|\\)} =~ expanded_path - - nesting = expanded_path[(expanded_root.size)..-1] - nesting = nesting[1..-1] if nesting && nesting[0] == ?/ - next if nesting.blank? - nesting_camel = nesting.camelize - begin - qualified_const_defined?(nesting_camel) - rescue NameError - next - end - [ nesting_camel ] - end.compact.flatten.compact.uniq + expanded_path = Pathname.new(path[/\A(.*?)(\.rb)?\Z/, 1]).expand_path + + bases.inject([]) do |paths, root| + expanded_root = Pathname.new(root).expand_path + nesting = expanded_path.relative_path_from(expanded_root).to_s + next paths if nesting =~ /\.\./ + paths << nesting.camelize + end.uniq end # Search for a file in load_paths matching the provided suffix. def search_for_file(path_suffix) - path_suffix = "#{path_suffix}.rb" unless path_suffix =~ /\.rb\Z/ + path_suffix = path_suffix.sub(/(\.rb)?$/, ".rb") + load_paths.each do |root| path = File.join(root, path_suffix) return path if File.file? path @@ -393,7 +406,7 @@ module ActiveSupport #:nodoc: result = nil newly_defined_paths = new_constants_in(*parent_paths) do - result = load_without_new_constant_marking path + result = Kernel.load path end autoloaded_constants.concat newly_defined_paths unless load_once_path?(path) @@ -405,7 +418,7 @@ module ActiveSupport #:nodoc: # Return the constant path for the provided parent and constant name. def qualified_name_for(mod, name) mod_name = to_constant_name mod - (%w(Object Kernel).include? mod_name) ? name.to_s : "#{mod_name}::#{name}" + mod_name == "Object" ? name.to_s : "#{mod_name}::#{name}" end # Load the constant named +const_name+ which is missing from +from_mod+. If @@ -413,38 +426,27 @@ module ActiveSupport #:nodoc: # using const_missing. def load_missing_constant(from_mod, const_name) log_call from_mod, const_name - if from_mod == Kernel - if ::Object.const_defined?(const_name) - log "Returning Object::#{const_name} for Kernel::#{const_name}" - return ::Object.const_get(const_name) - else - log "Substituting Object for Kernel" - from_mod = Object - end - end - - # If we have an anonymous module, all we can do is attempt to load from Object. - from_mod = Object if from_mod.name.blank? - unless qualified_const_defined?(from_mod.name) && Inflector.constantize(from_mod.name).object_id == from_mod.object_id + unless qualified_const_defined?(from_mod.name) && Inflector.constantize(from_mod.name).equal?(from_mod) raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!" end - raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if uninherited_const_defined?(from_mod, const_name) + raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if local_const_defined?(from_mod, const_name) qualified_name = qualified_name_for from_mod, const_name path_suffix = qualified_name.underscore name_error = NameError.new("uninitialized constant #{qualified_name}") file_path = search_for_file(path_suffix) + if file_path && ! loaded.include?(File.expand_path(file_path)) # We found a matching file to load require_or_load file_path - raise LoadError, "Expected #{file_path} to define #{qualified_name}" unless uninherited_const_defined?(from_mod, const_name) + raise LoadError, "Expected #{file_path} to define #{qualified_name}" unless local_const_defined?(from_mod, const_name) return from_mod.const_get(const_name) elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix) return mod elsif (parent = from_mod.parent) && parent != from_mod && - ! from_mod.parents.any? { |p| uninherited_const_defined?(p, const_name) } + ! from_mod.parents.any? { |p| local_const_defined?(p, const_name) } # If our parents do not have a constant named +const_name+ then we are free # to attempt to load upwards. If they do have such a constant, then this # const_missing must be due to from_mod::const_name, which should not @@ -471,7 +473,7 @@ module ActiveSupport #:nodoc: # Determine if the given constant has been automatically loaded. def autoloaded?(desc) # No name => anonymous module. - return false if desc.is_a?(Module) && desc.name.blank? + return false if desc.is_a?(Module) && desc.anonymous? name = to_constant_name desc return false unless qualified_const_defined? name return autoloaded_constants.include?(name) @@ -505,79 +507,26 @@ module ActiveSupport #:nodoc: # and will be removed immediately. def new_constants_in(*descs) log_call(*descs) - - # Build the watch frames. Each frame is a tuple of - # [module_name_as_string, constants_defined_elsewhere] - watch_frames = descs.collect do |desc| - if desc.is_a? Module - mod_name = desc.name - initial_constants = desc.local_constant_names - elsif desc.is_a?(String) || desc.is_a?(Symbol) - mod_name = desc.to_s - - # Handle the case where the module has yet to be defined. - initial_constants = if qualified_const_defined?(mod_name) - Inflector.constantize(mod_name).local_constant_names - else - [] - end - else - raise Argument, "#{desc.inspect} does not describe a module!" - end - - [mod_name, initial_constants] - end - - constant_watch_stack_mutex.synchronize do - constant_watch_stack.concat watch_frames - end + watch_frames = constant_watch_stack.add_modules(descs) aborting = true begin yield # Now yield to the code that is to define new constants. aborting = false ensure - # Find the new constants. - new_constants = watch_frames.collect do |mod_name, prior_constants| - # Module still doesn't exist? Treat it as if it has no constants. - next [] unless qualified_const_defined?(mod_name) - - mod = Inflector.constantize(mod_name) - next [] unless mod.is_a? Module - new_constants = mod.local_constant_names - prior_constants - - # Make sure no other frames takes credit for these constants. - constant_watch_stack_mutex.synchronize do - constant_watch_stack.each do |frame_name, constants| - constants.concat new_constants if frame_name == mod_name - end - end - - new_constants.collect do |suffix| - mod_name == "Object" ? suffix : "#{mod_name}::#{suffix}" - end - end.flatten + new_constants = constant_watch_stack.new_constants_for(watch_frames) log "New constants: #{new_constants * ', '}" + return new_constants unless aborting - if aborting - log "Error during loading, removing partially loaded constants " - new_constants.each { |name| remove_constant name } - new_constants.clear - end + log "Error during loading, removing partially loaded constants " + new_constants.each {|c| remove_constant(c) }.clear end - return new_constants + return [] ensure # Remove the stack frames that we added. - if defined?(watch_frames) && ! watch_frames.blank? - frame_ids = watch_frames.collect { |frame| frame.object_id } - constant_watch_stack_mutex.synchronize do - constant_watch_stack.delete_if do |watch_frame| - frame_ids.include? watch_frame.object_id - end - end - end + watch_frames.each {|f| constant_watch_stack.delete(f) } if watch_frames.present? end class LoadingModule #:nodoc: @@ -595,11 +544,11 @@ module ActiveSupport #:nodoc: # A module, class, symbol, or string may be provided. def to_constant_name(desc) #:nodoc: name = case desc - when String then desc.starts_with?('::') ? desc[2..-1] : desc + when String then desc.sub(/^::/, '') when Symbol then desc.to_s when Module - raise ArgumentError, "Anonymous modules have no name to be referenced by" if desc.name.blank? - desc.name + desc.name.presence || + raise(ArgumentError, "Anonymous modules have no name to be referenced by") else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}" end end @@ -607,16 +556,13 @@ module ActiveSupport #:nodoc: def remove_constant(const) #:nodoc: return false unless qualified_const_defined? const - const = $1 if /\A::(.*)\Z/ =~ const.to_s - names = const.to_s.split('::') - if names.size == 1 # It's under Object - parent = Object - else - parent = Inflector.constantize(names[0..-2] * '::') - end + # Normalize ::Foo, Foo, Object::Foo, and ::Object::Foo to Object::Foo + names = const.to_s.sub(/^::(Object)?/, 'Object::').split("::") + to_remove = names.pop + parent = Inflector.constantize(names * '::') log "removing constant #{const}" - parent.instance_eval { remove_const names.last } + parent.instance_eval { remove_const to_remove } return true end diff --git a/activesupport/lib/active_support/json/backends/yajl.rb b/activesupport/lib/active_support/json/backends/yajl.rb new file mode 100644 index 0000000000..d76f8b03e4 --- /dev/null +++ b/activesupport/lib/active_support/json/backends/yajl.rb @@ -0,0 +1,40 @@ +require 'yajl-ruby' unless defined?(Yajl) + +module ActiveSupport + module JSON + module Backends + module Yajl + ParseError = ::Yajl::ParseError + extend self + + # Parses a JSON string or IO and convert it into an object + def decode(json) + data = ::Yajl::Parser.new.parse(json) + if ActiveSupport.parse_json_times + convert_dates_from(data) + else + data + end + end + + private + def convert_dates_from(data) + case data + when nil + nil + when DATE_REGEX + DateTime.parse(data) + when Array + data.map! { |d| convert_dates_from(d) } + when Hash + data.each do |key, value| + data[key] = convert_dates_from(value) + end + else + data + end + end + end + end + end +end diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb index a5908365af..e357b6837a 100644 --- a/activesupport/lib/active_support/json/decoding.rb +++ b/activesupport/lib/active_support/json/decoding.rb @@ -6,12 +6,15 @@ module ActiveSupport mattr_accessor :parse_json_times module JSON + # Listed in order of preference. + DECODERS = %w(Yajl JSONGem Yaml) + class << self attr_reader :parse_error delegate :decode, :to => :backend def backend - self.backend = "Yaml" unless defined?(@backend) + set_default_backend unless defined?(@backend) @backend end @@ -31,6 +34,18 @@ module ActiveSupport ensure self.backend = old_backend end + + def set_default_backend + DECODERS.find do |name| + begin + self.backend = name + true + rescue LoadError + # Try next decoder. + false + end + end + end end end end diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb index 1646891e00..ab30984d62 100644 --- a/activesupport/lib/active_support/test_case.rb +++ b/activesupport/lib/active_support/test_case.rb @@ -21,12 +21,6 @@ module ActiveSupport alias_method :method_name, :name if method_defined? :name alias_method :method_name, :__name__ if method_defined? :__name__ else - # TODO: Figure out how to get the Rails::BacktraceFilter into minitest/unit - if defined?(Rails) && ENV['BACKTRACE'].nil? - require 'rails/backtrace_cleaner' - Test::Unit::Util::BacktraceFilter.module_eval { include Rails::BacktraceFilterForTestUnit } - end - Assertion = Test::Unit::AssertionFailedError require 'active_support/testing/default' diff --git a/activesupport/lib/active_support/version.rb b/activesupport/lib/active_support/version.rb index b92e469a08..6c6187be29 100644 --- a/activesupport/lib/active_support/version.rb +++ b/activesupport/lib/active_support/version.rb @@ -2,7 +2,7 @@ module ActiveSupport module VERSION #:nodoc: MAJOR = 3 MINOR = 0 - TINY = "0.beta" + TINY = "0.beta1" STRING = [MAJOR, MINOR, TINY].join('.') end diff --git a/activesupport/lib/active_support/whiny_nil.rb b/activesupport/lib/active_support/whiny_nil.rb index 4f6ff7d3b5..11b05efac1 100644 --- a/activesupport/lib/active_support/whiny_nil.rb +++ b/activesupport/lib/active_support/whiny_nil.rb @@ -11,7 +11,7 @@ # classes in NilClass::WHINERS the error message suggests which could be the # actual intended class: # -# $ script/runner nil.destroy +# $ rails runner nil.destroy # ... # You might have expected an instance of ActiveRecord::Base. # ... diff --git a/activesupport/test/core_ext/class_test.rb b/activesupport/test/core_ext/class_test.rb new file mode 100644 index 0000000000..b7f3dd9930 --- /dev/null +++ b/activesupport/test/core_ext/class_test.rb @@ -0,0 +1,29 @@ +require 'abstract_unit' +require 'active_support/core_ext/class' + +class A +end + +module X + class B + end +end + +module Y + module Z + class C + end + end +end + +class ClassTest < Test::Unit::TestCase + def test_retrieving_subclasses + @parent = eval("class D; end; D") + @sub = eval("class E < D; end; E") + @subofsub = eval("class F < E; end; F") + assert_equal 2, @parent.subclasses.size + assert_equal [@subofsub.to_s], @sub.subclasses + assert_equal [], @subofsub.subclasses + assert_equal [@sub.to_s, @subofsub.to_s].sort, @parent.subclasses.sort + end +end diff --git a/activesupport/test/core_ext/module/anonymous_test.rb b/activesupport/test/core_ext/module/anonymous_test.rb new file mode 100644 index 0000000000..7a78a3b012 --- /dev/null +++ b/activesupport/test/core_ext/module/anonymous_test.rb @@ -0,0 +1,14 @@ +require 'abstract_unit' +require 'active_support/core_ext/module/anonymous' + +class AnonymousTest < ActiveSupport::TestCase + test "an anonymous class or module are anonymous" do + assert Module.new.anonymous? + assert Class.new.anonymous? + end + + test "a named class or module are not anonymous" do + assert !Kernel.anonymous? + assert !Object.anonymous? + end +end
\ No newline at end of file diff --git a/activesupport/test/core_ext/module/reachable_test.rb b/activesupport/test/core_ext/module/reachable_test.rb new file mode 100644 index 0000000000..72892b77d5 --- /dev/null +++ b/activesupport/test/core_ext/module/reachable_test.rb @@ -0,0 +1,41 @@ +require 'abstract_unit' +require 'active_support/core_ext/module/reachable' + +class AnonymousTest < ActiveSupport::TestCase + test "an anonymous class or module is not reachable" do + assert !Module.new.reachable? + assert !Class.new.reachable? + end + + test "ordinary named classes or modules are reachable" do + assert Kernel.reachable? + assert Object.reachable? + end + + test "a named class or module whose constant has gone is not reachable" do + c = eval "class C; end; C" + m = eval "module M; end; M" + + self.class.send(:remove_const, :C) + self.class.send(:remove_const, :M) + + assert !c.reachable? + assert !m.reachable? + end + + test "a named class or module whose constants store different objects are not reachable" do + c = eval "class C; end; C" + m = eval "module M; end; M" + + self.class.send(:remove_const, :C) + self.class.send(:remove_const, :M) + + eval "class C; end" + eval "module M; end" + + assert C.reachable? + assert M.reachable? + assert !c.reachable? + assert !m.reachable? + end +end
\ No newline at end of file diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index 87f056ea85..1fe75d5930 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -72,13 +72,6 @@ class ModuleTest < Test::Unit::TestCase @david = Someone.new("David", Somewhere.new("Paulina", "Chicago")) end - def test_included_in_classes - assert One.included_in_classes.include?(Ab) - assert One.included_in_classes.include?(Xy::Bc) - assert One.included_in_classes.include?(Yz::Zy::Cd) - assert !One.included_in_classes.include?(De) - end - def test_delegation_to_methods assert_equal "Paulina", @david.street assert_equal "Chicago", @david.city @@ -170,11 +163,6 @@ class ModuleTest < Test::Unit::TestCase def test_local_constants assert_equal %w(Constant1 Constant3), Ab.local_constants.sort.map(&:to_s) end - - def test_as_load_path - assert_equal 'yz/zy', Yz::Zy.as_load_path - assert_equal 'yz', Yz.as_load_path - end end module BarMethodAliaser diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb index 0b2a9c418e..f31e7774e9 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -1,6 +1,7 @@ require 'abstract_unit' require 'active_support/time' require 'active_support/core_ext/object' +require 'active_support/core_ext/class/subclasses' class ClassA; end class ClassB < ClassA; end @@ -39,6 +40,55 @@ class Foo include Bar end +class ClassExtTest < Test::Unit::TestCase + def test_subclasses_of_should_find_nested_classes + assert Class.subclasses_of(ClassK).include?(Nested::ClassL) + end + + def test_subclasses_of_should_not_return_removed_classes + # First create the removed class + old_class = Nested.class_eval { remove_const :ClassL } + new_class = Class.new(ClassK) + Nested.const_set :ClassL, new_class + assert_equal "Nested::ClassL", new_class.name # Sanity check + + subclasses = Class.subclasses_of(ClassK) + assert subclasses.include?(new_class) + assert ! subclasses.include?(old_class) + ensure + Nested.const_set :ClassL, old_class unless defined?(Nested::ClassL) + end + + def test_subclasses_of_should_not_trigger_const_missing + const_missing = false + Nested.on_const_missing { const_missing = true } + + subclasses = Class.subclasses_of ClassK + assert !const_missing + assert_equal [ Nested::ClassL ], subclasses + + removed = Nested.class_eval { remove_const :ClassL } # keep it in memory + subclasses = Class.subclasses_of ClassK + assert !const_missing + assert subclasses.empty? + ensure + Nested.const_set :ClassL, removed unless defined?(Nested::ClassL) + end + + def test_subclasses_of_with_multiple_roots + classes = Class.subclasses_of(ClassI, ClassK) + assert_equal %w(ClassJ Nested::ClassL), classes.collect(&:to_s).sort + end + + def test_subclasses_of_doesnt_find_anonymous_classes + assert_equal [], Class.subclasses_of(Foo) + bar = Class.new(Foo) + assert_nothing_raised do + assert_equal [bar], Class.subclasses_of(Foo) + end + end +end + class ObjectTests < Test::Unit::TestCase class DuckTime def acts_like_time? diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index ca26f91e8c..d8145d467b 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -335,6 +335,11 @@ end class OutputSafetyTest < ActiveSupport::TestCase def setup @string = "hello" + @object = Class.new(Object) do + def to_s + "other" + end + end.new end test "A string is unsafe by default" do @@ -355,17 +360,15 @@ class OutputSafetyTest < ActiveSupport::TestCase end test "An object is unsafe by default" do - klass = Class.new(Object) do - def to_str - "other" - end - end + assert !@object.html_safe? + end - @string.html_safe - @string << klass.new + test "Adding an object to a safe string returns a safe string" do + string = @string.html_safe + string << @object - assert_equal "helloother", @string - assert !@string.html_safe? + assert_equal "helloother", string + assert string.html_safe? end test "Adding a safe string to another safe string returns a safe string" do @@ -391,9 +394,9 @@ class OutputSafetyTest < ActiveSupport::TestCase test "Concatting safe onto unsafe yields unsafe" do @other_string = "other" - @string.html_safe - @other_string.concat(@string) + string = @string.html_safe + @other_string.concat(string) assert !@other_string.html_safe? end @@ -406,17 +409,17 @@ class OutputSafetyTest < ActiveSupport::TestCase test "Concatting safe onto safe yields safe" do @other_string = "other".html_safe - @string.html_safe + string = @string.html_safe - @other_string.concat(@string) + @other_string.concat(string) assert @other_string.html_safe? end test "Concatting safe onto unsafe with << yields unsafe" do @other_string = "other" - @string.html_safe + string = @string.html_safe - @other_string << @string + @other_string << string assert !@other_string.html_safe? end @@ -429,9 +432,9 @@ class OutputSafetyTest < ActiveSupport::TestCase test "Concatting safe onto safe with << yields safe" do @other_string = "other".html_safe - @string.html_safe + string = @string.html_safe - @other_string << @string + @other_string << string assert @other_string.html_safe? end diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index 0fcf1eaf00..6ff6dfb607 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -1,7 +1,6 @@ require 'abstract_unit' require 'pp' require 'active_support/dependencies' -require 'active_support/core_ext/module/loading' require 'active_support/core_ext/kernel/reporting' module ModuleWithMissing @@ -43,12 +42,17 @@ class DependenciesTest < Test::Unit::TestCase require_dependency 'dependencies/service_one' require_dependency 'dependencies/service_two' assert_equal 2, ActiveSupport::Dependencies.loaded.size + ensure + Object.send(:remove_const, :ServiceOne) if Object.const_defined?(:ServiceOne) + Object.send(:remove_const, :ServiceTwo) if Object.const_defined?(:ServiceTwo) end def test_tracking_identical_loaded_files require_dependency 'dependencies/service_one' require_dependency 'dependencies/service_one' assert_equal 1, ActiveSupport::Dependencies.loaded.size + ensure + Object.send(:remove_const, :ServiceOne) if Object.const_defined?(:ServiceOne) end def test_missing_dependency_raises_missing_source_file @@ -130,10 +134,6 @@ class DependenciesTest < Test::Unit::TestCase end end - def test_as_load_path - assert_equal '', DependenciesTest.as_load_path - end - def test_module_loading with_autoloading_fixtures do assert_kind_of Module, A @@ -291,7 +291,7 @@ class DependenciesTest < Test::Unit::TestCase assert ActiveSupport::Dependencies.qualified_const_defined?("::Test::Unit::TestCase") end - def test_qualified_const_defined_should_not_call_method_missing + def test_qualified_const_defined_should_not_call_const_missing ModuleWithMissing.missing_count = 0 assert ! ActiveSupport::Dependencies.qualified_const_defined?("ModuleWithMissing::A") assert_equal 0, ModuleWithMissing.missing_count @@ -299,6 +299,10 @@ class DependenciesTest < Test::Unit::TestCase assert_equal 0, ModuleWithMissing.missing_count end + def test_qualified_const_defined_explodes_with_invalid_const_name + assert_raises(NameError) { ActiveSupport::Dependencies.qualified_const_defined?("invalid") } + end + def test_autoloaded? with_autoloading_fixtures do assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder") @@ -333,7 +337,6 @@ class DependenciesTest < Test::Unit::TestCase assert_equal "A", ActiveSupport::Dependencies.qualified_name_for(:Object, :A) assert_equal "A", ActiveSupport::Dependencies.qualified_name_for("Object", :A) assert_equal "A", ActiveSupport::Dependencies.qualified_name_for("::Object", :A) - assert_equal "A", ActiveSupport::Dependencies.qualified_name_for("::Kernel", :A) assert_equal "ActiveSupport::Dependencies::A", ActiveSupport::Dependencies.qualified_name_for(:'ActiveSupport::Dependencies', :A) assert_equal "ActiveSupport::Dependencies::A", ActiveSupport::Dependencies.qualified_name_for(ActiveSupport::Dependencies, :A) @@ -460,14 +463,6 @@ class DependenciesTest < Test::Unit::TestCase end end - def test_const_missing_on_kernel_should_fallback_to_object - with_autoloading_fixtures do - kls = Kernel::E - assert_equal "E", kls.name - assert_equal kls.object_id, Kernel::E.object_id - end - end - def test_preexisting_constants_are_not_marked_as_autoloaded with_autoloading_fixtures do require_dependency 'e' @@ -711,7 +706,7 @@ class DependenciesTest < Test::Unit::TestCase def test_autoload_doesnt_shadow_name_error with_autoloading_fixtures do Object.send(:remove_const, :RaisesNameError) if defined?(::RaisesNameError) - 2.times do + 2.times do |i| begin ::RaisesNameError::FooBarBaz.object_id flunk 'should have raised NameError when autoloaded file referenced FooBarBaz' diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb index 8fcb16abfb..d2e3efaa6b 100644 --- a/activesupport/test/json/decoding_test.rb +++ b/activesupport/test/json/decoding_test.rb @@ -45,10 +45,11 @@ class TestJSONDecoding < ActiveSupport::TestCase } # load the default JSON backend - ActiveSupport::JSON.backend + ActiveSupport::JSON.backend = 'Yaml' backends = %w(Yaml) backends << "JSONGem" if defined?(::JSON) + backends << "Yajl" if defined?(::Yajl) backends.each do |backend| TESTS.each do |json, expected| diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index cf9a635b5f..188b799f3f 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -50,13 +50,18 @@ class TestJSONEncoding < Test::Unit::TestCase StandardDateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005-02-01T15:15:10+00:00") ]] StandardStringTests = [[ 'this is the <string>', %("this is the <string>")]] + def sorted_json(json) + return json unless json =~ /^\{.*\}$/ + '{' + json[1..-2].split(',').sort.join(',') + '}' + end + constants.grep(/Tests$/).each do |class_tests| define_method("test_#{class_tests[0..-6].underscore}") do begin ActiveSupport.escape_html_entities_in_json = class_tests !~ /^Standard/ ActiveSupport.use_standard_json_time_format = class_tests =~ /^Standard/ self.class.const_get(class_tests).each do |pair| - assert_equal pair.last, ActiveSupport::JSON.encode(pair.first) + assert_equal pair.last, sorted_json(ActiveSupport::JSON.encode(pair.first)) end ensure ActiveSupport.escape_html_entities_in_json = false @@ -71,8 +76,7 @@ class TestJSONEncoding < Test::Unit::TestCase assert_equal %({\"a\":[1,2]}), ActiveSupport::JSON.encode('a' => [1,2]) assert_equal %({"1":2}), ActiveSupport::JSON.encode(1 => 2) - sorted_json = '{' + ActiveSupport::JSON.encode(:a => :b, :c => :d)[1..-2].split(',').sort.join(',') + '}' - assert_equal %({\"a\":\"b\",\"c\":\"d\"}), sorted_json + assert_equal %({\"a\":\"b\",\"c\":\"d\"}), sorted_json(ActiveSupport::JSON.encode(:a => :b, :c => :d)) end def test_utf8_string_encoded_properly_when_kcode_is_utf8 diff --git a/activesupport/test/rescuable_test.rb b/activesupport/test/rescuable_test.rb index 9f2b783b2e..ff77e16edd 100644 --- a/activesupport/test/rescuable_test.rb +++ b/activesupport/test/rescuable_test.rb @@ -7,12 +7,6 @@ class NuclearExplosion < StandardError end class MadRonon < StandardError - attr_accessor :message - - def initialize(message) - @message = message - super() - end end class Stargate |