Class: Object

Inherits:
BasicObject
Defined in:
lib/mspec/helpers/ruby_exe.rb,
lib/mspec/guards/bug.rb,
lib/mspec/guards/tty.rb,
lib/mspec/helpers/fs.rb,
lib/mspec/mocks/mock.rb,
lib/mspec/guards/user.rb,
lib/mspec/helpers/nan.rb,
lib/mspec/helpers/tmp.rb,
lib/mspec/helpers/argv.rb,
lib/mspec/helpers/hash.rb,
lib/mspec/matchers/eql.rb,
lib/mspec/mocks/object.rb,
lib/mspec/guards/endian.rb,
lib/mspec/guards/runner.rb,
lib/mspec/helpers/flunk.rb,
lib/mspec/runner/object.rb,
lib/mspec/runner/shared.rb,
lib/mspec/guards/support.rb,
lib/mspec/guards/version.rb,
lib/mspec/helpers/bignum.rb,
lib/mspec/matchers/equal.rb,
lib/mspec/guards/conflict.rb,
lib/mspec/guards/platform.rb,
lib/mspec/helpers/fixture.rb,
lib/mspec/matchers/be_nil.rb,
lib/mspec/matchers/output.rb,
lib/mspec/guards/superuser.rb,
lib/mspec/helpers/ducktype.rb,
lib/mspec/helpers/infinity.rb,
lib/mspec/matchers/be_true.rb,
lib/mspec/guards/background.rb,
lib/mspec/guards/compliance.rb,
lib/mspec/guards/extensions.rb,
lib/mspec/guards/quarantine.rb,
lib/mspec/helpers/metaclass.rb,
lib/mspec/matchers/be_close.rb,
lib/mspec/matchers/be_empty.rb,
lib/mspec/matchers/be_false.rb,
lib/mspec/matchers/complain.rb,
lib/mspec/expectations/should.rb,
lib/mspec/helpers/environment.rb,
lib/mspec/matchers/be_kind_of.rb,
lib/mspec/matchers/match_yaml.rb,
lib/mspec/matchers/respond_to.rb,
lib/mspec/guards/noncompliance.rb,
lib/mspec/helpers/mock_to_path.rb,
lib/mspec/matchers/equal_utf16.rb,
lib/mspec/matchers/have_method.rb,
lib/mspec/matchers/raise_error.rb,
lib/mspec/matchers/output_to_fd.rb,
lib/mspec/matchers/equal_element.rb,
lib/mspec/matchers/have_constant.rb,
lib/mspec/matchers/be_ancestor_of.rb,
lib/mspec/helpers/enumerator_class.rb,
lib/mspec/helpers/language_version.rb,
lib/mspec/matchers/be_an_instance_of.rb,
lib/mspec/matchers/have_class_variable.rb,
lib/mspec/matchers/have_instance_method.rb,
lib/mspec/matchers/have_instance_variable.rb,
lib/mspec/matchers/have_public_instance_method.rb,
lib/mspec/matchers/have_private_instance_method.rb,
lib/mspec/matchers/have_protected_instance_method.rb

Overview

The ruby_exe helper provides a wrapper for invoking the same Ruby interpreter 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 two keys: :options and :args. For example:

ruby_exe('file.rb', :options => "-w", :args => "> file.txt")

will be executed as

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

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

The RUBY_EXE constant can be set explicitly since the value is used each time ruby_exe is invoked. 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.

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.

Constant Summary collapse

NO_MATCHER_GIVEN =
Object.new

Instance Method Summary collapse

Instance Method Details

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



6
7
8
# File 'lib/mspec/runner/object.rb', line 6

def after(at=:each, &block)
  MSpec.current.after at, &block
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
# File 'lib/mspec/helpers/argv.rb', line 31

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

#as_superuserObject



10
11
12
13
14
15
16
# File 'lib/mspec/guards/superuser.rb', line 10

def as_superuser
  g = SuperUserGuard.new
  g.name = :as_superuser
  yield if g.yield?
ensure
  g.unregister
end

#as_userObject



10
11
12
13
14
15
16
# File 'lib/mspec/guards/user.rb', line 10

def as_user
  g = UserGuard.new
  g.name = :as_user
  yield if g.yield?
ensure
  g.unregister
end

#be_an_instance_of(expected) ⇒ Object



23
24
25
# File 'lib/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/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/mspec/matchers/be_close.rb', line 24

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

#be_emptyObject



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

def be_empty
  BeEmptyMatcher.new
end

#be_falseObject



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

def be_false
  BeFalseMatcher.new
end

#be_kind_of(expected) ⇒ Object



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

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

#be_nilObject



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

def be_nil
  BeNilMatcher.new
end

#be_trueObject



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

def be_true
  BeTrueMatcher.new
end

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



