Class: Gorp::TestCase

Inherits:
Test::Unit::TestCase
  • Object
show all
Includes:
Commands
Defined in:
lib/gorp/test.rb

Constant Summary collapse

@@base =
Object.new.extend(Gorp::Commands)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Commands

#cmd, #console, #db, #generate, #head, #irb, #issue, #log, #note, #omit, #overview, #popen3, #rails, #rake, #restart_server, #ruby, #runner, #secinclude, #secsplit, #section, stop_server

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



120
121
122
# File 'lib/gorp/test.rb', line 120

def raw
  @raw
end

Class Method Details

.herald(instance, name) ⇒ Object



44
45
# File 'lib/gorp/test.rb', line 44

def self.herald instance, name
end

.input(filename) ⇒ Object

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



77
78
79
80
81
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
# File 'lib/gorp/test.rb', line 77

def self.input filename
  # read $input output; remove front matter and footer
  input = open(File.join($WORK, "#{filename}.html")).read
  input.force_encoding('utf-8') if input.respond_to? :force_encoding
  head, body, tail = input.split /<body>\s+|\s+<\/body>/m

  # ruby 1.8.8 reverses the order
  body.gsub! /<a (id="[-.\w]+") (class="\w+")>/,'<a \2 \1>'

  # split into sections
  @@sections = body.split(/<a class="toc" id="section-(.*?)">/)
  @@sections[-1], env = @@sections.last.split(/<a class="toc" id="env">/)
  env, todos = env.split(/<a class="toc" id="todos">/)

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

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

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

.output(filename) ⇒ Object



108
109
110
111
# File 'lib/gorp/test.rb', line 108

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gorp/test.rb', line 48

def self.section number, title, &tests
  number = (sprintf "%f", number).sub(/0+$/,'') if number.kind_of? Float
  return if @@omit.include? number.to_s
  test "#{number} #{title}" do
    instance_eval {select number}
    begin
      instance_eval &tests
    ensure
      unless $!.instance_of? RuntimeError
        @raw =~ /<pre\sclass="stdin">edit\s([\w\/.]+)<\/pre>\s+
                 <pre\sclass="traceback">\s+
                 \#&lt;IndexError:\sregexp\snot\smatched&gt;\s+
                 (.*gorp\/lib\/gorp\/edit.rb.*\n\s+)*
                 ([\w\/.]+:\d+)/x
        fail "Edit #{$1} failed at #{$3}" if $1
      end
    end
  end
end

.sectionsObject



134
135
136
# File 'lib/gorp/test.rb', line 134

def self.sections
  @@sections
end

.suiteObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gorp/test.rb', line 9

def self.suite
  # Deferred loading of Rails infrastructure
  if File.exist? "#{$WORK}/.bundle/environment.rb"
    require "#{$WORK}/.bundle/environment.rb"
  end

  require 'active_support'
  require 'active_support/version'
  require 'active_support/test_case'

  # just enough infrastructure to get 'assert_select' to work
  require 'action_controller'
  begin
    # Rails (2.3.3 ish)
    require 'action_controller/assertions/selector_assertions'
    include ActionController::Assertions::SelectorAssertions
  rescue LoadError
    # Rails (3.0 ish)
    require 'action_dispatch/testing/assertions'
    require 'action_dispatch/testing/assertions/selector'
    include ActionDispatch::Assertions::SelectorAssertions
  end

  require 'action_controller/vendor/html-scanner/html/tokenizer'
  require 'action_controller/vendor/html-scanner/html/document'
  super
end

.test(name, &block) ⇒ Object



37
38
39
40
41
42
# File 'lib/gorp/test.rb', line 37

def self.test(name, &block)
  define_method("test_#{name.gsub(/\s+/,'_')}".to_sym) do
    self.class.herald self, name
    instance_eval &block
  end
end

Instance Method Details

#collect_stdoutObject



122
123
124
125
126
# File 'lib/gorp/test.rb', line 122

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



114
115
116
117
118
# File 'lib/gorp/test.rb', line 114

def select number
  raise "Section #{number} not found" unless @@sections.has_key? number.to_s
  @raw = @@sections[number.to_s]
  @selected = HTML::Document.new(@raw).root.children
end

#sort_hash(line) ⇒ Object



128
129
130
131
132
# File 'lib/gorp/test.rb', line 128

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

#ticket(number, info) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/gorp/test.rb', line 68

def ticket number, info
  return if info[:match] and not @raw =~ info[:match]
  return if block_given? and not yield(@raw)
  info[:list] ||= :rails

  fail "Ticket #{info[:list]}:#{number}: #{info[:title]}"
end