Sha256: 4016a298505d2fcfa298c94a1a75e53c78e5c95adb548179fd49d815320383f2

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

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

3 entries across 3 versions & 1 rubygems

Version Path
gir_ffi-0.7.7 test/ffi-glib/hash_table_test.rb
gir_ffi-0.7.6 test/ffi-glib/hash_table_test.rb
gir_ffi-0.7.5 test/ffi-glib/hash_table_test.rb