Class: Object

Inherits:
BasicObject
Includes:
PP::ObjectMixin
Defined in:
lib/framework/pp.rb,
lib/extensions/rhospec/rhospec.rb,
lib/extensions/rhospec/rhospec.rb,
lib/extensions/mspec/mspec/guards/bug.rb,
lib/extensions/mspec/mspec/helpers/fs.rb,
lib/extensions/mspec/mspec/helpers/io.rb,
lib/extensions/mspec/mspec/mocks/mock.rb,
lib/extensions/mspec/mspec/helpers/tmp.rb,
lib/extensions/mspec/mspec/helpers/argf.rb,
lib/extensions/mspec/mspec/helpers/argv.rb,
lib/extensions/mspec/mspec/matchers/eql.rb,
lib/extensions/mspec/mspec/mocks/object.rb,
lib/extensions/rexml/rexml/xpath_parser.rb,
lib/extensions/mspec/mspec/guards/endian.rb,
lib/extensions/mspec/mspec/helpers/flunk.rb,
lib/extensions/mspec/mspec/runner/object.rb,
lib/extensions/mspec/mspec/runner/shared.rb,
lib/extensions/rhoxml/rexml/xpath_parser.rb,
lib/extensions/mspec/mspec/guards/feature.rb,
lib/extensions/mspec/mspec/guards/support.rb,
lib/extensions/mspec/mspec/guards/version.rb,
lib/extensions/mspec/mspec/matchers/equal.rb,
lib/extensions/mspec/mspec/guards/conflict.rb,
lib/extensions/mspec/mspec/guards/platform.rb,
lib/extensions/mspec/mspec/helpers/fixture.rb,
lib/extensions/mspec/mspec/helpers/numeric.rb,
lib/extensions/mspec/mspec/helpers/warning.rb,
lib/extensions/mspec/mspec/matchers/be_nan.rb,
lib/extensions/mspec/mspec/matchers/be_nil.rb,
lib/extensions/mspec/mspec/matchers/output.rb,
lib/extensions/mspec/mspec/runner/evaluate.rb,
lib/extensions/mspec/mspec/guards/superuser.rb,
lib/extensions/mspec/mspec/helpers/datetime.rb,
lib/extensions/mspec/mspec/helpers/ruby_exe.rb,
lib/extensions/mspec/mspec/matchers/be_true.rb,
lib/extensions/mspec/mspec/guards/quarantine.rb,
lib/extensions/mspec/mspec/matchers/be_close.rb,
lib/extensions/mspec/mspec/matchers/be_empty.rb,
lib/extensions/mspec/mspec/matchers/be_false.rb,
lib/extensions/mspec/mspec/matchers/complain.rb,
lib/extensions/mspec/mspec/matchers/infinity.rb,
lib/extensions/mspec/mspec/expectations/should.rb,
lib/extensions/mspec/mspec/guards/block_device.rb,
lib/extensions/mspec/mspec/matchers/be_kind_of.rb,
lib/extensions/mspec/mspec/matchers/match_yaml.rb,
lib/extensions/mspec/mspec/matchers/respond_to.rb,
lib/extensions/mspec/mspec/helpers/mock_to_path.rb,
lib/extensions/mspec/mspec/matchers/have_method.rb,
lib/extensions/mspec/mspec/matchers/raise_error.rb,
lib/extensions/mspec/mspec/matchers/signed_zero.rb,
lib/extensions/mspec/mspec/matchers/block_caller.rb,
lib/extensions/mspec/mspec/matchers/output_to_fd.rb,
lib/extensions/mspec/mspec/matchers/equal_element.rb,
lib/extensions/mspec/mspec/matchers/have_constant.rb,
lib/extensions/mspec/mspec/matchers/be_ancestor_of.rb,
lib/extensions/mspec/mspec/matchers/be_computed_by.rb,
lib/extensions/mspec/mspec/matchers/be_true_or_false.rb,
lib/extensions/mspec/mspec/matchers/be_an_instance_of.rb,
lib/extensions/mspec/mspec/matchers/have_class_variable.rb,
lib/extensions/mspec/mspec/matchers/have_private_method.rb,
lib/extensions/mspec/mspec/matchers/have_instance_method.rb,
lib/extensions/mspec/mspec/matchers/have_singleton_method.rb,
lib/extensions/mspec/mspec/matchers/have_instance_variable.rb,
lib/extensions/mspec/mspec/matchers/have_public_instance_method.rb,
lib/extensions/mspec/mspec/matchers/have_private_instance_method.rb,
lib/extensions/mspec/mspec/matchers/have_protected_instance_method.rb

Overview

The ruby_exe helper provides a wrapper for invoking the same Ruby interpreter with the same falgs as the one running the specs and getting the output from running the code. If code is a file that exists, it will be run. Otherwise, code should be Ruby code that will be run with the -e command line option. For example:

