Module: TestTube

Defined in:
lib/test_tube.rb,
lib/test_tube/passer.rb,
lib/test_tube/invoker.rb,
lib/test_tube/experiment.rb

Overview

Namespace for the TestTube library. This library provides two main methods for conducting software experiments:

  • invoke: For testing blocks of code, with full exception handling

  • pass: For testing direct values, with a simpler and safer approach

Examples:

Using with a testing framework

require "test_tube"
require "matchi"

# Testing for exceptions
TestTube.invoke(matcher: Matchi::RaiseException.new(SystemExit), negate: false) do
  exit(true)  # Safely catches the SystemExit
end

# Testing direct values
TestTube.pass(42, matcher: Matchi::EqualTo.new(42), negate: false)

Defined Under Namespace

Classes: Experiment, Invoker, Passer

Class Method Summary collapse

Class Method Details

.invokeTestTube::Invoker

Invokes a block for testing. This method should be used when you need to:

  • Execute and test a block of code

  • Catch all possible exceptions (including SystemExit)

  • Handle potentially dangerous operations safely

Examples:

Testing computation result

require "test_tube"

class BeTheAnswer
  def match?
    42.equal?(yield)
  end
end

experiment = TestTube.invoke(matcher: BeTheAnswer.new, negate: false) do
  "101010".to_i(2)
end

experiment.actual  # => 42
experiment.error   # => nil
experiment.got     # => true

Returns:

  • (TestTube::Invoker)

    A software experiment capturing execution results and any errors

See Also:



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

def self.invoke(...)
  Invoker.new(...)
end

.passTestTube::Passer

Tests a value directly. This method should be used when you:

  • Already have the value to test

  • Don’t need to execute potentially dangerous code

  • Want a simpler and more direct testing approach

Examples:

Testing a direct value

require "test_tube"

class BeTheAnswer
  def match?
    42.equal?(yield)
  end
end

experiment = TestTube.pass("101010".to_i(2),
  matcher: BeTheAnswer.new,
  negate: false
)

experiment.actual  # => 42
experiment.error   # => nil
experiment.got     # => true

Returns:

See Also:



82
83
84
# File 'lib/test_tube.rb', line 82

def self.pass(...)
  Passer.new(...)
end