aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md3
-rw-r--r--activesupport/lib/active_support/json/encoding.rb12
-rw-r--r--activesupport/lib/active_support/json/variable.rb10
-rw-r--r--activesupport/lib/active_support/testing/setup_and_teardown.rb5
-rw-r--r--activesupport/test/json/encoding_test.rb17
-rw-r--r--activesupport/test/test_case_test.rb44
6 files changed, 79 insertions, 12 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 171740f075..b231bdd1ac 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -3,6 +3,9 @@
* Added #beginning_of_hour and #end_of_hour to Time and DateTime core
extensions. *Mark J. Titorenko*
+* ActiveSupport::JSON::Variable is deprecated. Define your own #as_json and #encode_json methods
+ for custom JSON string literals. *Erich Menge*
+
## Rails 3.2.3 (March 30, 2012) ##
diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb
index 4a19387511..91e4f07c6c 100644
--- a/activesupport/lib/active_support/json/encoding.rb
+++ b/activesupport/lib/active_support/json/encoding.rb
@@ -158,18 +158,18 @@ class Struct #:nodoc:
end
class TrueClass
- AS_JSON = ActiveSupport::JSON::Variable.new('true').freeze
- def as_json(options = nil) AS_JSON end #:nodoc:
+ def as_json(options = nil) self end #:nodoc:
+ def encode_json(encoder) to_s end #:nodoc:
end
class FalseClass
- AS_JSON = ActiveSupport::JSON::Variable.new('false').freeze
- def as_json(options = nil) AS_JSON end #:nodoc:
+ def as_json(options = nil) self end #:nodoc:
+ def encode_json(encoder) to_s end #:nodoc:
end
class NilClass
- AS_JSON = ActiveSupport::JSON::Variable.new('null').freeze
- def as_json(options = nil) AS_JSON end #:nodoc:
+ def as_json(options = nil) self end #:nodoc:
+ def encode_json(encoder) 'null' end #:nodoc:
end
class String
diff --git a/activesupport/lib/active_support/json/variable.rb b/activesupport/lib/active_support/json/variable.rb
index 5685ed18b7..17b709decc 100644
--- a/activesupport/lib/active_support/json/variable.rb
+++ b/activesupport/lib/active_support/json/variable.rb
@@ -1,7 +1,15 @@
+require 'active_support/deprecation'
+
module ActiveSupport
module JSON
- # A string that returns itself as its JSON-encoded form.
+ # Deprecated: A string that returns itself as its JSON-encoded form.
class Variable < String
+ def initialize(*args)
+ ActiveSupport::Deprecation.warn 'ActiveSupport::JSON::Variable is deprecated and will be removed in Rails 4.0. ' \
+ 'For your own custom JSON literals, define #as_json and #encode_json yourself.'
+ super
+ end
+
def as_json(options = nil) self end #:nodoc:
def encode_json(encoder) self end #:nodoc:
end
diff --git a/activesupport/lib/active_support/testing/setup_and_teardown.rb b/activesupport/lib/active_support/testing/setup_and_teardown.rb
index 22e41fa905..78d914f8cf 100644
--- a/activesupport/lib/active_support/testing/setup_and_teardown.rb
+++ b/activesupport/lib/active_support/testing/setup_and_teardown.rb
@@ -28,17 +28,22 @@ module ActiveSupport
end
module ForMiniTest
+ PASSTHROUGH_EXCEPTIONS = MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS rescue [NoMemoryError, SignalException, Interrupt, SystemExit]
def run(runner)
result = '.'
begin
run_callbacks :setup do
result = super
end
+ rescue *PASSTHROUGH_EXCEPTIONS => e
+ raise e
rescue Exception => e
result = runner.puke(self.class, method_name, e)
ensure
begin
run_callbacks :teardown
+ rescue *PASSTHROUGH_EXCEPTIONS => e
+ raise e
rescue Exception => e
result = runner.puke(self.class, method_name, e)
end
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb
index 8cf1a54a99..d327f34140 100644
--- a/activesupport/test/json/encoding_test.rb
+++ b/activesupport/test/json/encoding_test.rb
@@ -3,7 +3,7 @@ require 'abstract_unit'
require 'active_support/core_ext/string/inflections'
require 'active_support/json'
-class TestJSONEncoding < Test::Unit::TestCase
+class TestJSONEncoding < ActiveSupport::TestCase
class Foo
def initialize(a, b)
@a, @b = a, b
@@ -46,8 +46,6 @@ class TestJSONEncoding < Test::Unit::TestCase
HashlikeTests = [[ Hashlike.new, %({\"a\":1}) ]]
CustomTests = [[ Custom.new, '"custom"' ]]
- VariableTests = [[ ActiveSupport::JSON::Variable.new('foo'), 'foo'],
- [ ActiveSupport::JSON::Variable.new('alert("foo")'), 'alert("foo")']]
RegexpTests = [[ /^a/, '"(?-mix:^a)"' ], [/^\w{1,2}[a-z]+/ix, '"(?ix-m:^\\\\w{1,2}[a-z]+)"']]
DateTests = [[ Date.new(2005,2,1), %("2005/02/01") ]]
@@ -79,6 +77,13 @@ class TestJSONEncoding < Test::Unit::TestCase
end
end
+ def test_json_variable
+ assert_deprecated do
+ assert_equal ActiveSupport::JSON::Variable.new('foo'), 'foo'
+ assert_equal ActiveSupport::JSON::Variable.new('alert("foo")'), 'alert("foo")'
+ end
+ end
+
def test_hash_encoding
assert_equal %({\"a\":\"b\"}), ActiveSupport::JSON.encode(:a => :b)
assert_equal %({\"a\":1}), ActiveSupport::JSON.encode('a' => 1)
@@ -270,6 +275,12 @@ class TestJSONEncoding < Test::Unit::TestCase
JSON.parse(json_string_and_date))
end
+ def test_nil_true_and_false_represented_as_themselves
+ assert_equal nil, nil.as_json
+ assert_equal true, true.as_json
+ assert_equal false, false.as_json
+ end
+
protected
def object_keys(json_object)
diff --git a/activesupport/test/test_case_test.rb b/activesupport/test/test_case_test.rb
index 756d21b3e4..c4653b1ae6 100644
--- a/activesupport/test/test_case_test.rb
+++ b/activesupport/test/test_case_test.rb
@@ -19,7 +19,7 @@ module ActiveSupport
end
if defined?(MiniTest::Assertions) && TestCase < MiniTest::Assertions
- def test_callback_with_exception
+ def test_standard_error_raised_within_setup_callback_is_puked
tc = Class.new(TestCase) do
setup :bad_callback
def bad_callback; raise 'oh noes' end
@@ -38,7 +38,7 @@ module ActiveSupport
assert_equal 'oh noes', exception.message
end
- def test_teardown_callback_with_exception
+ def test_standard_error_raised_within_teardown_callback_is_puked
tc = Class.new(TestCase) do
teardown :bad_callback
def bad_callback; raise 'oh noes' end
@@ -56,6 +56,46 @@ module ActiveSupport
assert_equal test_name, name
assert_equal 'oh noes', exception.message
end
+
+ def test_passthrough_exception_raised_within_test_method_is_not_rescued
+ tc = Class.new(TestCase) do
+ def test_which_raises_interrupt; raise Interrupt; end
+ end
+
+ test_name = 'test_which_raises_interrupt'
+ fr = FakeRunner.new
+
+ test = tc.new test_name
+ assert_raises(Interrupt) { test.run fr }
+ end
+
+ def test_passthrough_exception_raised_within_setup_callback_is_not_rescued
+ tc = Class.new(TestCase) do
+ setup :callback_which_raises_interrupt
+ def callback_which_raises_interrupt; raise Interrupt; end
+ def test_true; assert true end
+ end
+
+ test_name = 'test_true'
+ fr = FakeRunner.new
+
+ test = tc.new test_name
+ assert_raises(Interrupt) { test.run fr }
+ end
+
+ def test_passthrough_exception_raised_within_teardown_callback_is_not_rescued
+ tc = Class.new(TestCase) do
+ teardown :callback_which_raises_interrupt
+ def callback_which_raises_interrupt; raise Interrupt; end
+ def test_true; assert true end
+ end
+
+ test_name = 'test_true'
+ fr = FakeRunner.new
+
+ test = tc.new test_name
+ assert_raises(Interrupt) { test.run fr }
+ end
end
end
end