Class: QED::Context

Inherits:
Module show all
Defined in:
lib/qed/script.rb

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ Context

Returns a new instance of Context.



125
126
127
# File 'lib/qed/script.rb', line 125

def initialize(script)
  @_script = script
end

Instance Method Details

#_bindingObject



129
130
131
# File 'lib/qed/script.rb', line 129

def _binding
  @_binding ||= binding
end

#after(&f) ⇒ Object

Set after step.



140
141
142
143
# File 'lib/qed/script.rb', line 140

def after(&f)
  @_after = f if f
  @_after
end

#before(&f) ⇒ Object

Set before step.



134
135
136
137
# File 'lib/qed/script.rb', line 134

def before(&f)
  @_before = f if f
  @_before
end

#fixture(fname, content = nil) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/qed/script.rb', line 165

def fixture(fname, content=nil)
  raise if File.directory?(fname)
  if content
    FileUtils.mkdir_p(File.dirname(fname))
    File.open(fname, 'w'){ |f| f << content }
  else
    raise LoadError, "no such fixture file -- #{fname}" unless File.exist?(fname)
    File.read(fname)
  end
end

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

Table-based steps.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/qed/script.rb', line 146

def table(file=nil, &blk)
  require 'yaml'

  file ||= File.basename(@_script.file).chomp(File.extname(@_script.file)) + '.yaml'

  Dir.ascend(Dir.pwd) do |path|
    f1 = File.join(path, file)
    f2 = File.join(path, 'fixtures', file)
    fr = File.file?(f1) ? f1 : File.exist?(f2) ? f2 : nil
    (file = fr; break) if fr
  end

  tbl = YAML.load(File.new(file))
  tbl.each do |set|
    @_script.run_step(set.to_yaml.tabto(2)){ blk.call(set) }
    #@_script.output.report_table(set)
  end
end