Class: PlatformGuard

Inherits:
SpecGuard show all
Defined in:
lib/extensions/mspec/mspec/guards/platform.rb

Constant Summary collapse

HOST_OS =
begin
  require 'rbconfig'
  RbConfig::CONFIG['host_os'] || RUBY_PLATFORM
rescue LoadError
  RUBY_PLATFORM
end.downcase

Instance Attribute Summary

Attributes inherited from SpecGuard

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SpecGuard

#add, clear, clear_guards, finish, guards, #record, report, #report_key, #reporting?, ruby_version, #run_if, #run_unless, #unregister, #yield?

Constructor Details

#initialize(*args) ⇒ PlatformGuard

Returns a new instance of PlatformGuard.



47
48
49
50
51
52
53
54
# File 'lib/extensions/mspec/mspec/guards/platform.rb', line 47

def initialize(*args)
  if args.last.is_a?(Hash)
    @options, @platforms = args.last, args[0..-2]
  else
    @options, @platforms = {}, args
  end
  @parameters = args
end

Class Method Details

.implementation?(*args) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/extensions/mspec/mspec/guards/platform.rb', line 4

def self.implementation?(*args)
  args.any? do |name|
    case name
    when :rubinius
      RUBY_NAME.start_with?('rbx')
    when :ruby, :jruby, :truffleruby, :ironruby, :macruby, :maglev, :topaz, :opal
      RUBY_NAME.start_with?(name.to_s)
    else
      raise "unknown implementation #{name}"
    end
  end
end

.os?(*oses) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
# File 'lib/extensions/mspec/mspec/guards/platform.rb', line 28

def self.os?(*oses)
  oses.any? do |os|
    raise ":java is not a valid OS" if os == :java
    if os == :windows
      HOST_OS =~ /(mswin|mingw)/
    else
      HOST_OS.include?(os.to_s)
    end
  end
end

.standard?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/extensions/mspec/mspec/guards/platform.rb', line 17

def self.standard?
  implementation? :ruby
end

.windows?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/extensions/mspec/mspec/guards/platform.rb', line 39

def self.windows?
  os?(:windows)
end

.wordsize?(size) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/extensions/mspec/mspec/guards/platform.rb', line 43

def self.wordsize?(size)
  size == 8 * 1.size
end

Instance Method Details

#match?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/extensions/mspec/mspec/guards/platform.rb', line 56

def match?
  match = @platforms.empty? ? true : PlatformGuard.os?(*@platforms)
  @options.each do |key, value|
    case key
    when :os
      match &&= PlatformGuard.os?(*value)
    when :wordsize
      match &&= PlatformGuard.wordsize? value
    end
  end
  match
end