summaryrefslogtreecommitdiff
path: root/spec/ffi_yajl/encoder_spec.rb
blob: 443e98713430b61189633fdfd2e7a5a8d94fadee (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
# encoding: UTF-8

require 'spec_helper'

describe "FFI_Yajl::Encoder" do

  let(:encoder) { FFI_Yajl::Encoder.new }

  it "encodes fixnums in keys as strings" do
    ruby = {1 => 2}
    expect(encoder.encode(ruby)).to eq('{"1":2}')
  end

  it "encodes floats in keys as strings" do
    ruby = {1.1 => 2}
    expect(encoder.encode(ruby)).to eq('{"1.1":2}')
  end

  it "encodes bignums in keys as strings" do
    ruby = {12345678901234567890 => 2}
    expect(encoder.encode(ruby)).to eq('{"12345678901234567890":2}')
  end

  # XXX: 127 == YAJL_MAX_DEPTH hardcodedness, zero control for us, it isn't even a twiddleable #define
  it "raises an exception for deeply nested arrays" do
    root = []
    a = root
    127.times { |_| a << []; a = a[0] }
    expect{ encoder.encode(root) }.to raise_error(FFI_Yajl::EncodeError)
  end

  it "raises an exception for deeply nested hashes" do
    root = {}
    a = root
    127.times {|_| a["a"] = {}; a = a["a"] }
    expect{ encoder.encode(root) }.to raise_error(FFI_Yajl::EncodeError)
  end

  it "encodes symbols in keys as strings" do
    ruby = {:thing => 1}
    expect(encoder.encode(ruby)).to eq('{"thing":1}')
  end

  it "encodes symbols in values as strings" do
    ruby = {"thing" => :one}
    expect(encoder.encode(ruby)).to eq('{"thing":"one"}')
  end
end