ruby_exe('path/to/some/file.rb')

will be executed as

`#{RUBY_EXE} 'path/to/some/file.rb'`

while

ruby_exe('puts "hello, world."')

will be executed as

`#{RUBY_EXE} -e 'puts "hello, world."'`

The ruby_exe helper also accepts an options hash with three keys: :options, :args and :env. For example:

ruby_exe('file.rb', :options => "-w",
                    :args => "> file.txt",
                    :env => { :FOO => "bar" })

will be executed as

`#{RUBY_EXE} -w #{'file.rb'} > file.txt`

with access to ENV with value “bar”.

If nil is passed for the first argument, the command line will be built only from the options hash.

The RUBY_EXE constant is setup by mspec automatically and is used by ruby_exe and ruby_cmd. The mspec runner script will set ENV to the name of the executable used to invoke the mspec-run script. The value of RUBY_EXE will be constructed as follows:

1. the value of ENV['RUBY_EXE']
2. an explicit value based on RUBY_NAME
3. cwd/(RUBY_NAME + $(EXEEXT) || $(exeext) || '')
4. $(bindir)/$(RUBY_INSTALL_NAME)

The value will only be used if the file exists and is executable. The flags will then be appended to the resulting value.

These 4 ways correspond to the following scenarios:

 1. Using the MSpec runner scripts, the name of the
    executable is explicitly passed by ENV['RUBY_EXE']
    so there is no ambiguity.

Otherwise, if using RSpec (or something else)

 2. Running the specs while developing an alternative
    Ruby implementation. This explicitly names the
    executable in the development directory based on
    the value of RUBY_NAME, which is probably initialized
    from the value of RUBY_ENGINE.
 3. Running the specs within the source directory for
    some implementation. (E.g. a local build directory.)
 4. Running the specs against some installed Ruby
    implementation.

Additionally, the flags passed to mspec (with -T on the command line or in the config with set :flags) will be appended to RUBY_EXE so that the interpreter is always called with those flags.

Constant Summary collapse

NO_BLOCK_PASSED =
"you must pass a block to the eventually method"
RUBY_EXE =
resolve_ruby_exe
NO_MATCHER_GIVEN =
Object.new

Instance Method Summary collapse

Methods included from PP::ObjectMixin

#pretty_print, #pretty_print_cycle, #pretty_print_inspect, #pretty_print_instance_variables

Instance Method Details

#after(at = :each, &block) ⇒ Object



176
177
178
# File 'lib/extensions/rhospec/rhospec.rb', line 176

def after(at=:each, &block)
    MSpec.current.after[ at ] = block
end

#argf(argv) ⇒ Object

Convenience helper for specs using ARGF. Set @argf to an instance of ARGF.class with the given argv. That instance must be used instead of ARGF as ARGF is global and it is not always possible to reset its state correctly.

The helper yields to the block and then close the files open by the instance. Example:

describe "That" do
  it "does something" do
    argf ['a', 'b'] do
      # do something
    end
  end
end


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/extensions/mspec/mspec/helpers/argf.rb', line 17

def argf(argv)
  #RHO
  raise 'RHO: not supported'

  if argv.empty? or argv.length > 2
    raise "Only 1 or 2 filenames are allowed for the argf helper so files can be properly closed: #{argv.inspect}"
  end
  @argf ||= nil
  raise "Cannot nest calls to the argf helper" if @argf

  @argf = ARGF.class.new(*argv)
  @__mspec_saved_argf_file__ = @argf.file
  begin
    yield
  ensure
    file1 = @__mspec_saved_argf_file__
    file2 = @argf.file # Either the first file or the second
    file1.close if !file1.closed? and file1 != STDIN
    file2.close if !file2.closed? and file2 != STDIN
    @argf = nil
    @__mspec_saved_argf_file__ = nil
  end
end

#argv(args) ⇒ Object

Convenience helper for altering ARGV. Saves the value of ARGV and sets it to args. If a block is given, yields to the block and then restores the value of ARGV. The previously saved value of ARGV can be restored by passing :restore. The former is useful in a single spec. The latter is useful in before/after actions. For example:

describe "This" do
  before do
    argv ['a', 'b']
  end

  after do
    argv :restore
  end

  it "does something" do
    # do something
  end
end

describe "That" do
  it "does something" do
    argv ['a', 'b'] do
      # do something
    end
  end
end


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/extensions/mspec/mspec/helpers/argv.rb', line 31

def argv(args)
  if args == :restore
    ARGV.replace(@__mspec_saved_argv__ || [])
  else
    @__mspec_saved_argv__ = ARGV.dup
    ARGV.replace args
    if block_given?
      begin
        yield
      ensure
        argv :restore
      end
    end
  end
end

#as_superuser(&block) ⇒ Object



