Skip to content

Commit

Permalink
ruby: Implement Enumerable for libdnf5::rpm::PackageSet.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackorp committed Oct 18, 2024
1 parent 217ed87 commit c15b4cd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
8 changes: 8 additions & 0 deletions bindings/libdnf5/rpm.i
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
%module "libdnf5/rpm"
#endif

#if defined(SWIGRUBY)
// Mixin modules for Ruby. This has to be declared before inclusion of the
// related header file.
%mixin libdnf5::rpm::PackageSet "Enumerable";
#endif

%include <exception.i>
%include <std_string.i>
%include <std_vector.i>
Expand Down Expand Up @@ -99,6 +105,8 @@ add_hash(libdnf5::rpm::Package)
add_iterator(PackageSet)
add_iterator(ReldepList)

add_ruby_each(libdnf5::rpm::PackageSet)

%feature("director") TransactionCallbacks;
%include "libdnf5/rpm/transaction_callbacks.hpp"
wrap_unique_ptr(TransactionCallbacksUniquePtr, libdnf5::rpm::TransactionCallbacks);
Expand Down
30 changes: 28 additions & 2 deletions test/ruby/libdnf5/rpm/test_package_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def test_filter_name()
query.filter_name(["pkg"])
assert_equal(1, query.size())

# TODO(dmach): implement each() so the query can be easily iterated or converted to an array
actual = []
it = query.begin()
while it != query.end()
Expand All @@ -55,7 +54,6 @@ def test_filter_name()
query.filter_name(["pk*"], Common::QueryCmp_GLOB)
assert_equal(2, query.size())

# TODO(dmach): implement each() so the query can be easily iterated or converted to an array
actual = []
it = query.begin()
while it != query.end()
Expand All @@ -65,4 +63,32 @@ def test_filter_name()

assert_equal(["pkg-1.2-3.x86_64", "pkg-libs-1:1.3-4.x86_64"], actual)
end

def test_implements_enumerable()
query = Rpm::PackageQuery.new(@base)
query.filter_name(["pkg"])
assert_equal(1, query.size())

# Using each() without a block should return Enumerator.
assert_instance_of(Enumerator, query.each)

# Using each() with a block should return the collection.
assert_instance_of(Rpm::PackageSet, query.each(&:get_name))

actual_nevra = query.map { |pkg| pkg.get_nevra }

assert_equal(["pkg-1.2-3.x86_64"], actual_nevra)

# ---

query = Rpm::PackageQuery.new(@base)
query.filter_name(["pk*"], Common::QueryCmp_GLOB)
assert_equal(2, query.size())

# Test other method than each that comes with Enumerable
actual = query.select { |pkg| pkg.get_name == "pkg-libs" }

assert_equal(1, actual.size)
assert_equal('pkg-libs-1:1.3-4.x86_64', actual.first.get_nevra)
end
end

0 comments on commit c15b4cd

Please sign in to comment.