Class: Spectus::ExpectationTarget

Inherits:
BasicObject
Defined in:
lib/spectus/expectation_target.rb

Overview

Wraps the target of an expectation.

Examples:

this { actual value } # => ExpectationTarget wrapping the block

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&subject) ⇒ ExpectationTarget

Create a new expection target

Parameters:

  • subject (Proc)

    the value which is compared with the expected value.



15
16
17
18
# File 'lib/spectus/expectation_target.rb', line 15

def initialize(&subject)
  @subject    = subject
  @challenge  = Challenge.new(:call)
end

Instance Attribute Details

#challengeChallenge (readonly)

Returns The challenge to call on the subject.

Returns:

  • (Challenge)

    The challenge to call on the subject.



28
29
30
# File 'lib/spectus/expectation_target.rb', line 28

def challenge
  @challenge
end

#subjectBasicObject (readonly)

Returns The front object to be tested.

Returns:

  • (BasicObject)

    The front object to be tested.



23
24
25
# File 'lib/spectus/expectation_target.rb', line 23

def subject
  @subject
end

Instance Method Details

#MAY(definition) ⇒ Result::Fail, Result::Pass

This word, or the adjective “OPTIONAL”, mean that an item is truly optional. One vendor may choose to include the item because a particular marketplace requires it or because the vendor feels that it enhances the product while another vendor may omit the same item. An implementation which does not include a particular option MUST be prepared to interoperate with another implementation which does include the option, though perhaps with reduced functionality. In the same vein an implementation which does include a particular option MUST be prepared to interoperate with another implementation which does not include the option (except, of course, for the feature the option provides.)

Examples:

Optional definition

this { 'foo'.bar }.MAY Match: /^foo$/

Parameters:

  • definition (Array, Hash, Symbol)

Returns:



105
106
107
# File 'lib/spectus/expectation_target.rb', line 105

def MAY(definition)
  RequirementLevel::Low.new(definition, false, subject, challenge).result
end

#MUST(definition) ⇒ Result::Fail, Result::Pass

This word, or the terms “REQUIRED” or “SHALL”, mean that the definition is an absolute requirement of the specification.

Examples:

_Absolute requirement_ definition

this { 'foo'.upcase }.MUST Eql: 'FOO'

Parameters:

  • definition (Array, Hash, Symbol)

Returns:



39
40
41
# File 'lib/spectus/expectation_target.rb', line 39

def MUST(definition)
  RequirementLevel::High.new(definition, false, subject, challenge).result
end

#MUST_NOT(definition) ⇒ Result::Fail, Result::Pass

This phrase, or the phrase “SHALL NOT”, mean that the definition is an absolute prohibition of the specification.

Examples:

_Absolute prohibition_ definition

this { 'foo'.size }.MUST_NOT Equal: 42

Parameters:

  • definition (Array, Hash, Symbol)

Returns:



52
53
54
# File 'lib/spectus/expectation_target.rb', line 52

def MUST_NOT(definition)
  RequirementLevel::High.new(definition, true, subject, challenge).result
end

#SHOULD(definition) ⇒ Result::Fail, Result::Pass

This word, or the adjective “RECOMMENDED”, mean that there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course.

Examples:

Recommended definition

this { 'foo'.valid_encoding? }.SHOULD Equal: true

Parameters:

  • definition (Array, Hash, Symbol)

Returns:



67
68
69
# File 'lib/spectus/expectation_target.rb', line 67

def SHOULD(definition)
  RequirementLevel::Medium.new(definition, false, subject, challenge).result
end

#SHOULD_NOT(definition) ⇒ Result::Fail, Result::Pass

This phrase, or the phrase “NOT RECOMMENDED” mean that there may exist valid reasons in particular circumstances when the particular behavior is acceptable or even useful, but the full implications should be understood and the case carefully weighed before implementing any behavior described with this label.

Examples:

_Not recommended_ definition

this { ''.blank? }.SHOULD_NOT RaiseException: NoMethodError

Parameters:

  • definition (Array, Hash, Symbol)

Returns:



83
84
85
# File 'lib/spectus/expectation_target.rb', line 83

def SHOULD_NOT(definition)
  RequirementLevel::Medium.new(definition, true, subject, challenge).result
end