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.



118
119
120
# File 'lib/fix/dsl.rb', line 118

def self.challenges
  []
end

.it(requirement = nil, &block) ⇒ Object

Defines a concrete spec definition.

Examples:

require "fix"

Fix { it MUST be 42 }

Fix do
  it { MUST be 42 }
end

Raises:

  • (::ArgumentError)


103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fix/dsl.rb', line 103

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(":")

  define_method(:"test_#{(requirement || block).object_id}") 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 define 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, &block) ⇒ Object

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.

  • block (Proc)

    The block to define the specs.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fix/dsl.rb', line 76

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

  const_set(:"Child#{block.object_id}", 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) { ... } ⇒ Object

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 propreties.

Yields:

  • The block that defines the specs for this context

Yield Returns:

  • (void)


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

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