summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2013-10-23 22:57:14 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2013-10-23 22:57:14 -0700
commitba4de36ba9854f94f8c62701a16f99025985a605 (patch)
tree6a5d1231d803780195592eda26c55554d9df3883 /spec
parentab8fb01420842760b45ffb862183f93f205b1c9c (diff)
downloadffi-yajl-ba4de36ba9854f94f8c62701a16f99025985a605.tar.gz
add the initial specs
Diffstat (limited to 'spec')
-rw-r--r--spec/ffi_yajl/json_gem_spec.rb99
-rw-r--r--spec/spec_helper.rb13
2 files changed, 112 insertions, 0 deletions
diff --git a/spec/ffi_yajl/json_gem_spec.rb b/spec/ffi_yajl/json_gem_spec.rb
new file mode 100644
index 0000000..3516dbd
--- /dev/null
+++ b/spec/ffi_yajl/json_gem_spec.rb
@@ -0,0 +1,99 @@
+
+require 'spec_helper'
+require 'ffi_yajl/json_gem'
+
+class Dummy; end
+
+describe "JSON Gem Compat API" do
+# it "shoud not mixin #to_json on base objects until compatability has been enabled" do
+# d = Dummy.new
+#
+# expect(d.respond_to?(:to_json)).to_not be_true
+# expect("".respond_to?(:to_json)).to_not be_true
+# expect(1.respond_to?(:to_json)).to_not be_true
+# expect("1.5".to_f.respond_to?(:to_json)).to_not be_true
+# expect([].respond_to?(:to_json)).to_not be_true
+# expect({:foo => "bar"}.respond_to?(:to_json)).to_not be_true
+# expect(true.respond_to?(:to_json)).to_not be_true
+# expect(false.respond_to?(:to_json)).to_not be_true
+# expect(nil.respond_to?(:to_json)).to_not be_true
+# end
+
+ it "should mixin #to_json on base objects after compatability has been enabled" do
+ d = Dummy.new
+
+ expect(d.respond_to?(:to_json)).to be_true
+ expect("".respond_to?(:to_json)).to be_true
+ expect(1.respond_to?(:to_json)).to be_true
+ expect("1.5".to_f.respond_to?(:to_json)).to be_true
+ expect([].respond_to?(:to_json)).to be_true
+ expect({:foo => "bar"}.respond_to?(:to_json)).to be_true
+ expect(true.respond_to?(:to_json)).to be_true
+ expect(false.respond_to?(:to_json)).to be_true
+ expect(nil.respond_to?(:to_json)).to be_true
+ end
+
+ it "should require yajl/json_gem to enable the compatability API" do
+ expect(defined?(JSON)).to be_true
+
+ expect(JSON.respond_to?(:parse)).to be_true
+ expect(JSON.respond_to?(:generate)).to be_true
+ expect(JSON.respond_to?(:pretty_generate)).to be_true
+ expect(JSON.respond_to?(:load)).to be_true
+ expect(JSON.respond_to?(:dump)).to be_true
+ end
+
+ context "ported tests for generation" do
+ before(:all) do
+ @hash = {
+ 'a' => 2,
+ 'b' => 3.141,
+ 'c' => 'c',
+ 'd' => [ 1, "b", 3.14 ],
+ 'e' => { 'foo' => 'bar' },
+ 'g' => "blah",
+ 'h' => 1000.0,
+ 'i' => 0.001
+ }
+
+ @json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},"g":"blah","h":1000.0,"i":0.001}'
+
+ @json3 = %{
+ {
+ "a": 2,
+ "b": 3.141,
+ "c": "c",
+ "d": [1, "b", 3.14],
+ "e": {"foo": "bar"},
+ "g": "blah",
+ "h": 1000.0,
+ "i": 0.001
+ }
+ }.chomp
+ end
+
+ it "should be able to unparse" do
+ json = JSON.generate(@hash)
+ expect(JSON.parse(@json2)).to eq(JSON.parse(json))
+ parsed_json = JSON.parse(json)
+ expect(@hash).to eq(parsed_json)
+ json = JSON.generate({1=>2})
+ expect('{"1":2}').to eql(json)
+ parsed_json = JSON.parse(json)
+ expect({"1"=>2}).to eq(parsed_json)
+ end
+
+ it "should be able to unparse pretty" do
+ json = JSON.pretty_generate(@hash)
+ expect(JSON.parse(@json3)).to eq(JSON.parse(json))
+ parsed_json = JSON.parse(json)
+ expect(@hash).to eq(parsed_json)
+ json = JSON.pretty_generate({1=>2})
+ test = "{\n \"1\": 2\n}".chomp
+ expect(test).to eq(json)
+ parsed_json = JSON.parse(json)
+ expect({"1"=>2}).to eq(parsed_json)
+ end
+ end
+
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644
index 0000000..83b4705
--- /dev/null
+++ b/spec/spec_helper.rb
@@ -0,0 +1,13 @@
+$: << File.expand_path(File.join(File.dirname( __FILE__ ), "../lib"))
+
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+
+ config.order = 'random'
+
+ config.expect_with :rspec do |c|
+ c.syntax = :expect
+ end
+end