Class: TestCaseGenerator::GeneratorPython
- Inherits:
-
Object
- Object
- TestCaseGenerator::GeneratorPython
- Defined in:
- lib/test_case_generator/generator_python.rb
Instance Method Summary collapse
- #can_handle?(source_fn) ⇒ Boolean
- #make_class_name(filename) ⇒ Object
- #write(ctx, source_fn) ⇒ Object
- #write_skeleton(dsl_context, source_fn) ⇒ Object
- #write_source(dsl_context, source_fn) ⇒ Object
Instance Method Details
#can_handle?(source_fn) ⇒ Boolean
6 7 8 |
# File 'lib/test_case_generator/generator_python.rb', line 6 def can_handle?(source_fn) File.extname(source_fn).eql? '.py' end |
#make_class_name(filename) ⇒ Object
15 16 17 |
# File 'lib/test_case_generator/generator_python.rb', line 15 def make_class_name(filename) File.basename filename, '.*' end |
#write(ctx, source_fn) ⇒ Object
10 11 12 13 |
# File 'lib/test_case_generator/generator_python.rb', line 10 def write(ctx, source_fn) write_skeleton ctx, source_fn unless File.exist? source_fn write_source ctx, source_fn end |
#write_skeleton(dsl_context, source_fn) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 73 74 75 76 77 78 |
# File 'lib/test_case_generator/generator_python.rb', line 19 def write_skeleton(dsl_context, source_fn) class_name = dsl_context.class_name || make_class_name(source_fn) File.open(source_fn, 'w') do |f| writer = IndentedWriter.new f writer.puts <<EOS #!/usr/bin/env python # -*- coding: utf-8 -*- import os import sys import unittest # from assets.stub_android import stub_api_model # from assets.stub_android import stub_android def print_patterns(patterns): def wrapper(fn): def _(self): print print "<<TEST>> %s" % (", ".join(patterns)) fn(self) return _ return wrapper def run_pending_tasks(fn): def _(self): fn(self) while len(self.pending_tasks) > 0: # http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list-in-python # Use instead of a weird syntax new_list = old_list[:] pending_tasks = list(self.pending_tasks) self.pending_tasks[:] = [] for task in pending_tasks: task() # Maybe added into self.pending_tasks return _ class #{class_name}(unittest.TestCase): def setUp(self): super(#{class_name}, self).setUp() self.pending_tasks = [] self.timer_tasks = [] def tearDown(self): super(#{class_name}, self).tearDown() def _run_timer_tasks(self): timer_tasks = list(self.timer_tasks) self.timer_tasks[:] = [] for task in timer_tasks: task() # Maybe added into self.timer_tasks # %% EOS end end |
#write_source(dsl_context, source_fn) ⇒ Object
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/test_case_generator/generator_python.rb', line 80 def write_source(dsl_context, source_fn) class_name = dsl_context.class_name || make_class_name(source_fn) tmp_fn = source_fn + '.tmp' source = File.open(source_fn).read File.open(tmp_fn, 'w') do |f| source.each_line do |line| f.puts line break if line =~ /^\s*#\s*%%\s*$/ end writer = IndentedWriter.new f writer.blank writer.block_indent ' ' do writer.puts '#' writer.puts '# 以下の行は自動生成されているので直接編集しないでください。' writer.puts '#' writer.puts '# Generated by Test Case Generator' writer.puts '# https://rubygems.org/gems/test_case_generator' writer.puts '#' end dsl_context.each do |pattern| method_name = pattern.join '_' writer.block_indent ' ' do writer.blank writer.puts "@print_patterns([#{pattern.map{|p| "'#{p}'"}.join(', ')}])" writer.puts "def test_#{method_name}(self):" pattern.each do |ptn| writer.block_indent ' ' do writer.puts "self.#{ptn}()" end end end end method_list = dsl_context.labels.map { |m| "'#{m}'" }.join(', ') writer.blank writer.puts <<EOS @classmethod def checkSanity(cls): sane = True msg = [] for method in [#{method_list}]: if not hasattr(cls, method): msg += [ ' def %s(self):' % method, ' pass', '', ] sane = False if not sane: print cls.__name__ + ' must implement following method(s):' print print "\\n".join(msg) raise SystemExit(1) if __name__ == '__main__': #{class_name}.checkSanity() unittest.main() EOS end FileUtils.move tmp_fn, source_fn end |