Class: Spectre::DefinitionContext
- Includes:
- Delegate
- Defined in:
- lib/spectre.rb
Instance Attribute Summary collapse
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#full_desc ⇒ Object
readonly
Returns the value of attribute full_desc.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#specs ⇒ Object
readonly
Returns the value of attribute specs.
Instance Method Summary collapse
-
#after(&block) ⇒ Object
Creates a new block which will be executed after each
itblock. -
#before(&block) ⇒ Object
Creates a new block which will be executed before each
itblock. -
#context(desc) ⇒ Object
Creates a new sub context with the given block.
-
#initialize(desc, file, engine, parent = nil) ⇒ DefinitionContext
constructor
A new instance of DefinitionContext.
-
#it(desc, tags: [], with: nil, &block) ⇒ Object
Creates a new specifiction (test).
-
#root ⇒ Object
The root context aka test subject.
-
#run(specs) ⇒ Object
:nodoc:.
-
#setup(&block) ⇒ Object
Adds a setup block which will be executed once at the beginning of a context.
-
#teardown(&block) ⇒ Object
Adds a teardown block which will be executed once at the end of a context.
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.
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 |
# File 'lib/spectre.rb', line 1232 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
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
1230 1231 1232 |
# File 'lib/spectre.rb', line 1230 def desc @desc end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
1230 1231 1232 |
# File 'lib/spectre.rb', line 1230 def file @file end |
#full_desc ⇒ Object (readonly)
Returns the value of attribute full_desc.
1230 1231 1232 |
# File 'lib/spectre.rb', line 1230 def full_desc @full_desc end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
1230 1231 1232 |
# File 'lib/spectre.rb', line 1230 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
1230 1231 1232 |
# File 'lib/spectre.rb', line 1230 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
1230 1231 1232 |
# File 'lib/spectre.rb', line 1230 def parent @parent end |
#specs ⇒ Object (readonly)
Returns the value of attribute specs.
1230 1231 1232 |
# File 'lib/spectre.rb', line 1230 def specs @specs 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.
1304 1305 1306 |
# File 'lib/spectre.rb', line 1304 def after &block @afters << block end |
#before(&block) ⇒ Object
Creates a new block which will be executed before each it block.
1295 1296 1297 |
# File 'lib/spectre.rb', line 1295 def before &block @befores << block end |
#context(desc) ⇒ Object
Creates a new sub context with the given block.
1263 1264 1265 1266 1267 1268 1269 1270 1271 |
# File 'lib/spectre.rb', line 1263 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.
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 |
# File 'lib/spectre.rb', line 1312 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, , data, file, block) @specs << spec end end |
#root ⇒ Object
The root context aka test subject.
1256 1257 1258 |
# File 'lib/spectre.rb', line 1256 def root @parent ? @parent.root : self end |
#run(specs) ⇒ Object
:nodoc:
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 |
# File 'lib/spectre.rb', line 1337 def run specs runs = [] selected = @specs.select { |x| specs.include? x } return runs if selected.empty? @engine.formatter.scope(@desc, self) do 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 @engine .contexts .select { |x| x.parent == self } .each do |context| @engine.logger.correlate do runs += context.run(specs) 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.
1278 1279 1280 |
# File 'lib/spectre.rb', line 1278 def setup &block @setups << block end |
#teardown(&block) ⇒ Object
Adds a teardown block which will be executed once at the end of a context. Multiple teardowns are allowed.
1287 1288 1289 |
# File 'lib/spectre.rb', line 1287 def teardown &block @teardowns << block end |