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

View File

@ -1,6 +1,7 @@
require 'testing_env' require 'testing_env'
require 'version' require 'version'
require 'os/mac/version' require 'os/mac/version'
require 'hardware'
class MacOSVersionTests < Test::Unit::TestCase class MacOSVersionTests < Test::Unit::TestCase
def setup def setup
@ -39,4 +40,38 @@ class MacOSVersionTests < Test::Unit::TestCase
assert_operator @v, :===, Version.new(10.7) assert_operator @v, :===, Version.new(10.7)
assert_operator @v, :<, Version.new(10.8) assert_operator @v, :<, Version.new(10.8)
end 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 end