Don't create MacOS.version multiple times

This commit is contained in:
Jack Nagel 2013-04-15 15:59:19 -05:00
parent 466a32df02
commit d504d50dcb
2 changed files with 49 additions and 8 deletions

View File

@ -5,19 +5,25 @@ module MacOS extend self
# This can be compared to numerics, strings, or symbols
# using the standard Ruby Comparable methods.
def version
Version.new(MACOS_VERSION)
@version ||= Version.new(MACOS_VERSION)
end
def cat
# PowerPC builds per processor, not per OS
return Hardware::CPU.family if Hardware::CPU.type == :ppc
@cat ||= uncached_cat
end
if version == :mountain_lion then :mountain_lion
elsif version == :lion then :lion
elsif version == :snow_leopard
def uncached_cat
case MacOS.version
when 10.8
:mountain_lion
when 10.7
:lion
when 10.6
Hardware.is_64_bit? ? :snow_leopard : :snow_leopard_32
elsif version == :leopard then :leopard
else nil
when 10.5
:leopard
else
Hardware::CPU.family if Hardware::CPU.type == :ppc
end
end

View File

@ -1,6 +1,7 @@
require 'testing_env'
require 'version'
require 'os/mac/version'
require 'hardware'
class MacOSVersionTests < Test::Unit::TestCase
def setup
@ -39,4 +40,38 @@ class MacOSVersionTests < Test::Unit::TestCase
assert_operator @v, :===, Version.new(10.7)
assert_operator @v, :<, Version.new(10.8)
end
def test_cat_tiger
MacOS.stubs(:version).returns(MacOS::Version.new(10.4))
Hardware::CPU.stubs(:type).returns(:ppc)
Hardware::CPU.stubs(:family).returns(:foo)
assert_equal :foo, MacOS.uncached_cat
end
def test_cat_leopard
MacOS.stubs(:version).returns(MacOS::Version.new(10.5))
assert_equal :leopard, MacOS.uncached_cat
end
def test_cat_snow_leopard_32
MacOS.stubs(:version).returns(MacOS::Version.new(10.6))
Hardware.stubs(:is_64_bit?).returns(false)
assert_equal :snow_leopard_32, MacOS.uncached_cat
end
def test_cat_snow_leopard_64
MacOS.stubs(:version).returns(MacOS::Version.new(10.6))
Hardware.stubs(:is_64_bit?).returns(true)
assert_equal :snow_leopard, MacOS.uncached_cat
end
def test_cat_lion
MacOS.stubs(:version).returns(MacOS::Version.new(10.7))
assert_equal :lion, MacOS.uncached_cat
end
def test_cat_mountain_lion
MacOS.stubs(:version).returns(MacOS::Version.new(10.8))
assert_equal :mountain_lion, MacOS.uncached_cat
end
end