class Erudite::Example
Code to be run and compared against its expected outcome.
Attributes
binding[W]
expected[R]
source[R]
Public Class Methods
format_exception(exception)
click to toggle source
# File lib/erudite/example.rb, line 42 def self.format_exception(exception) "#{exception.class.name}: #{exception.message}" end
new(source, result = nil, output = nil)
click to toggle source
# File lib/erudite/example.rb, line 13 def initialize(source, result = nil, output = nil) @source = source @expected = Outcome.new(result, output) end
pattern(string)
click to toggle source
# File lib/erudite/example.rb, line 58 def self.pattern(string) Regexp.new(Regexp.escape(string).gsub('\.\.\.', '.*?')) end
without_stdio() { || ... }
click to toggle source
# 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
Public Instance Methods
==(other)
click to toggle source
# File lib/erudite/example.rb, line 77 def ==(other) source == other.source && expected == other.expected end
actual()
click to toggle source
# 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
binding()
click to toggle source
# File lib/erudite/example.rb, line 18 def binding @binding ||= TOPLEVEL_BINDING.dup end
pass?()
click to toggle source
# File lib/erudite/example.rb, line 72 def pass? actual valid_result? && valid_output? end
run()
click to toggle source
# File lib/erudite/example.rb, line 26 def run [run!, nil] rescue Exception => exception # rubocop:disable Lint/RescueException [nil, exception] end
run!()
click to toggle source
# File lib/erudite/example.rb, line 22 def run! binding.eval(source, __FILE__, __LINE__) end
valid_output?()
click to toggle source
# 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?()
click to toggle source
# File lib/erudite/example.rb, line 62 def valid_result? return true unless expected.result self.class.pattern(expected.result).match(actual.result) end