Class: QED::Context

Inherits:
Module
  • Object
show all
Defined in:
lib/qed/script.rb,
lib/qed/script1.rb

Constant Summary collapse

TABLE =
/^TABLE\[(.*?)\]/i

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ Context

Returns a new instance of Context.



311
312
313
314
315
# File 'lib/qed/script.rb', line 311

def initialize(script)
  @_script = script
  @_when   = []
  @_tables = []
end

Instance Method Details

#_bindingObject



317
318
319
# File 'lib/qed/script.rb', line 317

def _binding
  @_binding ||= binding
end

#After(&procedure) ⇒ Object

After each step.



328
329
330
331
# File 'lib/qed/script.rb', line 328

def After(&procedure)
  @_after = procedure if procedure
  @_after
end

#Before(&procedure) ⇒ Object

Before each step.



322
323
324
325
# File 'lib/qed/script.rb', line 322

def Before(&procedure)
  @_before = procedure if procedure
  @_before
end

#Data(file, &content) ⇒ Object

Read/Write a fixture.



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/qed/script.rb', line 393

def Data(file, &content)
  raise if File.directory?(file)
  if content
    FileUtils.mkdir_p(File.dirname(fname))
    case File.extname(file)
    when '.yml', '.yaml'
      File.open(file, 'w'){ |f| f << content.call.to_yaml }
    else
      File.open(file, 'w'){ |f| f << content.call }
    end
  else
    #raise LoadError, "no such fixture file -- #{fname}" unless File.exist?(fname)
    case File.extname(file)
    when '.yml', '.yaml'
      YAML.load(File.new(file))
    else
      File.read(file)
    end
  end
end

#Table(file = nil, &blk) ⇒ Object

Table-based steps.



383
384
385
386
387
388
389
390
# File 'lib/qed/script.rb', line 383

def Table(file=nil, &blk)
  file = file || @_tables.last
  tbl = YAML.load(File.new(file))
  tbl.each do |set|
    blk.call(*set)
  end
  @_tables << file
end

#When(pattern = nil, &procedure) ⇒ Object

Comment match procedure.

This is useful for creating unobtrusive setup and (albeit more limited) teardown code. A pattern is matched against each comment as it is processed. If there is match, the code procedure is triggered, passing in any mathcing expression arguments.

Raises:

  • (ArgumentError)


355
356
357
358
359
360
361
362
# File 'lib/qed/script.rb', line 355

def When(pattern=nil, &procedure)
  return @_when unless procedure
  raise ArgumentError unless pattern
  unless Regexp === pattern
    pattern = __when_string_to_regexp(pattern)
  end
  @_when << [pattern, procedure]
end