Class: Cosmos::Suite

Inherits:
Object show all
Defined in:
lib/cosmos/script/suite.rb

Overview

Base class for Script Runner suites. COSMOS Suites inherit from Suite and can implement setup and teardown methods. Script groups are added via add_group(Group) and individual scripts added via add_script(Group, script_method).

Direct Known Subclasses

TestSuite, UnassignedSuite

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSuite

Create a new Suite



38
39
40
41
# File 'lib/cosmos/script/suite.rb', line 38

def initialize
  @scripts = {}
  @plans = []
end

Instance Attribute Details

#plansObject (readonly)

Returns the value of attribute plans.



31
32
33
# File 'lib/cosmos/script/suite.rb', line 31

def plans
  @plans
end

#scriptsObject (readonly)

Returns the value of attribute scripts.



30
31
32
# File 'lib/cosmos/script/suite.rb', line 30

def scripts
  @scripts
end

Instance Method Details

#<=>(other_suite) ⇒ Object

END PUBLIC API



75
76
77
# File 'lib/cosmos/script/suite.rb', line 75

def <=>(other_suite)
  self.name <=> other_suite.name
end

#add_group(group_class) ⇒ Object

Add a group to the suite



44
45
46
47
48
# File 'lib/cosmos/script/suite.rb', line 44

def add_group(group_class)
  group_class = Object.const_get(group_class.to_s.intern) unless group_class.class == Class
  @scripts[group_class] = group_class.new unless @scripts[group_class]
  @plans << [:GROUP, group_class, nil]
end

#add_group_setup(group_class) ⇒ Object

Add a group setup to the suite



58
59
60
61
62
# File 'lib/cosmos/script/suite.rb', line 58

def add_group_setup(group_class)
  group_class = Object.const_get(group_class.to_s.intern) unless group_class.class == Class
  @scripts[group_class] = group_class.new unless @scripts[group_class]
  @plans << [:GROUP_SETUP, group_class, nil]
end

#add_group_teardown(group_class) ⇒ Object

Add a group teardown to the suite



65
66
67
68
69
# File 'lib/cosmos/script/suite.rb', line 65

def add_group_teardown(group_class)
  group_class = Object.const_get(group_class.to_s.intern) unless group_class.class == Class
  @scripts[group_class] = group_class.new unless @scripts[group_class]
  @plans << [:GROUP_TEARDOWN, group_class, nil]
end

#add_script(group_class, script) ⇒ Object

Add a script to the suite



51
52
53
54
55
# File 'lib/cosmos/script/suite.rb', line 51

def add_script(group_class, script)
  group_class = Object.const_get(group_class.to_s.intern) unless group_class.class == Class
  @scripts[group_class] = group_class.new unless @scripts[group_class]
  @plans << [:SCRIPT, group_class, script]
end

#get_num_scriptsObject

Returns the number of scripts in the suite including setup and teardown methods



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cosmos/script/suite.rb', line 89

def get_num_scripts
  num_scripts = 0
  @plans.each do |type, group_class, script|
    case type
    when :GROUP
      num_scripts += group_class.get_num_scripts
    when :SCRIPT, :GROUP_SETUP, :GROUP_TEARDOWN
      num_scripts += 1
    end
  end
  num_scripts += 1 if self.class.method_defined?(:setup)
  num_scripts += 1 if self.class.method_defined?(:teardown)
  num_scripts
end

#nameObject

Name of the suite



80
81
82
83
84
85
86
# File 'lib/cosmos/script/suite.rb', line 80

def name
  if self.class != Suite
    self.class.to_s.split('::')[-1]
  else
    'UnassignedSuite'
  end
end

#run(&block) ⇒ Object

Run all the scripts



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
# File 'lib/cosmos/script/suite.rb', line 105

def run(&block)
  ScriptResult.suite = name()
  ScriptStatus.instance.total = get_num_scripts()
  results = []

  # Setup the suite
  result = run_setup(true)
  if result
    results << result
    yield result if block_given?
    raise StopScript if result.stopped
  end

  # Run each script
  @plans.each do |type, group_class, script|
    case type
    when :GROUP
      results.concat(run_group(group_class, true, &block))
    when :SCRIPT
      result = run_script(group_class, script, true)
      results << result
      yield result if block_given?
      raise StopScript if (result.exceptions and group_class.abort_on_exception) or result.stopped
    when :GROUP_SETUP
      result = run_group_setup(group_class, true)
      if result
        results << result
        yield result if block_given?
        raise StopScript if (result.exceptions and group_class.abort_on_exception) or result.stopped
      end
    when :GROUP_TEARDOWN
      result = run_group_teardown(group_class, true)
      if result
        results << result
        yield result if block_given?
        raise StopScript if (result.exceptions and group_class.abort_on_exception) or result.stopped
      end
    end
  end

  # Teardown the suite
  result = run_teardown(true)
  if result
    results << result
    yield result if block_given?
    raise StopScript if result.stopped
  end

  ScriptResult.suite = nil
  results
