Class: Linkage::Configuration::DSL::ExpectationWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/linkage/configuration.rb

Constant Summary collapse

VALID_OPERATORS =
[:==, :>, :<, :>=, :<=]
OPERATOR_OPPOSITES =
{
  :==   => :'!=',
  :>    => :<=,
  :<=   => :>,
  :<    => :>=,
  :>=   => :<
}

Instance Method Summary collapse

Constructor Details

#initialize(dsl, type, lhs, *args) ⇒ ExpectationWrapper

Returns a new instance of ExpectationWrapper.



35
36
37
38
39
# File 'lib/linkage/configuration.rb', line 35

def initialize(dsl, type, lhs, *args)
  @dsl = dsl
  @type = type
  @lhs = lhs
end

Instance Method Details

#compare_with(operator, rhs) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/linkage/configuration.rb', line 41

def compare_with(operator, rhs)
  # NOTE: lhs is always a DataWrapper

  if !rhs.is_a?(DataWrapper) || @lhs.static? || rhs.static? || @lhs.side == rhs.side
    @side = !@lhs.static? ? @lhs.side : rhs.side

    # If one of the objects in this comparison is a static function, we need to set the side
    # and the dataset based on the other object
    if rhs.is_a?(DataWrapper) && !rhs.static? && @lhs.is_a?(FunctionWrapper) && @lhs.static?
      @lhs.dataset = rhs.dataset
      @lhs.side = @side
    elsif @lhs.is_a?(DataWrapper) && !@lhs.static? && rhs.is_a?(FunctionWrapper) && rhs.static?
      rhs.dataset = @lhs.dataset
      rhs.side = @side
    end
  elsif rhs.is_a?(DataWrapper) && operator != :==
    # create an exhaustive expectation with the Compare comparator instead
    comparator = Comparators::Compare.new(@lhs.meta_object,
      MetaObject.new(operator.to_s), rhs.meta_object)

    score_range = Comparators::Compare.score_range
    threshold = @type == :must ? score_range.last : score_range.first

    expectation = Expectations::Exhaustive.new(comparator, threshold, :equal)
    @dsl.add_exhaustive_expectation(expectation)
    return self
  end

  exp_operator = @type == :must_not ? OPERATOR_OPPOSITES[operator] : operator

  rhs_meta_object = rhs.is_a?(DataWrapper) ? rhs.meta_object : MetaObject.new(rhs)
  @expectation = Expectations::Simple.create(@lhs.meta_object,
    rhs_meta_object, exp_operator)
  @dsl.add_simple_expectation(@expectation)
  self
end

#exactlyObject



84
85
86
87
88
# File 'lib/linkage/configuration.rb', line 84

def exactly
  if !@exact_match
    @expectation.exactly!
  end
end