class Take::Unit::Generator
Public Class Methods
new(parent, options)
click to toggle source
# File lib/take/unit/generator.rb, line 5 def initialize(parent, options) @parent = parent @groups = [@parent] @options = options end
Public Instance Methods
generate()
click to toggle source
# File lib/take/unit/generator.rb, line 11 def generate @output ||= begin @output = "" walk(@parent) @output end end
walk(node, *opts)
click to toggle source
# 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)
click to toggle source
# File lib/take/unit/generator.rb, line 89 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)
click to toggle source
# 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)
click to toggle source
# File lib/take/unit/generator.rb, line 132 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
Also aliased as: walk_before, walk_after
walk_parent(node)
click to toggle source
# File lib/take/unit/generator.rb, line 109 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_start(); CODE node.children.select(&:group?). each { |child| @output << " group_#{child.name}();\n" } @output << <<-CODE _uassert_stop(); return TEST_RETURN; } CODE end
walk_prefix(node)
click to toggle source
# File lib/take/unit/generator.rb, line 141 def walk_prefix(node) @output << "// prefix #{node.name}\n" node.children.select(&:block?).each { |child| walk(child, 0) } @output << "\n" end
walk_test(node)
click to toggle source
# 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 _uassert_test_stop(); } CODE end
write_file(output)
click to toggle source
# File lib/take/unit/generator.rb, line 19 def write_file(output) File.open(output, "w") do |f| f.write generate end end
Private Instance Methods
afters()
click to toggle source
# File lib/take/unit/generator.rb, line 168 def afters @groups.map(&:children).flatten.select(&:after?) end
befores()
click to toggle source
# File lib/take/unit/generator.rb, line 164 def befores @groups.map(&:children).flatten.select(&:before?) end
group()
click to toggle source
# File lib/take/unit/generator.rb, line 152 def group @groups.last end
group_name(test = nil)
click to toggle source
# File lib/take/unit/generator.rb, line 172 def group_name(test = nil) [*@groups[1..-1], test].compact.map(&:name). map { |n| n.gsub(/[\W]/, "_") }.join("_") end
line()
click to toggle source
# File lib/take/unit/generator.rb, line 156 def line @output.count("\n") + 2 end
reset()
click to toggle source
# File lib/take/unit/generator.rb, line 160 def reset @output << "#line #{line} \"#{@parent.name}.c\"\n" end