Sha256: f60e7ed26237f9e768f69cbaeee5320707d633ac3b1744e270c25980f7cb3cfe

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# encoding: UTF-8

require 'spec_helper'

describe JsonWriteStream::YieldingWriter do
  let(:stream) do
    StringIO.new.tap do |io|
      io.set_encoding(Encoding::UTF_8)
    end
  end

  let(:stream_writer) { JsonWriteStream::YieldingWriter.new(stream) }

  def check_roundtrip(obj)
    YieldingRoundtripChecker.check_roundtrip(obj)
  end

  def utf8(str)
    str.encode(Encoding::UTF_8)
  end

  it_behaves_like 'a json stream'

  it 'respects the "before" option' do
    stream_writer.write_object do |object_writer|
      object_writer.write_key_value('foo', 'bar', before: "\n  ")
    end

    expect(stream.string).to eq("{\n  \"foo\":\"bar\"}")
  end

  it 'respects the "between" option' do
    stream_writer.write_object do |object_writer|
      object_writer.write_key_value('foo', 'bar', between: ' ')
    end

    expect(stream.string).to eq('{"foo": "bar"}')
  end

  describe '#write_key_value' do
    it 'converts all keys to strings' do
      stream_writer.write_object do |object_writer|
        object_writer.write_key_value(123, 'abc')
      end

      expect(stream.string).to eq(utf8('{"123":"abc"}'))
    end

    it 'supports non-string values' do
      stream_writer.write_object do |object_writer|
        object_writer.write_key_value('abc', 123)
        object_writer.write_key_value('def', true)
      end

      expect(stream.string).to eq(utf8('{"abc":123,"def":true}'))
    end
  end

  describe '#close' do
    it 'closes the underlying stream' do
      stream_writer.close
      expect(stream).to be_closed
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json-write-stream-1.2.0 spec/yielding_spec.rb