test_case_generator 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8bd99d30932e8775eac157f3eb7142f12dd11c4a
4
- data.tar.gz: d0b84e08569ca7b155eeed76d25d12e1f5454d41
3
+ metadata.gz: 348c14e6a2d3ad48077a12777b9b7b63249809fa
4
+ data.tar.gz: af4523f2a76a2c1117a398c6c099cc0b58b01369
5
5
  SHA512:
6
- metadata.gz: 7c5ca5df5345806f5dc4698fa7a25ba6707d4287e7704c7bff70aa32bf2c273b4cfa8ec933f6a66d1305a782916e3d061c384bd31bb65e885abbaf0594a59065
7
- data.tar.gz: 6d07720070479490331372e3e5904efff1b55a7766ffb09999fa57d8143a0e7c4e8b84295134beaf0e1c7ff310da84dd4891f35a861ec2e0db62cd8a9ca78c08
6
+ metadata.gz: a125211db86819f2da05c418cc4b86eaca1f914b8bcac64f3508124f3ac06db9e9a2789b9fa248f2fb135bab44d160303a3a096221376e17dc3dfe5c1b99134c
7
+ data.tar.gz: 479757fbc7eda6e322abd5593d437536b3a61cc2182090c3fced99e771f4de6c1ef21b530bd6377b09dda77987ba017c9bc30362fa65bc8454b38b5333cb4d46
@@ -2,19 +2,29 @@
2
2
 
3
3
  require 'thor'
4
4
  require 'test_case_generator/dsl_context'
5
+ require 'test_case_generator/generator_factory'
5
6
  require 'test_case_generator/generator_objective_c'
7
+ require 'test_case_generator/generator_java'
8
+ require 'test_case_generator/generator_php'
9
+ require 'test_case_generator/generator_python'
6
10
 
7
11
  module TestCaseGenerator
8
12
  class CLI < Thor
9
- desc 'Injects test-cases', 'Injects test-cases into source file '
13
+ desc 'inject TEST_CASE TARGET_FILE', 'Injects test-cases into source file '
10
14
  def inject(testcase_fn, source_fn)
11
15
  puts "Injects #{testcase_fn} into #{source_fn}"
12
16
 
13
17
  loader = TestCaseGenerator::DSLContextLoader.new
14
18
  ctx = loader.load testcase_fn
15
- gen = TestCaseGenerator::GeneratorObjectiveC.new
16
- gen.write_header ctx, File.join(File.dirname(source_fn), File.basename(source_fn, File.extname(source_fn)) + 'Generated.h')
17
- gen.write_source ctx, source_fn
19
+
20
+ factory = TestCaseGenerator::GeneratorFactory.new
21
+ factory.register TestCaseGenerator::GeneratorObjectiveC.new
22
+ factory.register TestCaseGenerator::GeneratorJava.new
23
+ factory.register TestCaseGenerator::GeneratorPHP.new
24
+ factory.register TestCaseGenerator::GeneratorPython.new
25
+
26
+ gen = factory.query source_fn
27
+ gen.write ctx, source_fn
18
28
  end
19
29
  end
20
30
  end
