Class: Bcpm::Tests::Suite

Inherits:
Object
  • Object
show all
Defined in:
lib/bcpm/tests/suite.rb

Overview

Collection of test cases.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_path) ⇒ Suite

Blank suite.



19
20
21
22
23
24
# File 'lib/bcpm/tests/suite.rb', line 19

def initialize(local_path)
  @local_path = local_path
  @tests = []
  @matches = []
  @environments = []
end

Instance Attribute Details

#environmentsObject (readonly)

All the environments used in the tests.



10
11
12
# File 'lib/bcpm/tests/suite.rb', line 10

def environments
  @environments
end

#local_pathObject (readonly)

Path to the suite’s local repository.



16
17
18
# File 'lib/bcpm/tests/suite.rb', line 16

def local_path
  @local_path
end

#matchesObject (readonly)

All the matches used in the tests.



12
13
14
# File 'lib/bcpm/tests/suite.rb', line 12

def matches
  @matches
end

#testsObject (readonly)

All test cases.



14
15
16
# File 'lib/bcpm/tests/suite.rb', line 14

def tests
  @tests
end

Instance Method Details

#add_cases(case_files) ⇒ Object

Adds the given test cases to the suite.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bcpm/tests/suite.rb', line 27

def add_cases(case_files)    
  case_files.each do |file|
    code = File.read file
    klass = Class.new Bcpm::Tests::CaseBase
    klass._setup
    begin
      klass.class_eval code, file
    rescue Exception => e
      trace = short_backtrace e.backtrace
      print "Error in test case #{file}\n"
      print "#{e.class.name}: #{e.to_s}\n#{trace.join("\n")}\n\n"
      next
    end
    klass._post_eval
    @tests += klass.tests
    @matches += klass.matches
    @environments += klass.environments
  end
  self
end

#run(live = false) ⇒ Object

Runs all the tests in the suite.



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bcpm/tests/suite.rb', line 49

def run(live = false)
  environments.each { |e| e.setup local_path }

  wins, fails, errors, skipped, totals = 0, 0, 0, 0, 0
  failures = []
  tests.each_with_index do |test, i|
    unless test.match.environment.available?
      skipped += 1
      next
    end
    
    unless test.match.ran?
      test.match.run live
      # Only one match can run live, otherwise all hell will break loose.
      live = false
    end
    failure_string = nil
    begin
      if failure = test.check_output
        trace = short_backtrace failure.backtrace
        failure_string = "#{failure.to_s}\n#{trace.join("\n")}"
        fails += 1
        print 'F'
      else
        wins += 1
        print '.'
      end        
    rescue Exception => e
      errors += 1
      failure_string = "#{e.class.name}: #{e.to_s}\n#{e.backtrace.join("\n")}"
    end
    totals += 1
    if failure_string
      failures << [test, failure_string]
    end
  end
  print "\n#{totals} tests, #{wins} passed, #{fails} failures, #{errors} errors\n\n"
  failures.each_with_index do |failure, i|
    test, string = *failure
    print "#{'%3d' % (i + 1)}) Failed #{test.description}\n"

    print test.match.stash_data            
    print "#{string}\n\n"
  end

  environments.each { |e| e.teardown }
  self
end

#short_backtrace(backtrace) ⇒ Object

Trims backtrace to the confines of the test suite.



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/bcpm/tests/suite.rb', line 99

def short_backtrace(backtrace)
  trace = backtrace
  first_line = trace.find_index { |line| line.index local_path }
  last_line = trace.length - 1 - trace.reverse.find_index { |line| line.index local_path }
  # Leave the trace untouched if it doesn't go through the test suite.
  if first_line && last_line
    trace = trace[first_line..last_line]
    trace[-1] = trace[-1].sub /in `.*'/, 'in (test case)'
  end
  trace
end