Class: Rtml::Test::Simulator

Inherits:
Object
  • Object
show all
Includes:
Widgets
Defined in:
lib/rtml/test/simulator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Widgets

add_proxies_to_base, affecting, create_widgets_array, included, load_widget!, load_widgets, mapping

Constructor Details

#initialize(start_at = nil) ⇒ Simulator

Accepts a path to the TML document which should be requested. To pass raw TML, use this syntax:

Rtml::Test::Simulator.new(:tml => my_tml_code)


16
17
18
19
20
21
22
23
24
25
# File 'lib/rtml/test/simulator.rb', line 16

def initialize(start_at = nil)
  @session = ActionController::Integration::Session.new
  @variables = Rtml::Test::VariableScope.new
  @headers = HashWithIndifferentAccess.new
  if start_at.kind_of?(Hash)
    load_tml! start_at[:tml]
  else
    visit(start_at) unless start_at.nil?
  end
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



11
12
13
# File 'lib/rtml/test/simulator.rb', line 11

def app
  @app
end

#sessionObject (readonly)

Returns the value of attribute session.



11
12
13
# File 'lib/rtml/test/simulator.rb', line 11

def session
  @session
end

Instance Method Details

#fill_in(variable_name, value) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rtml/test/simulator.rb', line 164

def fill_in(variable_name, value)
  variable_name = variable_name.to_s unless variable_name.kind_of?(String)
  on_current_screen("input").each do |field|
    if field['name'] == variable_name
      variables[variable_name] = value
      return
    end
  end
  raise Rtml::Errors::SimulationError,
        "Form element for variable #{variable_name.inspect} does not appear on screen #{current_screen_id.inspect}"
end


109
110
111
112
113
114
115
116
117
118
# File 'lib/rtml/test/simulator.rb', line 109

def follow_link(path_or_text)
  on_current_screen("a").each do |hyperlink|
    if hyperlink['href'] == path_or_text || hyperlink.inner_text =~ /#{Regexp::escape path_or_text}/i
      visit hyperlink['href']
      return
    end
  end
  raise Rtml::Errors::SimulationError,
        "Hyperlink not found on screen #{current_screen_id.inspect}: #{path_or_text.inspect}"
end

#header(name, value) ⇒ Object



32
33
34
# File 'lib/rtml/test/simulator.rb', line 32

def header(name, value)
  @headers[name] = value
end

#headersObject



36
37
38
# File 'lib/rtml/test/simulator.rb', line 36

def headers
  @headers.dup
end

#load_tml!(tml) ⇒ Object

Instead of using “visit”, this just force-feeds some TML into the simulator. Useful for hard-coded, quick-and-dirty debug situations, but far less useful for testing a real app or a large document.



43
44
45
46
47
# File 'lib/rtml/test/simulator.rb', line 43

def load_tml!(tml)
  @path = nil
  build_app(tml)
  process
end

#post_data(location, data = {}) ⇒ Object



27
28
29
30
# File 'lib/rtml/test/simulator.rb', line 27

def post_data(location, data = {})
  post(location, data, @headers)
  load_tml!(response.body)
end

#press(which_button) ⇒ Object



120
121
122
123
124
125
# File 'lib/rtml/test/simulator.rb', line 120

def press(which_button)
  process do
    app.trigger_button_press(which_button)
    process # FIXME: is this even necessary?
  end
end

#process(forward = false, &block) ⇒ Object



49
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
76
77
78
79
# File 'lib/rtml/test/simulator.rb', line 49

def process(forward = false, &block)
  # FIXME: TODO: Refactor, this is too ugly!
  if block_given?
    uri = catch(:new_document) { yield; nil }
  else
    uri = catch(:new_document) do
      if forward
        app.continue_forward
      else
        app.continue
      end
      nil
    end
  end

  if uri
    if uri =~ /^tmlvar\:(.*)$/ # it's a TML variable referencing a screen ID
      uri = variables[$~[1]]
      if uri.blank?
        raise Rtml::Errors::SimulationError, "Empty screen reference: tmlvar #{$~[1]} on screen #{current_screen_id}"
      end
    end
    if uri == @path && current_screen_id == app.screens.first['id']
      raise Rtml::Errors::SimulationError,
            "Circular foreign reference: #{@path}##{current_screen_id}"
    end
    visit_uri(uri)
  end

  do_post_processing!
end

#swipe_card(carrier, options = {}) ⇒ Object

carrier can be any of :visa, :mastercard, :jcb, :amex, :discover, and :debit. if it’s anything else, the ‘card.pan’ and ‘card.scheme’ options must be specified.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rtml/test/simulator.rb', line 129

def swipe_card(carrier, options = {})
  options.reverse_merge!('card.cardholder_name' => 'Cardholder',
                         'card.effective_date' => Time.now,
                         'card.expiry_date' => 1.year.from_now,
                         'card.input_type' => 1,
                         'card.issue_number' => 0,
                         'card.issuer_name' => 'NA',
                         'card.scheme' => carrier.to_s.upcase,
                         'card.pan' => case carrier
                           when :visa then '4111111111111111'
                           when :mastercard then '5454545454545454'
                           when :amex then '3434343434343434'
                           when :discover then '6011000012345678'
                           when :jcb then '3083000012345678'
                           when :debit then '1000000012345678'
                           else nil
                         end
  )

  unless options['card.pan'] && options['card.scheme']
    raise Rtml::Errors::SimulationError, "Carrier not recognized and/or options are invalid"
  end

  card_parsers = on_current_screen("card")
  card_parsers.each do |card|
    if card['parser'] == 'mag' && card['parser_params'] == 'read_data'
      app.update_variables(options)
      process(:forward)
      return
    end
  end
  raise Rtml::Errors::SimulationError,
        "Could not find a suitable card parser (with parser 'mag' and parser_params 'read_data') on screen #{current_screen_id.inspect}"
end

#variablesObject



176
177
178
# File 'lib/rtml/test/simulator.rb', line 176

def variables
  app.variable_scope
end

#visit(path, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rtml/test/simulator.rb', line 81

def visit(path, options = {})
  options.reverse_merge! default_options
  path = "##{path}" if path.kind_of?(Symbol)
  if File.file?(path)
    build_app File.read(@path = path)
  else
    path, screen = path_and_screen_from path
    if !path.blank? && @path != path
      get_and_build(path)
    else
      app.screenflow.clear
    end
    if screen && !screen.blank? && screen != '#'
      app.jump_to_screen screen
    end
  end
  process if options[:process]
end

#visit_uri(uri) ⇒ Object

Visits the specified URI. This merely wraps a call to #visit, but is called internally by #process when following a direction to visit a new URI outside of the current document. This makes it useful for setting up test expectations because you can make sure that #visit_uri is (or isn’t) called without worrying about breaking #visit.



105
106
107
# File 'lib/rtml/test/simulator.rb', line 105

def visit_uri(uri)
  visit(uri)
end