Sha256: 5c37bae7d6193131ce041ccf56a8afd729da40d95c45d496e88508410c23dadc

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

require File.dirname(__FILE__) + '/../spec_helper'

describe V8::Object do
  subject { V8::Object }
  setup_context

  it "inherits V8::Value" do
    subject.new(:foo => 1).should be_kind_of(V8::Value)
  end
  
  describe ".new" do
    it "creates new v8 object" do
      subject.new.should be_kind_of(V8::Object)
    end

    context "when hash with properties passed" do
      it "creates object with set given properties" do
        obj = subject.new(:foo => 1, :bar => 2)
        obj[:foo].should == 1
        obj[:bar].should == 2
      end
    end
  end
  
  describe "#[] and #[]=" do
    it "sets and gets values of given key from current object" do
      obj = subject.new
      obj[:foo] = "bar"
      obj[:foo].should == "bar"
    end

    it "can get properties by numeric keys" do
      obj = subject.new
      obj[1] = 'foo'
      obj[1].should == 'foo'
    end

    it "are aliased with #get and #set" do
      obj = subject.new
      obj.set(:foo, "bar")
      obj.get(:foo).should == "bar"
    end
  end

  describe "#keys" do
    let(:obj) { subject.new(:foo => 1, :bar => 2) }

    it "returns list of keys belonging to an object" do
      obj.keys.should == ['foo', 'bar']
    end

    it "is aliased with #properties" do
      obj.properties.should == ['foo', 'bar']
    end
  end

  describe "an instance" do
    it "is enumerable" do
      res = {}
      obj = subject.new(:foo => 1, :bar => 2)
      obj.each {|k,v| res[k.to_s] = v }
      res['foo'].should == 1
      res['bar'].should == 2
    end

    it "is delegated properly" do
      obj = subject.new(:foo => 1, :bar => 2)
      obj.delegate['foo'].should == 1
      obj.delegate['bar'].should == 2
    end
  end

  describe "in context" do
    it "object reflected twice should be explicitly the same" do
      cxt.eval("foo = {a:1, b:2}", "<eval>")
      cxt[:foo].object_id.should == cxt[:foo].object_id
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mustang-0.1.1 spec/v8/object_spec.rb
mustang-0.1.0 spec/v8/object_spec.rb