10
11
12
# File 'lib/extensions/mspec/mspec/guards/superuser.rb', line 10

def as_superuser(&block)
  SuperUserGuard.new.run_if(:as_superuser, &block)
end

#as_user(&block) ⇒ Object



14
15
16
# File 'lib/extensions/mspec/mspec/guards/superuser.rb', line 14

def as_user(&block)
  SuperUserGuard.new.run_unless(:as_user, &block)
end

#be_an_instance_of(expected) ⇒ Object



23
24
25
# File 'lib/extensions/mspec/mspec/matchers/be_an_instance_of.rb', line 23

def be_an_instance_of(expected)
  BeAnInstanceOfMatcher.new(expected)
end

#be_ancestor_of(expected) ⇒ Object



21
22
23
# File 'lib/extensions/mspec/mspec/matchers/be_ancestor_of.rb', line 21

def be_ancestor_of(expected)
  BeAncestorOfMatcher.new(expected)
end

#be_close(expected, tolerance) ⇒ Object



24
25
26
# File 'lib/extensions/mspec/mspec/matchers/be_close.rb', line 24

def be_close(expected, tolerance)
  BeCloseMatcher.new(expected, tolerance)
end

#be_computed_by(sym, *args) ⇒ Object



34
35
36
# File 'lib/extensions/mspec/mspec/matchers/be_computed_by.rb', line 34

def be_computed_by(sym, *args)
  BeComputedByMatcher.new(sym, *args)
end

#be_emptyObject



17
18
19
# File 'lib/extensions/mspec/mspec/matchers/be_empty.rb', line 17

def be_empty
  BeEmptyMatcher.new
end

#be_falseObject



480
481
482
# File 'lib/extensions/rhospec/rhospec.rb', line 480

def be_false
  BeFalseMatcher.new
end

#be_kind_of(expected) ⇒ Object



21
22
23
# File 'lib/extensions/mspec/mspec/matchers/be_kind_of.rb', line 21

def be_kind_of(expected)
  BeKindOfMatcher.new(expected)
end

#be_nanObject



17
18
19
# File 'lib/extensions/mspec/mspec/matchers/be_nan.rb', line 17

def be_nan
  BeNaNMatcher.new
end

#be_negative_infinityObject



25
26
27
# File 'lib/extensions/mspec/mspec/matchers/infinity.rb', line 25

def be_negative_infinity
  InfinityMatcher.new(-1)
end

#be_negative_zeroObject



25
26
27
# File 'lib/extensions/mspec/mspec/matchers/signed_zero.rb', line 25

def be_negative_zero
  SignedZeroMatcher.new(-1)
end

#be_nilObject



472
473
474
# File 'lib/extensions/rhospec/rhospec.rb', line 472

def be_nil
  BeNilMatcher.new
end

#be_positive_infinityObject



21
22
23
# File 'lib/extensions/mspec/mspec/matchers/infinity.rb', line 21

def be_positive_infinity
  InfinityMatcher.new(1)
end

#be_positive_zeroObject



21
22
23
# File 'lib/extensions/mspec/mspec/matchers/signed_zero.rb', line 21

def be_positive_zero
  SignedZeroMatcher.new(1)
end

#be_trueObject



476
477
478
# File 'lib/extensions/rhospec/rhospec.rb', line 476

def be_true
  BeTrueMatcher.new
end

#be_true_or_falseObject



17
18
19
# File 'lib/extensions/mspec/mspec/matchers/be_true_or_false.rb', line 17

def be_true_or_false
  BeTrueOrFalseMatcher.new
end

#before(at = :each, &block) ⇒ Object



172
173
174
# File 'lib/extensions/rhospec/rhospec.rb', line 172

def before(at=:each, &block)
    MSpec.current.before[ at ] = block
end

#big_endian(&block) ⇒ Object



20
21
22
# File 'lib/extensions/mspec/mspec/guards/endian.rb', line 20

def big_endian(&block)
  BigEndianGuard.new.run_if(:big_endian, &block)
end

#bignum_value(plus = 0) ⇒ Object



12
13
14
# File 'lib/extensions/mspec/mspec/helpers/numeric.rb', line 12

def bignum_value(plus=0)
  0x8000_0000_0000_0000 + plus
end

#block_caller(timeout = 0.1) ⇒ Object



32
33
34
# File 'lib/extensions/mspec/mspec/matchers/block_caller.rb', line 32

def block_caller(timeout = 0.1)
  BlockingMatcher.new
end

#call_block(&block) ⇒ Object



192
193
194
195
196
197
# File 'lib/extensions/rhospec/rhospec.rb', line 192

def call_block(&block)
    block.call
    return nil
    rescue Exception => e
    return e
end

#complain(complaint = nil) ⇒ Object



