Class: TestCaseGenerator::GeneratorPython

Inherits:
Object
  • Object
show all
Defined in:
lib/test_case_generator/generator_python.rb

Instance Method Summary collapse

Instance Method Details

#can_handle?(source_fn) ⇒ Boolean

Returns:

  • (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

#write(ctx, source_fn) ⇒ Object



10
11
12
# File 'lib/test_case_generator/generator_python.rb', line 10

def write(ctx, source_fn)
  write_source ctx, source_fn
end

#write_source(dsl_context, source_fn) ⇒ Object



14
15
16
17
18
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
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/test_case_generator/generator_python.rb', line 14

def write_source(dsl_context, 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 "def test_#{method_name}(self):"

        pattern.each do |ptn|
          writer.block_indent '    ' do
            writer.puts "self.#{ptn}()"
          end
        end
      end
    end

    writer.blank
    writer.block_indent '    ' do
      writer.puts '@classmethod'
      writer.puts 'def checkSanity(cls):'
      writer.block_indent '    ' do
        writer.puts 'sane = True'
        writer.puts 'msg = []'
        writer.puts "for method in [#{dsl_context.labels.map { |m| "'#{m}'" }.join(', ')}]:"
        writer.block_indent '    ' do
          writer.puts 'if not hasattr(cls, method):'
          writer.block_indent '    ' do
            writer.puts 'msg += ['
            writer.block_indent '    ' do
              writer.puts "'    def %s(self):' % method,"
              writer.puts "'        pass',"
              writer.puts "'',"
            end
            writer.puts ']'
            writer.puts 'sane = False'
          end
        end

        writer.blank
        writer.puts 'if not sane:'
        writer.block_indent '    ' do
          writer.puts "print cls.__name__ + ' must implement following method(s):'"
          writer.puts 'print'
          writer.puts "print \"\\n\".join(msg)"
          writer.puts 'raise SystemExit(1)'
        end
      end
    end

    writer.blank
    writer.blank
    writer.puts "if __name__ == '__main__':"
    writer.block_indent '    ' do
      writer.puts 'CommandLineArgumentsTestCase.checkSanity()'
      writer.puts 'unittest.main()'
    end
  end

  FileUtils.move tmp_fn, source_fn
end