<commit_before># frozen_string_literal: true

require "gir_ffi_test_helper"

GirFFI.setup :GIMarshallingTests

describe GirFFI::BoxedBase do
  describe "initialize" do
    it "sets up the held struct pointer" do
      # NOTE: GObject::Value uses the generic constructor, unlike
      # GIMarshallingTests::BoxedStruct, which has its own constructor.
      value = GObject::Value.new
      _(value.to_ptr).wont_be_nil
    end
  end

  describe "copy_from" do
    it "returns a copy with owned false" do
      original = GIMarshallingTests::BoxedStruct.new
      copy = GIMarshallingTests::BoxedStruct.copy_from(original)
      ptr = copy.to_ptr
      _(ptr).wont_be :==, original.to_ptr
      _(ptr).wont_be :autorelease? if ptr.respond_to? :autorelease?
      _(copy.struct).wont_be :owned?
    end
  end

  describe "wrap_own" do
    it "wraps and owns the supplied pointer" do
      original = GIMarshallingTests::BoxedStruct.new
      copy = GIMarshallingTests::BoxedStruct.wrap_own(original.to_ptr)
      ptr = copy.to_ptr
      _(ptr).must_equal original.to_ptr
      _(ptr).wont_be :autorelease? if ptr.respond_to? :autorelease?
      _(copy.struct).must_be :owned?
    end
  end

  describe "upon garbage collection" do
    it "frees and disowns the underlying struct if it is owned" do
      skip "cannot be reliably tested on JRuby" if jruby?

      allow(GObject).to receive(:boxed_free)
      gtype = GIMarshallingTests::BoxedStruct.gtype

      owned_struct = GIMarshallingTests::BoxedStruct.new.struct
      owned_ptr = owned_struct.to_ptr

      unowned_struct = GIMarshallingTests::BoxedStruct.new.struct
      unowned_struct.owned = false
      unowned_ptr = unowned_struct.to_ptr

      GC.start
      # Creating a new object is sometimes needed to trigger enough garbage collection.
      GIMarshallingTests::BoxedStruct.new
      sleep 1
      GC.start
      GC.start

      expect(GObject).to have_received(:boxed_free).with(gtype, owned_ptr)
      expect(GObject).not_to have_received(:boxed_free).with(gtype, unowned_ptr)
      _(owned_struct).wont_be :owned?
    end
  end
end<commit_msg>Simplify testing of finalization for boxed types<commit_after># frozen_string_literal: true

require "gir_ffi_test_helper"

GirFFI.setup :GIMarshallingTests

describe GirFFI::BoxedBase do
  describe "initialize" do
    it "sets up the held struct pointer" do
      # NOTE: GObject::Value uses the generic constructor, unlike
      # GIMarshallingTests::BoxedStruct, which has its own constructor.
      value = GObject::Value.new
      _(value.to_ptr).wont_be_nil
    end
  end

  describe "copy_from" do
    it "returns a copy with owned false" do
      original = GIMarshallingTests::BoxedStruct.new
      copy = GIMarshallingTests::BoxedStruct.copy_from(original)
      ptr = copy.to_ptr
      _(ptr).wont_be :==, original.to_ptr
      _(ptr).wont_be :autorelease? if ptr.respond_to? :autorelease?
      _(copy.struct).wont_be :owned?
    end
  end

  describe "wrap_own" do
    it "wraps and owns the supplied pointer" do
      original = GIMarshallingTests::BoxedStruct.new
      copy = GIMarshallingTests::BoxedStruct.wrap_own(original.to_ptr)
      ptr = copy.to_ptr
      _(ptr).must_equal original.to_ptr
      _(ptr).wont_be :autorelease? if ptr.respond_to? :autorelease?
      _(copy.struct).must_be :owned?
    end
  end

  describe "upon garbage collection" do
    it "frees and disowns the underlying struct if it is owned" do
      allow(GObject).to receive(:boxed_free)
      gtype = GIMarshallingTests::BoxedStruct.gtype

      owned_struct = GIMarshallingTests::BoxedStruct.new.struct
      owned_ptr = owned_struct.to_ptr

      unowned_struct = GIMarshallingTests::BoxedStruct.new.struct
      unowned_struct.owned = false
      unowned_ptr = unowned_struct.to_ptr

      GIMarshallingTests::BoxedStruct.send :finalize, owned_struct
      GIMarshallingTests::BoxedStruct.send :finalize, unowned_struct

      expect(GObject).to have_received(:boxed_free).with(gtype, owned_ptr)
      expect(GObject).not_to have_received(:boxed_free).with(gtype, unowned_ptr)
      _(owned_struct).wont_be :owned?
    end
  end
end
{'fraction_non_alphanumeric': 0.08371531966224367, 'fraction_numerical': 0.00024125452352231604, 'mean_word_length': 3.5510428100987927, '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': '5877407', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1333, 'n_words': 313}