Module: MSpec

Defined in:
lib/extensions/mspec/mspec/matchers/include.rb,
lib/extensions/mspec/mspec/version.rb,
lib/extensions/mspec/mspec/runner/mspec.rb

Overview

Cannot override #include at the toplevel in MRI

Class Method Summary collapse

Class Method Details

.actions(action, *args) ⇒ Object



116
117
118
119
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 116

def self.actions(action, *args)
  actions = retrieve(action)
  actions.each { |obj| obj.send action, *args } if actions
end

.backtrace=(backtrace) ⇒ Object



57
58
59
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 57

def self.backtrace=(backtrace)
  @backtrace = backtrace
end

.clear_currentObject

Sets the toplevel ContextState to nil.



159
160
161
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 159

def self.clear_current
  store :current, nil
end

.clear_expectationsObject

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



314
315
316
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 314

def self.clear_expectations
  store :expectations, false
end

.clear_modesObject

Clears all registered modes.



218
219
220
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 218

def self.clear_modes
  store :modes, []
end

.delete_tag(tag) ⇒ Object

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.



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 384

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_tagsObject

Removes the tag file associated with a spec file.



405
406
407
408
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 405

def self.delete_tags
  file = tags_file
  File.delete file if File.exists? file
end

.disable_feature(feature) ⇒ Object



231
232
233
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 231

def self.disable_feature(feature)
  retrieve(:features)[feature] = false
end

.enable_feature(feature) ⇒ Object



227
228
229
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 227

def self.enable_feature(feature)
  retrieve(:features)[feature] = true
end

.expectationObject

Records that an expectation has been encountered in an example.



304
305
306
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 304

def self.expectation
  store :expectations, true
end

.expectation?Boolean

Returns true if an expectation has been encountered

Returns:

  • (Boolean)


309
310
311
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 309

def self.expectation?
  retrieve :expectations
end

.feature_enabled?(feature) ⇒ Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 235

def self.feature_enabled?(feature)
  retrieve(:features)[feature] || false
end

.file_countObject



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

def self.file_count
  @file_count
end

.filesObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 77

def self.files
  return unless files = retrieve(:files)

  shuffle files if randomize?
  files.each do |file|
#RHO
 puts "MSpec processing file: #{file}"
		
 if file.is_a?(Array)
  settings=file[1]
  settings.each do |setting|
	  run_spec(file[0],setting)
  end
 else
  run_spec(file,nil)
 end
#RHO
#      @env = Object.new
#      @env.extend MSpec

#      store :file, file
#      actions :load
#      protect("loading #{file}") { Kernel.load file }
#      actions :unload
  end
end

.guardObject

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



141
142
143
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 141

def self.guard
  @guarded << true
end

.guarded?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 149

def self.guarded?
  not @guarded.empty?
end

.include(*expected) ⇒ Object



28
29
30
# File 'lib/extensions/mspec/mspec/matchers/include.rb', line 28

def include(*expected)
  IncludeMatcher.new(*expected)
end

.mode?(mode) ⇒ Boolean

Returns true if mode is registered.

Returns:

  • (Boolean)


223
224
225
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 223

def self.mode?(mode)
  retrieve(:modes).include? mode
end

.protect(location, &block) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 121

def self.protect(location, &block)
  begin
    @env.instance_eval(&block)
    return true
  rescue SystemExit
    raise
  rescue Exception => exc
    #RHO
    puts "FAIL: #{current} - #{exc.message}\n" + (@backtrace ? exc.backtrace.join("\n") : "")
    @exc_count+=1
    #RHO
    
    register_exit 1
    actions :exception, ExceptionState.new(current && current.state, location, exc)
    return false
  end
end

.randomize(flag = true) ⇒ Object



285
286
287
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 285

def self.randomize(flag=true)
  @randomize = flag
end

.randomize?Boolean

Returns:

  • (Boolean)


289
290
291
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 289

def self.randomize?
  @randomize == true
end

.read_tags(keys) ⇒ Object

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



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 338

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) ⇒ Object

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


272
273
274
275
276
277
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 272

def self.register(symbol, action)
  unless value = retrieve(symbol)
    value = store symbol, []
  end
  value << action unless value.include? action
end

.register_current(state) ⇒ Object

Sets the toplevel ContextState to state.



154
155
156
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 154

def self.register_current(state)
  store :current, state
end

.register_exit(code) ⇒ Object

Stores the exit code used by the runner scripts.



179
180
181
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 179

def self.register_exit(code)
  store :exit, code
end

.register_mode(mode) ⇒ Object

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


212
213
214
215
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 212

def self.register_mode(mode)
  modes = retrieve :modes
  modes << mode unless modes.include? mode
end

.register_shared(state) ⇒ Object

Stores the shared ContextState keyed by description.



169
170
171
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 169

def self.register_shared(state)
  @shared[state.to_s] = state
end

.register_tags_patterns(patterns) ⇒ Object

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.



201
202
203
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 201

def self.register_tags_patterns(patterns)
  store :tags_patterns, patterns
end

.retrieve(symbol) ⇒ Object



239
240
241
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 239

def self.retrieve(symbol)
  instance_variable_get :"@#{symbol}"
end

.retrieve_shared(desc) ⇒ Object

Returns the shared ContextState matching description.



174
175
176
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 174

def self.retrieve_shared(desc)
  @shared[desc.to_s]
end

.run_spec(file, settings) ⇒ Object



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

def self.run_spec(file,settings)
	@env = Object.new
	@env.extend MSpec

	$spec_settings = settings

	store :file, file
	actions :load
	protect("loading #{file}") { Kernel.load file }
	actions :unload
end

.shuffle(ary) ⇒ Object



293
294
295
296
297
298
299
300
301
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 293

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) ⇒ Object



243
244
245
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 243

def self.store(symbol, value)
  instance_variable_set :"@#{symbol}", value
end

.tags_fileObject

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.



328
329
330
331
332
333
334
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 328

def self.tags_file
  patterns = retrieve(:tags_patterns) ||
             [[%r(spec/), 'spec/tags/'], [/_spec.rb$/, '_tags.txt']]
  patterns.inject(retrieve(:file).dup) do |file, pattern|
    file.gsub(*pattern)
  end
end

.unguardObject



145
146
147
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 145

def self.unguard
  @guarded.pop
end

.unregister(symbol, action) ⇒ Object



279
280
281
282
283
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 279

def self.unregister(symbol, action)
  if value = retrieve(symbol)
    value.delete action
  end
end

.write_tag(tag) ⇒ Object

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



367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 367

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) ⇒ Object

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



356
357
358
359
360
361
362
363
# File 'lib/extensions/mspec/mspec/runner/mspec.rb', line 356

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