Class: Take::Unit::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/take/unit/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent, options) ⇒ Generator

Returns a new instance of Generator.



5
6
7
8
9
# File 'lib/take/unit/generator.rb', line 5

def initialize(parent, options)
  @parent  = parent
  @groups  = [@parent]
  @options = options
end

Instance Method Details

#generateObject



11
12
13
14
15
16
17
# File 'lib/take/unit/generator.rb', line 11

def generate
  @output ||= begin
    @output = ""
    walk(@parent)
    @output
  end
end

#walk(node, *opts) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/take/unit/generator.rb', line 25

def walk(node, *opts)
  case
  when node.group?
    walk_group(node, *opts)
  when node.test?
    walk_test(node, *opts)
  when node.block?
    walk_block(node, *opts)
  when node.parent?
    walk_parent(node, *opts)
  when node.before?
    walk_before(node, *opts)
  when node.after?
    walk_after(node, *opts)
  when node.prefix?
    walk_prefix(node, *opts)
  else
    raise ArgumentError, "Invalid argument given: #{node.class} (#{node})"
  end
end

#walk_block(node, indent = 2) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/take/unit/generator.rb', line 69

def walk_block(node, indent = 2)
  @output << "#line \"#{node.source.file}\" #{node.source.line + 2}"
  lines = node.body.each_line
  deindent = if scan = node.body.scan(/^[ \t]*(?=\S)/).min
    scan.size
  else
    0
  end

  body = lines.map { |l| l.gsub(/^[ \t]{#{deindent}}/, "") }.
    map { |l| (" " * indent) << l }.join("").rstrip.gsub(/^[ \t]+$/, "")

  @output << body << "\n"
  reset
end

#walk_group(node) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/take/unit/generator.rb', line 46

def walk_group(node)
  @groups << node
  group.children.select(&:test?).
    each { |child| walk(child) }

  @output << "// group #{group_name}\n"
  @output << "void group_#{group_name}()\n{\n"
  befores.each { |child| walk(child) }
  @output << group.children.select(&:test?).
    map { |child| "  test_#{group_name(child)}();" }.
    join("\n")
  @output << "\n"
  afters.each { |child| walk(child) }
  @output << "}\n\n"
  @groups.pop
end

#walk_node(node) ⇒ Object Also known as: walk_before, walk_after



90
91
92
93
94
95
96
97
# File 'lib/take/unit/generator.rb', line 90

def walk_node(node)
  if node.name
    @output << "// #{node.class.name.gsub(/\A.*::/, "").downcase} #{node.name}\n"
  end

  node.children.each { |child| walk(child) }
  @output << "\n"
end

#walk_parent(node) ⇒ Object



85
86
87
88
# File 'lib/take/unit/generator.rb', line 85

def walk_parent(node)
  node.children.select(&:prefix?).each { |child| walk(child) }
  node.children.reject(&:prefix?).each { |child| walk(child) }
end

#walk_prefix(node) ⇒ Object



99
100
101
102
103
# File 'lib/take/unit/generator.rb', line 99

def walk_prefix(node)
  @output << "// prefix #{node.name}\n"
  node.children.select(&:block?).each { |child| walk(child, 0) }
  @output << "\n"
end

#walk_test(node) ⇒ Object



63
64
65
66
67
# File 'lib/take/unit/generator.rb', line 63

def walk_test(node)
  @output << "void test_#{group_name(node)}()\n{\n"
  node.children.select(&:block?).each { |child| walk(child) }
  @output << "}\n\n"
end

#write_fileObject



19
20
21
22
23
# File 'lib/take/unit/generator.rb', line 19

def write_file
  File.open("#{@parent.name}.c", "w") do |f|
    f.write generate
  end
end