@@ -0,0 +1,20 @@
1
+ require 'test_case_generator/dsl_context'
2
+ require 'test_case_generator/indented_writer'
3
+
4
+ module TestCaseGenerator
5
+ class GeneratorFactory
6
+ def initialize
7
+ @generators = []
8
+ end
9
+
10
+ def register(generator)
11
+ @generators << generator
12
+ end
13
+
14
+ def query(source_fn)
15
+ @generators.each do |g|
16
+ return g if g.can_handle? source_fn
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,74 @@
1
+ require 'test_case_generator/dsl_context'
2
+ require 'test_case_generator/indented_writer'
3
+
4
+ module TestCaseGenerator
5
+ class GeneratorJava
6
+ def can_handle?(source_fn)
7
+ File.extname(source_fn).eql? '.java'
8
+ end
9
+
10
+ def write(ctx, source_fn)
11
+ write_interface ctx, File.join(File.dirname(source_fn), File.basename(source_fn, File.extname(source_fn)) + 'Generated.java')
12
+ write_source ctx, source_fn
13
+ end
14
+
15
+ def write_interface(dsl_context, header_fn)
16
+ interface_name = File.basename(header_fn, File.extname(header_fn))
17
+ tmp_fn = header_fn + '.tmp'
18
+ File.open(tmp_fn, 'w') do |f|
19
+ writer = IndentedWriter.new f
20
+
21
+ writer.blank
22
+ writer.puts '//'
23
+ writer.blank
24
+ writer.puts "public interface #{interface_name} {"
25
+
26
+ writer.block_indent ' ' do
27
+ dsl_context.labels.each do |label|
28
+ method_name = label
29
+ writer.puts "void #{method_name}();"
30
+ end
31
+ end
32
+
33
+ writer.puts '}'
34
+ end
35
+
36
+ FileUtils.move tmp_fn, header_fn
37
+ end
38
+
39
+ def write_source(dsl_context, source_fn)
40
+ tmp_fn = source_fn + '.tmp'
41
+ source = File.open(source_fn).read
42
+ File.open(tmp_fn, 'w') do |f|
43
+ source.each_line do |line|
44
+ f.puts line
45
+ break if line =~ /^\s*\/\/\s*%%\s*$/
46
+ end
47
+
48
+ writer = IndentedWriter.new f
49
+
50
+ dsl_context.each do |pattern|
51
+ method_name = pattern.join '_'
52
+ writer.block_indent ' ' do
53
+ writer.blank
54
+ writer.puts '@Test'
55
+ writer.puts "public void test_#{method_name}() {"
56
+
57
+ pattern.each do |ptn|
58
+ writer.block_indent ' ' do
59
+ writer.puts "#{ptn}();"
60
+ end
61
+ end
62
+
63
+ writer.puts '}'
64
+ end
65
+ end
66
+
67
+ writer.blank
68
+ writer.puts '@end'
69
+ end
70
+
71
+ FileUtils.move tmp_fn, source_fn
72
+ end
73
+ end
74
+ end
@@ -1,38 +1,17 @@
1
1
  require 'test_case_generator/dsl_context'
2
+ require 'test_case_generator/indented_writer'
2
3
 
3
4
  module TestCaseGenerator
4
- class IndentedWriter
5
- def initialize(out)
6
- @out = out
7
- @indent = []
8
- @current_indent = ''
9
- end
10
-
11
- def puts(line)
12
- @out.puts @current_indent + line
13
- end
14
-
15
- def blank
16
- @out.puts
17
- end
18
-
19
- def indent(txt)
20
- @indent.push @current_indent
21
- @current_indent += txt
5
+ class GeneratorObjectiveC
6
+ def can_handle?(source_fn)
7
+ File.extname(source_fn).eql? '.m'
22
8
  end
23
9
 
24
- def unindent
25
- @current_indent = @indent.pop
10
+ def write(ctx, source_fn)
11
+ write_header ctx, File.join(File.dirname(source_fn), File.basename(source_fn, File.extname(source_fn)) + 'Generated.h')
12
+ write_source ctx, source_fn
26
13
  end
27
14
 
28
- def block_indent(txt)
29
- indent txt
30
- yield
31
- unindent
32
- end
33
- end
34
-
35
- class GeneratorObjectiveC
36
15
  def write_header(dsl_context, header_fn)
37
16
  protocol_name = File.basename(header_fn, File.extname(header_fn))
38
17
  tmp_fn = header_fn + '.tmp'
