Permalink
Browse files

Clean up operating system.

Consistent spacing. Full variable names rather than single letter
variables. Remove writer methods as there should be no need to change
version or name once created. Also ensured that eql? works like ==.
  • Loading branch information...
1 parent 90bd165 commit f315a0c70e812a09cacba99b271fc1e8fc47b869 @jnunemaker committed Jan 4, 2013
Showing with 73 additions and 31 deletions.
  1. +16 −16 lib/user_agent_parser/operating_system.rb
  2. +2 −3 lib/user_agent_parser/parser.rb
  3. +55 −12 spec/operating_system_spec.rb
@@ -1,28 +1,28 @@
module UserAgentParser
-
class OperatingSystem
-
- attr_accessor :name, :version
-
- def initialize(name="Other", version=nil)
- self.name = name
- self.version = version
+ attr_reader :name, :version
+
+ def initialize(name = 'Other', version = nil)
+ @name = name
+ @version = version
end
-
+
def to_s
- s = name
- s += " #{version}" if version
- s
+ string = name
+ unless version.nil?
+ string += " #{version}"
+ end
+ string
end
-
+
def inspect
"#<#{self.class} #{to_s}>"
end
-
+
def ==(other)
name == other.name && version == other.version
end
-
- end
-end
+ alias_method :eql?, :==
+ end
+end
@@ -79,9 +79,8 @@ def os_from_pattern_match pattern, match
v2 = pattern["v2_replacement"].sub('$1', v2 || '') if pattern["v2_replacement"]
v3 = pattern["v3_replacement"].sub('$1', v3 || '') if pattern["v3_replacement"]
v4 = pattern["v3_replacement"].sub('$1', v3 || '') if pattern["v4_replacement"]
- os = OperatingSystem.new(os)
- os.version = version_from_segments(v1, v2, v3, v4)
- os
+ version = version_from_segments(v1, v2, v3, v4)
+ OperatingSystem.new(os, version)
end
def version_from_segments(*segments)
@@ -3,27 +3,70 @@
describe UserAgentParser::OperatingSystem do
describe "#to_s" do
it "returns a string of just the name" do
- UserAgentParser::OperatingSystem.new("Windows").to_s.must_equal "Windows"
+ os = UserAgentParser::OperatingSystem.new("Windows")
+ os.to_s.must_equal "Windows"
end
+
it "returns a string of family and version" do
- v = UserAgentParser::Version.new("7")
- UserAgentParser::OperatingSystem.new("Windows", v).to_s.must_equal "Windows 7"
+ version = UserAgentParser::Version.new("7")
+ os = UserAgentParser::OperatingSystem.new("Windows", version)
+ os.to_s.must_equal "Windows 7"
end
end
+
describe "#==" do
- it "should return true for same user agents across different O/S's" do
- os1 = UserAgentParser::OperatingSystem.new("Windows", UserAgentParser::Version.new("7"))
- os2 = UserAgentParser::OperatingSystem.new("Windows", UserAgentParser::Version.new("7"))
+ it "returns true for same user agents across different O/S's" do
+ version = UserAgentParser::Version.new("7")
+ os1 = UserAgentParser::OperatingSystem.new("Windows", version)
+ os2 = UserAgentParser::OperatingSystem.new("Windows", version)
os1.must_equal os2
end
+
+ it "returns false for same name, different versions" do
+ seven = UserAgentParser::Version.new("7")
+ eight = UserAgentParser::Version.new("8")
+ os1 = UserAgentParser::OperatingSystem.new("Windows", seven)
+ os2 = UserAgentParser::OperatingSystem.new("Windows", eight)
+ os1.wont_equal os2
+ end
+
+ it "returns false for different name, same version" do
+ version = UserAgentParser::Version.new("7")
+ os1 = UserAgentParser::OperatingSystem.new("Windows", version)
+ os2 = UserAgentParser::OperatingSystem.new("Blah", version)
+ os1.wont_equal os2
+ end
end
- describe "#inspect" do
- before do
- @ua = UserAgentParser::OperatingSystem.new("OS X", UserAgentParser::Version.new("10.7.4"))
+
+ describe "#eql?" do
+ it "returns true for same user agents across different O/S's" do
+ version = UserAgentParser::Version.new("7")
+ os1 = UserAgentParser::OperatingSystem.new("Windows", version)
+ os2 = UserAgentParser::OperatingSystem.new("Windows", version)
+ assert_equal true, os1.eql?(os2)
end
- it "should return the name and version" do
- @ua.inspect.to_s.must_equal '#<UserAgentParser::OperatingSystem OS X 10.7.4>'
+
+ it "returns false for same name, different versions" do
+ seven = UserAgentParser::Version.new("7")
+ eight = UserAgentParser::Version.new("8")
+ os1 = UserAgentParser::OperatingSystem.new("Windows", seven)
+ os2 = UserAgentParser::OperatingSystem.new("Windows", eight)
+ assert_equal false, os1.eql?(os2)
+ end
+
+ it "returns false for different name, same version" do
+ version = UserAgentParser::Version.new("7")
+ os1 = UserAgentParser::OperatingSystem.new("Windows", version)
+ os2 = UserAgentParser::OperatingSystem.new("Blah", version)
+ assert_equal false, os1.eql?(os2)
+ end
+ end
+
+ describe "#inspect" do
+ it "returns class name and instance to_s" do
+ version = UserAgentParser::Version.new("10.7.4")
+ os = UserAgentParser::OperatingSystem.new("OS X", version)
+ os.inspect.to_s.must_equal '#<UserAgentParser::OperatingSystem OS X 10.7.4>'
end
end
-
end

0 comments on commit f315a0c

Please sign in to comment.