Sha256: 7949876e368f56cb60795ad4bd4404982b55c97838c5725417675f9ffdd79d3f

Contents?: true

Size: 1.53 KB

Versions: 6

Compression:

Stored size: 1.53 KB

Contents

require 'spec_helper'

shared_examples_for "a JSON interface" do
  it "dumps to JSON" do
    person.to_json.should eql json
  end

  it "loads from JSON" do
    JSON.parse(json).should == person
  end

  context "when nesting other structures" do
    before do
      person.friends = [Person.new(:name => 'Jane')]
    end

    it "loads them into their corresponding structures" do
      json = person.to_json
      JSON.parse(json).friends.first.should be_a Person
    end
  end
end

describe Structure do
  let(:person) { Person.new(:name => 'Joe') }
  let(:json) { '{"json_class":"Person","name":"Joe","age":null,"friends":[]}' }

  context "without Active Support" do
    before(:all) do
      require 'structure/json'
    end

    it_behaves_like "a JSON interface"
  end

  context "with Active Support" do
    before(:all) do
      require 'active_support/ordered_hash'
      require 'active_support/json'
      load 'structure/json.rb'
    end

    after(:all) do
      Object.send(:remove_const, :ActiveSupport)
    end

    it_behaves_like "a JSON interface"

    describe "#as_json" do
      it "returns a hash" do
        person.as_json.should be_a Hash
      end

      it "selects a subset of attributes" do
        as_json = person.as_json(:only => :name)
        as_json.should have_key :name
        as_json.should_not have_key :age
      end

      it "rejects a subset of attributes" do
        as_json = person.as_json(:except => :name)
        as_json.should_not have_key :name
        as_json.should have_key :age
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
structure-0.12.1 spec/structure/json_spec.rb
structure-0.12.0 spec/structure/json_spec.rb
structure-0.11.0 spec/structure/json_spec.rb
structure-0.10.0 spec/structure/json_spec.rb
structure-0.9.0 spec/structure/json_spec.rb
structure-0.8.0 spec/structure/json_spec.rb