Sha256: 65a8062a6ca0d752156b73d81ca79c8cf5366f1b902429fb52bc4529d76dc93f

Contents?: true

Size: 1.12 KB

Versions: 48

Compression:

Stored size: 1.12 KB

Contents

require 'json'

describe "Hash#to_json" do
  it "returns a string of all key and value pairs" do
    {}.to_json.should == "{}"
    {"a" => 1, "b" => 2}.to_json.should == '{"a":1, "b":2}'

    hash = {"a" => 1, "b" => false, "c" => nil, "d" => true}
    JSON.parse(hash.to_json).should == hash
  end
end

describe "Array#to_json" do
  it "returns a string of all array elements converted to json" do
    [].to_json.should == "[]"
    [1, 2, 3].to_json.should == "[1, 2, 3]"
    [true, nil, false, "3", 42].to_json.should == '[true, null, false, "3", 42]'
  end
end

describe "Boolean#to_json" do
  it "returns 'true' when true" do
    true.to_json.should == "true"
  end

  it "returns 'false' when false" do
    false.to_json.should == "false"
  end
end

describe "Kernel#to_json" do
  it "returns an escaped #to_s of the receiver" do
    self.to_json.should be_kind_of(String)
  end
end

describe "NilClass#to_json" do
  it "returns 'null'" do
    nil.to_json.should == "null"
  end
end

describe "String#to_json" do
  it "returns an escaped string" do
    "foo".to_json.should == "\"foo\""
    "bar\nbaz".to_json.should == "\"bar\\nbaz\""
  end
end

Version data entries

48 entries across 48 versions & 3 rubygems

Version Path
opal-0.10.6 spec/opal/stdlib/json/ext_spec.rb
opal-0.10.6.beta spec/opal/stdlib/json/ext_spec.rb
opal-0.10.5 spec/opal/stdlib/json/ext_spec.rb
opal-0.10.4 spec/opal/stdlib/json/ext_spec.rb
opal-0.11.0.rc1 spec/opal/stdlib/json/ext_spec.rb
opal-0.10.3 spec/opal/stdlib/json/ext_spec.rb
opal-0.10.2 spec/opal/stdlib/json/ext_spec.rb
opal-0.10.1 spec/opal/stdlib/json/ext_spec.rb
opal-0.10.0 spec/opal/stdlib/json/ext_spec.rb
opal-0.10.0.rc2 spec/opal/stdlib/json/ext_spec.rb
opal-0.9.4 spec/opal/stdlib/json/ext_spec.rb
opal-0.9.3 spec/opal/stdlib/json/ext_spec.rb
opal-0.10.0.rc1 spec/opal/stdlib/json/ext_spec.rb
opal-0.10.0.beta5 spec/opal/stdlib/json/ext_spec.rb
opal-0.10.0.beta4 spec/opal/stdlib/json/ext_spec.rb
opal-0.10.0.beta3 spec/opal/stdlib/json/ext_spec.rb
opal-0.10.0.beta2 spec/opal/stdlib/json/ext_spec.rb
opal-0.10.0.beta1 spec/opal/stdlib/json/ext_spec.rb
opal-0.9.2 spec/opal/stdlib/json/ext_spec.rb
opal-0.9.0 spec/opal/stdlib/json/ext_spec.rb