summaryrefslogtreecommitdiff
path: root/test/psych/test_stream.rb
blob: 9807207661f3e7236c0faa4e020350c0c96886f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'psych/helper'

module Psych
  class TestStream < TestCase
    def test_parse_stream_yields_documents
      list = []
      Psych.parse_stream("--- foo\n...\n--- bar") do |doc|
        list << doc.to_ruby
      end
      assert_equal %w{ foo bar }, list
    end

    def test_parse_stream_break
      list = []
      Psych.parse_stream("--- foo\n...\n--- `") do |doc|
        list << doc.to_ruby
        break
      end
      assert_equal %w{ foo }, list
    end

    def test_load_stream_yields_documents
      list = []
      Psych.load_stream("--- foo\n...\n--- bar") do |ruby|
        list << ruby
      end
      assert_equal %w{ foo bar }, list
    end

    def test_load_stream_break
      list = []
      Psych.load_stream("--- foo\n...\n--- `") do |ruby|
        list << ruby
        break
      end
      assert_equal %w{ foo }, list
    end

    def test_explicit_documents
      io     = StringIO.new
      stream = Psych::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_start_takes_block
      io     = StringIO.new
      stream = Psych::Stream.new(io)
      stream.start do |emitter|
        emitter.push({ 'foo' => 'bar' })
      end

      assert stream.finished?, 'stream finished'
      assert_match(/^---/, io.string)
      assert_match(/\.\.\.$/, io.string)
    end

    def test_no_backreferences
      io     = StringIO.new
      stream = Psych::Stream.new(io)
      stream.start do |emitter|
        x = { 'foo' => 'bar' }
        emitter.push x
        emitter.push x
      end

      assert stream.finished?, 'stream finished'
      assert_match(/^---/, io.string)
      assert_match(/\.\.\.$/, io.string)
      assert_equal 2, io.string.scan('---').length
      assert_equal 2, io.string.scan('...').length
      assert_equal 2, io.string.scan('foo').length
      assert_equal 2, io.string.scan('bar').length
    end
  end
end