Sha256: f5608220533ad2ab6fcc56a7bf4109adb0381f4001010a5064936373f04d61a4

Contents?: true

Size: 1.92 KB

Versions: 9

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require 'gir_ffi_test_helper'

describe GLib::HashTable do
  it 'knows its key and value types' do
    hsh = GLib::HashTable.new :gint32, :utf8
    assert_equal :gint32, hsh.key_type
    assert_equal :utf8, hsh.value_type
  end

  describe '::from' do
    it 'creates a GHashTable from a Ruby hash' do
      hsh = GLib::HashTable.from [:utf8, :gint32],
                                 'foo' => 23, 'bar' => 32
      assert_equal({ 'foo' => 23, 'bar' => 32 }, hsh.to_hash)
    end

    it 'return its argument if given a GHashTable' do
      hsh = GLib::HashTable.from [:utf8, :gint32], 'foo' => 23, 'bar' => 32
      hsh2 = GLib::HashTable.from [:utf8, :gint32], hsh
      assert hsh2.equal? hsh
    end

    it 'wraps its argument if given a pointer' do
      hsh = GLib::HashTable.from [:utf8, :gint32], 'foo' => 23, 'bar' => 32
      pointer = hsh.to_ptr
      assert_instance_of FFI::Pointer, pointer
      hsh2 = GLib::HashTable.from [:utf8, :gint32], pointer
      assert_instance_of GLib::HashTable, hsh2
      refute hsh2.equal? hsh
      hsh2.to_hash.must_equal hsh.to_hash
    end
  end

  it 'allows key-value pairs to be inserted' do
    h = GLib::HashTable.new :utf8, :utf8
    h.insert 'foo', 'bar'
    h.to_hash.must_equal 'foo' => 'bar'
  end

  it 'includes Enumerable' do
    GLib::HashTable.must_include Enumerable
  end

  describe 'a HashTable provided by the system' do
    before do
      GirFFI.setup :Regress
      @hash = Regress.test_ghash_container_return
    end

    it 'has a working #each method' do
      a = {}
      @hash.each { |k, v| a[k] = v }
      a.must_be :==,
                'foo' => 'bar',
                'baz' => 'bat',
                'qux' => 'quux'
    end

    it 'has a working #to_hash method' do
      @hash.to_hash.must_be :==,
                            'foo' => 'bar',
                            'baz' => 'bat',
                            'qux' => 'quux'
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
gir_ffi-0.14.1 test/ffi-glib/hash_table_test.rb
gir_ffi-0.14.0 test/ffi-glib/hash_table_test.rb
gir_ffi-0.13.1 test/ffi-glib/hash_table_test.rb
gir_ffi-0.13.0 test/ffi-glib/hash_table_test.rb
gir_ffi-0.12.1 test/ffi-glib/hash_table_test.rb
gir_ffi-0.12.0 test/ffi-glib/hash_table_test.rb
gir_ffi-0.11.4 test/ffi-glib/hash_table_test.rb
gir_ffi-0.11.3 test/ffi-glib/hash_table_test.rb
gir_ffi-0.11.2 test/ffi-glib/hash_table_test.rb