Sha256: b76474281c0b2fea457d57912150e3b70ccbd1472300599f515f66706e333ad6

Contents?: true

Size: 875 Bytes

Versions: 25

Compression:

Stored size: 875 Bytes

Contents

module Matest
  class EvalErr
    def initialize(str)
      @string = str
    end
    def size
      inspect.size
    end
    def to_s
      @string
    end
    def inspect
      @string
    end
  end

  class Evaluator
    def initialize(example, block)
      @example = example
      @block = block
    end

    def eval_string(exp_string)
      limit_length(eval_in_context(exp_string).inspect)
    rescue StandardError => ex
      EvalErr.new("#{ex.class}: #{ex.message}")
    end

    private

    MAX_INSPECT_SIZE = 2000

    def limit_length(string)
      if string.size > MAX_INSPECT_SIZE
        string[0..MAX_INSPECT_SIZE] + " (...truncated...)"
      else
        string
      end
    end

    def eval_in_context(exp_string)
      exp_proc = "proc { #{exp_string} }"
      blk = eval(exp_proc, @block.binding)
      @example.instance_eval(&blk)
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
matest-1.7.4 lib/matest/evaluator.rb
matest-1.7.3 lib/matest/evaluator.rb
matest-1.7.2 lib/matest/evaluator.rb
matest-1.7.1 lib/matest/evaluator.rb
matest-1.7.0 lib/matest/evaluator.rb
matest-1.6.9 lib/matest/evaluator.rb
matest-1.6.6 lib/matest/evaluator.rb
matest-1.6.5 lib/matest/evaluator.rb
matest-1.6.4 lib/matest/evaluator.rb
matest-1.6.3 lib/matest/evaluator.rb
matest-1.6.2 lib/matest/evaluator.rb
matest-1.6.1 lib/matest/evaluator.rb
matest-1.6.0 lib/matest/evaluator.rb
matest-1.5.6 lib/matest/evaluator.rb
matest-1.5.5 lib/matest/evaluator.rb
matest-1.5.4 lib/matest/evaluator.rb
matest-1.5.3 lib/matest/evaluator.rb
matest-1.5.2 lib/matest/evaluator.rb
matest-1.5.1 lib/matest/evaluator.rb
matest-1.5.0 lib/matest/evaluator.rb