1require 'psych/helper'
2
3module Psych
4  class TestStream < TestCase
5    def test_parse_partial
6      rb = Psych.parse("--- foo\n...\n--- `").to_ruby
7      assert_equal 'foo', rb
8    end
9
10    def test_load_partial
11      rb = Psych.load("--- foo\n...\n--- `")
12      assert_equal 'foo', rb
13    end
14
15    def test_parse_stream_yields_documents
16      list = []
17      Psych.parse_stream("--- foo\n...\n--- bar") do |doc|
18        list << doc.to_ruby
19      end
20      assert_equal %w{ foo bar }, list
21    end
22
23    def test_parse_stream_break
24      list = []
25      Psych.parse_stream("--- foo\n...\n--- `") do |doc|
26        list << doc.to_ruby
27        break
28      end
29      assert_equal %w{ foo }, list
30    end
31
32    def test_load_stream_yields_documents
33      list = []
34      Psych.load_stream("--- foo\n...\n--- bar") do |ruby|
35        list << ruby
36      end
37      assert_equal %w{ foo bar }, list
38    end
39
40    def test_load_stream_break
41      list = []
42      Psych.load_stream("--- foo\n...\n--- `") do |ruby|
43        list << ruby
44        break
45      end
46      assert_equal %w{ foo }, list
47    end
48
49    def test_explicit_documents
50      io     = StringIO.new
51      stream = Psych::Stream.new(io)
52      stream.start
53      stream.push({ 'foo' => 'bar' })
54
55      assert !stream.finished?, 'stream not finished'
56      stream.finish
57      assert stream.finished?, 'stream finished'
58
59      assert_match(/^---/, io.string)
60      assert_match(/\.\.\.$/, io.string)
61    end
62
63    def test_start_takes_block
64      io     = StringIO.new
65      stream = Psych::Stream.new(io)
66      stream.start do |emitter|
67        emitter.push({ 'foo' => 'bar' })
68      end
69
70      assert stream.finished?, 'stream finished'
71      assert_match(/^---/, io.string)
72      assert_match(/\.\.\.$/, io.string)
73    end
74
75    def test_no_backreferences
76      io     = StringIO.new
77      stream = Psych::Stream.new(io)
78      stream.start do |emitter|
79        x = { 'foo' => 'bar' }
80        emitter.push x
81        emitter.push x
82      end
83
84      assert stream.finished?, 'stream finished'
85      assert_match(/^---/, io.string)
86      assert_match(/\.\.\.$/, io.string)
87      assert_equal 2, io.string.scan('---').length
88      assert_equal 2, io.string.scan('...').length
89      assert_equal 2, io.string.scan('foo').length
90      assert_equal 2, io.string.scan('bar').length
91    end
92  end
93end
94