Skip to content

Commit

Permalink
Add test for Package#satisfied?
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jan 11, 2025
1 parent 53556d8 commit be3359f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
6 changes: 1 addition & 5 deletions lib/rubygems-requirements-system/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ def installed?(package)
return false
end

return true if package.version.nil?

current_version = Gem::Version.new(package_config.version)
required_version = Gem::Version.new(package.version)
current_version.__send__(package.operator, required_version)
package.satisfied?(package_config.version)
end
end

Expand Down
12 changes: 10 additions & 2 deletions lib/rubygems-requirements-system/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

module RubyGemsRequirementsSystem
Package = Struct.new(:id, :operator, :version) do
Package = Struct.new(:id, :operator, :required_version) do
class << self
def parse(input)
new(*input.split(/\s*(==|>=|>|<=|<)\s*/, 3))
Expand All @@ -23,8 +23,16 @@ def parse(input)

def valid?
return false if id.empty?
return false if operator and version.nil?
return false if operator and required_version.nil?
true
end

def satisfied?(target_version)
return true if required_version.nil?

target = Gem::Version.new(target_version)
required = Gem::Version.new(required_version)
target.__send__(operator, required)
end
end
end
70 changes: 69 additions & 1 deletion test/test-package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class TestPackage < Test::Unit::TestCase
Package = RubyGemsRequirementsSystem::Package

class TestParse < self
sub_test_case(".parse") do
def test_id
assert_equal(Package.new("cairo"),
Package.parse("cairo"))
Expand Down Expand Up @@ -49,4 +49,72 @@ def test_operator_less_than
Package.parse("cairo < 1.0"))
end
end

sub_test_case("#satisfied?") do
def test_no_required_version
assert do
Package.new("cairo").satisfied?("1.0.0")
end
end

def test_equal_true
assert do
Package.new("cairo", "==", "1.0.0").satisfied?("1.0.0")
end
end

def test_equal_false
assert do
not Package.new("cairo", "==", "1.0.0").satisfied?("1.0.1")
end
end

def test_greater_than_equal_true
assert do
Package.new("cairo", ">=", "1.0.2").satisfied?("1.0.2")
end
end

def test_greater_than_equal_false
assert do
not Package.new("cairo", ">=", "1.0.2").satisfied?("1.0.1")
end
end

def test_greater_than_true
assert do
Package.new("cairo", ">", "1.0.2").satisfied?("1.0.3")
end
end

def test_greater_than_false
assert do
not Package.new("cairo", ">", "1.0.2").satisfied?("1.0.2")
end
end

def test_less_than_equal_true
assert do
Package.new("cairo", "<=", "1.0.2").satisfied?("1.0.2")
end
end

def test_less_than_equal_false
assert do
not Package.new("cairo", "<=", "1.0.2").satisfied?("1.0.3")
end
end

def test_less_than_true
assert do
Package.new("cairo", "<", "1.0.2").satisfied?("1.0.1")
end
end

def test_less_than_false
assert do
not Package.new("cairo", "<", "1.0.2").satisfied?("1.0.2")
end
end
end
end

0 comments on commit be3359f

Please sign in to comment.