Class: GUnit::TestCase

Inherits:
Object
  • Object
show all
Includes:
Assertions
Defined in:
lib/gunit/test_case.rb

Overview

count, run, display

Constant Summary collapse

TEST_METHOD_PREFIX =
'test'
@@method_count =
0
@@context_stack =
[ GUnit::Context.new ]
@@test_method_contexts =
{}
@@autorun =
true
@@subclasses =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assertions

#assert, #assert_equal, #assert_raises

Constructor Details

#initialize(method = nil) ⇒ TestCase

Returns a new instance of TestCase.



27
28
29
# File 'lib/gunit/test_case.rb', line 27

def initialize(method=nil)
  self.method_name = method
end

Instance Attribute Details

#contextObject



137
138
139
# File 'lib/gunit/test_case.rb', line 137

def context
  self.class.context_for_method(self.method_name)
end

#method_nameObject

Returns the value of attribute method_name.



18
19
20
# File 'lib/gunit/test_case.rb', line 18

def method_name
  @method_name
end

Class Method Details

.autorunObject



49
50
51
52
53
54
55
# File 'lib/gunit/test_case.rb', line 49

def self.autorun
  if GUnit::TestCase.autorun?
    test_runner = GUnit::TestRunner.instance
    test_runner.tests << suite
    test_runner.run!
  end
end

.autorun=(a) ⇒ Object



41
42
43
# File 'lib/gunit/test_case.rb', line 41

def self.autorun=(a)
  @@autorun = !a.nil? && a
end

.autorun?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/gunit/test_case.rb', line 45

def self.autorun?
  @@autorun
end

.context(*args, &blk) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gunit/test_case.rb', line 75

def self.context(*args, &blk)
  new_context = if blk
    GUnit::Context.new(args.first, &blk)
  else
    GUnit::Context.new(args.first)
  end
  new_context.parent = current_context
  @@context_stack << new_context
  current_context.run(self)
  @@context_stack.pop
end

.context_for_method(method_name) ⇒ Object



71
72
73
# File 'lib/gunit/test_case.rb', line 71

def self.context_for_method(method_name)
  @@test_method_contexts[method_name] || @@context_stack.first
end

.exercise(*args, &blk) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/gunit/test_case.rb', line 96

def self.exercise(*args, &blk)
  exercise = if blk
    GUnit::Exercise.new(args.first, &blk)
  else
    GUnit::Exercise.new(args.first)
  end
  current_context.exercise = exercise
end

.inherited(subclass) ⇒ Object



35
36
37
38
39
# File 'lib/gunit/test_case.rb', line 35

def self.inherited(subclass)
  subclasses << subclass
  # autorun must happen at exit, otherwise the subclass won't be loaded
  at_exit { subclass.autorun }
end

.message_to_test_method_name(msg) ⇒ Object



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

def self.message_to_test_method_name(msg)
  if msg && msg != ''
    [TEST_METHOD_PREFIX, msg.downcase.gsub(/ |@/,'_')].join('_').to_sym
  else
    TEST_METHOD_PREFIX.to_sym
  end
end

.setup(*args, &blk) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/gunit/test_case.rb', line 87

def self.setup(*args, &blk)
  setup = if blk
    GUnit::Setup.new(args.first, &blk)
  else
    GUnit::Setup.new(args.first)
  end
  current_context.setups << setup
end

.subclassesObject



31
32
33
# File 'lib/gunit/test_case.rb', line 31

def self.subclasses
  @@subclasses
end

.suiteObject



57
58
59
60
61
62
63
64
# File 'lib/gunit/test_case.rb', line 57

def self.suite
  # Create an new instance of self.class for each test method and add them to a TestSuite Object
  test_suite = TestSuite.new
  test_methods.each do |test_method|
    test_suite.tests << new(test_method)
  end
  test_suite
end

.teardown(*args, &blk) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/gunit/test_case.rb', line 105

def self.teardown(*args, &blk)
  teardown = if blk
    GUnit::Teardown.new(args.first, &blk)
  else
    GUnit::Teardown.new(args.first)
  end
  current_context.teardowns << teardown
end

.test_methods(prefix = TEST_METHOD_PREFIX) ⇒ Object



66
67
68
69
# File 'lib/gunit/test_case.rb', line 66

def self.test_methods(prefix=TEST_METHOD_PREFIX)
  method_names = instance_methods.find_all{|method| method =~ /\A#{prefix}/ && ! GUnit::TestCase.instance_methods.include?(method) }
  method_names.map!{|m| m.to_sym }
end

.verify(*args, &blk) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/gunit/test_case.rb', line 114

def self.verify(*args, &blk)
  test_method_name = message_to_unique_test_method_name(args.first)
  define_method(test_method_name) do
    verification = if blk
      GUnit::Verification.new(args.first, &blk)
    else
      GUnit::Verification.new(args.first)
    end
    verification.run(self)
  end
  @@method_count += 1
  @@test_method_contexts[test_method_name] = current_context
  test_method_name
end

Instance Method Details

#runObject



141
142
143
144
145
146
147
# File 'lib/gunit/test_case.rb', line 141

def run
  self.run_setups
  self.run_excercise
  response = self.send(self.method_name.to_sym)
  self.run_teardowns
  response
end