Class: DRG::Spec

Inherits:
Ruby::Const show all
Defined in:
lib/drg/spec.rb

Constant Summary

Constants inherited from Ruby::Const

Ruby::Const::CONSTANT_DEFS

Instance Attribute Summary collapse

Attributes inherited from Ruby::Const

#sexp

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Ruby::Const

#class?, #class_vars, #func_by_name, #funcs, #initialization_args, #instance_vars, #module?, #name, #type

Constructor Details

#initialize(file) ⇒ Spec

Returns a new instance of Spec.



33
34
35
36
37
# File 'lib/drg/spec.rb', line 33

def initialize(file)
  @file = file
  @ruby = DRG::Ruby.new(file)
  super @ruby.const
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



31
32
33
# File 'lib/drg/spec.rb', line 31

def file
  @file
end

#rubyObject (readonly)

Returns the value of attribute ruby.



31
32
33
# File 'lib/drg/spec.rb', line 31

def ruby
  @ruby
end

Class Method Details

.generate(file) ⇒ Object

Class

generate a rspec file based on existing code



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/drg/spec.rb', line 5

def self.generate(file)
  spec = DRG::Spec.new(file)
  return if spec.funcs.empty? # nothing to do
  lines = [%Q(require "spec_helper"), %Q(), %Q(describe #{spec.const} do)]
  if spec.class?
    spec.initialization_args.each do |arg|
      lines << %Q(  let(:#{arg}) {})
    end
    lines << %Q()
    lines << %Q(  subject { described_class.new #{spec.initialization_args.join(', ')} })
  elsif spec.module?
    lines << %Q(  subject { Class.new { include #{spec.const} }.new })
  end

  lines << %Q()
  spec.funcs.reject(&:private?).each do |func|
    lines << %Q(  describe #{spec.quote("#{func.class? ? '.' : '#'}#{func.name}")} do)
    func.conditions.each do |condition|
      lines.concat spec.collect_contexts(condition, '    ')
    end
    lines << %Q(  end) << %Q()
  end
  lines << %Q(end) << %Q()
  lines
end

Instance Method Details

#collect_contexts(condition, indent = '', contexts = []) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/drg/spec.rb', line 46

def collect_contexts(condition, indent = '', contexts = [])
  new_indent = indent + '  '
  contexts << %Q(#{indent}context #{quote(tr(condition.short_statement))} do) << %Q(#{new_indent}before {})
  unless condition.return_value.empty?
    contexts << %Q(#{new_indent}it #{quote(condition.return_value)} do) << %Q(#{new_indent}end)
  end
  if condition.nested_conditions.any?
    condition.nested_conditions.each { |nc| collect_contexts(nc, new_indent, contexts) }
  end
  contexts << %Q(#{indent}end) << %Q() # /context
  contexts << %Q(#{indent}context #{quote(tr(negate(condition.short_statement)))} do) << %Q(#{new_indent}before {})
  contexts << %Q(#{indent}end)
  contexts
end

#constObject



39
40
41
42
43
44
# File 'lib/drg/spec.rb', line 39

def const
  @const ||= begin
    require file
    Kernel.const_get(ruby.const.name)
  end
end

#negate(phrase) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/drg/spec.rb', line 70

def negate(phrase)
  if phrase[/^unless /]
    phrase.sub /^unless /, 'if '
  else
    "not #{phrase}"
  end
end

#quote(txt) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/drg/spec.rb', line 61

def quote(txt)
  txt.strip!
  if txt =~ /"/
    "%Q(#{txt})"
  else
    %Q("#{txt}")
  end
end

#tr(phrase) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/drg/spec.rb', line 78

def tr(phrase)
  phrase.sub! /^if /, 'when '
  phrase.sub! /^not if /, 'unless '
  phrase.sub! /^if not /, 'unless '
  phrase.sub! %r"then$", ''
  phrase
end