aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-10-04 03:28:42 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-10-04 03:28:42 +0000
commitb1968708e12972515fdc8eefdcaff95edbebc76b (patch)
tree93c179fda5d5428bb12bbdb4332f4450d523f77c /activesupport/test
parenta30a1a9d5f06f404421d65393bcf9d73885789cb (diff)
downloadrails-b1968708e12972515fdc8eefdcaff95edbebc76b.tar.gz
rails-b1968708e12972515fdc8eefdcaff95edbebc76b.tar.bz2
rails-b1968708e12972515fdc8eefdcaff95edbebc76b.zip
Hash#to_json takes :only or :except options to specific or omit certain hash keys. Enumerable#to_json passes through its options to each element. Closes #9751.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7736 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/json/encoding_test.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb
index ba468141b4..dee5b928d3 100644
--- a/activesupport/test/json/encoding_test.rb
+++ b/activesupport/test/json/encoding_test.rb
@@ -69,8 +69,29 @@ class TestJSONEncoding < Test::Unit::TestCase
assert_equal %w( "$" "A" "A0" "A0B" "_" "a" 0 1 ), object_keys(values.to_json)
end
+ def test_hash_should_allow_key_filtering_with_only
+ assert_equal %({"a": 1}), { 'a' => 1, :b => 2, :c => 3 }.to_json(:only => 'a')
+ end
+
+ def test_hash_should_allow_key_filtering_with_except
+ assert_equal %({"b": 2}), { 'foo' => 'bar', :b => 2, :c => 3 }.to_json(:except => ['foo', :c])
+ end
+
protected
def object_keys(json_object)
json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort
end
end
+
+uses_mocha 'JsonOptionsTests' do
+ class JsonOptionsTests < Test::Unit::TestCase
+ def test_enumerable_should_passthrough_options_to_elements
+ json_options = { :include => :posts }
+ ActiveSupport::JSON.expects(:encode).with(1, json_options)
+ ActiveSupport::JSON.expects(:encode).with(2, json_options)
+ ActiveSupport::JSON.expects(:encode).with('foo', json_options)
+
+ [1, 2, 'foo'].to_json(json_options)
+ end
+ end
+end