2
3
4
# File 'lib/mspec/runner/object.rb', line 2

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

#big_endianObject



26
27
28
29
30
31
32
# File 'lib/mspec/guards/endian.rb', line 26

def big_endian
  g = BigEndianGuard.new
  g.name = :big_endian
  yield if g.yield?
ensure
  g.unregister
end

#bignum_value(plus = 0) ⇒ Object



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

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

#complain(complaint = nil) ⇒ Object



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

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

#compliant_on(*args) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/mspec/guards/compliance.rb', line 22

def compliant_on(*args)
  g = CompliantOnGuard.new(*args)
  g.name = :compliant_on
  yield if g.yield?
ensure
  g.unregister
end

#conflicts_with(*modules) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/mspec/guards/conflict.rb', line 11

def conflicts_with(*modules)
  g = ConflictsGuard.new(*modules)
  g.name = :conflicts_with
  yield if g.yield? true
ensure
  g.unregister
end

#cp(source, dest) ⇒ Object

Copies a file



3
4
5
6
7
8
9
10
11
# File 'lib/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

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



10
11
12
# File 'lib/mspec/runner/object.rb', line 10

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

#deviates_on(*args) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/mspec/guards/noncompliance.rb', line 13

def deviates_on(*args)
  g = NonComplianceGuard.new(*args)
  g.name = :deviates_on
  yield if g.yield?
ensure
  g.unregister
end

#doc(*a) ⇒ Object

For ReadRuby compatiability



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

def doc(*a)
end

#does_not_respond_to(sym) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/mspec/helpers/ducktype.rb', line 10

def does_not_respond_to(sym)
  metaclass.class_eval <<-END
    def respond_to?(sym, include_private=false)
      sym.to_sym == #{sym.to_sym.inspect} ? false : super
    end
  END
end

#enumerator_classObject

Returns the Enumerator class (either Enumerator or Enumerable::Enumerator) depending of the version.



6
7
8
# File 'lib/mspec/helpers/enumerator_class.rb', line 6

def enumerator_class
  SpecVersion.new(RUBY_VERSION) < "1.9" ? Enumerable::Enumerator : Enumerator
end

#envObject



4
5
6
7
8
9
10
11
12
# File 'lib/mspec/helpers/environment.rb', line 4

def env
  env = ""
  if SpecGuard.windows?
    env = Hash[*`cmd.exe /C set`.split("\n").map { |e| e.split("=", 2) }.flatten]
  else
    env = Hash[*`env`.split("\n").map { |e| e.split("=", 2) }.flatten]
  end
  env
end

#eql(expected) ⇒ Object



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

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

#equal(expected) ⇒ Object



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

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

#equal_element(*args) ⇒ Object



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

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

#equal_utf16(expected) ⇒ Object



31
32
33
# File 'lib/mspec/matchers/equal_utf16.rb', line 31

def equal_utf16(expected)
  EqualUtf16Matcher.new(expected)
end

#extended_on(*args) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/mspec/guards/extensions.rb', line 13

def extended_on(*args)
  g = ExtensionsGuard.new(*args)
  g.name = :extended_on
  yield if g.yield?
ensure
  g.unregister
end

#fake!(sym, value = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/mspec/helpers/ducktype.rb', line 24

def fake!(sym, value=nil)
  responds_to sym

  metaclass.class_eval <<-END
    def method_missing(sym, *args)
      return #{value.inspect} if sym.to_sym == #{sym.to_sym.inspect}
    end
  END
end

#fixture(dir, *args) ⇒ Object

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

+dir+ == "some/path"

and

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

then the result is the expanded path of

"some/fixtures/dir/file.txt".


15
16
17
18
19
# File 'lib/mspec/helpers/fixture.rb', line 15

def fixture(dir, *args)
  path = File.dirname(dir)
  path = path[0..-7] if path[-7..-1] == "/shared"
  File.expand_path(File.join(path, "fixtures", args))
end

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



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

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

#hash_classObject



7
8
9
# File 'lib/mspec/helpers/hash.rb', line 7

def hash_class
  Hash
end

#have_class_variable(variable) ⇒ Object



9
10
11
# File 'lib/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/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/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/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/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/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_protected_instance_method(method, include_super = true) ⇒ Object



21
22
23
# File 'lib/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/mspec/matchers/have_public_instance_method.rb', line 21

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

#home_directoryObject



28
29
30
31
# File 'lib/mspec/helpers/environment.rb', line 28

def home_directory
  return ENV['HOME'] unless SpecGuard.windows?
  windows_env_echo('HOMEDRIVE') + windows_env_echo('HOMEPATH')
end

#infinity_valueObject



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

def infinity_value
  1/0.0
end

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



14
15
16
# File 'lib/mspec/runner/object.rb', line 14

def it(msg, &block)
  MSpec.current.it msg, &block
