Module: Tina4::Testing

Defined in:
lib/tina4/testing.rb

Defined Under Namespace

Classes: TestContext, TestFailure, TestSuite

Class Method Summary collapse

Class Method Details

.describe(name, &block) ⇒ Object



22
23
24
25
26
# File 'lib/tina4/testing.rb', line 22

def describe(name, &block)
  suite = TestSuite.new(name)
  suite.instance_eval(&block)
  suites << suite
end

.expect_equal(args, expected) ⇒ Object

Expectation builder: expect_equal(args, expected)



51
52
53
# File 'lib/tina4/testing.rb', line 51

def expect_equal(args, expected)
  { type: :equal, args: args, expected: expected }
end

.expect_false(args) ⇒ Object

Expectation builder: expect_false(args)



66
67
68
# File 'lib/tina4/testing.rb', line 66

def expect_false(args)
  { type: :false, args: args }
end

.expect_raises(exception_class, args) ⇒ Object

Expectation builder: expect_raises(exception_class, args)



56
57
58
# File 'lib/tina4/testing.rb', line 56

def expect_raises(exception_class, args)
  { type: :raises, exception: exception_class, args: args }
end

.expect_true(args) ⇒ Object

Expectation builder: expect_true(args)



61
62
63
# File 'lib/tina4/testing.rb', line 61

def expect_true(args)
  { type: :true, args: args }
end

.inline_registryObject



87
88
89
# File 'lib/tina4/testing.rb', line 87

def inline_registry
  @inline_registry ||= []
end

.reset!Object



16
17
18
19
20
# File 'lib/tina4/testing.rb', line 16

def reset!
  @suites = []
  @inline_registry = []
  @results = { passed: 0, failed: 0, errors: 0, tests: [] }
end

.resultsObject



12
13
14
# File 'lib/tina4/testing.rb', line 12

def results
  @results ||= { passed: 0, failed: 0, errors: 0, tests: [] }
end

.run_all(quiet: false, failfast: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tina4/testing.rb', line 28

def run_all(quiet: false, failfast: false)
  reset_results
  suites.each do |suite|
    run_suite(suite, quiet: quiet, failfast: failfast)
    break if failfast && results[:failed] > 0
  end
  # Run inline-registered tests
  inline_registry.each do |entry|
    run_inline_entry(entry, quiet: quiet)
    break if failfast && results[:failed] > 0
  end
  print_results unless quiet
  results
end

.suitesObject



8
9
10
# File 'lib/tina4/testing.rb', line 8

def suites
  @suites ||= []
end

.tests(*assertions, name: nil, &block) ⇒ Object

Register a callable with inline expectations (mirrors Python's @tests decorator).

Tina4::Testing.tests(
Tina4::Testing.expect_equal([5, 3], 8),
Tina4::Testing.expect_raises(ArgumentError, [nil]),
) { |a, b| raise ArgumentError, "b required" if b.nil?; a + b }

Raises:

  • (ArgumentError)


77
78
79
80
81
82
83
84
85
# File 'lib/tina4/testing.rb', line 77

def tests(*assertions, name: nil, &block)
  raise ArgumentError, "tests requires a block" unless block_given?
  inline_registry << {
    fn: block,
    name: name || "anonymous",
    assertions: assertions
  }
  block
end