summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/guards/platform_spec.rb
blob: 88a7ad86f23072d3aaee997d74d0ada025fa0a73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
require 'spec_helper'
require 'mspec/guards'

RSpec.describe Object, "#platform_is" do
  before :each do
    @guard = PlatformGuard.new :dummy
    allow(PlatformGuard).to receive(:new).and_return(@guard)
    ScratchPad.clear
  end

  it "does not yield when #os? returns false" do
    allow(PlatformGuard).to receive(:os?).and_return(false)
    platform_is(:ruby) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).not_to eq(:yield)
  end

  it "yields when #os? returns true" do
    allow(PlatformGuard).to receive(:os?).and_return(true)
    platform_is(:solarce) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).to eq(:yield)
  end

  it "returns what #os? returns when no block is given" do
    allow(PlatformGuard).to receive(:os?).and_return(true)
    expect(platform_is(:solarce)).to eq(true)
    allow(PlatformGuard).to receive(:os?).and_return(false)
    expect(platform_is(:solarce)).to eq(false)
  end

  it "sets the name of the guard to :platform_is" do
    platform_is(:solarce) { }
    expect(@guard.name).to eq(:platform_is)
  end

  it "calls #unregister even when an exception is raised in the guard block" do
    expect(@guard).to receive(:match?).and_return(true)
    expect(@guard).to receive(:unregister)
    expect do
      platform_is(:solarce) { raise Exception }
    end.to raise_error(Exception)
  end
end

RSpec.describe Object, "#platform_is_not" do
  before :each do
    @guard = PlatformGuard.new :dummy
    allow(PlatformGuard).to receive(:new).and_return(@guard)
    ScratchPad.clear
  end

  it "does not yield when #os? returns true" do
    allow(PlatformGuard).to receive(:os?).and_return(true)
    platform_is_not(:ruby) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).not_to eq(:yield)
  end

  it "yields when #os? returns false" do
    allow(PlatformGuard).to receive(:os?).and_return(false)
    platform_is_not(:solarce) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).to eq(:yield)
  end

  it "returns the opposite of what #os? returns when no block is given" do
    allow(PlatformGuard).to receive(:os?).and_return(true)
    expect(platform_is_not(:solarce)).to eq(false)
    allow(PlatformGuard).to receive(:os?).and_return(false)
    expect(platform_is_not(:solarce)).to eq(true)
  end

  it "sets the name of the guard to :platform_is_not" do
    platform_is_not(:solarce) { }
    expect(@guard.name).to eq(:platform_is_not)
  end

  it "calls #unregister even when an exception is raised in the guard block" do
    expect(@guard).to receive(:match?).and_return(false)
    expect(@guard).to receive(:unregister)
    expect do
      platform_is_not(:solarce) { raise Exception }
    end.to raise_error(Exception)
  end
end

RSpec.describe Object, "#platform_is :wordsize => SIZE_SPEC" do
  before :each do
    @guard = PlatformGuard.new :darwin, :wordsize => 32
    allow(PlatformGuard).to receive(:os?).and_return(true)
    allow(PlatformGuard).to receive(:new).and_return(@guard)
    ScratchPad.clear
  end

  it "yields when #wordsize? returns true" do
    allow(PlatformGuard).to receive(:wordsize?).and_return(true)
    platform_is(:wordsize => 32) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).to eq(:yield)
  end

  it "doesn not yield when #wordsize? returns false" do
    allow(PlatformGuard).to receive(:wordsize?).and_return(false)
    platform_is(:wordsize => 32) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).not_to eq(:yield)
  end
end

RSpec.describe Object, "#platform_is_not :wordsize => SIZE_SPEC" do
  before :each do
    @guard = PlatformGuard.new :darwin, :wordsize => 32
    allow(PlatformGuard).to receive(:os?).and_return(true)
    allow(PlatformGuard).to receive(:new).and_return(@guard)
    ScratchPad.clear
  end

  it "yields when #wordsize? returns false" do
    allow(PlatformGuard).to receive(:wordsize?).and_return(false)
    platform_is_not(:wordsize => 32) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).to eq(:yield)
  end

  it "doesn not yield when #wordsize? returns true" do
    allow(PlatformGuard).to receive(:wordsize?).and_return(true)
    platform_is_not(:wordsize => 32) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).not_to eq(:yield)
  end
end

