Class: SpecMe::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/spec_me/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/spec_me/renderer.rb', line 3

def config
  @config
end

Instance Method Details

#code(ast) ⇒ Object



24
25
26
# File 'lib/spec_me/renderer.rb', line 24

def code(ast)
  ast.map{ |node| render_node node }.flatten
end

#description(file_name:, count: 1, **params) ⇒ Object



28
29
30
31
32
# File 'lib/spec_me/renderer.rb', line 28

def description( file_name:, count: 1, **params)
  example_group_name = params.fetch :example_group_name, "#{file_name} #{count}"
  rspec_tag = params.fetch :rspec_tag, config.spec_me_tag
  [%{describe '#{example_group_name}', :#{rspec_tag} do}] #.tap{|r| p description: r}
end

#end_lines(n, indent = 0) ⇒ Object



34
35
36
37
38
# File 'lib/spec_me/renderer.rb', line 34

def end_lines n, indent=0
  n.times.map{ |i|
    '  ' * (n-i.succ+indent) + 'end'
  }
end

#example(count:) ⇒ Object



40
41
42
# File 'lib/spec_me/renderer.rb', line 40

def example( count:)
  [%{  it 'example #{count}' do}] 
end

#render(ast, file_name:, count: 1, spec_helper: nil, **params) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/spec_me/renderer.rb', line 5

def render( ast, file_name:, count:1, spec_helper: nil, **params)
  (
    spec_helper_lines(spec_helper) +
    description( file_name: file_name, count: count, **params ) +
    ast.each_with_index.map do | block_node, idx  |
      render_one( block_node, count: count + idx, **params )
    end +
    end_lines( 1 )
  ).flatten
end

#render_node(node) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/spec_me/renderer.rb', line 44

def render_node node
  case node.first
  when :comment
    %{    # #{node.last}}
  when :ruby
    %{    #{node.last}}
  when :equality_xp
    [%{    expect( #{node[1]} ).to eq(},
     %{      #{node.last}},
       %{    )}]
  end
end

#render_one(block_node, count: 1, **params) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/spec_me/renderer.rb', line 16

def render_one( block_node, count:1, **params)
  (
    example( count: count ) +
    code(block_node) +
    end_lines(1, 1)
  )# .tap{ |r| puts r }
end

#spec_helper_lines(spec_helper) ⇒ Object



56
57
58
59
# File 'lib/spec_me/renderer.rb', line 56

def spec_helper_lines spec_helper
  spec_helper ||= config.spec_me_helper
  [ %{require '#{spec_helper}'}, '' ]
end