Class: SpecEvaluate

Inherits:
Object show all
Defined in:
lib/mspec/runner/evaluate.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ruby, desc) ⇒ SpecEvaluate

Returns a new instance of SpecEvaluate


10
11
12
13
# File 'lib/mspec/runner/evaluate.rb', line 10

def initialize(ruby, desc)
  @ruby = ruby
  @desc = desc || self.class.desc
end

Class Method Details

.descObject


6
7
8
# File 'lib/mspec/runner/evaluate.rb', line 6

def self.desc
  @desc ||= "evaluates "
end

.desc=(desc) ⇒ Object


2
3
4
# File 'lib/mspec/runner/evaluate.rb', line 2

def self.desc=(desc)
  @desc = desc
end

Instance Method Details

#define(&block) ⇒ Object


38
39
40
41
42
43
44
45
46
47
# File 'lib/mspec/runner/evaluate.rb', line 38

def define(&block)
  ruby = @ruby
  desc = @desc
  evaluator = self

  specify "#{desc} #{format ruby}" do
    evaluator.instance_eval(ruby)
    evaluator.instance_eval(&block)
  end
end

#format(ruby, newline = true) ⇒ Object

Formats the Ruby source code for reabable output in the -fs formatter option. If the source contains no newline characters, wraps the source in single quotes to set if off from the rest of the description string. If the source does contain newline characters, sets the indent level to four characters.


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mspec/runner/evaluate.rb', line 20

def format(ruby, newline=true)
  if /\n/ =~ ruby
    lines = ruby.rstrip.each_line.to_a
    if /( *)/ =~ lines.first
      if $1.size > 4
        dedent = $1.size - 4
        ruby = lines.map { |l| l[dedent..-1] }.join
      else
        indent = " " * (4 - $1.size)
        ruby = lines.map { |l| "#{indent}#{l}" }.join
      end
    end
    "\n#{ruby}"
  else
    "'#{ruby}'"
  end
end