Files

Class/Module Index [+]

Quicksearch

MSpec

Cannot override include at the toplevel in MRI

Constants

VERSION

Public Class Methods

actions(action, *args) click to toggle source
# File lib/mspec/runner/mspec.rb, line 61
def self.actions(action, *args)
  actions = retrieve(action)
  actions.each { |obj| obj.send action, *args } if actions
end
clear_current() click to toggle source

Sets the toplevel ContextState to nil.

# File lib/mspec/runner/mspec.rb, line 99
def self.clear_current
  store :current, nil
end
clear_expectations() click to toggle source

Resets the flag that an expectation has been encountered in an example.

# File lib/mspec/runner/mspec.rb, line 253
def self.clear_expectations
  store :expectations, false
end
clear_modes() click to toggle source

Clears all registered modes.

# File lib/mspec/runner/mspec.rb, line 157
def self.clear_modes
  store :modes, []
end
current() click to toggle source

Returns the toplevel ContextState.

# File lib/mspec/runner/mspec.rb, line 104
def self.current
  retrieve :current
end
delete_tag(tag) click to toggle source

Deletes tag from the tag file if it exists. Returns true if the tag is deleted, false otherwise. Deletes the tag file if it is empty.

# File lib/mspec/runner/mspec.rb, line 323
def self.delete_tag(tag)
  deleted = false
  pattern = /#{tag.tag}.*#{Regexp.escape(tag.escape(tag.description))}/
  file = tags_file
  if File.exist? file
    lines = IO.readlines(file)
    File.open(file, "wb") do |f|
      lines.each do |line|
        unless pattern =~ line.chomp
          f.puts line unless line.empty?
        else
          deleted = true
        end
      end
    end
    File.delete file unless File.size? file
  end
  return deleted
end
delete_tags() click to toggle source

Removes the tag file associated with a spec file.

# File lib/mspec/runner/mspec.rb, line 344
def self.delete_tags
  file = tags_file
  File.delete file if File.exists? file
end
describe(mod, options=nil, &block) click to toggle source
# File lib/mspec/runner/mspec.rb, line 30
def self.describe(mod, options=nil, &block)
  state = ContextState.new mod, options
  state.parent = current

  MSpec.register_current state
  state.describe(&block)

  state.process unless state.shared? or current
end
disable_feature(feature) click to toggle source
# File lib/mspec/runner/mspec.rb, line 170
def self.disable_feature(feature)
  retrieve(:features)[feature] = false
end
enable_feature(feature) click to toggle source
# File lib/mspec/runner/mspec.rb, line 166
def self.enable_feature(feature)
  retrieve(:features)[feature] = true
end
exit_code() click to toggle source

Retrieves the stored exit code.

# File lib/mspec/runner/mspec.rb, line 124
def self.exit_code
  retrieve(:exit).to_i
end
expectation() click to toggle source

Records that an expectation has been encountered in an example.

# File lib/mspec/runner/mspec.rb, line 243
def self.expectation
  store :expectations, true
end
expectation?() click to toggle source

Returns true if an expectation has been encountered

# File lib/mspec/runner/mspec.rb, line 248
def self.expectation?
  retrieve :expectations
end
feature_enabled?(feature) click to toggle source
# File lib/mspec/runner/mspec.rb, line 174
def self.feature_enabled?(feature)
  retrieve(:features)[feature] || false
end
files() click to toggle source
# File lib/mspec/runner/mspec.rb, line 46
def self.files
  return unless files = retrieve(:files)

  shuffle files if randomize?
  files.each do |file|
    @env = Object.new
    @env.extend MSpec

    store :file, file
    actions :load
    protect("loading #{file}") { Kernel.load file }
    actions :unload
  end
end
guard() click to toggle source

Guards can be nested, so a stack is necessary to know when we have exited the toplevel guard.

# File lib/mspec/runner/mspec.rb, line 81
def self.guard
  @guarded << true
end
guarded?() click to toggle source
# File lib/mspec/runner/mspec.rb, line 89
def self.guarded?
  not @guarded.empty?
end
include(*expected) click to toggle source
# File lib/mspec/matchers/include.rb, line 28
def include(*expected)
  IncludeMatcher.new(*expected)
end
mode?(mode) click to toggle source

Returns true if mode is registered.

# File lib/mspec/runner/mspec.rb, line 162
def self.mode?(mode)
  retrieve(:modes).include? mode
end
process() click to toggle source
# File lib/mspec/runner/mspec.rb, line 40
def self.process
  actions :start
  files
  actions :finish
end
protect(location, &block) click to toggle source
# File lib/mspec/runner/mspec.rb, line 66
def self.protect(location, &block)
  begin
    @env.instance_eval(&block)
    return true
  rescue SystemExit
    raise
  rescue Exception => exc
    register_exit 1
    actions :exception, ExceptionState.new(current && current.state, location, exc)
    return false
  end
end
randomize(flag=true) click to toggle source
# File lib/mspec/runner/mspec.rb, line 224
def self.randomize(flag=true)
  @randomize = flag
end
randomize?() click to toggle source
# File lib/mspec/runner/mspec.rb, line 228
def self.randomize?
  @randomize == true
end
read_tags(keys) click to toggle source

Returns a list of tags matching any tag string in keys based on the return value of keys.include?("tag_name")

# File lib/mspec/runner/mspec.rb, line 277
def self.read_tags(keys)
  tags = []
  file = tags_file
  if File.exist? file
    File.open(file, "rb") do |f|
      f.each_line do |line|
        line.chomp!
        next if line.empty?
        tag = SpecTag.new line.chomp
        tags << tag if keys.include? tag.tag
      end
    end
  end
  tags
