Skip to content

Commit

Permalink
Add test for Package.parse
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jan 11, 2025
1 parent e4157ce commit 53556d8
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 9 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ on:
- push
- pull_request
jobs:
unit:
name: Unit
runs-on: ubuntu-latest
timeout-minutes: 5
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ruby
bundler-cache: true
- name: Test
run: |
bundle exec test/run.rb
docker:
name: ${{ matrix.distribution }}
strategy:
Expand Down
11 changes: 2 additions & 9 deletions lib/rubygems-requirements-system/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,10 @@

require_relative "version"

require_relative "package"
require_relative "platform"

module RubyGemsRequirementsSystem
Package = Struct.new(:id, :operator, :version) do
def valid?
return false if id.empty?
return false if operator and version.nil?
true
end
end

Requirement = Struct.new(:packages, :system_packages) do
def satisfied?
packages.any? do |package|
Expand Down Expand Up @@ -116,7 +109,7 @@ def parse_requirements(gemspec_requirements)

def parse_packages(raw_packages)
packages = raw_packages.split(/\s*\|\s*/).collect do |raw_package|
Package.new(*raw_package.split(/\s*(==|!=|>=|>|<=|<)\s*/, 3))
Package.parse(raw_package)
end
# Ignore this requirement if any invalid package is included.
# We must not report an error for this because
Expand Down
30 changes: 30 additions & 0 deletions lib/rubygems-requirements-system/package.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (C) 2025 Ruby-GNOME Project Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

module RubyGemsRequirementsSystem
Package = Struct.new(:id, :operator, :version) do
class << self
def parse(input)
new(*input.split(/\s*(==|>=|>|<=|<)\s*/, 3))
end
end

def valid?
return false if id.empty?
return false if operator and version.nil?
true
end
end
end
16 changes: 16 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (C) 2025 Ruby-GNOME Project Team
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

require_relative "../lib/rubygems-requirements-system/package"
22 changes: 22 additions & 0 deletions test/run.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env ruby
#
# Copyright (C) 2025 Ruby-GNOME Project Team
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

$VERBOSE = true

require "test-unit"

exit(Test::Unit::AutoRunner.run(true, __dir__))
52 changes: 52 additions & 0 deletions test/test-package.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (C) 2025 Ruby-GNOME Project Team
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

require_relative "helper"

class TestPackage < Test::Unit::TestCase
Package = RubyGemsRequirementsSystem::Package

class TestParse < self
def test_id
assert_equal(Package.new("cairo"),
Package.parse("cairo"))
end

def test_operator_equal
assert_equal(Package.new("cairo", "==", "1.0"),
Package.parse("cairo == 1.0"))
end

def test_operator_greater_than_equal
assert_equal(Package.new("cairo", ">=", "1.0"),
Package.parse("cairo >= 1.0"))
end

def test_operator_greater_than
assert_equal(Package.new("cairo", ">", "1.0"),
Package.parse("cairo > 1.0"))
end

def test_operator_less_than_equal
assert_equal(Package.new("cairo", "<=", "1.0"),
Package.parse("cairo <= 1.0"))
end

def test_operator_less_than
assert_equal(Package.new("cairo", "<", "1.0"),
Package.parse("cairo < 1.0"))
end
end
end

0 comments on commit 53556d8

Please sign in to comment.