|
4 | 4 |
|
5 | 5 | RSpec.describe MacOSVersion do |
6 | 6 | let(:version) { described_class.new("10.14") } |
| 7 | + let(:tahoe_major) { described_class.new("26.0") } |
7 | 8 | let(:big_sur_major) { described_class.new("11.0") } |
8 | 9 | let(:big_sur_update) { described_class.new("11.1") } |
| 10 | + let(:frozen_version) { described_class.new("10.14").freeze } |
9 | 11 |
|
10 | | - describe ".kernel_major_version" do |
| 12 | + describe "::kernel_major_version" do |
11 | 13 | it "returns the kernel major version" do |
12 | 14 | expect(described_class.kernel_major_version(version)).to eq "18" |
| 15 | + expect(described_class.kernel_major_version(tahoe_major)).to eq "25" |
13 | 16 | expect(described_class.kernel_major_version(big_sur_major)).to eq "20" |
14 | 17 | expect(described_class.kernel_major_version(big_sur_update)).to eq "20" |
15 | 18 | end |
|
19 | 22 | end |
20 | 23 | end |
21 | 24 |
|
| 25 | + describe "::from_symbol" do |
| 26 | + it "raises an error if the symbol is not a valid macOS version" do |
| 27 | + expect do |
| 28 | + described_class.from_symbol(:foo) |
| 29 | + end.to raise_error(MacOSVersion::Error, "unknown or unsupported macOS version: :foo") |
| 30 | + end |
| 31 | + |
| 32 | + it "creates a new version from a valid macOS version" do |
| 33 | + symbol_version = described_class.from_symbol(:mojave) |
| 34 | + expect(symbol_version).to eq(version) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + describe "#new" do |
| 39 | + it "raises an error if the version is not a valid macOS version" do |
| 40 | + expect do |
| 41 | + described_class.new("1.2") |
| 42 | + end.to raise_error(MacOSVersion::Error, 'unknown or unsupported macOS version: "1.2"') |
| 43 | + end |
| 44 | + |
| 45 | + it "creates a new version from a valid macOS version" do |
| 46 | + string_version = described_class.new("11") |
| 47 | + expect(string_version).to eq(:big_sur) |
| 48 | + end |
| 49 | + end |
| 50 | + |
22 | 51 | specify "comparison with Symbol" do |
23 | 52 | expect(version).to be > :high_sierra |
24 | 53 | expect(version).to eq :mojave |
25 | 54 | # We're explicitly testing the `===` operator results here. |
26 | 55 | expect(version).to be === :mojave # rubocop:disable Style/CaseEquality |
27 | 56 | expect(version).to be < :catalina |
| 57 | + |
| 58 | + # This should work like a normal comparison but the result won't be added |
| 59 | + # to the `@comparison_cache` hash because the object is frozen. |
| 60 | + expect(frozen_version).to eq :mojave |
| 61 | + expect(frozen_version.instance_variable_get(:@comparison_cache)).to eq({}) |
28 | 62 | end |
29 | 63 |
|
30 | 64 | specify "comparison with Integer" do |
|
64 | 98 | end |
65 | 99 | end |
66 | 100 |
|
67 | | - describe "#new" do |
68 | | - it "raises an error if the version is not a valid macOS version" do |
69 | | - expect do |
70 | | - described_class.new("1.2") |
71 | | - end.to raise_error(MacOSVersion::Error, 'unknown or unsupported macOS version: "1.2"') |
| 101 | + describe "#strip_patch" do |
| 102 | + let(:catalina_update) { described_class.new("10.15.1") } |
| 103 | + |
| 104 | + it "returns the version without the patch" do |
| 105 | + expect(big_sur_update.strip_patch).to eq(described_class.new("11")) |
| 106 | + expect(catalina_update.strip_patch).to eq(described_class.new("10.15")) |
72 | 107 | end |
73 | 108 |
|
74 | | - it "creates a new version from a valid macOS version" do |
75 | | - string_version = described_class.new("11") |
76 | | - expect(string_version).to eq(:big_sur) |
| 109 | + it "returns self if version is null" do |
| 110 | + expect(described_class::NULL.strip_patch).to be described_class::NULL |
77 | 111 | end |
78 | 112 | end |
79 | 113 |
|
80 | | - describe "#from_symbol" do |
81 | | - it "raises an error if the symbol is not a valid macOS version" do |
82 | | - expect do |
83 | | - described_class.from_symbol(:foo) |
84 | | - end.to raise_error(MacOSVersion::Error, "unknown or unsupported macOS version: :foo") |
85 | | - end |
| 114 | + specify "#to_sym" do |
| 115 | + version_symbol = :mojave |
86 | 116 |
|
87 | | - it "creates a new version from a valid macOS version" do |
88 | | - symbol_version = described_class.from_symbol(:mojave) |
89 | | - expect(symbol_version).to eq(version) |
90 | | - end |
| 117 | + # We call this more than once to exercise the caching logic |
| 118 | + expect(version.to_sym).to eq(version_symbol) |
| 119 | + expect(version.to_sym).to eq(version_symbol) |
| 120 | + |
| 121 | + # This should work like a normal but the symbol won't be stored as the |
| 122 | + # `@sym` instance variable because the object is frozen. |
| 123 | + expect(frozen_version.to_sym).to eq(version_symbol) |
| 124 | + expect(frozen_version.instance_variable_get(:@sym)).to be_nil |
| 125 | + |
| 126 | + expect(described_class::NULL.to_sym).to eq(:dunno) |
91 | 127 | end |
92 | 128 |
|
93 | 129 | specify "#pretty_name" do |
| 130 | + version_pretty_name = "Mojave" |
| 131 | + |
94 | 132 | expect(described_class.new("10.11").pretty_name).to eq("El Capitan") |
95 | | - expect(described_class.new("10.14").pretty_name).to eq("Mojave") |
| 133 | + |
| 134 | + # We call this more than once to exercise the caching logic |
| 135 | + expect(version.pretty_name).to eq(version_pretty_name) |
| 136 | + expect(version.pretty_name).to eq(version_pretty_name) |
| 137 | + |
| 138 | + # This should work like a normal but the computed name won't be stored as |
| 139 | + # the `@pretty_name` instance variable because the object is frozen. |
| 140 | + expect(frozen_version.pretty_name).to eq(version_pretty_name) |
| 141 | + expect(frozen_version.instance_variable_get(:@pretty_name)).to be_nil |
96 | 142 | end |
97 | 143 |
|
98 | 144 | specify "#inspect" do |
99 | 145 | expect(described_class.new("11").inspect).to eq("#<MacOSVersion: \"11\">") |
100 | 146 | end |
101 | 147 |
|
102 | | - specify "#requires_nehalem_cpu?", :needs_macos do |
103 | | - expect(Hardware::CPU).to receive(:type).at_least(:twice).and_return(:intel) |
104 | | - expect(described_class.new("10.14").requires_nehalem_cpu?).to be true |
105 | | - expect(described_class.new("10.12").requires_nehalem_cpu?).to be false |
| 148 | + specify "#outdated_release?" do |
| 149 | + expect(described_class.new(described_class::SYMBOLS.values.first).outdated_release?).to be false |
| 150 | + expect(described_class.new("10.0").outdated_release?).to be true |
| 151 | + end |
| 152 | + |
| 153 | + specify "#prerelease?" do |
| 154 | + expect(described_class.new("1000").prerelease?).to be true |
| 155 | + end |
| 156 | + |
| 157 | + specify "#unsupported_release?" do |
| 158 | + expect(described_class.new("10.0").unsupported_release?).to be true |
| 159 | + expect(described_class.new("1000").prerelease?).to be true |
| 160 | + end |
| 161 | + |
| 162 | + describe "#requires_nehalem_cpu?", :needs_macos do |
| 163 | + context "when CPU is Intel" do |
| 164 | + it "returns true if version requires a Nehalem CPU" do |
| 165 | + allow(Hardware::CPU).to receive(:type).and_return(:intel) |
| 166 | + expect(described_class.new("10.14").requires_nehalem_cpu?).to be true |
| 167 | + end |
| 168 | + |
| 169 | + it "returns false if version does not require a Nehalem CPU" do |
| 170 | + allow(Hardware::CPU).to receive(:type).and_return(:intel) |
| 171 | + expect(described_class.new("10.12").requires_nehalem_cpu?).to be false |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + context "when CPU is not Intel" do |
| 176 | + it "raises an error" do |
| 177 | + allow(Hardware::CPU).to receive(:type).and_return(:arm) |
| 178 | + expect { described_class.new("10.14").requires_nehalem_cpu? } |
| 179 | + .to raise_error(ArgumentError) |
| 180 | + end |
| 181 | + end |
| 182 | + |
| 183 | + it "returns false when version is null" do |
| 184 | + expect(described_class::NULL.requires_nehalem_cpu?).to be false |
| 185 | + end |
106 | 186 | end |
107 | 187 | end |
0 commit comments