Class: YardExampleTest::Example::Evaluator

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

Overview

Manages binding creation and code evaluation for example expressions

Each Evaluator is constructed with a fallback binding (whose +self+ is the +Minitest::Spec+ instance, providing access to any methods included on YardExampleTest::Example such as +RSpec::Matchers+) and a snapshot of instance variables from the spec instance (set by +before+ hooks). These are used to build per-scope evaluation contexts that mirror the documented class's namespace.

Instance Method Summary collapse

Constructor Details

#initialize(fallback_binding:, instance_variables:) ⇒ Evaluator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new evaluator

Examples:

Evaluator.new(fallback_binding: binding, instance_variables: {})


31
32
33
34
# File 'lib/yard_example_test/example/evaluator.rb', line 31

def initialize(fallback_binding:, instance_variables:)
  @fallback_binding = fallback_binding
  @instance_variables = instance_variables
end

Instance Method Details

#evaluate(code, bind) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Evaluates a Ruby code string in the given scope

Examples:

evaluator.evaluate('1 + 1', nil) # => 2

Raises:

  • (StandardError)

    any error raised during evaluation propagates



51
52
53
# File 'lib/yard_example_test/example/evaluator.rb', line 51

def evaluate(code, bind)
  context(bind).eval(code)
end

#evaluate_with_assertion(code, bind) ⇒ Object, StandardError

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Evaluates a Ruby code string, capturing any +StandardError+ as a value

If evaluation raises a +StandardError+, the error itself is returned instead of propagating. This allows callers to compare raised errors against expected error values.

Examples:

evaluator.evaluate_with_assertion('raise "oops"', nil) # => RuntimeError


71
72
73
74
75
# File 'lib/yard_example_test/example/evaluator.rb', line 71

def evaluate_with_assertion(code, bind)
  evaluate(code, bind)
rescue StandardError => e
  e
end