Class: Spectre::DefinitionContext

Inherits:
Object
  • Object
show all
Includes:
Delegate
Defined in:
lib/spectre.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Delegate

#instance_eval, #instance_exec, #method_missing, #respond_to_missing?

Constructor Details

#initialize(desc, file, engine, parent = nil) ⇒ DefinitionContext

Returns a new instance of DefinitionContext.



1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
# File 'lib/spectre.rb', line 1239

def initialize desc, file, engine, parent = nil
  @engine = engine
  @parent = parent
  @desc = desc
  @file = file
  @specs = []

  @setups = []
  @teardowns = []

  @befores = []
  @afters = []

  @name = @desc.downcase.gsub(/[^a-z0-9]+/, '_')
  @name = @parent.name + '-' + @name unless @parent.nil?

  @full_desc = @parent.nil? ? @desc : "#{@parent.full_desc} #{@desc}"

  @engine.contexts << self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Spectre::Delegate

Instance Attribute Details

#aftersObject (readonly)

Returns the value of attribute afters.



1236
1237
1238
# File 'lib/spectre.rb', line 1236

def afters
  @afters
end

#beforesObject (readonly)

Returns the value of attribute befores.



1236
1237
1238
# File 'lib/spectre.rb', line 1236

def befores
  @befores
end

#descObject (readonly)

Returns the value of attribute desc.



1236
1237
1238
# File 'lib/spectre.rb', line 1236

def desc
  @desc
end

#fileObject (readonly)

Returns the value of attribute file.



1236
1237
1238
# File 'lib/spectre.rb', line 1236

def file
  @file
end

#full_descObject (readonly)

Returns the value of attribute full_desc.



1236
1237
1238
# File 'lib/spectre.rb', line 1236

def full_desc
  @full_desc
end

#idObject (readonly)

Returns the value of attribute id.



1236
1237
1238
# File 'lib/spectre.rb', line 1236

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



1236
1237
1238
# File 'lib/spectre.rb', line 1236

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



1236
1237
1238
# File 'lib/spectre.rb', line 1236

def parent
  @parent
end

#setupsObject (readonly)

Returns the value of attribute setups.



1236
1237
1238
# File 'lib/spectre.rb', line 1236

def setups
  @setups
end

#specsObject (readonly)

Returns the value of attribute specs.



1236
1237
1238
# File 'lib/spectre.rb', line 1236

def specs
  @specs
end

#teardownsObject (readonly)

Returns the value of attribute teardowns.



1236
1237
1238
# File 'lib/spectre.rb', line 1236

def teardowns
  @teardowns
end

Instance Method Details

#after(&block) ⇒ Object

Creates a new block which will be executed after each it block. These blocks are ensured to be executed, even on failure or error.



1398
1399
1400
# File 'lib/spectre.rb', line 1398

def after &block
  @afters << block
end

#before(&block) ⇒ Object

Creates a new block which will be executed before each it block.



1389
1390
1391
# File 'lib/spectre.rb', line 1389

def before &block
  @befores << block
end

#childrenObject

Returns all direct child contexts



1270
1271
1272
# File 'lib/spectre.rb', line 1270

def children
  @engine.contexts.select { |x| x.parent == self }
end

#context(desc) ⇒ Object

Creates a new sub context with the given block.



1357
1358
1359
1360
1361
1362
1363
1364
1365
# File 'lib/spectre.rb', line 1357

def context(desc, &)
  file = caller
    .first
    .gsub(/:in .*/, '')
    .gsub(Dir.pwd, '.')

  context = DefinitionContext.new(desc, file, @engine, self)
  context.instance_eval(&)
end

#it(desc, tags: [], with: nil, &block) ⇒ Object

Creates a new specifiction (test). This is the main building block of a spectre tests and contains all test logic.



1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
# File 'lib/spectre.rb', line 1406

def it desc, tags: [], with: nil, &block
  file = caller
    .first
    .gsub(/:in .*/, '')
    .gsub(Dir.pwd, '.')

  with ||= [nil]

  initial_index = @engine
    .contexts
    .select { |x| x.root.name == root.name }
    .flat_map(&:specs)
    .count + 1

  with.each_with_index do |data, index|
    spec_index = initial_index + index
    name = "#{root.name}-#{spec_index}"

    spec = Specification.new(self, name, desc, tags, data, file, block)

    @specs << spec
  end
end

#rootObject

The root context aka test subject.



1263
1264
1265
# File 'lib/spectre.rb', line 1263

def root
  @parent ? @parent.root : self
end

#run(specs_set) ⇒ Object

Execute this context with its specs, setups, and teardowns. Recursively executes child contexts using the tree structure.



1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
# File 'lib/spectre.rb', line 1278

def run(specs_set)
  runs = []

  # Find specs in this context that should be executed
  selected = @specs.select { |x| specs_set.include? x }

  # Check if any child contexts have specs to execute
  has_child_specs = children.any? { |child| child.specs_in_tree?(specs_set) }

  # Skip this context if it has no matching specs and no children with specs
  return runs if selected.empty? && !has_child_specs

  @engine.formatter.scope(@desc, self) do
    # Execute setup, specs, and teardown for this context
    if selected.any?
      setup_bag = nil

      if @setups.any?
        setup_run = RunContext.new(@engine, self, :setup) do |run_context|
          @setups.each do |block|
            @engine.formatter.scope('setup', :setup) do
              @engine.logger.correlate do
                @engine.logger.debug("setup \"#{@desc}\"")
                run_context.execute(nil, &block)
              end
            end
          end
        end

        setup_bag = setup_run.bag
        runs << setup_run
      end

      # Only run specs if setup was successful
      if runs.all? { |x| x.status == :success }
        runs += selected.map do |spec|
          @engine.logger.correlate do
            spec.run(@engine, @befores, @afters, setup_bag)
          end
        end
      end

      if @teardowns.any?
        runs << RunContext.new(@engine, self, :teardown, setup_bag) do |run_context|
          @teardowns.each do |block|
            @engine.formatter.scope('teardown', :teardown) do
              @engine.logger.correlate do
                @engine.logger.debug("teardown \"#{@desc}\"")
                run_context.execute(nil, &block)
              end
            end
          end
        end
      end
    end

    # Recursively execute child contexts
    children.each do |child_context|
      @engine.logger.correlate do
        runs += child_context.run(specs_set)
      end
    end
  end

  runs
end

#setup(&block) ⇒ Object

Adds a setup block which will be executed once at the beginning of a context. Multiple setups are allowed.



1372
1373
1374
# File 'lib/spectre.rb', line 1372

def setup &block
  @setups << block
end

#specs_in_tree?(specs_set) ⇒ Boolean

Check recursively if this context or any of its children have specs in the filter set

Returns:

  • (Boolean)


1348
1349
1350
1351
1352
# File 'lib/spectre.rb', line 1348

def specs_in_tree?(specs_set)
  return true if @specs.any? { |x| specs_set.include? x }

  children.any? { |child| child.specs_in_tree?(specs_set) }
end

#teardown(&block) ⇒ Object

Adds a teardown block which will be executed once at the end of a context. Multiple teardowns are allowed.



1381
1382
1383
# File 'lib/spectre.rb', line 1381

def teardown &block
  @teardowns << block
end