53
54
55
# File 'lib/extensions/mspec/mspec/matchers/complain.rb', line 53

def complain(complaint=nil)
  ComplainMatcher.new(complaint)
end

#conflicts_with(*modules, &block) ⇒ Object

In some cases, libraries will modify another Ruby method’s behavior. The specs for the method’s behavior will then fail if that library is loaded. This guard will not run if any of the specified constants exist in Object.constants.



16
17
18
# File 'lib/extensions/mspec/mspec/guards/conflict.rb', line 16

def conflicts_with(*modules, &block)
  ConflictsGuard.new(*modules).run_unless(:conflicts_with, &block)
end

#cp(source, dest) ⇒ Object

Copies a file



3
4
5
6
7
8
9
10
11
# File 'lib/extensions/mspec/mspec/helpers/fs.rb', line 3

def cp(source, dest)
  File.open(dest, "w") do |d|
    File.open(source, "r") do |s|
      while data = s.read(1024)
        d.write data
      end
    end
  end
end

#dcloneObject



8
9
10
# File 'lib/extensions/rexml/rexml/xpath_parser.rb', line 8

def dclone
  clone
end

#describe(mod, msg = nil, options = nil, &block) ⇒ Object Also known as: context



180
181
182
# File 'lib/extensions/rhospec/rhospec.rb', line 180

def describe(mod, msg=nil, options=nil, &block)
    MSpec.describe mod, msg, &block
end

#doc(*a) ⇒ Object

For ReadRuby compatiability



23
24
# File 'lib/extensions/mspec/mspec/runner/object.rb', line 23

def doc(*a)
end

#eql(expected) ⇒ Object



23
24
25
# File 'lib/extensions/mspec/mspec/matchers/eql.rb', line 23

def eql(expected)
  EqlMatcher.new(expected)
end

#equal(expected) ⇒ Object



484
485
486
# File 'lib/extensions/rhospec/rhospec.rb', line 484

def equal(expected)
  EqualMatcher.new(expected)
end

#equal_element(*args) ⇒ Object



75
76
77
# File 'lib/extensions/mspec/mspec/matchers/equal_element.rb', line 75

def equal_element(*args)
  EqualElementMatcher.new(*args)
end

#evaluate(str, desc = nil, &block) ⇒ Object



51
52
53
# File 'lib/extensions/mspec/mspec/runner/evaluate.rb', line 51

def evaluate(str, desc=nil, &block)
  SpecEvaluate.new(str, desc).define(&block)
end

#eventually(options = {}, &block) ⇒ Object

Raises:



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/extensions/rhospec/rhospec.rb', line 202

def eventually options = {}, &block
    raise NO_BLOCK_PASSED if block.nil?
    timeout = options[:timeout] || 5
    delay = options[:delay] || 0.25
    last_error = nil
    begin_time = Time.now
    
    while (Time.now - begin_time) < timeout
        e = call_block(&block)
        if e then
            last_error = e
            sleep delay
            else
            return
        end        
    end
    
    raise last_error
end

#fixnum_maxObject



23
24
25
# File 'lib/extensions/mspec/mspec/helpers/numeric.rb', line 23

def fixnum_max
  (2**30) - 1
end

#fixnum_minObject



27
28
29
# File 'lib/extensions/mspec/mspec/helpers/numeric.rb', line 27

def fixnum_min
  -(2**30)
end

#fixture(file, *args) ⇒ Object

Returns the name of a fixture file by adjoining the directory of the file argument with “fixtures” and the contents of the args array. For example,

+file+ == "some/example_spec.rb"

and

+args+ == ["subdir", "file.txt"]

then the result is the expanded path of

"some/fixtures/subdir/file.txt".


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/extensions/mspec/mspec/helpers/fixture.rb', line 15

def fixture(file, *args)
  path = File.dirname(file)
  path = path[0..-7] if path[-7..-1] == "/shared"
  fixtures = path[-9..-1] == "/fixtures" ? "" : "fixtures"
  if File.respond_to?(:realpath)
    path = File.realpath(path)
  else
    path = File.expand_path(path)
  end
  File.join(path, fixtures, args)
end

#flunk(msg = "This example is a failure") ⇒ Object



2
3
4
# File 'lib/extensions/mspec/mspec/helpers/flunk.rb', line 2

def flunk(msg="This example is a failure")
  SpecExpectation.fail_with "Failed:", msg
end

#fmode(mode) ⇒ Object

This helper simplifies passing file access modes regardless of whether the :encoding feature is enabled. Only the access specifier itself will be returned if :encoding is not enabled. Otherwise, the full mode string will be returned (i.e. the helper is a no-op).



92
93
94
95
96
97
98
# File 'lib/extensions/mspec/mspec/helpers/io.rb', line 92

def fmode(mode)
  if FeatureGuard.enabled? :encoding
    mode
  else
    mode.split(':').first
  end
