module Take class Unit class Generator def initialize(parent, options) @parent = parent @groups = [@parent] @options = options end def generate @output ||= begin @output = "" walk(@parent) @output end end def write_file(output) File.open(output, "w") do |f| f.write generate end end 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 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 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 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 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 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 def walk_prefix(node) @output << "// prefix #{node.name}\n" node.children.select(&:block?).each { |child| walk(child, 0) } @output << "\n" end alias_method :walk_before, :walk_node alias_method :walk_after, :walk_node private def group @groups.last end def line @output.count("\n") + 2 end def reset @output << "#line #{line} \"#{@parent.name}.c\"\n" end def befores @groups.map(&:children).flatten.select(&:before?) end def afters @groups.map(&:children).flatten.select(&:after?) end def group_name(test = nil) [*@groups[1..-1], test].compact.map(&:name). map { |n| n.gsub(/[\W]/, "_") }.join("_") end end end end