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 Matcher

be, be_an_instance_of, be_false, be_nil, be_true, be_within, change, eq, match, raise_exception, satisfy

Methods included from Requirement

MAY, MAY!, MUST, MUST!, MUST_NOT, MUST_NOT!, SHOULD, SHOULD!, SHOULD_NOT, SHOULD_NOT!

Class Method Details

.challengesArray<Defi::Challenge>

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::Challenge>)

    A list of challenges.



109
110
111
# File 'lib/fix/dsl.rb', line 109

def self.challenges
  []
end

.it(requirement) ⇒ Object

Defines a concrete spec definition.

Examples:

require "fix"

Fix { it MUST be 42 }


97
98
99
100
101
102
103
104
# File 'lib/fix/dsl.rb', line 97

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

  define_method(:"test_#{requirement.object_id}") do
    [location, requirement, self.class.challenges]
  end
end

.let(name, &block) ⇒ Symbol

Sets a user-defined property.

Examples:

require "fix"

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

Parameters:

  • name (String, Symbol)

    The name of the property.

  • block (Proc)

    The content of the method to define.

Returns:

  • (Symbol)

    A private method that define the block content.



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

def self.let(name, &block)
  private define_method(name, &block)
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.



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

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.send(method_name, *args, **kwargs)
    super() + [challenge]
  end

  klass.instance_eval(&block)
  klass
end

.with(**kwargs, &block) ⇒ 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.

  • block (Proc)

    The block to define the specs.



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

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