RSpec.describe PlatformGuard, ".implementation?" do
  it "returns true if passed :ruby and RUBY_ENGINE == 'ruby'" do
    stub_const 'RUBY_ENGINE', 'ruby'
    expect(PlatformGuard.implementation?(:ruby)).to eq(true)
  end

  it "returns true if passed :rubinius and RUBY_ENGINE == 'rbx'" do
    stub_const 'RUBY_ENGINE', 'rbx'
    expect(PlatformGuard.implementation?(:rubinius)).to eq(true)
  end

  it "returns true if passed :jruby and RUBY_ENGINE == 'jruby'" do
    stub_const 'RUBY_ENGINE', 'jruby'
    expect(PlatformGuard.implementation?(:jruby)).to eq(true)
  end

  it "returns true if passed :ironruby and RUBY_ENGINE == 'ironruby'" do
    stub_const 'RUBY_ENGINE', 'ironruby'
    expect(PlatformGuard.implementation?(:ironruby)).to eq(true)
  end

  it "returns true if passed :maglev and RUBY_ENGINE == 'maglev'" do
    stub_const 'RUBY_ENGINE', 'maglev'
    expect(PlatformGuard.implementation?(:maglev)).to eq(true)
  end

  it "returns true if passed :topaz and RUBY_ENGINE == 'topaz'" do
    stub_const 'RUBY_ENGINE', 'topaz'
    expect(PlatformGuard.implementation?(:topaz)).to eq(true)
  end

  it "returns true if passed :ruby and RUBY_ENGINE matches /^ruby/" do
    stub_const 'RUBY_ENGINE', 'ruby'
    expect(PlatformGuard.implementation?(:ruby)).to eq(true)

    stub_const 'RUBY_ENGINE', 'ruby1.8'
    expect(PlatformGuard.implementation?(:ruby)).to eq(true)

    stub_const 'RUBY_ENGINE', 'ruby1.9'
    expect(PlatformGuard.implementation?(:ruby)).to eq(true)
  end

  it "works for an unrecognized name" do
    stub_const 'RUBY_ENGINE', 'myrubyimplementation'
    expect(PlatformGuard.implementation?(:myrubyimplementation)).to eq(true)
    expect(PlatformGuard.implementation?(:other)).to eq(false)
  end
end

RSpec.describe PlatformGuard, ".standard?" do
  it "returns true if implementation? returns true" do
    expect(PlatformGuard).to receive(:implementation?).with(:ruby).and_return(true)
    expect(PlatformGuard.standard?).to be_truthy
  end

  it "returns false if implementation? returns false" do
    expect(PlatformGuard).to receive(:implementation?).with(:ruby).and_return(false)
    expect(PlatformGuard.standard?).to be_falsey
  end
end

RSpec.describe PlatformGuard, ".wordsize?" do
  it "returns true when arg is 32 and 1.size is 4" do
    expect(PlatformGuard.wordsize?(32)).to eq(1.size == 4)
  end

  it "returns true when arg is 64 and 1.size is 8" do
    expect(PlatformGuard.wordsize?(64)).to eq(1.size == 8)
  end
end

RSpec.describe PlatformGuard, ".os?" do
  before :each do
    stub_const 'PlatformGuard::PLATFORM', 'solarce'
  end

  it "returns false when arg does not match the platform" do
    expect(PlatformGuard.os?(:ruby)).to eq(false)
  end

  it "returns false when no arg matches the platform" do
    expect(PlatformGuard.os?(:ruby, :jruby, :rubinius, :maglev)).to eq(false)
  end

  it "returns true when arg matches the platform" do
    expect(PlatformGuard.os?(:solarce)).to eq(true)
  end

  it "returns true when any arg matches the platform" do
    expect(PlatformGuard.os?(:ruby, :jruby, :solarce, :rubinius, :maglev)).to eq(true)
  end

  it "returns true when arg is :windows and the platform contains 'mswin'" do
    stub_const 'PlatformGuard::PLATFORM', 'mswin32'
    expect(PlatformGuard.os?(:windows)).to eq(true)
  end

  it "returns true when arg is :windows and the platform contains 'mingw'" do
    stub_const 'PlatformGuard::PLATFORM', 'i386-mingw32'
    expect(PlatformGuard.os?(:windows)).to eq(true)
  end

  it "returns false when arg is not :windows and RbConfig::CONFIG['host_os'] contains 'mswin'" do
    stub_const 'PlatformGuard::PLATFORM', 'i386-mswin32'
    expect(PlatformGuard.os?(:linux)).to eq(false)
  end

  it "returns false when arg is not :windows and RbConfig::CONFIG['host_os'] contains 'mingw'" do
    stub_const 'PlatformGuard::PLATFORM', 'i386-mingw32'
    expect(PlatformGuard.os?(:linux)).to eq(false)
  end