end

#have_class_variable(variable) ⇒ Object



9
10
11
# File 'lib/extensions/mspec/mspec/matchers/have_class_variable.rb', line 9

def have_class_variable(variable)
  HaveClassVariableMatcher.new(variable)
end

#have_constant(variable) ⇒ Object



9
10
11
# File 'lib/extensions/mspec/mspec/matchers/have_constant.rb', line 9

def have_constant(variable)
  HaveConstantMatcher.new(variable)
end

#have_instance_method(method, include_super = true) ⇒ Object



21
22
23
# File 'lib/extensions/mspec/mspec/matchers/have_instance_method.rb', line 21

def have_instance_method(method, include_super=true)
  HaveInstanceMethodMatcher.new method, include_super
end

#have_instance_variable(variable) ⇒ Object



9
10
11
# File 'lib/extensions/mspec/mspec/matchers/have_instance_variable.rb', line 9

def have_instance_variable(variable)
  HaveInstanceVariableMatcher.new(variable)
end

#have_method(method, include_super = true) ⇒ Object



21
22
23
# File 'lib/extensions/mspec/mspec/matchers/have_method.rb', line 21

def have_method(method, include_super=true)
  HaveMethodMatcher.new method, include_super
end

#have_private_instance_method(method, include_super = true) ⇒ Object



21
22
23
# File 'lib/extensions/mspec/mspec/matchers/have_private_instance_method.rb', line 21

def have_private_instance_method(method, include_super=true)
  HavePrivateInstanceMethodMatcher.new method, include_super
end

#have_private_method(method, include_super = true) ⇒ Object



21
22
23
# File 'lib/extensions/mspec/mspec/matchers/have_private_method.rb', line 21

def have_private_method(method, include_super=true)
  HavePrivateMethodMatcher.new method, include_super
end

#have_protected_instance_method(method, include_super = true) ⇒ Object



21
22
23
# File 'lib/extensions/mspec/mspec/matchers/have_protected_instance_method.rb', line 21

def have_protected_instance_method(method, include_super=true)
  HaveProtectedInstanceMethodMatcher.new method, include_super
end

#have_public_instance_method(method, include_super = true) ⇒ Object



21
22
23
# File 'lib/extensions/mspec/mspec/matchers/have_public_instance_method.rb', line 21

def have_public_instance_method(method, include_super=true)
  HavePublicInstanceMethodMatcher.new method, include_super
end

#have_singleton_method(method, include_super = true) ⇒ Object



21
22
23
# File 'lib/extensions/mspec/mspec/matchers/have_singleton_method.rb', line 21

def have_singleton_method(method, include_super=true)
  HaveSingletonMethodMatcher.new method, include_super
end

#infinity_valueObject



8
9
10
# File 'lib/extensions/mspec/mspec/helpers/numeric.rb', line 8

def infinity_value
  1/0.0
end

#it(msg, &block) ⇒ Object Also known as: specify



184
185
186
# File 'lib/extensions/rhospec/rhospec.rb', line 184

def it(msg, &block)
    MSpec.current.tests[ msg ] = block
end

#it_behaves_like(desc, meth, obj = nil) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/extensions/mspec/mspec/runner/shared.rb', line 4

def it_behaves_like(desc, meth, obj=nil)
  send :before, :all do
    @method = meth
    @object = obj
  end

  send :it_should_behave_like, desc.to_s
end

#it_should_behave_like(desc) ⇒ Object



188
189
190
# File 'lib/extensions/rhospec/rhospec.rb', line 188

def it_should_behave_like(desc)
    raise "it_should_behave_like not implemented"
end

#little_endian(&block) ⇒ Object



24
25
26
# File 'lib/extensions/mspec/mspec/guards/endian.rb', line 24

def little_endian(&block)
  BigEndianGuard.new.run_unless(:little_endian, &block)
end

#match_yaml(expected) ⇒ Object



43
44
45
# File 'lib/extensions/mspec/mspec/matchers/match_yaml.rb', line 43

def match_yaml(expected)
  MatchYAMLMatcher.new(expected)
end

#mkdir_p(path) ⇒ Object

Creates each directory in path that does not exist.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/extensions/mspec/mspec/helpers/fs.rb', line 14

def mkdir_p(path)
  parts = File.expand_path(path).split %r[/|\\]
  name = parts.shift
  parts.each do |part|
    name = File.join name, part

    if File.file? name
      raise ArgumentError, "path component of #{path} is a file"
    end

    Dir.mkdir name unless File.directory? name
  end
end

#mock(name, options = {}) ⇒ Object



17
18
19
# File 'lib/extensions/mspec/mspec/mocks/object.rb', line 17

def mock(name, options={})
  MockObject.new name, options
end

#mock_int(val) ⇒ Object



