Class: Quarry::Markup::Table

Inherits:
Step show all
Defined in:
lib/quarry/markup/table.rb

Overview

Table

Instance Attribute Summary collapse

Attributes inherited from Step

#after, #before, #code, #lineno, #spec

Instance Method Summary collapse

Methods inherited from Step

#tab, #to_s

Constructor Details

#initialize(spec, code, lineno, ioc = {}) ⇒ Table

Returns a new instance of Table.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/quarry/markup/table.rb', line 15

def initialize(spec, code, lineno, ioc={})
  super(spec, code, lineno, ioc)

  comment = ioc[:comment]

  if md = /\(\*?\)/.match(comment)
    @fname = md[1]
  else
    @fname = 'default.yaml'  # TODO: more intelligent default?
  end

  @file = File.join('tables', @fname)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/quarry/markup/table.rb', line 12

def data
  @data
end

#fileObject (readonly)

Returns the value of attribute file.



11
12
13
# File 'lib/quarry/markup/table.rb', line 11

def file
  @file
end

#varsObject (readonly)

Returns the value of attribute vars.



13
14
15
# File 'lib/quarry/markup/table.rb', line 13

def vars
  @vars
end

Instance Method Details

#load_tableObject



29
30
31
32
33
# File 'lib/quarry/markup/table.rb', line 29

def load_table
  table = YAML.load(File.new(file))  # yaml or csv ?
  vars, *data = *table
  return vars, data
end

#run(runner, spec, context, output) ⇒ Object

Run a specification tabular step.

TODO: Table reporting needs to be improved. TODO: Currently on simple object types can be used b/c of set’s use of #inspect.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/quarry/markup/table.rb', line 40

def run(runner, spec, context, output)
  vars, rows = *load_table

  #context.instance_eval(runner.before, spec.file) if runner.before
  context.instance_eval(before.code, spec.file) if before

  begin
    output.report_macro(self); puts #(little bit of a hack here)
    rows.each do |row|
      set  = vars.zip(row).map{ |a| "#{a[0]}=#{a[1].inspect}" }.join('; ')
      set  = (' ' * tab) + set
      code = "#{set}\n#{code()}"
      step = Step.new(spec, set, lineno) # fuax step
      begin
        context.instance_eval(code, spec.file, lineno)
        #output.report_literal(set)
        output.report_pass(step) #(self)
      rescue Assertion => error
        output.report_fail(step, error) #(self, error)
      rescue Exception => error
        output.report_error(step, error) #(self, error)
      end
    end
  ensure
    #context.instance_eval(runner.after, spec.file) if runner.after
    context.instance_eval(after.code, spec.file) if after
  end
end

#to_scriptObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/quarry/markup/table.rb', line 70

def to_script
  script = []
  vars, rows = *load_table
  script << before.code if before
  rows.each do |row|
    set  = vars.zip(row).map{ |a| "#{a[0]}=#{a[1].inspect}" }.join('; ')
    set  = (' ' * tab) + set
    code = "#{set}\n#{code()}"
    script << code
  end
  script << after.code if after
  script.join("\n")
end