Class: Stencil::ViewMatcher::AtPath::Expectation

Inherits:
Object
  • Object
show all
Defined in:
lib/stencil/spec/view_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, actual) ⇒ Expectation

Returns a new instance of Expectation.



82
83
84
85
86
# File 'lib/stencil/spec/view_matcher.rb', line 82

def initialize(path, actual)
  @path, @actual = path, actual
  @thumb = @actual
  @into = []
end

Instance Method Details

#deref_failObject



140
141
142
# File 'lib/stencil/spec/view_matcher.rb', line 140

def deref_fail
  ::Spec::Expectations.fail_with("Member: #{@thumb.class.name} at (#{@into[0..-2].inspect}) has no index #{@into.last.inspect}")
end

#dereferenceObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/stencil/spec/view_matcher.rb', line 121

def dereference
  @path.each do |segment|
    @into << segment
    case @thumb
    when Array
      unless Integer === segment
        deref_fail
      end
    when Hash
      unless @thumb.has_key?(segment)
        deref_fail
      end
    else
      deref_fail
    end
    @thumb = @thumb[segment] rescue deref_fail
  end
end

#should(matcher = nil, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/stencil/spec/view_matcher.rb', line 88

def should(matcher=nil, &block)
  dereference
  if matcher.nil?
    ::Spec::Matchers.last_should = :should
    ::Spec::Matchers.last_matcher = matcher
    return PositiveOperatorMatcher.new(@path, @thumb)
  end

  begin
    Spec::Expectations::PositiveExpectationHandler.handle_matcher(@thumb, matcher, &block)
  rescue ::Spec::Expectations::ExpectationNotMetError => enme
    message = "At #{@path.inspect}\n" + enme.message
    Kernel::raise(Spec::Expectations::ExpectationNotMetError.new(message))
  end
end

#should_not(matcher = nil, &block) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/stencil/spec/view_matcher.rb', line 104

def should_not(matcher=nil, &block)
  dereference

  if matcher.nil?
    ::Spec::Matchers.last_should = :should_not
    ::Spec::Matchers.last_matcher = matcher
    return NegativeOperatorMatcher.new(@path, @thumb) 
  end

  begin
    Spec::Expectations::NegativeExpectationHandler.handle_matcher(@thumb, matcher, &block)
  rescue ::Spec::Expectations::ExpectationNotMetError => enme
    message = "At #{@path.inspect}\n" + enme.message
    Kernel::raise(Spec::Expectations::ExpectationNotMetError.new(message))
  end
end