Class: Erudite::Example

Inherits:
Object
  • Object
show all
Defined in:
lib/erudite/example.rb

Overview

Code to be run and compared against its expected outcome.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, result = nil, output = nil) ⇒ Example

Returns a new instance of Example.



12
13
14
15
# File 'lib/erudite/example.rb', line 12

def initialize(source, result = nil, output = nil)
  @source = source
  @expected = Outcome.new(result, output)
end

Instance Attribute Details

#bindingObject



17
18
19
# File 'lib/erudite/example.rb', line 17

def binding
  @binding ||= TOPLEVEL_BINDING.dup
end

#expectedObject (readonly)

Returns the value of attribute expected.



9
10
11
# File 'lib/erudite/example.rb', line 9

def expected
  @expected
end

#sourceObject (readonly)

Returns the value of attribute source.



8
9
10
# File 'lib/erudite/example.rb', line 8

def source
  @source
end

Class Method Details

.format_exception(exception) ⇒ Object



39
40
41
# File 'lib/erudite/example.rb', line 39

def self.format_exception(exception)
  "#{exception.class.name}: #{exception.message}"
end

.pattern(string) ⇒ Object



55
56
57
# File 'lib/erudite/example.rb', line 55

def self.pattern(string)
  Regexp.new(Regexp.escape(string).gsub('\.\.\.', '.*?'))
end

.without_stdioObject



31
32
33
34
35
36
37
# File 'lib/erudite/example.rb', line 31

def self.without_stdio
  stdin, stdout, stderr = $stdin, $stdout, $stderr
  $stdin = $stdout = $stderr = io = StringIO.new
  [yield, io]
ensure
  $stdin, $stdout, $stderr = stdin, stdout, stderr
end

Instance Method Details

#==(other) ⇒ Object



74
75
76
77
# File 'lib/erudite/example.rb', line 74

def ==(other)
  source == other.source &&
    expected == other.expected
end

#actualObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/erudite/example.rb', line 43

def actual
  return @actual if defined?(@actual)

  result, io = self.class.without_stdio do
    result, exception = run
    warn(self.class.format_exception(exception)) if exception
    result.inspect
  end

  @actual = Outcome.new(result, io.string)
end

#pass?Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/erudite/example.rb', line 69

def pass?
  actual
  valid_result? && valid_output?
end

#runObject



25
26
27
28
29
# File 'lib/erudite/example.rb', line 25

def run
  [run!, nil]
rescue Exception => exception # rubocop:disable Lint/RescueException
  [nil, exception]
end

#run!Object



21
22
23
# File 'lib/erudite/example.rb', line 21

def run!
  binding.eval(source, __FILE__, __LINE__)
end

#valid_output?Boolean

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/erudite/example.rb', line 64

def valid_output?
  return true unless expected.output
  self.class.pattern(expected.output).match(actual.output)
end

#valid_result?Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/erudite/example.rb', line 59

def valid_result?
  return true unless expected.result
  self.class.pattern(expected.result).match(actual.result)
end