lib/take/unit/generator.rb in take-0.0.7 vs lib/take/unit/generator.rb in take-0.0.8

- old
+ new

@@ -4,10 +4,11 @@ def initialize(parent, options) @parent = parent @groups = [@parent] @options = options + @group_functions = [] end def generate @output ||= begin @output = "" @@ -46,47 +47,100 @@ 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) } + @group_functions << "group_#{group_name}" + + @output << <<-CODE +// group #{group_name} +void group_#{group_name}() +{ + + output(TEXT_COLOR_MAGENTA "\\tSET \\"" TEXT_COLOR_BOLD_MAGENTA + "#{group_name}" TEXT_COLOR_MAGENTA "\\":\\n"); +CODE @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 def walk_test(node) - @output << "void test_#{group_name(node)}()\n{\n" + @output << <<-CODE +void test_#{group_name(node)}() +{ + tests++; + int test_success = 1; + output(TEXT_COLOR_MAGENTA "\\t\\tTEST \\"" TEXT_COLOR_BOLD_MAGENTA + "#{node.name}" TEXT_COLOR_MAGENTA "\\": "); + +CODE + befores.each { |child| walk(child) } node.children.select(&:block?).each { |child| walk(child) } - @output << "}\n\n" + afters.each { |child| walk(child) } + @output << <<-CODE + + if(test_success) + { +#ifdef VERBOSE + output("\\n\\t\\t\\t" TEXT_COLOR_GREEN "OK\\n"); +#else + output(TEXT_COLOR_BOLD_GREEN "OK\\n"); +#endif + } +} +CODE end def walk_block(node, indent = 2) - @output << "#line \"#{node.source.file}\" #{node.source.line + 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]+$/, "") + 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() +{ + output(TEXT_COLOR_MAGENTA "FILE \\"" TEXT_COLOR_BOLD_MAGENTA + __FILE TEXT_COLOR_MAGENTA "\\":\\n"); + +CODE + + @group_functions.each { |func| @output << " #{func}();\n" } + + @output << <<-CODE + output2("\\n\\t" TEXT_COLOR_MAGENTA "RESULT:\\n\\t\\t" + "PASSED: " TEXT_COLOR_BOLD_MAGENTA "%zu" + TEXT_COLOR_MAGENTA "\\n\\t\\tFAILED: " + TEXT_COLOR_BOLD_MAGENTA "%zu" TEXT_COLOR_MAGENTA + "\\n", tests - failed, failed); + + return TEST_RETURN; +} + +CODE + end def walk_node(node) if node.name @output << "// #{node.class.name.gsub(/\A.*::/, "").downcase} #{node.name}\n" @@ -114,10 +168,10 @@ def line @output.count("\n") + 2 end def reset - @output << "#line \"#{@parent.name}.c\" #{line}\n" + @output << "#line #{line} \"#{@parent.name}.c\"\n" end def befores @groups.map(&:children).flatten.select(&:before?) end