Class: Fix::Dsl Private

Inherits:
Object
  • Object
show all
Extended by:
Matcher, Requirement
Defined in:
lib/fix/dsl.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.

Abstract class for handling the domain-specific language.

Class Method Summary collapse

Methods included from Requirement

MAY, MUST, MUST_NOT, SHOULD, SHOULD_NOT

Class Method Details

.challengesArray<Defi::Method>

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.

The list of challenges to be addressed to the object to be tested.

Returns:

  • (Array<Defi::Method>)

    A list of challenges.



135
136
137
# File 'lib/fix/dsl.rb', line 135

def self.challenges
  []
end

.it(requirement = nil) { ... } ⇒ Symbol

Defines a concrete spec definition.

Examples:

require "fix"

Fix { it MUST be 42 }

Fix do
  it { MUST be 42 }
end

Parameters:

  • requirement (Object, nil) (defaults to: nil)

    The requirement to test

Yields:

  • A block defining the requirement if not provided directly

Yield Returns:

  • (Object)

    The requirement definition

Returns:

  • (Symbol)

    Name of the generated test method

Raises:

  • (ArgumentError)

    If neither or both requirement and block are provided



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fix/dsl.rb', line 119

def self.it(requirement = nil, &block)
  raise ::ArgumentError, "Must provide either requirement or block, not both" if requirement && block
  raise ::ArgumentError, "Must provide either requirement or block" unless requirement || block

  location = caller_locations(1, 1).fetch(0)
  location = [location.path, location.lineno].join(":")

  test_method_name = :"test_#{(requirement || block).object_id}"
  define_method(test_method_name) do
    [location, requirement || singleton_class.class_eval(&block), self.class.challenges]
  end
end

.let(name) { ... } ⇒ Symbol

Sets a user-defined property.

Examples:

require "fix"

Fix do
  let(:name) { "Bob" }
end

Parameters:

  • name (String, Symbol)

    The name of the property.

Yields:

  • The block that defines the property’s value

Yield Returns:

  • (Object)

    The value to be returned by the property

Returns:

  • (Symbol)

    A private method that defines the block content.



32
33
34
# File 'lib/fix/dsl.rb', line 32

def self.let(name, &)
  private define_method(name, &)
end

.on(method_name, *args, **kwargs) { ... } ⇒ Class

Defines an example group that describes a unit to be tested.

Examples:

require "fix"

Fix do
  on :+, 2 do
    it MUST be 42
  end
end

Parameters:

  • method_name (String, Symbol)

    The method to send to the subject

  • args (Array)

    Positional arguments to pass to the method

  • kwargs (Hash)

    Keyword arguments to pass to the method

Yields:

  • The block containing the specifications for this context

Yield Returns:

  • (void)

Returns:

  • (Class)

    A new class representing this context



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fix/dsl.rb', line 83

def self.on(method_name, *args, **kwargs, &block)
  klass = ::Class.new(self)
  klass.const_get(:CONTEXTS) << klass

  const_name = :"MethodContext_#{block.object_id}"
  const_set(const_name, klass)

  klass.define_singleton_method(:challenges) do
    challenge = ::Defi::Method.new(method_name, *args, **kwargs)
    super() + [challenge]
  end

  klass.instance_eval(&block)
  klass
end

.with(**kwargs) { ... } ⇒ Class

Defines an example group with user-defined properties that describes a unit to be tested.

Examples:

require "fix"

Fix do
  with password: "secret" do
    it MUST be true
  end
end

Parameters:

  • kwargs (Hash)

    The list of properties to define in this context

Yields:

  • The block that defines the specs for this context

Yield Returns:

  • (void)

Returns:

  • (Class)

    A new class representing this context



55
56
57
58
59
60
61
# File 'lib/fix/dsl.rb', line 55

def self.with(**kwargs, &)
  klass = ::Class.new(self)
  klass.const_get(:CONTEXTS) << klass
  kwargs.each { |name, value| klass.let(name) { value } }
  klass.instance_eval(&)
  klass
end