Class: Concordion::Invoker

Inherits:
Object
  • Object
show all
Includes:
LoaderHelper, Utility
Defined in:
lib/concordion/invoker.rb

Instance Method Summary collapse

Methods included from Utility

#concordion_arguments, #concordion_cmd_attr_exists?, #concordion_cmd_attr_for, #concordion_property_reference, #concordion_variable_name, #has_property_reference?, #instrumentation

Methods included from StringUtility

#assignment, #attr_writer_method?, #concordion_assignment, #concordion_method_name, #ends_in_empty_parens?, #escape_single_quotes, #has_arguments?, #has_assignment?, #is_direct_method_call?

Methods included from PluralToSingularUtility

#singular

Methods included from SnakeCaseUtility

#snake_case, #snake_cased_goldmaster_name, #snake_cased_test_name

Methods included from Constants

#concordion_command_attributes, #supported?

Methods included from LoaderHelper

#path_for

Constructor Details

#initialize(conc) ⇒ Invoker

Returns a new instance of Invoker.



9
10
11
# File 'lib/concordion/invoker.rb', line 9

def initialize(conc)
  @concordion = conc
end

Instance Method Details

#arg_values_for(cpr) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/concordion/invoker.rb', line 60

def arg_values_for(cpr)
    arg_values = []
    if has_arguments?(cpr.system_under_test)
      arg_values = handle_args(cpr)
    end
    arg_values
end

#class_from_no_method_error(e) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/concordion/invoker.rb', line 104

def class_from_no_method_error(e)
  error_message = except_str_from(e)
  if error_message =~ /</ and error_message =~ /:/
    error_message.split("<").last.split(":").first
  else
    "Class not known"
  end
end

#commandsObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/concordion/invoker.rb', line 126

def commands
  cmds = {}
  cmds["assertequals"] = Proc.new { |a, b| 
    result = (a.to_s == b)
    { :result => result, :actual => a, :expected => b } }
  cmds["execute"] = Proc.new { |a,b| 
    result = true
    result = false if a.to_s =~ /Missing method/
    {:result => result, :actual => a, :expected => b } }
  cmds["verifyrows"] = Proc.new { |a,b| { :result => a.size == b, :actual => a.size, :expected => b } }
  cmds["asserttrue"] = Proc.new { |a, b| 
    result = a
    result = false if a.instance_of?(String)
    { :result => result, :actual => false,  :expected => true } }
  cmds["assert_image"] = Proc.new { |actual_data, expected_image_url| 
    expected_data = File.read(path_for(expected_image_url))
    { :result => actual_data == expected_data, :actual => "[Image data of size #{actual_data.size} omitted]",  :expected => "[Expected to match #{expected_image_url}]" } 
  }
  cmds
end

#dereference_error_message(e) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/concordion/invoker.rb', line 25

def dereference_error_message(e)
  rv = "["
  clazz = e.to_s.split(":")[1]
  if clazz == 'NilClass'
    rv += "No more rows"
  end
  rv += "]"
end

#except_str_from(e) ⇒ Object



100
101
102
# File 'lib/concordion/invoker.rb', line 100

def except_str_from(e)
  e.message
end

#handle_args(cpr) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/concordion/invoker.rb', line 47

def handle_args(cpr)
    arg_vars = concordion_arguments(cpr.system_under_test)
    arg_values = arg_vars.collect {|var|
       if var == '#TEXT'
          cpr.content
       else
           @concordion.get_variable(var)
       end
    }

    arg_values
end

#invoke_concordion(cpr, sut_rv) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/concordion/invoker.rb', line 113

def invoke_concordion(cpr, sut_rv)
  cmd = commands[cpr.concordion_command]
  if cpr.is_assert_image_command?
    cmd.call(sut_rv, cpr.image_location)
  elsif cpr.is_verify_command?
    cmd.call(sut_rv, cpr.num_results_expected)
  else
    cmd.call(sut_rv, cpr.content)
  end

end

#invoke_sut(cpr, test_context) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/concordion/invoker.rb', line 35

def invoke_sut(cpr, test_context)
  sut_rv = nil
  if cpr.needs_dereference?
    sut_rv = try_to_dereference(cpr)
  else 

    sut_rv = try_to_invoke_sut(cpr,test_context)
  end

  sut_rv
end

#method_from_no_method_error(e) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/concordion/invoker.rb', line 91

def method_from_no_method_error(e)
  error_message = except_str_from(e)
  if error_message =~ /`/
    error_message.split("`").last.split("'").first
  else
    error_message
  end
end

#report_error(e, cpr = "BUG IN CONNCORDION") ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/concordion/invoker.rb', line 79

def report_error(e, cpr="BUG IN CONNCORDION")
  rv = nil
  if e.to_s =~ /nil:NilClass/
    rv = "[Parse failed for: #{cpr}, cause: (#{e})]"
  else
    method = method_from_no_method_error(e)
    clazz = class_from_no_method_error(e)
    rv = "[Missing method '#{method}' in fixture #{clazz} ]"
  end
  rv
end

#try_to_dereference(cpr) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/concordion/invoker.rb', line 13

def try_to_dereference(cpr)
  rv = nil
  begin
    rv = @concordion.dereference(cpr.system_under_test)
  rescue NoMethodError => e
    method = method_from_no_method_error(e)

    rv = dereference_error_message(e)
  end
  rv
end

#try_to_invoke_sut(cpr, test_context) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/concordion/invoker.rb', line 68

def try_to_invoke_sut(cpr, test_context)
  sut_rv = nil
  begin
      conc_method = concordion_method_name(cpr.system_under_test)
      sut_rv = test_context.send(conc_method, *arg_values_for(cpr))
  rescue NoMethodError => e
    sut_rv = report_error(e, cpr)
  end
  sut_rv
end