21
22
23
# File 'lib/extensions/mspec/mspec/mocks/object.rb', line 21

def mock_int(val)
  MockIntObject.new(val)
end

#mock_numeric(name, options = {}) ⇒ Object



25
26
27
# File 'lib/extensions/mspec/mspec/mocks/object.rb', line 25

def mock_numeric(name, options={})
  NumericMockObject.new name, options
end

#mock_to_path(path) ⇒ Object



2
3
4
5
6
7
# File 'lib/extensions/mspec/mspec/helpers/mock_to_path.rb', line 2

def mock_to_path(path)
  # Cannot use our Object#mock here since it conflicts with RSpec
  obj = MockObject.new('path')
  obj.should_receive(:to_path).and_return(path)
  obj
end

#nan_valueObject



4
5
6
# File 'lib/extensions/mspec/mspec/helpers/numeric.rb', line 4

def nan_value
  0/0.0
end

#new_datetime(opts = {}) ⇒ Object

The new_datetime helper makes writing DateTime specs more simple by providing default constructor values and accepting a Hash of only the constructor values needed for the particular spec. For example:

new_datetime :hour => 1, :minute => 20

Possible keys are:

:year, :month, :day, :hour, :minute, :second, :offset and :sg.


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/extensions/mspec/mspec/helpers/datetime.rb', line 11

def new_datetime(opts={})
  require 'date'

  value = {
    :year   => -4712,
    :month  => 1,
    :day    => 1,
    :hour   => 0,
    :minute => 0,
    :second => 0,
    :offset => 0,
    :sg     => Date::ITALY
  }.merge opts

  DateTime.new value[:year], value[:month], value[:day], value[:hour],
    value[:minute], value[:second], value[:offset], value[:sg]
end

#new_fd(name, mode = "w:utf-8") ⇒ Object

Creates a “bare” file descriptor (i.e. one that is not associated with any Ruby object). The file descriptor can safely be passed to IO.new without creating a Ruby object alias to the fd.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/extensions/mspec/mspec/helpers/io.rb', line 68

def new_fd(name, mode="w:utf-8")
  mode = options_or_mode(mode)

  if mode.kind_of? Hash
    if mode.key? :mode
      mode = mode[:mode]
    else
      raise ArgumentError, "new_fd options Hash must include :mode"
    end
  end

  IO.sysopen name, fmode(mode)
end

#new_io(name, mode = "w:utf-8") ⇒ Object

Creates an IO instance for a temporary file name. The file must be deleted.



84
85
86
# File 'lib/extensions/mspec/mspec/helpers/io.rb', line 84

def new_io(name, mode="w:utf-8")
  IO.new new_fd(name, options_or_mode(mode)), options_or_mode(mode)
end

#not_supported_on(*args, &block) ⇒ Object



13
14
15
# File 'lib/extensions/mspec/mspec/guards/support.rb', line 13

def not_supported_on(*args, &block)
  SupportedGuard.new(*args).run_unless(:not_supported_on, &block)
end

#options_or_mode(oom) ⇒ Object

This helper simplifies passing file access modes or options regardless of whether the :encoding feature is enabled. Only the access specifier itself will be returned if :encoding is not enabled. Otherwise, the full mode string or option will be returned (i.e. the helper is a no-op).



104
105
106
107
108
109
110
111
112
# File 'lib/extensions/mspec/mspec/helpers/io.rb', line 104

def options_or_mode(oom)
  return fmode(oom) if oom.kind_of? String

  if FeatureGuard.enabled? :encoding
    oom
  else
    fmode(oom[:mode] || "r:utf-8")
  end
end

#output(stdout = nil, stderr = nil) ⇒ Object



64
65
66
# File 'lib/extensions/mspec/mspec/matchers/output.rb', line 64

def output(stdout=nil, stderr=nil)
  OutputMatcher.new(stdout, stderr)
end

#output_to_fd(what, where = STDOUT) ⇒ Object



68
69
70
# File 'lib/extensions/mspec/mspec/matchers/output_to_fd.rb', line 68

def output_to_fd(what, where = STDOUT)
  OutputToFDMatcher.new what, where
end

#platform_is(*args, &block) ⇒ Object



71
72
73
# File 'lib/extensions/mspec/mspec/guards/platform.rb', line 71

def platform_is(*args, &block)
  PlatformGuard.new(*args).run_if(:platform_is, &block)
end

#platform_is_not(*args, &block) ⇒ Object



75
76
77
# File 'lib/extensions/mspec/mspec/guards/platform.rb', line 75

def platform_is_not(*args, &block)
  PlatformGuard.new(*args).run_unless(:platform_is_not, &block)
end

#quarantine!(&block) ⇒ Object



10
11
12
# File 'lib/extensions/mspec/mspec/guards/quarantine.rb', line 10

