Class: Take::Unit::Generator
- Inherits:
-
Object
- Object
- Take::Unit::Generator
- Defined in:
- lib/take/unit/generator.rb
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(parent, options) ⇒ Generator
constructor
A new instance of Generator.
- #walk(node, *opts) ⇒ Object
- #walk_block(node, indent = 2) ⇒ Object
- #walk_group(node) ⇒ Object
- #walk_node(node) ⇒ Object (also: #walk_before, #walk_after)
- #walk_parent(node) ⇒ Object
- #walk_prefix(node) ⇒ Object
- #walk_test(node) ⇒ Object
- #write_file(output) ⇒ Object
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, ) @parent = parent @groups = [@parent] @options = end |
Instance Method Details
#generate ⇒ Object
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
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/take/unit/generator.rb', line 109 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 71 72 |
# 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}() { 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" group.children.select(&:group?). each { |child| @output << " group_#{group_name}_" \ "#{child.name}();\n" } @output << "}\n\n" @groups.pop end |
#walk_node(node) ⇒ Object Also known as: walk_before, walk_after
157 158 159 160 161 162 163 164 |
# File 'lib/take/unit/generator.rb', line 157 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
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/take/unit/generator.rb', line 129 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 node.children.select(&:group?). each { |child| @output << " group_#{child.name}();\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 |
#walk_prefix(node) ⇒ Object
166 167 168 169 170 |
# File 'lib/take/unit/generator.rb', line 166 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
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 101 102 103 104 105 106 107 |
# File 'lib/take/unit/generator.rb', line 74 def walk_test(node) @output << <<-CODE void test_#{group_name(node)}() { tests++; int test_skip = 0, 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) } afters.each { |child| walk(child) } @output << <<-CODE if(test_skip) { output(TEXT_COLOR_BOLD_YELLOW "PASS\\n"); } else if(test_success) { #ifdef VERBOSE output("\\n\\t\\t\\t" TEXT_COLOR_GREEN "OK\\n"); #else output(TEXT_COLOR_BOLD_GREEN "OK\\n"); #endif } else { failed++; } } 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 |