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.



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

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

Instance Attribute Details

#bindingObject



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

def binding
  @binding ||= TOPLEVEL_BINDING.dup
end

#expectedObject (readonly)

Returns the value of attribute expected.



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

def expected
  @expected
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Class Method Details

.format_exception(exception) ⇒ Object



42
43
44
# File 'lib/erudite/example.rb', line 42

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

.pattern(string) ⇒ Object



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

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

.without_stdioObject



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

def self.without_stdio
  stdin, stdout, stderr, argv = $stdin, $stdout, $stderr, $ARGV.dup
  io = $stdin = $stdout = $stderr = StringIO.new
  $ARGV.clear
  [yield, io]
ensure
  $stdin, $stdout, $stderr = stdin, stdout, stderr
  $ARGV.replace(argv)
end

Instance Method Details

#==(other) ⇒ Object



77
78
79
80
# File 'lib/erudite/example.rb', line 77

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

#actualObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/erudite/example.rb', line 46

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)


72
73
74
75
# File 'lib/erudite/example.rb', line 72

def pass?
  actual
  valid_result? && valid_output?
end

#runObject



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

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

#run!Object



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

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

#valid_output?Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/erudite/example.rb', line 67

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

#valid_result?Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/erudite/example.rb', line 62

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