Skip to content

Commit

Permalink
fixing some existing encoder tests due to change in Yajl for multiple…
Browse files Browse the repository at this point in the history
… tokens in the same stream. Added initial specs for JSON Gem compatability API
  • Loading branch information
brianmario committed Jun 18, 2009
1 parent e71a698 commit 2e4cc38
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 16 deletions.
32 changes: 16 additions & 16 deletions spec/encoding/encoding_spec.rb
Expand Up @@ -56,9 +56,9 @@
end

it "should encode with :pretty turned on and a single space indent, to an IO" do
output = "{\n \"foo\": {\n \"name\": \"bar\",\n \"id\": 1234\n }\n}\n"
output = "{\n \"foo\": {\n \"name\": \"bar\",\n \"id\": 1234\n }\n}"
if RUBY_VERSION.include?('1.9') # FIXME
output = "{\n \"foo\": {\n \"id\": 1234,\n \"name\": \"bar\"\n }\n}\n"
output = "{\n \"foo\": {\n \"id\": 1234,\n \"name\": \"bar\"\n }\n}"
end
obj = {:foo => {:id => 1234, :name => "bar"}}
io = StringIO.new
Expand All @@ -69,9 +69,9 @@
end

it "should encode with :pretty turned on and a single space indent, and return a String" do
output = "{\n \"foo\": {\n \"name\": \"bar\",\n \"id\": 1234\n }\n}\n"
output = "{\n \"foo\": {\n \"name\": \"bar\",\n \"id\": 1234\n }\n}"
if RUBY_VERSION.include?('1.9') # FIXME
output = "{\n \"foo\": {\n \"id\": 1234,\n \"name\": \"bar\"\n }\n}\n"
output = "{\n \"foo\": {\n \"id\": 1234,\n \"name\": \"bar\"\n }\n}"
end
obj = {:foo => {:id => 1234, :name => "bar"}}
encoder = Yajl::Encoder.new(:pretty => true, :indent => ' ')
Expand All @@ -80,9 +80,9 @@
end

it "should encode with :pretty turned on and a tab character indent, to an IO" do
output = "{\n\t\"foo\": {\n\t\t\"name\": \"bar\",\n\t\t\"id\": 1234\n\t}\n}\n"
output = "{\n\t\"foo\": {\n\t\t\"name\": \"bar\",\n\t\t\"id\": 1234\n\t}\n}"
if RUBY_VERSION.include?('1.9') # FIXME
output = "{\n\t\"foo\": {\n\t\t\"id\": 1234,\n\t\t\"name\": \"bar\"\n\t}\n}\n"
output = "{\n\t\"foo\": {\n\t\t\"id\": 1234,\n\t\t\"name\": \"bar\"\n\t}\n}"
end
obj = {:foo => {:id => 1234, :name => "bar"}}
io = StringIO.new
Expand All @@ -93,9 +93,9 @@
end

it "should encode with :pretty turned on and a tab character indent, and return a String" do
output = "{\n\t\"foo\": {\n\t\t\"name\": \"bar\",\n\t\t\"id\": 1234\n\t}\n}\n"
output = "{\n\t\"foo\": {\n\t\t\"name\": \"bar\",\n\t\t\"id\": 1234\n\t}\n}"
if RUBY_VERSION.include?('1.9') # FIXME
output = "{\n\t\"foo\": {\n\t\t\"id\": 1234,\n\t\t\"name\": \"bar\"\n\t}\n}\n"
output = "{\n\t\"foo\": {\n\t\t\"id\": 1234,\n\t\t\"name\": \"bar\"\n\t}\n}"
end
obj = {:foo => {:id => 1234, :name => "bar"}}
encoder = Yajl::Encoder.new(:pretty => true, :indent => "\t")
Expand All @@ -104,9 +104,9 @@
end

it "should encode with it's class method with :pretty and a tab character indent options set, to an IO" do
output = "{\n\t\"foo\": {\n\t\t\"name\": \"bar\",\n\t\t\"id\": 1234\n\t}\n}\n"
output = "{\n\t\"foo\": {\n\t\t\"name\": \"bar\",\n\t\t\"id\": 1234\n\t}\n}"
if RUBY_VERSION.include?('1.9') # FIXME
output = "{\n\t\"foo\": {\n\t\t\"id\": 1234,\n\t\t\"name\": \"bar\"\n\t}\n}\n"
output = "{\n\t\"foo\": {\n\t\t\"id\": 1234,\n\t\t\"name\": \"bar\"\n\t}\n}"
end
obj = {:foo => {:id => 1234, :name => "bar"}}
io = StringIO.new
Expand All @@ -116,19 +116,19 @@
end