@@ -0,0 +1,71 @@
1
+ require 'test_case_generator/dsl_context'
2
+ require 'test_case_generator/indented_writer'
3
+
4
+ module TestCaseGenerator
5
+ class GeneratorPHP
6
+ def can_handle?(source_fn)
7
+ File.extname(source_fn).eql? '.php'
8
+ end
9
+
10
+ def write(ctx, source_fn)
11
+ write_header ctx, File.join(File.dirname(source_fn), File.basename(source_fn, File.extname(source_fn)) + 'Generated.php')
12
+ write_source ctx, source_fn
13
+ end
14
+
15
+ def write_header(dsl_context, header_fn)
16
+ protocol_name = File.basename(header_fn, File.extname(header_fn))
17
+ tmp_fn = header_fn + '.tmp'
18
+ File.open(tmp_fn, 'w') do |f|
19
+ writer = IndentedWriter.new f
20
+
21
+ writer.blank
22
+ writer.puts "interface #{protocol_name} {"
23
+
24
+ writer.block_indent ' ' do
25
+ dsl_context.labels.each do |label|
26
+ method_name = label
27
+ writer.puts "function #{method_name}();"
28
+ end
29
+ end
30
+
31
+ writer.puts '}'
32
+ end
33
+
34
+ FileUtils.move tmp_fn, header_fn
35
+ end
36
+
37
+ def write_source(dsl_context, source_fn)
38
+ tmp_fn = source_fn + '.tmp'
39
+ source = File.open(source_fn).read
40
+ File.open(tmp_fn, 'w') do |f|
41
+ source.each_line do |line|
42
+ f.puts line
43
+ break if line =~ /^\s*\/\/\s*%%\s*$/
44
+ end
45
+
46
+ writer = IndentedWriter.new f
47
+
48
+ dsl_context.each do |pattern|
49
+ method_name = pattern.join '_'
50
+ writer.block_indent ' ' do
51
+ writer.blank
52
+ writer.puts "public function test_#{method_name}() {"
53
+
54
+ pattern.each do |ptn|
55
+ writer.block_indent ' ' do
56
+ writer.puts "$this->#{ptn}();"
57
+ end
58
+ end
59
+
60
+ writer.puts '}'
61
+ end
62
+ end
63
+
64
+ writer.blank
65
+ writer.puts '}'
66
+ end
67
+
68
+ FileUtils.move tmp_fn, source_fn
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,93 @@
1
+ require 'test_case_generator/dsl_context'
2
+ require 'test_case_generator/indented_writer'
3
+
4
+ module TestCaseGenerator
5
+ class GeneratorPython
6
+ def can_handle?(source_fn)
7
+ File.extname(source_fn).eql? '.py'
8
+ end
9
+
10
+ def write(ctx, source_fn)
11
+ write_source ctx, source_fn
12
+ end
13
+
14
+ def write_source(dsl_context, source_fn)
15
+ tmp_fn = source_fn + '.tmp'
16
+ source = File.open(source_fn).read
17
+ File.open(tmp_fn, 'w') do |f|
18
+ source.each_line do |line|
19
+ f.puts line
20
+ break if line =~ /^\s*#\s*%%\s*$/
21
+ end
22
+
23
+ writer = IndentedWriter.new f
24
+ writer.blank
25
+ writer.block_indent ' ' do
26
+ writer.puts '#'
27
+ writer.puts '# 以下の行は自動生成されているので直接編集しないでください。'
28
+ writer.puts '#'
29
+ writer.puts '# Generated by Test Case Generator'
30
+ writer.puts '# https://rubygems.org/gems/test_case_generator'
31
+ writer.puts '#'
32
+ end
33
+
34
+ dsl_context.each do |pattern|
35
+ method_name = pattern.join '_'
36
+ writer.block_indent ' ' do
37
+ writer.blank
38
+ writer.puts "def test_#{method_name}(self):"
39
+
40
+ pattern.each do |ptn|
41
+ writer.block_indent ' ' do
42
+ writer.puts "self.#{ptn}()"
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ writer.blank
49
+ writer.block_indent ' ' do
50
+ writer.puts '@classmethod'
51
+ writer.puts 'def checkSanity(cls):'
52
+ writer.block_indent ' ' do
53
+ writer.puts 'sane = True'
54
+ writer.puts 'msg = []'
55
+ writer.puts "for method in [#{dsl_context.labels.map { |m| "'#{m}'" }.join(', ')}]:"
56
+ writer.block_indent ' ' do
57
+ writer.puts 'if not hasattr(cls, method):'
58
+ writer.block_indent ' ' do
59
+ writer.puts 'msg += ['
60
+ writer.block_indent ' ' do
61
+ writer.puts "' def %s(self):' % method,"
62
+ writer.puts "' pass',"
63
+ writer.puts "'',"
64
+ end
65
+ writer.puts ']'
66
+ writer.puts 'sane = False'
67
+ end
68
+ end
69
+
70
+ writer.blank
71
+ writer.puts 'if not sane:'
72
+ writer.block_indent ' ' do
73
+ writer.puts "print cls.__name__ + ' must implement following method(s):'"
74
+ writer.puts 'print'
75
+ writer.puts "print \"\\n\".join(msg)"
76
+ writer.puts 'raise SystemExit(1)'
77
+ end
78
+ end
79
+ end
80
+
81
+ writer.blank
82
+ writer.blank
83
+ writer.puts "if __name__ == '__main__':"
84
+ writer.block_indent ' ' do
85
+ writer.puts 'CommandLineArgumentsTestCase.checkSanity()'
86
+ writer.puts 'unittest.main()'
87
+ end
88
+ end
89
+
90
+ FileUtils.move tmp_fn, source_fn
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,32 @@
1
+ module TestCaseGenerator
2
+ class IndentedWriter
3
+ def initialize(out)
4
+ @out = out
5
+ @indent = []
6
+ @current_indent = ''
7
+ end
8
+
9
+ def puts(line)
10
+ @out.puts @current_indent + line
11
+ end
12
+
13
+ def blank
14
+ @out.puts
15
+ end
16
+
17
+ def indent(txt)
18
+ @indent.push @current_indent
19
+ @current_indent += txt
20
+ end
21
+
22
+ def unindent
23
+ @current_indent = @indent.pop
24
+ end
25
+
26
+ def block_indent(txt)
27
+ indent txt
28
+ yield
29
+ unindent
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module TestCaseGenerator
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ['Kaoru Yanase']
10
10
  spec.email = ['kaoru@yumemi.co.jp']