def quarantine!(&block)
  QuarantineGuard.new.run_unless(:quarantine!, &block)
end

#raise_error(exception = Exception, message = nil, &block) ⇒ Object



488
489
490
# File 'lib/extensions/rhospec/rhospec.rb', line 488

def raise_error(exception=Exception, message=nil, &block)
  RaiseErrorMatcher.new(exception, message, &block)
end

#resolve_ruby_exeObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/extensions/mspec/mspec/helpers/ruby_exe.rb', line 109

def resolve_ruby_exe
  [:env, :engine, :name, :install_name].each do |option|
    next unless exe = ruby_exe_options(option)

    if File.file?(exe) and File.executable?(exe)
      exe = File.expand_path(exe)
      exe = exe.tr('/', '\\') if PlatformGuard.windows?
      flags = ENV['RUBY_FLAGS']
      if flags and !flags.empty?
        return exe + ' ' + flags
      else
        return exe
      end
    end
  end
  #RHO
  #raise Exception, "Unable to find a suitable ruby executable."
  nil
end

#respond_to(expected) ⇒ Object



21
22
23
# File 'lib/extensions/mspec/mspec/matchers/respond_to.rb', line 21

def respond_to(expected)
  RespondToMatcher.new(expected)
end

#rm_r(*paths) ⇒ Object

Recursively removes all files and directories in path if path is a directory. Removes the file if path is a file.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/extensions/mspec/mspec/helpers/fs.rb', line 31

def rm_r(*paths)
  paths.each do |path|
    path = File.expand_path path

    prefix = SPEC_TEMP_DIR
    unless path[0, prefix.size] == prefix
      raise ArgumentError, "#{path} is not prefixed by #{prefix}"
    end

    # File.symlink? needs to be checked first as
    # File.exist? returns false for dangling symlinks
    if File.symlink? path
      File.unlink path
    elsif File.directory? path
      Dir.entries(path).each { |x| rm_r "#{path}/#{x}" unless x =~ /^\.\.?$/ }
      Dir.rmdir path
    elsif File.exist? path
      File.delete path
    end
  end
end

#ruby_bug(bug, version, &block) ⇒ Object



27
28
29
# File 'lib/extensions/mspec/mspec/guards/bug.rb', line 27

def ruby_bug(bug, version, &block)
  BugGuard.new(bug, version).run_unless(:ruby_bug, &block)
end

#ruby_cmd(code, opts = {}) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/extensions/mspec/mspec/helpers/ruby_exe.rb', line 167

def ruby_cmd(code, opts = {})
  body = code

  if opts[:escape]
    raise "escape: true is no longer supported in ruby_cmd, use ruby_exe or a fixture"
  end

  if code and !File.exist?(code)
    body = "-e #{code.inspect}"
  end

  [RUBY_EXE, opts[:options], body, opts[:args]].compact.join(' ')
end

#ruby_exe(code, opts = {}) ⇒ Object



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
# File 'lib/extensions/mspec/mspec/helpers/ruby_exe.rb', line 129

def ruby_exe(code, opts = {})

  #RHO not supported
  raise 'RHO: not supported'

  if opts[:dir]
    raise "ruby_exe(..., dir: dir) is no longer supported, use Dir.chdir"
  end

  env = opts[:env] || {}
  saved_env = {}
  env.each do |key, value|
    key = key.to_s
    saved_env[key] = ENV[key] if ENV.key? key
    ENV[key] = value
  end

  escape = opts.delete(:escape)
  if code and !File.exist?(code) and escape != false
    tmpfile = tmp("rubyexe.rb")
    File.open(tmpfile, "w") { |f| f.write(code) }
    code = tmpfile
  end

  begin
    platform_is_not :opal do
      `#{ruby_cmd(code, opts)}`
    end
  ensure
    saved_env.each { |key, value| ENV[key] = value }
    env.keys.each do |key|
      key = key.to_s
      ENV.delete key unless saved_env.key? key
    end
    File.delete tmpfile if tmpfile
  end
end

#ruby_exe_options(option) ⇒ Object



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
# File 'lib/extensions/mspec/mspec/helpers/ruby_exe.rb', line 80

def ruby_exe_options(option)
  case option
  when :env
    ENV['RUBY_EXE']
  when :engine
    case RUBY_NAME
    when 'rbx'
      "bin/rbx"
    when 'jruby'
      "bin/jruby"
    when 'maglev'
      "maglev-ruby"
    when 'topaz'
      "topaz"
    when 'ironruby'
      "ir"
    end
  when :name
    require 'rbconfig'
    bin = RUBY_NAME + (RbConfig::CONFIG['EXEEXT'] || RbConfig::CONFIG['exeext'] || '')
    File.join(".", bin)
  when :install_name
    require 'rbconfig'
    bin = RbConfig::CONFIG["RUBY_INSTALL_NAME"] || RbConfig::CONFIG["ruby_install_name"]
    bin << (RbConfig::CONFIG['EXEEXT'] || RbConfig::CONFIG['exeext'] || '')
    File.join(RbConfig::CONFIG['bindir'], bin)
  end
