Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mspec updates through 202411051711 #70

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/mspec/guards/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,34 @@ def self.wsl?
end

WORD_SIZE = 1.size * 8
deprecate_constant :WORD_SIZE

POINTER_SIZE = begin
require 'rbconfig/sizeof'
RbConfig::SIZEOF["void*"] * 8
rescue LoadError
WORD_SIZE
[0].pack('j').size
headius marked this conversation as resolved.
Show resolved Hide resolved
end

C_LONG_SIZE = if defined?(RbConfig::SIZEOF[])
RbConfig::SIZEOF["long"] * 8
else
[0].pack('l!').size
end

def self.wordsize?(size)
warn "#wordsize? is deprecated, use #c_long_size?"
size == WORD_SIZE
end

def self.pointer_size?(size)
size == POINTER_SIZE
end

def self.c_long_size?(size)
size == C_LONG_SIZE
end

def initialize(*args)
if args.last.is_a?(Hash)
@options, @platforms = args.last, args[0..-2]
Expand All @@ -85,10 +97,13 @@ def match?
case key
when :os
match &&= PlatformGuard.os?(*value)
when :wordsize
match &&= PlatformGuard.wordsize? value
when :pointer_size
match &&= PlatformGuard.pointer_size? value
when :wordsize
warn ":wordsize is deprecated, use :c_long_size"
match &&= PlatformGuard.wordsize? value
when :c_long_size
match &&= PlatformGuard::c_long_size? value
end
end
match
Expand Down
28 changes: 24 additions & 4 deletions lib/mspec/helpers/numeric.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mspec/guards/platform'

def nan_value
Expand All @@ -15,11 +16,13 @@ def bignum_value(plus = 0)
end

def max_long
2**(0.size * 8 - 1) - 1
long_byte_size = [0].pack('l!').size
headius marked this conversation as resolved.
Show resolved Hide resolved
2**(long_byte_size * 8 - 1) - 1
end

def min_long
-(2**(0.size * 8 - 1))
long_byte_size = [0].pack('l!').size
-(2**(long_byte_size * 8 - 1))
end

# This is a bit hairy, but we need to be able to write specs that cover the
Expand All @@ -28,15 +31,32 @@ def min_long
# specs based on the relationship between values rather than specific
# values.
if PlatformGuard.standard? or PlatformGuard.implementation? :topaz
if PlatformGuard.wordsize? 32
limits_available = begin
require 'rbconfig/sizeof'
defined?(RbConfig::LIMITS.[]) && ['FIXNUM_MAX', 'FIXNUM_MIN'].all? do |key|
Integer === RbConfig::LIMITS[key]
end
rescue LoadError
false
end

if limits_available
def fixnum_max
RbConfig::LIMITS['FIXNUM_MAX']
end

def fixnum_min
RbConfig::LIMITS['FIXNUM_MIN']
end
elsif PlatformGuard.c_long_size? 32
def fixnum_max
(2**30) - 1
end

def fixnum_min
-(2**30)
end
elsif PlatformGuard.wordsize? 64
elsif PlatformGuard.c_long_size? 64
def fixnum_max
(2**62) - 1
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mspec/runner/actions/leakchecker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ class << self
attr_accessor :count
end

def new(data)
def new(...)
LeakChecker::TempfileCounter.count += 1
super(data)
super
end
}
LeakChecker.const_set(:TempfileCounter, m)

class << Tempfile::Remover
class << Tempfile
prepend LeakChecker::TempfileCounter
end
end
Expand Down
38 changes: 19 additions & 19 deletions spec/guards/platform_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,44 +81,44 @@
end
end

RSpec.describe Object, "#platform_is :wordsize => SIZE_SPEC" do
RSpec.describe Object, "#platform_is :c_long_size => SIZE_SPEC" do
before :each do
@guard = PlatformGuard.new :darwin, :wordsize => 32
@guard = PlatformGuard.new :darwin, :c_long_size => 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 }
it "yields when #c_long_size? returns true" do
allow(PlatformGuard).to receive(:c_long_size?).and_return(true)
platform_is(:c_long_size => 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 }
it "doesn not yield when #c_long_size? returns false" do
allow(PlatformGuard).to receive(:c_long_size?).and_return(false)
platform_is(:c_long_size => 32) { ScratchPad.record :yield }
expect(ScratchPad.recorded).not_to eq(:yield)
end
end

RSpec.describe Object, "#platform_is_not :wordsize => SIZE_SPEC" do
RSpec.describe Object, "#platform_is_not :c_long_size => SIZE_SPEC" do
before :each do
@guard = PlatformGuard.new :darwin, :wordsize => 32
@guard = PlatformGuard.new :darwin, :c_long_size => 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 }
it "yields when #c_long_size? returns false" do
allow(PlatformGuard).to receive(:c_long_size?).and_return(false)
platform_is_not(:c_long_size => 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 }
it "doesn not yield when #c_long_size? returns true" do
allow(PlatformGuard).to receive(:c_long_size?).and_return(true)
platform_is_not(:c_long_size => 32) { ScratchPad.record :yield }
expect(ScratchPad.recorded).not_to eq(:yield)
end
end
Expand Down Expand Up @@ -184,13 +184,13 @@
end
end

RSpec.describe PlatformGuard, ".wordsize?" do
RSpec.describe PlatformGuard, ".c_long_size?" do
it "returns true when arg is 32 and 1.size is 4" do
expect(PlatformGuard.wordsize?(32)).to eq(1.size == 4)
expect(PlatformGuard.c_long_size?(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)
expect(PlatformGuard.c_long_size?(64)).to eq(1.size == 8)
end
end

Expand Down