11
11
 
12
- spec.summary = 'Generates test-cases for Objective-C.'
12
+ spec.summary = 'Generates test-cases for Objective-C, python and etc..'
13
13
  # spec.description = %q{TODO: Write a longer description or delete this line.}
14
14
  spec.homepage = 'https://github.com/ym-kaoru/test_case_generator'
15
15
  spec.license = 'MIT'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: test_case_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaoru Yanase
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-25 00:00:00.000000000 Z
11
+ date: 2015-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -103,7 +103,12 @@ files:
103
103
  - lib/test_case_generator.rb
104
104
  - lib/test_case_generator/cli.rb
105
105
  - lib/test_case_generator/dsl_context.rb
106
+ - lib/test_case_generator/generator_factory.rb
107
+ - lib/test_case_generator/generator_java.rb
106
108
  - lib/test_case_generator/generator_objective_c.rb
109
+ - lib/test_case_generator/generator_php.rb
110
+ - lib/test_case_generator/generator_python.rb
111
+ - lib/test_case_generator/indented_writer.rb
107
112
  - lib/test_case_generator/utils.rb
108
113
  - lib/test_case_generator/version.rb
109
114
  - test_case_generator.gemspec
@@ -131,5 +136,5 @@ rubyforge_project:
131
136
  rubygems_version: 2.4.5.1
132
137
  signing_key:
133
138
  specification_version: 4
134
- summary: Generates test-cases for Objective-C.
139
+ summary: Generates test-cases for Objective-C, python and etc..
135
140
  test_files: []