From 044d6010fd22ede84b5afd93bce7c4f0ff97adbd Mon Sep 17 00:00:00 2001 From: tenderlove Date: Sat, 22 May 2010 22:12:39 +0000 Subject: * ext/psych/lib/psych/json/stream.rb: adding a JSON streaming API * ext/psych/lib/psych/stream.rb: ditto * ext/psych/lib/psych.rb: using autoload * ext/psych/lib/psych/json.rb: ditto * ext/psych/lib/psych/json/tree_builder.rb: refactor * ext/psych/lib/psych/visitors/json_tree.rb: refactor git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27961 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/psych/json/test_stream.rb | 75 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 test/psych/json/test_stream.rb (limited to 'test/psych/json') diff --git a/test/psych/json/test_stream.rb b/test/psych/json/test_stream.rb new file mode 100644 index 0000000000..712465d4c0 --- /dev/null +++ b/test/psych/json/test_stream.rb @@ -0,0 +1,75 @@ +require_relative '../helper' + +module Psych + module JSON + class TestStream < TestCase + def setup + @io = StringIO.new + @stream = Psych::JSON::Stream.new(@io) + @stream.start + end + + def test_explicit_documents + @io = StringIO.new + @stream = Psych::JSON::Stream.new(@io) + @stream.start + + @stream.push({ 'foo' => 'bar' }) + + assert !@stream.finished?, 'stream not finished' + @stream.finish + assert @stream.finished?, 'stream finished' + + assert_match(/^---/, @io.string) + assert_match(/\.\.\.$/, @io.string) + end + + def test_null + @stream.push(nil) + assert_match(/^--- null/, @io.string) + end + + def test_string + @stream.push "foo" + assert_match(/(['"])foo\1/, @io.string) + end + + def test_symbol + @stream.push :foo + assert_match(/(['"])foo\1/, @io.string) + end + + def test_int + @stream.push 10 + assert_match(/^--- 10/, @io.string) + end + + def test_float + @stream.push 1.2 + assert_match(/^--- 1.2/, @io.string) + end + + def test_hash + hash = { 'one' => 'two' } + @stream.push hash + + json = @io.string + assert_match(/}$/, json) + assert_match(/^--- \{/, json) + assert_match(/['"]one['"]/, json) + assert_match(/['"]two['"]/, json) + end + + def test_list_to_json + list = %w{ one two } + @stream.push list + + json = @io.string + assert_match(/]$/, json) + assert_match(/^--- \[/, json) + assert_match(/['"]one['"]/, json) + assert_match(/['"]two['"]/, json) + end + end + end +end -- cgit v1.2.3