Class: Lookout::Expectations

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lookout-3.0/expectations.rb

Overview

Represents the “Expectations” keyword in expectation files and the expectation files themselves. Used by Interfaces::Commandline and should be used by other interfaces as the main access point to expectation files. Implemented as an Enumerable over the expect blocks found in any of the expectations blocks in the expectation file.

Defined Under Namespace

Classes: Context

Constant Summary collapse

@@expectations =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.evaluate(path, &block) ⇒ Object



41
# File 'lib/lookout-3.0/expectations.rb', line 41

def evaluate(path, &block) @@expectations[path] << block; self end

.load(path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lookout-3.0/expectations.rb', line 18

def load(path)
  @@expectations[path] = []
  begin
    begin
      Kernel.load path, true
    rescue LoadError => e
      raise e,
        'cannot load expectations from file: %s' % e,
        [(Array(e.backtrace).find{ |b| b.start_with? path } or '%s:0' % path)] +
          Array(e.backtrace) unless e.message.end_with? path
      raise e,
        'cannot load expectations from file: no such file or directory',
        ['%s:0' % path]
    rescue SyntaxError => e
      raise unless matches = (/\A(.*?:\d+): (.*)/m.match(e.message) rescue nil)
      raise SyntaxError, matches[2], [matches[1]] + Array(e.backtrace)
    end
    @@expectations[path]
  ensure
    @@expectations.delete path
  end
end

Instance Method Details

# {|expect| ... } ⇒ Object #Enumerator<Expect::Object>

Overloads:

  • # {|expect| ... } ⇒ Object

    Enumerates the expect blocks.

    Yield Parameters:

    Raises:

    • (NoMemoryError, SignalException, SystemExit)

      If raised; all other exceptions are caught and turned into failing expect blocks, located at the source of the exception

  • #Enumerator<Expect::Object>

    Returns An Enumerator over the expect blocks.

    Returns:

    • (Enumerator<Expect::Object>)

      An Enumerator over the expect blocks



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lookout-3.0/expectations.rb', line 53

def each
  return enum_for(__method__) unless block_given?
  context = Lookout::Expectations::Context.new{ |expect| yield expect }
  self.class.load(path).each do |expectations|
    context.instance_eval(&expectations)
  end
  self
rescue NoMemoryError, SignalException, SystemExit
  raise
rescue Exception => e
  raise unless location = (Array(e.backtrace).first rescue nil)
  file, line = Lookout.location(location)
  raise unless file and line
  yield nil.to_lookout_expected.expect(file, line){ raise e }
  self
end