<commit_before>require File.expand_path('../gir_ffi_test_helper.rb', File.dirname(__FILE__))

describe GirFFI::Builder::Type::Struct do
  describe "#pretty_print" do
    it "returns a class block" do
      mock(info = Object.new).safe_name { "Bar" }
      stub(info).namespace { "Foo" }

      builder = GirFFI::Builder::Type::Struct.new(info)

      assert_equal "class Bar\nend", builder.pretty_print
    end
  end

  describe "for a struct with a simple layout" do
    before do
      stub(@type = Object.new).pointer? { false }
      stub(@type).tag { :gint32 }

      stub(field = Object.new).field_type { @type }
      stub(field).name { "bar" }
      stub(field).offset { 0 }
      stub(field).writable? { true }

      stub(@struct = Object.new).safe_name { 'Bar' }
      stub(@struct).namespace { 'Foo' }
      stub(@struct).fields { [ field ] }
      stub(@struct).find_method { }

      @builder = GirFFI::Builder::Type::Struct.new @struct
    end

    it "creates the correct layout specification" do
      spec = @builder.send :layout_specification
      assert_equal [:bar, :int32, 0], spec
    end

    it "creates getter and setter methods" do
      m = Module.new { module Lib; end }
      stub(GirFFI::Builder).build_module('Foo') { m }

      c = Class.new

      @builder.instance_eval { @klass = c }
      @builder.send :setup_field_accessors
      methods = c.instance_methods
      assert_includes methods, :bar
      assert_includes methods, :bar=
    end
  end

  describe "for a struct with a layout with a fixed-length array" do
    before do
      stub(subtype = Object.new).pointer? { false }
      stub(subtype).tag { :foo }

      stub(@type = Object.new).pointer? { false }
      stub(@type).tag { :array }
      stub(@type).array_fixed_size { 2 }
      stub(@type).param_type { subtype }

      stub(field = Object.new).field_type { @type }
      stub(field).name { "bar" }
      stub(field).offset { 0 }

      stub(@struct = Object.new).safe_name { 'Bar' }
      stub(@struct).namespace { 'Foo' }
      stub(@struct).fields { [ field ] }
    end

    it "creates the correct ffi type for the array" do
      builder = GirFFI::Builder::Type::Struct.new @struct
      spec = builder.send :itypeinfo_to_ffitype_for_struct, @type
      assert_equal [:foo, 2], spec
    end

    it "creates the correct layout specification" do
      builder = GirFFI::Builder::Type::Struct.new @struct
      spec = builder.send :layout_specification
      assert_equal [:bar, [:foo, 2], 0], spec
    end
  end

  describe "for a struct without defined fields" do
    it "uses a single field of the parent struct type as the default layout" do
      @gir = GObjectIntrospection::IRepository.default
      @gir.require 'GObject', nil

      stub(info = Object.new).parent { @gir.find_by_name 'GObject', 'Object' }
      stub(info).fields { [] }
      stub(info).info_type { :object }
      stub(info).safe_name { 'Bar' }
      stub(info).namespace { 'Foo' }

      @classbuilder = GirFFI::Builder::Type::Object.new info

      spec = @classbuilder.send :layout_specification
      assert_equal [:parent, GObject::Object::Struct, 0], spec
    end
  end
end


<commit_msg>Make tests pass on Ruby 1.8.7.<commit_after>require File.expand_path('../gir_ffi_test_helper.rb', File.dirname(__FILE__))

describe GirFFI::Builder::Type::Struct do
  describe "#pretty_print" do
    it "returns a class block" do
      mock(info = Object.new).safe_name { "Bar" }
      stub(info).namespace { "Foo" }

      builder = GirFFI::Builder::Type::Struct.new(info)

      assert_equal "class Bar\nend", builder.pretty_print
    end
  end

  describe "for a struct with a simple layout" do
    before do
      stub(@type = Object.new).pointer? { false }
      stub(@type).tag { :gint32 }

      stub(field = Object.new).field_type { @type }
      stub(field).name { "bar" }
      stub(field).offset { 0 }
      stub(field).writable? { true }

      stub(@struct = Object.new).safe_name { 'Bar' }
      stub(@struct).namespace { 'Foo' }
      stub(@struct).fields { [ field ] }
      stub(@struct).find_method { }

      @builder = GirFFI::Builder::Type::Struct.new @struct
    end

    it "creates the correct layout specification" do
      spec = @builder.send :layout_specification
      assert_equal [:bar, :int32, 0], spec
    end

    it "creates getter and setter methods" do
      m = Module.new { module Lib; end }
      stub(GirFFI::Builder).build_module('Foo') { m }

      c = Class.new

      refute c.method_defined?(:bar)
      refute c.method_defined?(:bar=)

      @builder.instance_eval { @klass = c }
      @builder.send :setup_field_accessors

      assert c.method_defined?(:bar)
      assert c.method_defined?(:bar=)
    end
  end

  describe "for a struct with a layout with a fixed-length array" do
    before do
      stub(subtype = Object.new).pointer? { false }
      stub(subtype).tag { :foo }

      stub(@type = Object.new).pointer? { false }
      stub(@type).tag { :array }
      stub(@type).array_fixed_size { 2 }
      stub(@type).param_type { subtype }

      stub(field = Object.new).field_type { @type }
      stub(field).name { "bar" }
      stub(field).offset { 0 }

      stub(@struct = Object.new).safe_name { 'Bar' }
      stub(@struct).namespace { 'Foo' }
      stub(@struct).fields { [ field ] }
    end

    it "creates the correct ffi type for the array" do
      builder = GirFFI::Builder::Type::Struct.new @struct
      spec = builder.send :itypeinfo_to_ffitype_for_struct, @type
      assert_equal [:foo, 2], spec
    end

    it "creates the correct layout specification" do
      builder = GirFFI::Builder::Type::Struct.new @struct
      spec = builder.send :layout_specification
      assert_equal [:bar, [:foo, 2], 0], spec
    end
  end

  describe "for a struct without defined fields" do
    it "uses a single field of the parent struct type as the default layout" do
      @gir = GObjectIntrospection::IRepository.default
      @gir.require 'GObject', nil

      stub(info = Object.new).parent { @gir.find_by_name 'GObject', 'Object' }
      stub(info).fields { [] }
      stub(info).info_type { :object }
      stub(info).safe_name { 'Bar' }
      stub(info).namespace { 'Foo' }

      @classbuilder = GirFFI::Builder::Type::Object.new info

      spec = @classbuilder.send :layout_specification
      assert_equal [:parent, GObject::Object::Struct, 0], spec
    end
  end
end



{'fraction_non_alphanumeric': 0.1294941634241245, 'fraction_numerical': 0.004202334630350194, 'mean_word_length': 2.827278141751042, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6793696', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2070, 'n_words': 509}