end

#run_group(group_class, internal = false, &block) ⇒ Object

Run a specific group



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/cosmos/script/suite.rb', line 158

def run_group(group_class, internal = false, &block)
  ScriptResult.suite = name() unless internal

  # Determine if this group_class is in the plan and the number of scripts associated with this group_class
  in_plan = false
  num_scripts = 0
  @plans.each do |plan_type, plan_group_class, plan_script|
    if plan_type == :GROUP and group_class == plan_group_class
      in_plan = true
    end
    if (plan_type == :GROUP_SETUP and group_class == plan_group_class) or
       (plan_type == :GROUP_TEARDOWN and group_class == plan_group_class) or
       (plan_script and group_class == plan_group_class)
      num_scripts += 1
    end
  end

  if in_plan
    ScriptStatus.instance.total = group_class.get_num_scripts() unless internal
    results = @scripts[group_class].run(&block)
  else
    results = []
    ScriptStatus.instance.total = num_scripts unless internal

    # Run each setup, teardown, or script associated with this group_class in the order
    # defined in the plan
    @plans.each do |plan_type, plan_group_class, plan_script|
      if plan_group_class == group_class
        case plan_type
        when :SCRIPT
          result = run_script(plan_group_class, plan_script, true)
          results << result
          yield result if block_given?
        when :GROUP_SETUP
          result = run_group_setup(plan_group_class, true)
          if result
            results << result
            yield result if block_given?
          end
        when :GROUP_TEARDOWN
          result = run_group_teardown(plan_group_class, true)
          if result
            results << result
            yield result if block_given?
          end
        end
      end
    end
  end

  ScriptResult.suite = nil unless internal
  return results
end

#run_group_setup(group_class, internal = false) ⇒ Object



245
246
247
248
249
250
251
# File 'lib/cosmos/script/suite.rb', line 245

def run_group_setup(group_class, internal = false)
  ScriptResult.suite = name() unless internal
  ScriptStatus.instance.total = 1 unless internal
  result = @scripts[group_class].run_setup
  ScriptResult.suite = nil unless internal
  result
end

#run_group_teardown(group_class, internal = false) ⇒ Object



253
254
255
256
257
258
259
# File 'lib/cosmos/script/suite.rb', line 253

def run_group_teardown(group_class, internal = false)
  ScriptResult.suite = name() unless internal
  ScriptStatus.instance.total = 1 unless internal
  result = @scripts[group_class].run_teardown
  ScriptResult.suite = nil unless internal
  result
end

#run_script(group_class, script, internal = false) ⇒ Object

Run a specific script



213
214
215
216
217
218
219
# File 'lib/cosmos/script/suite.rb', line 213

def run_script(group_class, script, internal = false)
  ScriptResult.suite = name() unless internal
  ScriptStatus.instance.total = 1 unless internal
  result = @scripts[group_class].run_script(script)
  ScriptResult.suite = nil unless internal
  result
end

#run_setup(internal = false) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
# File 'lib/cosmos/script/suite.rb', line 221

def run_setup(internal = false)
  ScriptResult.suite = name() unless internal
  result = nil
  if self.class.method_defined?(:setup) and @scripts.length > 0
    ScriptStatus.instance.total = 1 unless internal
    ScriptStatus.instance.status = "#{self.class} : setup"
    result = @scripts[@scripts.keys[0]].run_method(self, :setup)
  end
  ScriptResult.suite = nil unless internal
  result
end

#run_teardown(internal = false) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/cosmos/script/suite.rb', line 233

def run_teardown(internal = false)
  ScriptResult.suite = name() unless internal
  result = nil
  if self.class.method_defined?(:teardown) and @scripts.length > 0
    ScriptStatus.instance.total = 1 unless internal
    ScriptStatus.instance.status = "#{self.class} : teardown"
    result = @scripts[@scripts.keys[0]].run_method(self, :teardown)
  end
  ScriptResult.suite = nil unless internal
  result
end