Skip to content

Commit

Permalink
Tests for <=>
Browse files Browse the repository at this point in the history
  • Loading branch information
amazimbe committed Sep 3, 2024
1 parent 18e7cad commit ca8b905
Show file tree
Hide file tree
Showing 4 changed files with 363 additions and 253 deletions.
50 changes: 1 addition & 49 deletions maven/lib/dependabot/maven/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,61 +40,13 @@ def to_s
end

def prerelease?
tokens.to_a.flatten.any? do |token|
bucket.to_a.flatten.any? do |token|
token.is_a?(Integer) && token.negative?
end
end

def <=>(other)
bucket <=> other.bucket
# cmp = compare_tokens(bucket.tokens, other.bucket.tokens)
# return cmp unless cmp.zero?

# compare_additions(bucket.addition, other.bucket.addition)
end

private

def compare_tokens(a, b) # rubocop:disable Naming/MethodParameterName
max_idx = [a.size, b.size].max - 1
(0..max_idx).each do |idx|
cmp = compare_token_pair(a[idx], b[idx])
return cmp unless cmp.zero?
end
0
end

def compare_token_pair(a = 0, b = 0) # rubocop:disable Metrics/PerceivedComplexity
a ||= 0
b ||= 0

if a.is_a?(Integer) && b.is_a?(String)
return a <= 0 ? -1 : 1
end

if a.is_a?(String) && b.is_a?(Integer)
return b <= 0 ? 1 : -1
end

if a == Dependabot::Maven::VersionParser::SP && b.is_a?(String) && b != Dependabot::Maven::VersionParser::SP
return -1
end

if b == Dependabot::Maven::VersionParser::SP && a.is_a?(String) && a != Dependabot::Maven::VersionParser::SP
return 1
end

a <=> b # a and b are both ints or strings
end

def compare_additions(first, second)
return 0 if first.nil? && second.nil?

(first || empty_addition) <=> (second || empty_addition)
end

def empty_addition
TokenBucket.new([])
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions maven/lib/dependabot/maven/version_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ def parse
@result = @token_bucket
parse_version(false)

# raise ArgumentError, version_string if @result.to_a.empty?
# @result.to_a
raise ArgumentError, "Malformed version string #{version_string}" if @result.to_a.empty?

@result
end

Expand All @@ -113,7 +113,7 @@ def parse_addition(token = nil)
parse_version(true)
end

def parse_version(number_begins_partition)
def parse_version(number_begins_partition) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
# skip leading v if any
scanner.skip(/v/)

Expand Down
2 changes: 1 addition & 1 deletion maven/spec/dependabot/maven/version_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
it "returns the correct array" do
valid_versions.each do |input|
version, result = input
expect(described_class.parse(version)).to eq(result)
expect(described_class.parse(version).to_a).to eq(result)
end
end
end
Expand Down
Loading

0 comments on commit ca8b905

Please sign in to comment.