end

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



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

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

  send :it_should_behave_like, desc.to_s
end

#it_should_behave_like(desc) ⇒ Object



18
19
20
# File 'lib/mspec/runner/object.rb', line 18

def it_should_behave_like(desc)
  MSpec.current.it_should_behave_like desc
end

#language_version(dir, name) ⇒ Object

Helper for syntax-sensitive specs. The specs should be placed in a file in the versions subdirectory. For example, suppose language/method_spec.rb contains specs whose syntax depends on the Ruby version. In the language/method_spec.rb use the helper as follows:

language_version __FILE__, "method"

Then add a file “language/versions/method_1.8.rb” for the specs that are syntax-compatible with Ruby 1.8.x.

The most version-specific file will be loaded. If the version is 1.8.6, “method_1.8.6.rb” will be loaded if it exists, otherwise “method_1.8.rb” will be loaded if it exists.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mspec/helpers/language_version.rb', line 17

def language_version(dir, name)
  path = File.dirname(File.expand_path(dir))

  [SpecGuard.ruby_version(:tiny), SpecGuard.ruby_version].each do |version|
    file = File.join path, "versions", "#{name}_#{version}.rb"
    if File.exists? file
      require file
      break
    end
  end

  nil
end

#little_endianObject



34
35
36
37
38
39
40
# File 'lib/mspec/guards/endian.rb', line 34

def little_endian
  g = LittleEndianGuard.new
  g.name = :little_endian
  yield if g.yield?
ensure
  g.unregister
end

#match_yaml(expected) ⇒ Object



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

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

#metaclassObject



3
4
5
# File 'lib/mspec/helpers/metaclass.rb', line 3

def metaclass
  class << self; self; end
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/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/mspec/mocks/object.rb', line 17

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

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



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

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

#mock_to_path(path) ⇒ Object



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

def mock_to_path(path)
  obj = mock('path')
  obj.should_receive(:to_path).and_return(path)
  obj
end

#nan_valueObject



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

def nan_value
  0/0.0
end

#new_hash(*args, &block) ⇒ Object

Returns a new instance of hash_class.



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

def new_hash(*args, &block)
  if block
    hash_class.new(&block)
  elsif args.size == 1
    value = args.first
    if value.is_a?(Hash) or value.is_a?(hash_class)
      hash_class[value]
    else
      hash_class.new value
    end
  else
    hash_class[*args]
  end
end

#not_compliant_on(*args) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mspec/guards/compliance.rb', line 30

def not_compliant_on(*args)
  g = NotCompliantOnGuard.new(*args)
  g.name = :not_compliant_on
  yield if g.yield?
ensure
  g.unregister
end

#not_supported_on(*args) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/mspec/guards/support.rb', line 13

def not_supported_on(*args)
  g = SupportedGuard.new(*args)
  g.name = :not_supported_on
  yield if g.yield?
ensure
  g.unregister
end

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



64
65
66
# File 'lib/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/mspec/matchers/output_to_fd.rb', line 68

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

#platform_is(*args) ⇒ Object



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

def platform_is(*args)
  g = PlatformGuard.new(*args)
  g.name = :platform_is
  yield if g.yield?
ensure
  g.unregister
end

#platform_is_not(*args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/mspec/guards/platform.rb', line 36

def platform_is_not(*args)
  g = PlatformGuard.new(*args)
  g.name = :platform_is_not
  yield if g.yield? true
ensure
  g.unregister
end

#process_is_foregroundObject



14
15
16
17
18
19
20
# File 'lib/mspec/guards/background.rb', line 14

def process_is_foreground
  g = BackgroundGuard.new
  g.name = :process_is_foreground
  yield if g.yield? true
ensure
  g.unregister
end

#quarantine!Object



10
11
12
13
14
15
16
# File 'lib/mspec/guards/quarantine.rb', line 10

def quarantine!
  g = QuarantineGuard.new
  g.name = :quarantine!
  yield if g.yield?
ensure
  g.unregister
end

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



45
46
47
# File 'lib/mspec/matchers/raise_error.rb', line 45

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

#resolve_ruby_exeObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mspec/helpers/ruby_exe.rb', line 94

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

    # It has been reported that File.executable is not reliable
    # on Windows platforms (see commit 56bc555c). So, we check the
    # platform. 
    if File.exists?(exe) and (SpecGuard.windows? or File.executable?(exe))
      return cmd
    end
  end
  nil
end

#respond_to(expected) ⇒ Object



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

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

#responds_to(sym) ⇒ Object



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

def responds_to(sym)
  metaclass.class_eval <<-END
    def respond_to?(sym, include_private=false)
      sym.to_sym == #{sym.to_sym.inspect} ? true : super
    end
  END
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
# File 'lib/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

    if File.directory? path
      Dir.entries(path).each { |x| rm_r "#{path}/#{x}" unless x =~ /^\.\.?$/ }
      Dir.rmdir path
    elsif File.exists? path
      File.delete path
    end
  end
