Skip to content

Commit

Permalink
Make user agent versions comparable (#68)
Browse files Browse the repository at this point in the history
Co-authored-by: Mikhail Doronin <mikhail.doronin@capitainetrain.com>
  • Loading branch information
misdoro and Mikhail Doronin authored Apr 18, 2022
1 parent 23023d2 commit addec94
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ user_agent.version.major
=> "9"
user_agent.version.minor
=> "0"
user_agent.family == "IE" && user_agent.version >= "9"
=> true
operating_system = user_agent.os
=> #<UserAgentParser::OperatingSystem Windows Vista>
operating_system.to_s
Expand Down
11 changes: 8 additions & 3 deletions lib/user_agent_parser/version.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# frozen_string_literal: true

require 'rubygems/version'

module UserAgentParser
class Version
include Comparable

# Private: Regex used to split string version string into major, minor,
# patch, and patch_minor.
SEGMENTS_REGEX = /\d+\-\d+|\d+[a-zA-Z]+$|\d+|[A-Za-z][0-9A-Za-z-]*$/
SEGMENTS_REGEX = /\d+\-\d+|\d+[a-zA-Z]+$|\d+|[A-Za-z][0-9A-Za-z-]*$/.freeze

attr_reader :version
alias to_s version
Expand Down Expand Up @@ -45,9 +49,10 @@ def eql?(other)
version == other.version
end

alias == eql?
def <=>(other)
Gem::Version.new(version).<=>(Gem::Version.new(other.to_s))
end

# Private
def segments
@segments ||= version.scan(SEGMENTS_REGEX)
end
Expand Down
78 changes: 78 additions & 0 deletions spec/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,84 @@
end
end

describe '#<=>' do
it 'accepts string for comparison' do
version = UserAgentParser::Version.new('1.2.3')

assert_operator version, :<, '1.2.4'
assert_operator version, :==, '1.2.3'
assert_operator version, :>, '1.2.2'
end

it 'accepts another instance of Version for comparison' do
version = UserAgentParser::Version.new('1.2.3')

assert_operator version, :>, UserAgentParser::Version.new('1.2.2')
assert_operator version, :==, UserAgentParser::Version.new('1.2.3')
assert_operator version, :<, UserAgentParser::Version.new('1.2.4')
end

it 'is comparing major version' do
version = UserAgentParser::Version.new('1.2.3')

assert_operator version, :<, '2'
assert_operator version, :>=, '1'
assert_operator version, :>, '0'
end

it 'is comparing minor version' do
version = UserAgentParser::Version.new('1.2.3')

assert_operator version, :<, '2.0'
assert_operator version, :<, '1.3'
assert_operator version, :>=, '1.2'
assert_operator version, :>, '1.1'
assert_operator version, :>, '0.1'
end

it 'is comparing patch level' do
version = UserAgentParser::Version.new('1.2.3')

assert_operator version, :<, '1.2.4'
assert_operator version, :>=, '1.2.3'
assert_operator version, :<=, '1.2.3'
assert_operator version, :>, '1.2.2'
end

it 'is comparing patch_minor level correctly' do
version = UserAgentParser::Version.new('1.2.3.p1')

assert_operator version, :<, '1.2.4'
assert_operator version, :<, '1.2.3.p2'
assert_operator version, :>=, '1.2.3.p1'
assert_operator version, :<=, '1.2.3.p1'
assert_operator version, :>, '1.2.3.p0'
assert_operator version, :>, '1.2.2'
assert_operator version, :>, '1.1'
end

it 'is correctly comparing versions with different lengths' do
version = UserAgentParser::Version.new('1.42.3')

assert_operator version, :<, '1.142'
assert_operator version, :<, '1.42.4'
assert_operator version, :>=, '1.42'
assert_operator version, :>, '1.14'
assert_operator version, :>, '1.7'
assert_operator version, :>, '1.3'
end

it 'does its best to compare string versions' do
version = UserAgentParser::Version.new('1.2.3.a')

assert_operator version, :<, '1.2.4'
assert_operator version, :<, '1.2.3.b'
assert_operator version, :<, '1.2.3.p1'
assert_operator version, :<, '1.2.3.p0'
assert_operator version, :>, '1.2.2'
end
end

describe '#inspect' do
it 'returns the class and version' do
version = UserAgentParser::Version.new('1.2.3')
Expand Down

0 comments on commit addec94

Please sign in to comment.