Skip to content

Commit

Permalink
Can override callbacks to WrapExpressions
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshCheek committed Sep 15, 2014
1 parent 4c75ac4 commit 3ed8083
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
59 changes: 30 additions & 29 deletions lib/seeing_is_believing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,35 @@ def self.call(*args)
end

def initialize(program, options={})
@program = program
@matrix_filename = options.fetch :matrix_filename, 'seeing_is_believing/the_matrix' # how to hijack the env
@filename = options[:filename]
@stdin = to_stream options.fetch(:stdin, '')
@require = options.fetch :require, []
@load_path = options.fetch :load_path, []
@encoding = options.fetch :encoding, nil
@timeout = options[:timeout]
@debugger = options.fetch :debugger, Debugger.new(stream: nil)
@ruby_executable = options.fetch :ruby_executable, 'ruby'
@number_of_captures = options.fetch :number_of_captures, Float::INFINITY
@program = program
@matrix_filename = options.fetch :matrix_filename, 'seeing_is_believing/the_matrix' # how to hijack the env
@filename = options[:filename]
@stdin = to_stream options.fetch(:stdin, '')
@require = options.fetch :require, []
@load_path = options.fetch :load_path, []
@encoding = options.fetch :encoding, nil
@timeout = options[:timeout]
@debugger = options.fetch :debugger, Debugger.new(stream: nil)
@ruby_executable = options.fetch :ruby_executable, 'ruby'
@number_of_captures = options.fetch :number_of_captures, Float::INFINITY

# TODO: would be nice to have before_all and after_all be callbacks, too
@wrap_expressions_callbacks = {}
@wrap_expressions_callbacks[:before_all] = options.fetch :before_all, "begin; $SiB.max_line_captures = #{number_of_captures_as_str}; require 'pp'; "
@wrap_expressions_callbacks[:after_all] = options.fetch :after_all, ";rescue Exception;"\
"lambda {"\
"line_number = $!.backtrace.grep(/\#{__FILE__}/).first[/:\\d+/][1..-1].to_i;"\
"$SiB.record_exception line_number, $!;"\
"$SiB.exitstatus = 1;"\
"$SiB.exitstatus = $!.status if $!.kind_of? SystemExit;"\
"}.call;"\
"end"
@wrap_expressions_callbacks[:before_each] = options.fetch :before_each, -> line_number { "(" }
@wrap_expressions_callbacks[:after_each] = options.fetch :after_each, -> line_number { ").tap { |value| "\
"$SiB.record_result(:inspect, #{line_number}, value);"\
"$SiB.record_result(:pp, #{line_number}, value) { PP.pp value, '' }"\
"}"
}
end

def call
Expand All @@ -48,25 +66,8 @@ def to_stream(string_or_stream)
StringIO.new string_or_stream
end

# TODO: would be nice to have before_all and after_all be callbacks, too
def program_that_will_record_expressions
WrapExpressions.call "#{@program}\n",
before_all: "begin; $SiB.max_line_captures = #{number_of_captures_as_str}; require 'pp'; ",
after_all: ";rescue Exception;"\
"lambda {"\
"line_number = $!.backtrace.grep(/\#{__FILE__}/).first[/:\\d+/][1..-1].to_i;"\
"$SiB.record_exception line_number, $!;"\
"$SiB.exitstatus = 1;"\
"$SiB.exitstatus = $!.status if $!.kind_of? SystemExit;"\
"}.call;"\
"end",
before_each: -> line_number { "(" },
after_each: -> line_number {
").tap { |value| "\
"$SiB.record_result(:inspect, #{line_number}, value);"\
"$SiB.record_result(:pp, #{line_number}, value) { PP.pp value, '' }"\
"}"
}
WrapExpressions.call "#{@program}\n", @wrap_expressions_callbacks
end

def result_for(program)
Expand Down
9 changes: 9 additions & 0 deletions spec/seeing_is_believing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ def values_for(input, options={})
expect(invoke(input)[1]).to eq ['"NUM"']
end

it 'allows uers to override WrapExpressions initialization keys' do
expect(invoke(':body', before_all: 'if true;', after_all: ';end;')[1]).to eq [':body']
expect(invoke(':body', before_all: 'if false;', after_all: ';end;')[1]).to eq []

result = invoke ':body', before_each: -> line_number { "$SiB.record_result(:inspect, #{line_number*200}, (" },
after_each: -> line_number { ").to_s.upcase + #{line_number}.inspect)" }
expect(result[200, :inspect]).to eq ['"BODY1"']
end

it 'remembers context of previous lines' do
expect(values_for("a=12\na*2")).to eq [['12'], ['24']]
end
Expand Down

0 comments on commit 3ed8083

Please sign in to comment.