Sha256: e05bd5c16a2e5822e2deac8aaa1a962ec8a19535f25bd5453522287fdc4298c4

Contents?: true

Size: 1.22 KB

Versions: 28

Compression:

Stored size: 1.22 KB

Contents

require 'native'

describe Hash do
  it 'turns a native JS object into a hash' do
    obj = %x{
      {
        a: 1,
        b: "two",
        c: {
          d: 1,
        },
        e: [
          {
            f: 'g',
            h: [null],
          },
        ],
      }
    }

    h = Hash.new(obj)
    expected_hash = {
      a: 1,
      b: "two",
      c: {
        d: 1,
      },
      e: [
        {
          f: 'g',
          h: [nil],
        },
      ],
    }

    expect(h).to eq(expected_hash)
  end

  it 'turns Object.create(null) JS objects into a hash' do
    %x{
      var obj = Object.create(null);
      var foo = Object.create(null);
      var bar = Object.create(null);
      obj.foo = foo;
      foo.bar = bar;
      bar.baz = 'baz';
    }
    hash = Hash.new(`obj`)

    expect(hash).to eq({ foo: { bar: { baz: 'baz' } } })
  end

  describe '#to_n' do
    it 'converts a hash with native objects as values' do
      obj = { 'a_key' => `{ key: 1 }` }
      native = obj.to_n
      `#{native}.a_key.key`.should == 1
    end

    it 'passes Ruby objects that cannot be converted' do
      object = Object.new
      hash = { foo: object }
      native = hash.to_n
      expect(`#{native}.foo`).to eq object
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
opal-1.7.4 spec/opal/stdlib/native/hash_spec.rb
opal-1.7.3 spec/opal/stdlib/native/hash_spec.rb
opal-1.7.2 spec/opal/stdlib/native/hash_spec.rb
opal-1.7.1 spec/opal/stdlib/native/hash_spec.rb
opal-1.7.0 spec/opal/stdlib/native/hash_spec.rb
opal-1.7.0.rc1 spec/opal/stdlib/native/hash_spec.rb
opal-1.6.1 spec/opal/stdlib/native/hash_spec.rb
opal-1.6.0 spec/opal/stdlib/native/hash_spec.rb
opal-1.6.0.rc1 spec/opal/stdlib/native/hash_spec.rb
opal-1.6.0.alpha1 spec/opal/stdlib/native/hash_spec.rb
opal-1.5.1 spec/opal/stdlib/native/hash_spec.rb
opal-1.5.0 spec/opal/stdlib/native/hash_spec.rb
opal-1.5.0.rc1 spec/opal/stdlib/native/hash_spec.rb
opal-1.4.1 spec/opal/stdlib/native/hash_spec.rb
opal-1.4.0 spec/opal/stdlib/native/hash_spec.rb
opal-1.4.0.alpha1 spec/opal/stdlib/native/hash_spec.rb
opal-1.3.2 spec/opal/stdlib/native/hash_spec.rb
opal-1.3.1 spec/opal/stdlib/native/hash_spec.rb
opal-1.3.0 spec/opal/stdlib/native/hash_spec.rb
opal-1.3.0.rc1 spec/opal/stdlib/native/hash_spec.rb