Sha256: 71187bdc66a26ae7b1f9a4a0e6e307a986eb2f3edca03f57439a23a23cd557cd

Contents?: true

Size: 1.19 KB

Versions: 9

Compression:

Stored size: 1.19 KB

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
    lineno = block.source_location.last

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

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

    code = valid_lines[1..-2].join

    return code unless code == ""

    valid_lines = [lines[lineno-1]].join
    code = Ripper::SexpBuilder.new(valid_lines).parse.last.last.last.last
    Sorcerer.source(code)
  rescue Sorcerer::Resource::NotSexpError => e
    "Matest::SkipMe.new"
  end

  def lines
    @lines ||= get_lines
  end

  def get_lines
    file = File.open(block.source_location.first)
    source = file.read
    source.each_line.to_a
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
matest-1.7.4 lib/matest/example_block.rb
matest-1.7.3 lib/matest/example_block.rb
matest-1.7.2 lib/matest/example_block.rb
matest-1.7.1 lib/matest/example_block.rb
matest-1.7.0 lib/matest/example_block.rb
matest-1.6.9 lib/matest/example_block.rb
matest-1.6.6 lib/matest/example_block.rb
matest-1.6.5 lib/matest/example_block.rb
matest-1.6.4 lib/matest/example_block.rb