end

#ruby_version_is(*args, &block) ⇒ Object



492
493
494
# File 'lib/extensions/rhospec/rhospec.rb', line 492

def ruby_version_is(*args)
  yield
end

#should(matcher = NO_MATCHER_GIVEN) ⇒ Object



448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/extensions/rhospec/rhospec.rb', line 448

def should(matcher=nil)

  if matcher
    unless matcher.matches?(self)
      Expectation.fail_with(*matcher.failure_message)
    end
  else
      PositiveOperatorMatcher.new(self)
      #raise "Expected '#{matcher.inspect.to_s}' equal to nil" if self != nil
  end
  
end

#should_not(matcher = NO_MATCHER_GIVEN) ⇒ Object



461
462
463
464
465
466
467
468
469
470
# File 'lib/extensions/rhospec/rhospec.rb', line 461

def should_not(matcher=nil)
  if matcher
    if matcher.matches?(self)
      Expectation.fail_with(*matcher.negative_failure_message)
    end
  else
      #raise "Expected '#{self.inspect.to_s}' not equal to nil" if self == nil
      NegativeOperatorMatcher.new(self)
  end
end

#should_not_receive(sym) ⇒ Object



12
13
14
15
# File 'lib/extensions/mspec/mspec/mocks/object.rb', line 12

def should_not_receive(sym)
  proxy = Mock.install_method self, sym
  proxy.exactly(0).times
end

#should_receive(sym) ⇒ Object



8
9
10
# File 'lib/extensions/mspec/mspec/mocks/object.rb', line 8

def should_receive(sym)
  Mock.install_method self, sym
end

#stub!(sym) ⇒ Object



4
5
6
# File 'lib/extensions/mspec/mspec/mocks/object.rb', line 4

def stub!(sym)
  Mock.install_method self, sym, :stub
end

#suppress_warningObject



2
3
4
5
6
7
8
# File 'lib/extensions/mspec/mspec/helpers/warning.rb', line 2

def suppress_warning
  verbose = $VERBOSE
  $VERBOSE = nil
  yield
ensure
  $VERBOSE = verbose
end

#tmp(name, uniquify = true) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/extensions/mspec/mspec/helpers/tmp.rb', line 34

def tmp(name, uniquify=true)
  Dir.mkdir SPEC_TEMP_DIR unless Dir.exist? SPEC_TEMP_DIR

  if uniquify and !name.empty?
    slash = name.rindex "/"
    index = slash ? slash + 1 : 0
    name.insert index, "#{SPEC_TEMP_UNIQUIFIER.succ!}-"
  end

  File.join SPEC_TEMP_DIR, name
end

#touch(name, mode = "w") ⇒ Object

Creates a file name. Creates the directory for name if it does not exist.



55
56
57
58
59
60
61
# File 'lib/extensions/mspec/mspec/helpers/fs.rb', line 55

def touch(name, mode="w")
  mkdir_p File.dirname(name)

  File.open(name, mode) do |f|
    yield f if block_given?
  end
end

#with_block_device(&block) ⇒ Object



15
16
17
# File 'lib/extensions/mspec/mspec/guards/block_device.rb', line 15

def with_block_device(&block)
  BlockDeviceGuard.new.run_if(:with_block_device, &block)
end

#with_feature(*features, &block) ⇒ Object

Provides better documentation in the specs by naming sets of features that work together as a whole. Examples include :encoding, :fiber, :continuation, :fork.

Usage example:

with_feature :encoding do
  # specs for a method that provides aspects
  # of the encoding feature
end

Multiple features must all be enabled for the guard to run:

with_feature :one, :two do
  # these specs will run if features :one AND
  # :two are enabled.
end

The implementation must explicitly enable a feature by adding code like the following to the .mspec configuration file:

MSpec.enable_feature :encoding


40
41
42
# File 'lib/extensions/mspec/mspec/guards/feature.rb', line 40

def with_feature(*features, &block)
  FeatureGuard.new(*features).run_if(:with_feature, &block)
end

#with_timezone(name, offset = nil, daylight_saving_zone = "") ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/extensions/mspec/mspec/helpers/datetime.rb', line 29

def with_timezone(name, offset = nil, daylight_saving_zone = "")
  zone = name.dup

  if offset
    # TZ convention is backwards
    offset = -offset

    zone += offset.to_s
    zone += ":00:00"
  end
  zone += daylight_saving_zone

  old = ENV["TZ"]
  ENV["TZ"] = zone

  begin
    yield
  ensure
    ENV["TZ"] = old
  end
end