it "should encode with it's class method with :pretty and a tab character indent options set, and return a String" do
output = "{\n\t\"foo\": {\n\t\t\"name\": \"bar\",\n\t\t\"id\": 1234\n\t}\n}\n"
output = "{\n\t\"foo\": {\n\t\t\"name\": \"bar\",\n\t\t\"id\": 1234\n\t}\n}"
if RUBY_VERSION.include?('1.9') # FIXME
output = "{\n\t\"foo\": {\n\t\t\"id\": 1234,\n\t\t\"name\": \"bar\"\n\t}\n}\n"
output = "{\n\t\"foo\": {\n\t\t\"id\": 1234,\n\t\t\"name\": \"bar\"\n\t}\n}"
end
obj = {:foo => {:id => 1234, :name => "bar"}}
output = Yajl::Encoder.encode(obj, :pretty => true, :indent => "\t")
output.should == output
end

it "should encode with it's class method with :pretty and a tab character indent options set, to a block" do
output = "{\n\t\"foo\": {\n\t\t\"name\": \"bar\",\n\t\t\"id\": 1234\n\t}\n}\n"
output = "{\n\t\"foo\": {\n\t\t\"name\": \"bar\",\n\t\t\"id\": 1234\n\t}\n}"
if RUBY_VERSION.include?('1.9') # FIXME
output = "{\n\t\"foo\": {\n\t\t\"id\": 1234,\n\t\t\"name\": \"bar\"\n\t}\n}\n"
output = "{\n\t\"foo\": {\n\t\t\"id\": 1234,\n\t\t\"name\": \"bar\"\n\t}\n}"
end
obj = {:foo => {:id => 1234, :name => "bar"}}
output = ''
Expand All @@ -147,9 +147,9 @@
encoder.encode(obj, io)
end
io.rewind
output = "{\"baz\":1234,\"foo\":\"bar\"}\n{\"baz\":1234,\"foo\":\"bar\"}\n{\"baz\":1234,\"foo\":\"bar\"}\n{\"baz\":1234,\"foo\":\"bar\"}\n{\"baz\":1234,\"foo\":\"bar\"}\n"
output = "{\"baz\":1234,\"foo\":\"bar\"}{\"baz\":1234,\"foo\":\"bar\"}{\"baz\":1234,\"foo\":\"bar\"}{\"baz\":1234,\"foo\":\"bar\"}{\"baz\":1234,\"foo\":\"bar\"}"
if RUBY_VERSION.include?('1.9') # FIXME
output = "{\"foo\":\"bar\",\"baz\":1234}\n{\"foo\":\"bar\",\"baz\":1234}\n{\"foo\":\"bar\",\"baz\":1234}\n{\"foo\":\"bar\",\"baz\":1234}\n{\"foo\":\"bar\",\"baz\":1234}\n"
output = "{\"foo\":\"bar\",\"baz\":1234}{\"foo\":\"bar\",\"baz\":1234}{\"foo\":\"bar\",\"baz\":1234}{\"foo\":\"bar\",\"baz\":1234}{\"foo\":\"bar\",\"baz\":1234}"
end
io.read.should == output
end
Expand Down
47 changes: 47 additions & 0 deletions spec/encoding/json_gem_compatability.rb
@@ -0,0 +1,47 @@
# encoding: UTF-8
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')

class Dummy; end

describe "JSON Gem compatability API" do
it "shoud not mixin #to_json on base objects until Yajl::Encoder.enable_json_gem_compatability has been called" do
d = Dummy.new

d.respond_to?(:to_json).should_not be_true
"".respond_to?(:to_json).should_not be_true
1.respond_to?(:to_json).should_not be_true
"1.5".to_f.respond_to?(:to_json).should_not be_true
[].respond_to?(:to_json).should_not be_true
{:foo => "bar"}.respond_to?(:to_json).should_not be_true
true.respond_to?(:to_json).should_not be_true
false.respond_to?(:to_json).should_not be_true
nil.respond_to?(:to_json).should_not be_true
end

it "should mixin #to_json on base objects after Yajl::Encoder.enable_json_gem_compatability has been called" do
Yajl::Encoder.enable_json_gem_compatability
d = Dummy.new

d.respond_to?(:to_json).should be_true
"".respond_to?(:to_json).should be_true
1.respond_to?(:to_json).should be_true
"1.5".to_f.respond_to?(:to_json).should be_true
[].respond_to?(:to_json).should be_true
{:foo => "bar"}.respond_to?(:to_json).should be_true
true.respond_to?(:to_json).should be_true
false.respond_to?(:to_json).should be_true
nil.respond_to?(:to_json).should be_true
end

it "should require yajl/json_gem to enable the compatability API" do
require 'yajl/json_gem'

defined?(JSON).should be_true

JSON.respond_to?(:parse).should be_true
JSON.respond_to?(:generate).should be_true
JSON.respond_to?(:pretty_generate).should be_true
JSON.respond_to?(:load).should be_true
JSON.respond_to?(:dump).should be_true
end
end

0 comments on commit 2e4cc38

Please sign in to comment.