Class: QED::Script

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

Overview

Script

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, output = nil) ⇒ Script

New Script



30
31
32
33
34
35
36
37
38
39
# File 'lib/qed/script.rb', line 30

def initialize(file, output=nil)
  @file   = file
  @output = output || Reporter::Verbatim.new #(self)

  source = File.read(file)
  index  = source.rindex('---')

  @source = source[0...index]
  @helper = source[index+3...-1].strip
end

Instance Attribute Details

#fileObject (readonly)

def self.load(file, output=nil)

new(File.read(file), output)

end



26
27
28
# File 'lib/qed/script.rb', line 26

def file
  @file
end

#outputObject (readonly)

Returns the value of attribute output.



27
28
29
# File 'lib/qed/script.rb', line 27

def output
  @output
end

Instance Method Details

#contextObject

The run context.



122
123
124
# File 'lib/qed/script.rb', line 122

def context
  @context ||= Context.new(self)
end

#runObject

Run the script.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/qed/script.rb', line 46

def run
  #steps = @source.split(/\n\s*$/)
  eval(@helper, context._binding, @file) if @helper
  steps.each do |step|
    output.report_step(step)
    case step
    when /^[=#]/
      output.report_header(step)
    when /^\S/
      output.report_comment(step)
    else
      run_step(step)
    end
  end
end

#run_step(step, &blk) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/qed/script.rb', line 63

def run_step(step, &blk)
  context.before.call if context.before
  begin
    if blk
      blk.call #eval(step, context._binding)
    else
      eval(step, context._binding, @file) # TODO: would be nice to know file and lineno here
    end
    output.report_pass(step)
  rescue Assertion => error
    output.report_fail(step, error)
  rescue Exception => error
    output.report_error(step, error)
  ensure
    context.after.call if context.after
  end
end

#stepsObject

Cut-up script into steps.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/qed/script.rb', line 82

def steps
  @steps ||= (
    code  = false
    str   = ''
    steps = []
    @source.each_line do |line|
      if /^\s*$/.match line
        str << line
      elsif /^[=]/.match line
        steps << str.chomp("\n")
        steps << line.chomp("\n")
        str = ''
        #str << line
        code = false
      elsif /^\S/.match line
        if code
          steps << str.chomp("\n")
          str = ''
          str << line
          code = false
        else
          str << line
        end
      else
        if code
          str << line
        else
          steps << str
          str = ''
          str << line
          code = true
        end
      end
    end
    steps << str
    steps
  )
end