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
17 18 19 |
# File 'lib/erudite/example.rb', line 17 def binding @binding ||= TOPLEVEL_BINDING.dup end |
#expected ⇒ Object (readonly)
Returns the value of attribute expected.
9 10 11 |
# File 'lib/erudite/example.rb', line 9 def expected @expected end |
#source ⇒ Object (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.}" 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_stdio ⇒ Object
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 |
#actual ⇒ Object
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
69 70 71 72 |
# File 'lib/erudite/example.rb', line 69 def pass? actual valid_result? && valid_output? end |
#run ⇒ Object
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
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
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 |