Skip to content

Instantly share code, notes, and snippets.

@FeepingCreature
Created December 11, 2019 09:19
Show Gist options
  • Save FeepingCreature/4228805edb4ef425ba47e134d3f7408e to your computer and use it in GitHub Desktop.
Save FeepingCreature/4228805edb4ef425ba47e134d3f7408e to your computer and use it in GitHub Desktop.
require 'test/unit'
# patch in ^ operator support
class Gem::Version
def bump_major
self.class.new segments[0].succ
end
end
Gem::Requirement.__send__ :remove_const, 'OPS'
Gem::Requirement.__send__ :remove_const, 'PATTERN_RAW'
Gem::Requirement.__send__ :remove_const, 'PATTERN'
class Gem::Requirement
OPS = {
"=" => lambda { |v, r| v == r },
"!=" => lambda { |v, r| v != r },
">" => lambda { |v, r| v > r },
"<" => lambda { |v, r| v < r },
">=" => lambda { |v, r| v >= r },
"<=" => lambda { |v, r| v <= r },
"~>" => lambda { |v, r| v >= r && v.release < r.bump },
"^" => lambda { |v, r| v >= r && v.release < r.bump_major }
}.freeze
quoted = OPS.keys.map { |k| Regexp.quote k }.join "|"
PATTERN_RAW = "\\s*(#{quoted})?\\s*(#{Gem::Version::VERSION_PATTERN})\\s*".freeze
PATTERN = /\A#{PATTERN_RAW}\z/.freeze
end
requirement = Gem::Requirement.new '^ 2.3.4'
puts requirement
class TestSemverOperator < Test::Unit::TestCase
def test_simple
requirement = Gem::Requirement.new '^ 2.3.4'
assert_false requirement.satisfied_by? Gem::Version.create '2.2.5'
assert_false requirement.satisfied_by? Gem::Version.create '2.3.3'
assert requirement.satisfied_by? Gem::Version.create '2.3.4'
assert requirement.satisfied_by? Gem::Version.create '2.4.0'
assert requirement.satisfied_by? Gem::Version.create '2.5.0'
assert_false requirement.satisfied_by? Gem::Version.create '3.0.0'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment