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

#bundle, #cmd, #console, #db, #flag, #generate, #irb, #issue, #log, #note, #omit, #overview, #popen3, #rails, #rails_epoc, #rake, #restart_server, #ruby, #runner, #secinclude, #secsplit, #section, #section_head, stop_server, #test, #unbundle

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



144
145
146
# File 'lib/gorp/test.rb', line 144

def raw
  @raw
end

Class Method Details

.herald(instance, name) ⇒ Object



59
60
# File 'lib/gorp/test.rb', line 59

def self.herald instance, name
end

.input(filename) ⇒ Object

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



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
120
121
122
# File 'lib/gorp/test.rb', line 93

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



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

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



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

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



158
159
160
# File 'lib/gorp/test.rb', line 158

def self.sections
  @@sections
end

.suiteObject



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gorp/test.rb', line 10

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
    # Rails (4.2 ish)
    require 'rails-dom-testing'
    include Rails::Dom::Testing::Assertions::SelectorAssertions

    # Rails 4.2
    ActiveSupport.test_order = :random rescue nil
  rescue LoadError
    # Rails (3.0 ish)
    require 'action_dispatch/testing/assertions'
    require 'action_dispatch/testing/assertions/selector'
    include ActionDispatch::Assertions::SelectorAssertions

    begin
      # Rails 2/3
      require 'action_controller/vendor/html-scanner/html/tokenizer'
      require 'action_controller/vendor/html-scanner/html/document'
    rescue LoadError
      # Rails 4
      require 'action_view/vendor/html-scanner/html/tokenizer'
      require 'action_view/vendor/html-scanner/html/document'
    end
  end

  super
end

.test(name, &block) ⇒ Object



52
53
54
55
56
57
# File 'lib/gorp/test.rb', line 52

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



146
147
148
149
150
# File 'lib/gorp/test.rb', line 146

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

#parse_for_select(raw) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/gorp/test.rb', line 129

def parse_for_select raw
  if defined? Rails::Dom::Testing::Assertions::SelectorAssertions
    Nokogiri::HTML::DocumentFragment.parse(raw)
  else
    HTML::Document.new(raw).root.children
  end
end

#select(number) ⇒ Object

select an individual section from the HTML



138
139
140
141
142
# File 'lib/gorp/test.rb', line 138

def select number
  raise "Section #{number} not found" unless @@sections.has_key? number.to_s
  @raw = @@sections[number.to_s]
  @selected = parse_for_select @raw
end

#sort_hash(line) ⇒ Object



152
153
154
155
156
# File 'lib/gorp/test.rb', line 152

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

#ticket(number, info) ⇒ Object



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

def ticket number, info
  return if Gorp::Config[:ignore_tickets]
  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