<commit_before># frozen_string_literal: true

require "gir_ffi_test_helper"

GirFFI.setup :Regress
GirFFI.setup :GIMarshallingTests

describe GirFFI::Builder do
  let(:gir) { GObjectIntrospection::IRepository.default }

  describe ".build_class" do
    it "does not replace existing classes" do
      oldclass = GObject::Object
      GirFFI::Builder.build_class get_introspection_data("GObject", "Object")
      _(GObject::Object).must_equal oldclass
    end
  end

  describe ".build_module" do
    it "refuses to build existing modules defined elsewhere" do
      result = _(-> { GirFFI::Builder.build_module("Array") }).must_raise RuntimeError
      _(result.message).must_equal "The module Array was already defined elsewhere"
    end

    describe "building a module for the first time" do
      before do
        save_module :Regress
        GirFFI::Builder.build_module "Regress"
      end

      it "creates a Lib module ready to attach functions from the shared library" do
        gir = GObjectIntrospection::IRepository.default
        expected = [gir.shared_library("Regress")]
        assert_equal expected, Regress::Lib.ffi_libraries.map(&:name)
      end

      after do
        restore_module :Regress
      end
    end

    describe "building a module that already exists" do
      it "does not replace the existing module" do
        oldmodule = Regress
        GirFFI::Builder.build_module "Regress"
        assert_equal oldmodule, Regress
      end

      it "does not replace the existing Lib module" do
        oldmodule = Regress::Lib
        GirFFI::Builder.build_module "Regress"
        assert_equal oldmodule, Regress::Lib
      end
    end

    it "passes the version on to ModuleBuilder" do
      builder = double(generate: nil)
      expect(GirFFI::Builders::ModuleBuilder).to receive(:new).
        with("Foo", namespace: "Foo", version: "1.0").
        and_return builder

      GirFFI::Builder.build_module "Foo", "1.0"
    end
  end

  describe ".build_by_gtype" do
    it "returns the class types known to the GIR" do
      result = GirFFI::Builder.build_by_gtype GObject::Object.gtype
      _(result).must_equal GObject::Object
    end

    it "returns the class for user-defined types" do
      klass = Class.new GIMarshallingTests::OverridesObject
      Object.const_set "Derived#{Sequence.next}", klass
      gtype = GirFFI.define_type klass<commit_msg>Add unit test for building class derived from non-GObject root<commit_after># frozen_string_literal: true

require "gir_ffi_test_helper"

GirFFI.setup :Regress
GirFFI.setup :GIMarshallingTests

describe GirFFI::Builder do
  let(:gir) { GObjectIntrospection::IRepository.default }

  describe ".build_class" do
    it "does not replace existing classes" do
      oldclass = GObject::Object
      GirFFI::Builder.build_class get_introspection_data("GObject", "Object")
      _(GObject::Object).must_equal oldclass
    end
  end

  describe ".build_module" do
    it "refuses to build existing modules defined elsewhere" do
      result = _(-> { GirFFI::Builder.build_module("Array") }).must_raise RuntimeError
      _(result.message).must_equal "The module Array was already defined elsewhere"
    end

    describe "building a module for the first time" do
      before do
        save_module :Regress
        GirFFI::Builder.build_module "Regress"
      end

      it "creates a Lib module ready to attach functions from the shared library" do
        gir = GObjectIntrospection::IRepository.default
        expected = [gir.shared_library("Regress")]
        assert_equal expected, Regress::Lib.ffi_libraries.map(&:name)
      end

      after do
        restore_module :Regress
      end
    end

    describe "building a module that already exists" do
      it "does not replace the existing module" do
        oldmodule = Regress
        GirFFI::Builder.build_module "Regress"
        assert_equal oldmodule, Regress
      end

      it "does not replace the existing Lib module" do
        oldmodule = Regress::Lib
        GirFFI::Builder.build_module "Regress"
        assert_equal oldmodule, Regress::Lib
      end
    end

    it "passes the version on to ModuleBuilder" do
      builder = double(generate: nil)
      expect(GirFFI::Builders::ModuleBuilder).to receive(:new).
        with("Foo", namespace: "Foo", version: "1.0").
        and_return builder

      GirFFI::Builder.build_module "Foo", "1.0"
    end
  end

  describe ".build_by_gtype" do
    it "returns the class types known to the GIR" do
      result = GirFFI::Builder.build_by_gtype GObject::Object.gtype
      _(result).must_equal GObject::Object
    end

    it "returns the class for user-defined types" do
      klass = Class.new GIMarshallingTests::OverridesObject
      Object.const_set "Derived#{Sequence.next}", klass
      gtype = GirFFI.define_type klass

      found_klass = GirFFI::Builder.build_by_gtype gtype
      _(found_klass).must_equal klass
    end

    it "returns the class for user-defined types not derived from GObject" do
      klass = Class.new Regress::TestFundamentalObject
      Object.const_set "Derived#{Sequence.next}", klass
      gtype = GirFFI.define_type klass
{'fraction_non_alphanumeric': 0.08226867982965544, 'fraction_numerical': 0.001548586914440573, 'mean_word_length': 3.140224358974359, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5804583', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1520, 'n_words': 445}