end

#ruby_bug(bug, version) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/mspec/guards/bug.rb', line 17

def ruby_bug(bug, version)
  g = BugGuard.new bug, version
  g.name = :ruby_bug
  yield if g.yield? true
ensure
  g.unregister
end

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



109
110
111
112
113
114
115
116
117
# File 'lib/mspec/helpers/ruby_exe.rb', line 109

def ruby_exe(code, opts = {})
  body = code
  working_dir = opts[:dir] || "."
  Dir.chdir(working_dir) do
    body = "-e #{code.inspect}" if code and not File.exists?(code)
    cmd = [RUBY_EXE, ENV['RUBY_FLAGS'], opts[:options], body, opts[:args]]
    `#{cmd.compact.join(' ')}`
  end
end

#ruby_exe_options(option) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mspec/helpers/ruby_exe.rb', line 69

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 'ironruby'
      "ir"
    end
  when :name
    bin = RUBY_NAME + (Config::CONFIG['EXEEXT'] || Config::CONFIG['exeext'] || '')
    File.join(".", bin)
  when :install_name
    bin = Config::CONFIG["RUBY_INSTALL_NAME"] || Config::CONFIG["ruby_install_name"]
    bin << (Config::CONFIG['EXEEXT'] || Config::CONFIG['exeext'] || '')
    File.join(Config::CONFIG['bindir'], bin)
  end
end

#ruby_version_is(*args) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/mspec/guards/version.rb', line 31

def ruby_version_is(*args)
  g = VersionGuard.new(*args)
  g.name = :ruby_version_is
  yield if g.yield?
ensure
  g.unregister
end

#runner_is(*args) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/mspec/guards/runner.rb', line 19

def runner_is(*args)
  g = RunnerGuard.new(*args)
  g.name = :runner_is
  yield if g.yield?
ensure
  g.unregister
end

#runner_is_not(*args) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/mspec/guards/runner.rb', line 27

def runner_is_not(*args)
  g = RunnerGuard.new(*args)
  g.name = :runner_is_not
  yield if g.yield? true
ensure
  g.unregister
end

#should(matcher = NO_MATCHER_GIVEN) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/mspec/expectations/should.rb', line 3

def should(matcher=NO_MATCHER_GIVEN)
  MSpec.expectation
  MSpec.actions :expectation, MSpec.current.state
  unless matcher.equal?(NO_MATCHER_GIVEN)
    unless matcher.matches?(self)
      SpecExpectation.fail_with(*matcher.failure_message)
    end
  else
    SpecPositiveOperatorMatcher.new(self)
  end
end

#should_not(matcher = NO_MATCHER_GIVEN) ⇒ Object



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

def should_not(matcher=NO_MATCHER_GIVEN)
  MSpec.expectation
  MSpec.actions :expectation, MSpec.current.state
  unless matcher.equal?(NO_MATCHER_GIVEN)
    if matcher.matches?(self)
      SpecExpectation.fail_with(*matcher.negative_failure_message)
    end
  else
    SpecNegativeOperatorMatcher.new(self)
  end
end

#should_not_receive(sym) ⇒ Object



12
13
14
15
# File 'lib/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/mspec/mocks/object.rb', line 8

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

#stub!(sym) ⇒ Object



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

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

#tmp(name) ⇒ Object



27
28
29
30
31
# File 'lib/mspec/helpers/tmp.rb', line 27

def tmp(name)
  Dir.mkdir SPEC_TEMP_DIR unless File.exists? SPEC_TEMP_DIR

  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.



51
52
53
54
55
56
57
# File 'lib/mspec/helpers/fs.rb', line 51

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

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

#undefine(sym) ⇒ Object



18
19
20
21
22
# File 'lib/mspec/helpers/ducktype.rb', line 18

def undefine(sym)
  metaclass.class_eval <<-END
    undef_method #{sym.to_sym.inspect}
  END
end

#usernameObject



18
19
20
21
22
23
24
25
26
# File 'lib/mspec/helpers/environment.rb', line 18

def username
  user = ""
  if SpecGuard.windows?
    user = windows_env_echo('USERNAME')
  else
    user = `whoami`.strip
  end
  user
end

#windows_env_echo(var) ⇒ Object



14
15
16
# File 'lib/mspec/helpers/environment.rb', line 14

def windows_env_echo(var)
  `cmd.exe /C ECHO %#{var}%`.strip
end

#with_ttyObject



13
14
15
16
17
18
19
# File 'lib/mspec/guards/tty.rb', line 13

def with_tty
  g = TTYGuard.new
  g.name = :with_tty
  yield if g.yield?
ensure
  g.unregister
end