end

RSpec.describe PlatformGuard, ".os?" do
  it "returns true if called with the current OS or architecture" do
    os = RbConfig::CONFIG["host_os"].sub("-gnu", "")
    arch = RbConfig::CONFIG["host_arch"]
    expect(PlatformGuard.os?(os)).to eq(true)
    expect(PlatformGuard.os?(arch)).to eq(true)
    expect(PlatformGuard.os?("#{arch}-#{os}")).to eq(true)
  end
end

RSpec.describe PlatformGuard, ".os? on JRuby" do
  before :all do
    @verbose = $VERBOSE
    $VERBOSE = nil
  end

  after :all do
    $VERBOSE = @verbose
  end

  before :each do
    @ruby_platform = Object.const_get :RUBY_PLATFORM
    Object.const_set :RUBY_PLATFORM, 'java'
  end

  after :each do
    Object.const_set :RUBY_PLATFORM, @ruby_platform
  end

  it "raises an error when testing for a :java platform" do
    expect {
      PlatformGuard.os?(:java)
    }.to raise_error(":java is not a valid OS")
  end

  it "returns true when arg is :windows and RUBY_PLATFORM contains 'java' and os?(:windows) is true" do
    stub_const 'PlatformGuard::PLATFORM', 'mswin32'
    expect(PlatformGuard.os?(:windows)).to eq(true)
  end

  it "returns true when RUBY_PLATFORM contains 'java' and os?(argument) is true" do
    stub_const 'PlatformGuard::PLATFORM', 'amiga'
    expect(PlatformGuard.os?(:amiga)).to eq(true)
  end
end

RSpec.describe PlatformGuard, ".os?" do
  before :each do
    stub_const 'PlatformGuard::PLATFORM', 'unreal'
  end

  it "returns true if argument matches RbConfig::CONFIG['host_os']" do
    expect(PlatformGuard.os?(:unreal)).to eq(true)
  end

  it "returns true if any argument matches RbConfig::CONFIG['host_os']" do
    expect(PlatformGuard.os?(:bsd, :unreal, :amiga)).to eq(true)
  end

  it "returns false if no argument matches RbConfig::CONFIG['host_os']" do
    expect(PlatformGuard.os?(:bsd, :netbsd, :amiga, :msdos)).to eq(false)
  end

  it "returns false if argument does not match RbConfig::CONFIG['host_os']" do
    expect(PlatformGuard.os?(:amiga)).to eq(false)
  end

  it "returns true when arg is :windows and RbConfig::CONFIG['host_os'] contains 'mswin'" do
    stub_const 'PlatformGuard::PLATFORM', 'i386-mswin32'
    expect(PlatformGuard.os?(:windows)).to eq(true)
  end

  it "returns true when arg is :windows and RbConfig::CONFIG['host_os'] contains 'mingw'" do
    stub_const 'PlatformGuard::PLATFORM', 'i386-mingw32'
    expect(PlatformGuard.os?(:windows)).to eq(true)
  end

  it "returns false when arg is not :windows and RbConfig::CONFIG['host_os'] contains 'mswin'" do
    stub_const 'PlatformGuard::PLATFORM', 'i386-mingw32'
    expect(PlatformGuard.os?(:linux)).to eq(false)
  end

  it "returns false when arg is not :windows and RbConfig::CONFIG['host_os'] contains 'mingw'" do
    stub_const 'PlatformGuard::PLATFORM', 'i386-mingw32'
    expect(PlatformGuard.os?(:linux)).to eq(false)
  end
end

RSpec.describe PlatformGuard, ".windows?" do
  it "returns true on windows" do
    stub_const 'PlatformGuard::PLATFORM', 'i386-mingw32'
    expect(PlatformGuard.windows?).to eq(true)
  end

  it "returns false on non-windows" do
    stub_const 'PlatformGuard::PLATFORM', 'i586-linux'
    expect(PlatformGuard.windows?).to eq(false)
  end
end