Sha256: 23deab8d5091e7b7c2562b96b9dbc2b35e73df4f313d96e6cad68c972188849f

Contents?: true

Size: 943 Bytes

Versions: 6

Compression:

Stored size: 943 Bytes

Contents

require "ripper"
require "sorcerer"

class ExampleBlock
  attr_reader :block
  attr_reader :code
  attr_reader :sexp
  attr_reader :assertion
  attr_reader :assertion_sexp

  def initialize(block)
    @block = block

    @code = generate_code

    @sexp = Ripper::SexpBuilder.new(code).parse.last
    @assertion_sexp = @sexp.last
    @assertion = Sorcerer.source(assertion_sexp)
  end

  def call
    block.call
  end

  def source_location
    block.source_location
  end

  private

  def generate_code
    file = File.open(block.source_location.first)
    source = file.read
    lines = source.each_line.to_a

    lineno = block.source_location.last

    current_line = lineno-1
    valid_lines = [lines[current_line]]

    valid_lines

    until Ripper::SexpBuilder.new(valid_lines.join("\n")).parse
      current_line += 1
      valid_lines << lines[current_line]
    end
    code_array = valid_lines[1..-2]
    code_array.join
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
matest-1.4.1 lib/matest/example_block.rb
matest-1.4.0 lib/matest/example_block.rb
matest-1.3.2 lib/matest/example_block.rb
matest-1.3.1 lib/matest/example_block.rb
matest-1.3.0 lib/matest/example_block.rb
matest-1.2.0 lib/matest/example_block.rb