end
register(symbol, action) click to toggle source

This method is used for registering actions that are run at particular points in the spec cycle:

:start        before any specs are run
:load         before a spec file is loaded
:enter        before a describe block is run
:before       before a single spec is run
:add          while a describe block is adding examples to run later
:expectation  before a 'should', 'should_receive', etc.
:example      after an example block is run, passed the block
:exception    after an exception is rescued
:after        after a single spec is run
:leave        after a describe block is run
:unload       after a spec file is run
:finish       after all specs are run

Objects registered as actions above should respond to a method of the same name. For example, if an object is registered as a :start action, it should respond to a start method call.

Additionally, there are two "action" lists for filtering specs:

:include  return true if the spec should be run
:exclude  return true if the spec should NOT be run
# File lib/mspec/runner/mspec.rb, line 211
def self.register(symbol, action)
  unless value = retrieve(symbol)
    value = store symbol, []
  end
  value << action unless value.include? action
end
register_current(state) click to toggle source

Sets the toplevel ContextState to state.

# File lib/mspec/runner/mspec.rb, line 94
def self.register_current(state)
  store :current, state
end
register_exit(code) click to toggle source

Stores the exit code used by the runner scripts.

# File lib/mspec/runner/mspec.rb, line 119
def self.register_exit(code)
  store :exit, code
end
register_files(files) click to toggle source

Stores the list of files to be evaluated.

# File lib/mspec/runner/mspec.rb, line 129
def self.register_files(files)
  store :files, files
end
register_mode(mode) click to toggle source

Registers an operating mode. Modes recognized by MSpec:

:pretend - actions execute but specs are not run
:verify - specs are run despite guards and the result is
          verified to match the expectation of the guard
:report - specs that are guarded are reported
:unguarded - all guards are forced off
# File lib/mspec/runner/mspec.rb, line 151
def self.register_mode(mode)
  modes = retrieve :modes
  modes << mode unless modes.include? mode
end
register_shared(state) click to toggle source

Stores the shared ContextState keyed by description.

# File lib/mspec/runner/mspec.rb, line 109
def self.register_shared(state)
  @shared[state.to_s] = state
end
register_tags_patterns(patterns) click to toggle source

Stores one or more substitution patterns for transforming a spec filename into a tags filename, where each pattern has the form:

[Regexp, String]

See also tags_file.

# File lib/mspec/runner/mspec.rb, line 140
def self.register_tags_patterns(patterns)
  store :tags_patterns, patterns
end
retrieve(symbol) click to toggle source
# File lib/mspec/runner/mspec.rb, line 178
def self.retrieve(symbol)
  instance_variable_get :"@#{symbol}"
end
retrieve_shared(desc) click to toggle source

Returns the shared ContextState matching description.

# File lib/mspec/runner/mspec.rb, line 114
def self.retrieve_shared(desc)
  @shared[desc.to_s]
end
shuffle(ary) click to toggle source
# File lib/mspec/runner/mspec.rb, line 232
def self.shuffle(ary)
  return if ary.empty?

  size = ary.size
  size.times do |i|
    r = rand(size - i - 1)
    ary[i], ary[r] = ary[r], ary[i]
  end
end
store(symbol, value) click to toggle source
# File lib/mspec/runner/mspec.rb, line 182
def self.store(symbol, value)
  instance_variable_set :"@#{symbol}", value
end
tags_file() click to toggle source

Transforms a spec filename into a tags filename by applying each substitution pattern in :tags_pattern. The default patterns are:

[%r(/spec/), '/spec/tags/'], [/_spec.rb$/, '_tags.txt']

which will perform the following transformation:

path/to/spec/class/method_spec.rb => path/to/spec/tags/class/method_tags.txt

See also register_tags_patterns.

# File lib/mspec/runner/mspec.rb, line 267
def self.tags_file
  patterns = retrieve(:tags_patterns) ||
             [[%(spec/), 'spec/tags/'], [/_spec.rb$/, '_tags.txt']]
  patterns.inject(retrieve(:file).dup) do |file, pattern|
    file.gsub(*pattern)
  end
end
unguard() click to toggle source
# File lib/mspec/runner/mspec.rb, line 85
def self.unguard
  @guarded.pop
end
unregister(symbol, action) click to toggle source
# File lib/mspec/runner/mspec.rb, line 218
def self.unregister(symbol, action)
  if value = retrieve(symbol)
    value.delete action
  end
end
write_tag(tag) click to toggle source

Writes tag to the tag file if it does not already exist. Returns true if the tag is written, false otherwise.

# File lib/mspec/runner/mspec.rb, line 306
def self.write_tag(tag)
  string = tag.to_s
  file = tags_file
  path = File.dirname file
  FileUtils.mkdir_p path unless File.exist? path
  if File.exist? file
    File.open(file, "rb") do |f|
      f.each_line { |line| return false if line.chomp == string }
    end
  end
  File.open(file, "ab") { |f| f.puts string }
  return true
end
write_tags(tags) click to toggle source

Writes each tag in tags to the tag file. Overwrites the tag file if it exists.

# File lib/mspec/runner/mspec.rb, line 295
def self.write_tags(tags)
  file = tags_file
  path = File.dirname file
  FileUtils.mkdir_p path unless File.exist? path
  File.open(file, "wb") do |f|
    tags.each { |t| f.puts t }
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.