Class: Citron::TestCase
Overview
Test Case encapsulates a collection of unit tests organized into groups of contexts.
Class Attribute Summary collapse
-
.label ⇒ Object
readonly
Brief description of the test case.
-
.tags ⇒ Object
readonly
Symbol list of tags.
-
.tests ⇒ Object
readonly
List of tests and sub-cases.
-
.unit ⇒ Object
readonly
Code unit that is subject of test case.
Class Method Summary collapse
-
.<<(test_object) ⇒ Object
Add new test or sub-case.
-
.__set__(settings = {}, &block) ⇒ Object
Initialize new TestCase.
- .calc_unit(label) ⇒ Object
-
.Context(label, *tags, &block) ⇒ Object
(also: context)
Create a sub-case.
- .inspect ⇒ Object
-
.Ok(*args) ⇒ Object
(also: ok)
Actualize a parameterized test.
-
.Setup(label = nil, &proc) ⇒ Object
(also: setup)
Setup is used to set things up for each unit test.
-
.Skip(reason = true, &block) ⇒ Object
(also: skip)
Mark tests or sub-cases to be skipped.
-
.skip=(reason) ⇒ Object
Set test case to be skipped.
-
.skip? ⇒ Boolean, String
Is test case to be skipped?.
-
.Teardown(&proc) ⇒ Object
(also: teardown)
Teardown procedure is used to clean-up after each unit test.
-
.Test(label = nil, *tags, &procedure) ⇒ Object
(also: test)
Create a test, or a parameterized test.
-
.to_s ⇒ String
Test case label.
Instance Method Summary collapse
-
#each ⇒ Object
Iterate over each test and sub-case.
-
#setup ⇒ Object
Dummy method for setup.
-
#size ⇒ Fixnum
Number of tests and sub-cases.
-
#teardown ⇒ Object
Dummy method for teardown.
-
#to_s ⇒ String
Test case label.
Class Attribute Details
.label ⇒ Object (readonly)
Brief description of the test case.
11 12 13 |
# File 'lib/citron/test_case.rb', line 11 def label @label end |
.tags ⇒ Object (readonly)
Symbol list of tags. Trailing element may be Hash of ‘symbol => object`.
15 16 17 |
# File 'lib/citron/test_case.rb', line 15 def @tags end |
.tests ⇒ Object (readonly)
List of tests and sub-cases.
18 19 20 |
# File 'lib/citron/test_case.rb', line 18 def tests @tests end |
.unit ⇒ Object (readonly)
Code unit that is subject of test case.
21 22 23 |
# File 'lib/citron/test_case.rb', line 21 def unit @unit end |
Class Method Details
.<<(test_object) ⇒ Object
Add new test or sub-case.
69 70 71 72 |
# File 'lib/citron/test_case.rb', line 69 def <<(test_object) @tests ||= [] @tests << test_object end |
.__set__(settings = {}, &block) ⇒ Object
Initialize new TestCase.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/citron/test_case.rb', line 25 def __set__(settings={}, &block) @label = settings[:label] @tags = settings[:tags] @skip = settings[:skip] @unit = calc_unit(@label) @tests = [] class_eval(&block) end |
.calc_unit(label) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/citron/test_case.rb', line 40 def calc_unit(label) case label when Module, Class @label when /^(\.|\#|\:\:)\w+/ if Module === superclass.unit [superclass.unit, @label].join('') else @label end end end |
.Context(label, *tags, &block) ⇒ Object Also known as: context
Create a sub-case.
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/citron/test_case.rb', line 111 def Context(label, *, &block) context = Class.new(self) context.__set__( :skip => @_skip, :label => label, :tags => , &block ) self << context context end |
.inspect ⇒ Object
241 |
# File 'lib/citron/test_case.rb', line 241 alias :inspect :to_s |
.Ok(*args) ⇒ Object Also known as: ok
Better name than ‘Ok` ?
Actualize a parameterized test.
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/citron/test_case.rb', line 169 def Ok(*args) settings, procedure = *@_test test = TestProc.new(settings) do procedure.call(*args) end self << test return test end |
.Setup(label = nil, &proc) ⇒ Object Also known as: setup
Setup is used to set things up for each unit test. The setup procedure is run before each unit.
190 191 192 193 194 |
# File 'lib/citron/test_case.rb', line 190 def Setup(label=nil, &proc) define_method(:setup, &proc) # if the setup is reset, then so should the teardown define_method(:teardown){} end |
.Skip(reason = true, &block) ⇒ Object Also known as: skip
Mark tests or sub-cases to be skipped. If block is given, then tests defined within the block are skipped. Without a block all subsquent tests defined in a context will be skipped.
229 230 231 232 233 234 235 236 237 |
# File 'lib/citron/test_case.rb', line 229 def Skip(reason=true, &block) if block @_skip = reason block.call if block @_skip = false else @_skip = reason end end |
.skip=(reason) ⇒ Object
Set test case to be skipped.
97 98 99 |
# File 'lib/citron/test_case.rb', line 97 def skip=(reason) @skip = reason end |
.skip? ⇒ Boolean, String
Is test case to be skipped?
86 87 88 |
# File 'lib/citron/test_case.rb', line 86 def skip? @skip end |
.Teardown(&proc) ⇒ Object Also known as: teardown
Teardown procedure is used to clean-up after each unit test.
201 202 203 |
# File 'lib/citron/test_case.rb', line 201 def Teardown(&proc) define_method(:teardown, &proc) end |
.Test(label = nil, *tags, &procedure) ⇒ Object Also known as: test
Create a test, or a parameterized test.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/citron/test_case.rb', line 137 def Test(label=nil, *, &procedure) file, line, _ = *caller[0].split(':') settings = { :context => self, :skip => @_skip, :label => label, :tags => , :file => file, :line => line } if procedure.arity == 0 || (RUBY_VERSION < '1.9' && procedure.arity == -1) test = TestProc.new(settings, &procedure) self << test @_test = nil return test else @_test = [settings, procedure] end end |
.to_s ⇒ String
Test case label.
248 249 250 |
# File 'lib/citron/test_case.rb', line 248 def to_s label.to_s end |
Instance Method Details
#each ⇒ Object
Iterate over each test and sub-case.
257 258 259 260 261 262 263 264 265 266 |
# File 'lib/citron/test_case.rb', line 257 def each self.class.tests.each do |test_object| case test_object when Class #TestCase yield(test_object.new) when TestProc yield(test_object.for(self)) end end end |
#setup ⇒ Object
Dummy method for setup.
289 290 |
# File 'lib/citron/test_case.rb', line 289 def setup end |
#size ⇒ Fixnum
Number of tests and sub-cases.
273 274 275 |
# File 'lib/citron/test_case.rb', line 273 def size self.class.tests.size end |
#teardown ⇒ Object
Dummy method for teardown.
295 296 |
# File 'lib/citron/test_case.rb', line 295 def teardown end |
#to_s ⇒ String
Test case label.
282 283 284 |
# File 'lib/citron/test_case.rb', line 282 def to_s self.class.label.to_s end |