Class: Citron::TestCase

Inherits:
World
  • Object
show all
Defined in:
lib/citron/test_case.rb

Overview

Test Case encapsulates a collection of unit tests organized into groups of contexts.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.labelObject (readonly)

Brief description of the test case.



11
12
13
# File 'lib/citron/test_case.rb', line 11

def label
  @label
end

.tagsObject (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
  @tags
end

.testsObject (readonly)

List of tests and sub-cases.



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

def tests
  @tests
end

.unitObject (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.

Parameters:

  • test_obejct (Class<TestCase>, TestProc)

    Test sub-case or procedure to add to this 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.

Parameters:

  • label (String)

    The breif description of the test case.

  • tags (Array<Symbol,Hash>)

    List of symbols with optional trailing ‘symbol=>object` hash. These can be used as a means of filtering tests.



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/citron/test_case.rb', line 111

def Context(label, *tags, &block)
  context = Class.new(self)
  context.__set__(
    :skip    => @_skip,
    :label   => label,
    :tags    => tags,
    &block
  )

  self << context

  context
end

.inspectObject



241
# File 'lib/citron/test_case.rb', line 241

alias :inspect :to_s

.Ok(*args) ⇒ Object Also known as: ok

TODO:

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.

Parameters:

  • label (String) (defaults to: nil)

    A brief description of what the setup procedure sets-up.



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.

Examples:

skip("awaiting new feature") do
  test "some test" do
    ...
  end
end
skip("not on jruby") if jruby?
test "some test" do
  ...
end

Parameters:

  • reason (Boolean, String) (defaults to: true)

    Set to false or nil if not skipped, otherwise true or a string explain why to skip.



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.

Parameters:

  • reason (Boolean, String)

    Set to false or nil if not skipped, otherwise true or a string explain why to skip.



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?

Returns:

  • (Boolean, String)

    If false or nil if not skipped, otherwise true or a string explain why to skip.



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.

Parameters:

  • label (String) (defaults to: nil)

    The breif description of the test case.

  • tags (Array<Symbol,Hash>)

    List of symbols with optional trailing ‘symbol=>object` hash. These can be used as a means of filtering tests.



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, *tags, &procedure)
  file, line, _ = *caller[0].split(':')

  settings = {
    :context => self,
    :skip    => @_skip,
    :label   => label,
    :tags    => 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_sString

Test case label.

Returns:

  • (String)


248
249
250
# File 'lib/citron/test_case.rb', line 248

def to_s
  label.to_s
end

Instance Method Details

#eachObject

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

#setupObject

Dummy method for setup.



289
290
# File 'lib/citron/test_case.rb', line 289

def setup
end

#sizeFixnum

Number of tests and sub-cases.

Returns:

  • (Fixnum)

    size



273
274
275
# File 'lib/citron/test_case.rb', line 273

def size
  self.class.tests.size
end

#teardownObject

Dummy method for teardown.



295
296
# File 'lib/citron/test_case.rb', line 295

def teardown
end

#to_sString

Test case label.

Returns:

  • (String)


282
283
284
# File 'lib/citron/test_case.rb', line 282

def to_s
  self.class.label.to_s
end