Class: Erudite::Example
- Inherits:
-
Object
- Object
- Erudite::Example
- Defined in:
- lib/erudite/example.rb
Overview
Code to be run and compared against its expected outcome.
Instance Attribute Summary collapse
- #binding ⇒ Object
-
#expected ⇒ Object
readonly
Returns the value of attribute expected.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #actual ⇒ Object
-
#initialize(source, result = nil, output = nil) ⇒ Example
constructor
A new instance of Example.
- #pass? ⇒ Boolean
- #run ⇒ Object
- #run! ⇒ Object
- #valid_output? ⇒ Boolean
- #valid_result? ⇒ Boolean
Constructor Details
Instance Attribute Details
#binding ⇒ Object
18 19 20 |
# File 'lib/erudite/example.rb', line 18 def binding @binding ||= TOPLEVEL_BINDING.dup end |
#expected ⇒ Object (readonly)
Returns the value of attribute expected.
10 11 12 |
# File 'lib/erudite/example.rb', line 10 def expected @expected end |
#source ⇒ Object (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.}" 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_stdio ⇒ Object
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 |
#actual ⇒ Object
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
72 73 74 75 |
# File 'lib/erudite/example.rb', line 72 def pass? actual valid_result? && valid_output? end |
#run ⇒ Object
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
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
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 |