Class: Book::TestCase

Inherits:
ActiveSupport::TestCase
  • Object
show all
Defined in:
lib/gorp/test.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.input(filename) ⇒ Object

read and pre-process $input.html (only done once, and cached)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gorp/test.rb', line 50

def self.input filename
  # read $input output; remove front matter and footer
  input = open("#{filename}.html").read
  head, body, tail = input.split /<body>\s+|\s+<\/body>/m

  # split into sections
  @@sections = body.split(/<a class="toc" name="section-(.*?)">/)

  # convert to a Hash
  @@sections = Hash[*@@sections.unshift(:contents)]
  @@sections[:head] = head
  @@sections[:tail] = tail

  # reattach anchors
  @@sections.each do |key,value|
    next unless key =~ /^\d/
    @@sections[key] = "<a class=\"toc\" name=\"section-#{key}\">#{value}"
  end

  # report version
  body =~ /rails .*?-v<\/pre>\s+.*?>(.*)<\/pre>/
  @@version = $1
  @@version += ' (git)' if body =~ /ln -s.*vendor.rails/
  @@version += ' (edge)' if body =~ /rails:freeze:edge/
  STDERR.puts @@version
end

.output(filename) ⇒ Object



77
78
79
80
# File 'lib/gorp/test.rb', line 77

def self.output filename
  $output = filename
  at_exit { HTMLRunner.run(self) }
end

.section(number, title, &tests) ⇒ Object

micro DSL allowing the definition of optional tests



40
41
42
43
44
45
46
47
# File 'lib/gorp/test.rb', line 40

def self.section number, title, &tests
  number = (sprintf "%f", number).sub(/0+$/,'') if number.kind_of? Float
  return if ARGV.include? 'partial' and !@@sections.has_key? number.to_s
  test "#{number} #{title}" do
    instance_eval {select number}
    instance_eval &tests
  end
end

.sectionsObject



103
104
105
# File 'lib/gorp/test.rb', line 103

def self.sections
  @@sections
end

Instance Method Details

#collect_stdoutObject



91
92
93
94
95
# File 'lib/gorp/test.rb', line 91

def collect_stdout
  css_select('.stdout').map do |tag|
    tag.children.join.gsub('&lt;','<').gsub('&gt;','>')
  end
end

#select(number) ⇒ Object

select an individual section from the HTML



83
84
85
86
87
88
89
# File 'lib/gorp/test.rb', line 83

def select number
  raise "Section #{number} not found" unless @@sections.has_key? number.to_s
  @selected = HTML::Document.new(@@sections[number.to_s]).root.children
  assert @@sections[number.to_s] !~
    /<pre class="traceback">\s+#&lt;IndexError: regexp not matched&gt;/,
    "edit failed"
end

#sort_hash(line) ⇒ Object



97
98
99
100
101
# File 'lib/gorp/test.rb', line 97

def sort_hash line
  line.sub(/^(=> )?\{.*\}$/) do |match|
    "#{$1}{#{match.scan(/:?"?\w+"?=>[^\[].*?(?=, |\})/).sort.join(', ')}}"
  end
end