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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/take/unit/generator.rb', line 102

def walk_block(node, indent = 2)
  if node.source
    @output << "#line #{node.source.line + 2}" \
      " \"#{node.source.file}\""
  end
  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
62
63
64
65
66
67
68
69
70
# File 'lib/take/unit/generator.rb', line 46

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

  group.children.select(&:group?).each { |child| walk(child) }

  @output << <<-CODE
// group #{group_name}
void group_#{group_name}()
{
  _uassert_group_start("#{group_name}");
CODE
  @output << group.children.select(&:test?).
    map { |child| "  test_#{group_name(child)}();" }.
    join("\n")
  @output << "\n"
  group.children.select(&:group?).
    each { |child| @output << "  group_#{group_name}_" \
    "#{child.name}();\n" }
  @output << "  _uassert_group_stop();\n}\n\n"


  @groups.pop
end

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



145
146
147
148
149
150
151
152
# File 'lib/take/unit/generator.rb', line 145

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/take/unit/generator.rb', line 122

def walk_parent(node)
  node.children.select(&:prefix?).each { |child| walk(child) }
  node.children.reject(&:prefix?).each { |child| walk(child) }
  @output << <<-CODE
int main()
{
  _uassert_begin();

CODE

  node.children.select(&:group?).
    each { |child| @output << "  group_#{child.name}();\n" }

  @output << <<-CODE
  _uassert_stop();

  return TEST_RETURN;
}

CODE

end

#walk_prefix(node) ⇒ Object



154
155
156
157
158
# File 'lib/take/unit/generator.rb', line 154

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/take/unit/generator.rb', line 72

def walk_test(node)
  @output << <<-CODE
void test_#{group_name(node)}()
{
  _uassert_test_start("#{node.name}");

CODE
  befores.each { |child| walk(child) }
  node.children.select(&:block?).each { |child| walk(child) }
  afters.each { |child| walk(child) }
  @output << <<-CODE

  if(test_skip)
  {
    _uassert_test_skip();
  }
  else if(test_success)
  {
    _uassert_test_success();
  }
  else
  {
    _uassert_test_fail();
  }

  _uassert_test_end();
}
CODE
end

#write_file(output) ⇒ Object



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

def write_file(output)
  File.open(output, "w") do |f|
    f.write generate
  end
end