Class: Fix::Set Private

Inherits:
Object
  • Object
show all
Defined in:
lib/fix/set.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A Set represents a collection of test specifications that can be executed as a test suite. It manages the lifecycle of specifications, including:

  • Building and loading specifications from contexts

  • Executing specifications in isolation using process forking

  • Reporting test results

  • Managing test execution flow and exit status

Examples:

Creating and running a simple test set

set = Fix::Set.build(:Calculator) do
  on(:add, 2, 3) do
    it MUST eq 5
  end
end
set.test { Calculator.new }

Loading and running a registered test set

set = Fix::Set.load(:Calculator)
set.match? { Calculator.new } #=> true

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*contexts) ⇒ Set

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes a new Set with given test contexts.

The contexts are processed to extract test specifications and randomized to ensure test isolation and catch order dependencies.

Examples:

Creating a set with multiple contexts

Fix::Set.new(base_context, admin_context, guest_context)

Parameters:

  • contexts (Array<Fix::Dsl>)

    List of specification contexts to include



84
85
86
# File 'lib/fix/set.rb', line 84

def initialize(*contexts)
  @expected = Doc.extract_specifications(*contexts).shuffle
end

Class Method Details

.build(name) { ... } ⇒ Fix::Set

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds a new Set from a specification block.

This method:

  1. Creates a new DSL class for the specification

  2. Evaluates the specification block in this context

  3. Optionally registers the specification under a name

  4. Returns a Set instance ready for testing

Examples:

Building a named specification

Fix::Set.build(:Calculator) do
  on(:add, 2, 3) { it MUST eq 5 }
end

Building an anonymous specification

Fix::Set.build(nil) do
  it MUST be_positive
end

Parameters:

  • name (Symbol, nil)

    Optional name to register the specification under

Yields:

  • Block containing the specification definition using Fix DSL

Returns:

  • (Fix::Set)

    A new specification set ready for testing



53
54
55
56
57
58
59
# File 'lib/fix/set.rb', line 53

def self.build(name, &block)
  klass = ::Class.new(Dsl)
  klass.const_set(:CONTEXTS, [klass])
  klass.instance_eval(&block)
  Doc.const_set(name, klass) unless name.nil?
  new(*klass.const_get(:CONTEXTS))
end

.load(name) ⇒ Fix::Set

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Loads a previously registered specification set by name.

Examples:

Loading a registered specification

Fix::Set.load(:Calculator)  #=> #<Fix::Set:...>

Parameters:

  • name (Symbol)

    The name of the registered specification

Returns:

  • (Fix::Set)

    The loaded specification set

Raises:

  • (NameError)

    If the specification name is not found



71
72
73
# File 'lib/fix/set.rb', line 71

def self.load(name)
  new(*Doc.fetch(name))
end

Instance Method Details

#match? { ... } ⇒ Boolean

Verifies if a subject matches all specifications without exiting.

This method is useful for:

  • Conditional testing where exit on failure is not desired

  • Integration into larger test suites

  • Programmatic test result handling

Examples:

Basic matching

set.match? { Calculator.new }  #=> true

Conditional testing

if set.match? { user_input }
  process_valid_input(user_input)
else
  handle_invalid_input
end

Yields:

  • Block that returns the subject to test

Yield Returns:

  • (Object)

    The subject to test against specifications

Returns:

  • (Boolean)

    true if all tests pass, false otherwise

Raises:



111
112
113
114
115
# File 'lib/fix/set.rb', line 111

def match?(&subject)
  raise Error::MissingSubjectBlock unless subject

  expected.all? { |spec| run_spec(*spec, &subject) }
end

#test { ... } ⇒ Boolean

Executes the complete test suite against a subject.

This method provides a comprehensive test run that:

  • Executes all specifications in random order

  • Runs each test in isolation via process forking

  • Reports results for each specification

  • Exits with appropriate status code

Examples:

Basic test execution

set.test { Calculator.new }

Testing with dependencies

set.test {
  calc = Calculator.new
  calc.precision = :high
  calc
}

Yields:

  • Block that returns the subject to test

Yield Returns:

  • (Object)

    The subject to test against specifications

Returns:

  • (Boolean)

    true if all tests pass

Raises:



142
143
144
# File 'lib/fix/set.rb', line 142

def test(&subject)
  match?(&subject) || exit_with_failure
end

#to_sString

Returns a string representation of the test set.

Examples:

set.to_s #=> "fix [<specification list>]"

Returns:

  • (String)

    Human-readable description of the test set



154
155
156
# File 'lib/fix/set.rb', line 154

def to_s
  "fix #{expected.inspect}"
end