Debian Codegraph @

test/system/systest_generator.rb

raw symbols SCIP
1 # ==========================================
2 # CMock Project - Automatic Mock Generation for C
3 # Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4 # [Released under MIT License. Please refer to license.txt for details]
5 # ==========================================
6
7 require 'yaml'
8
9 SYS_TEST_GEN_ROOT = File.expand_path( File.dirname( __FILE__ ) ) + '/'
10 GENERATED_PATH = SYS_TEST_GEN_ROOT + 'generated/'
11 BUILD_PATH = SYS_TEST_GEN_ROOT + 'build/'
12 CASES_PATH = SYS_TEST_GEN_ROOT + 'cases/'
13
14 TYPES_H = 'types.h'
15 UNITY_H = 'unity.h'
16 CMOCK_H = 'cmock.h'
17 UNITY_HELPER_H = 'unity_helper.h'
18 UNITY_HELPER_C = 'unity_helper.c'
19 MOCKABLE_H = 'mockable.h'
20
21 YAML_EXTENSION = '.yml'
22 TEST_PREFIX = 'test_'
23 MOCK_PREFIX = 'mock_'
24 H_EXTENSION = '.h'
25 C_EXTENSION = '.c'
26
27
28 class SystemTestGenerator
29
30 def generate_files(test_cases)
31 test_cases.each do |filename|
32 yaml_hash = YAML.load_file(filename)
33
34 name = File.basename(filename, YAML_EXTENSION)
35 namix = "#{name}_"
36
37 generate_cmock_config(yaml_hash, namix)
38 generate_code(yaml_hash, namix, name)
39 end
40 end
41
42 private
43
44 def generate_cmock_config(yaml_hash, namix)
45 cmock_yaml = yaml_hash.clone
46 cmock_yaml.delete(:systest)
47 cmock = cmock_yaml[:cmock]
48
49 cmock[:mock_path] = GENERATED_PATH
50 cmock[:includes] = (cmock[:includes] || []) + [namix + TYPES_H]
51 cmock[:mock_prefix] = MOCK_PREFIX
52 if not yaml_hash[:systest][:unity_helper].nil?
53 cmock[:includes] << namix + UNITY_HELPER_H
54 cmock[:unity_helper_path] = GENERATED_PATH + namix + UNITY_HELPER_H
55 end
56
57 File.open(GENERATED_PATH + namix + 'cmock' + YAML_EXTENSION, 'w') do |out|
58 YAML.dump(cmock_yaml, out)
59 end
60 end
61
62 def generate_code(yaml_hash, namix, name)
63 generate_types_file(yaml_hash, namix)
64 generate_mockable_file(yaml_hash, namix)
65 generate_unity_helper_files(yaml_hash, namix)
66
67 generate_test_file(yaml_hash, namix, name)
68 generate_source_file(yaml_hash, namix, name)
69 generate_skeleton_file(yaml_hash, namix, name)
70 end
71
72 def generate_types_file(yaml_hash, namix)
73 types = yaml_hash[:systest][:types]
74 return if types.nil?
75
76 write_header_file(GENERATED_PATH + namix + TYPES_H, namix.upcase + 'TYPES_H') do |out|
77 out.puts(types)
78 end
79 end
80
81 def generate_mockable_file(yaml_hash, namix)
82 mockable = yaml_hash[:systest][:mockable]
83 return if mockable.nil?
84
85 write_header_file(GENERATED_PATH + namix + MOCKABLE_H, namix.upcase + 'MOCKABLE_H', [namix + TYPES_H]) do |out|
86 out.puts(mockable)
87 end
88 end
89
90 def generate_unity_helper_files(yaml_hash, namix)
91 unity_helper = yaml_hash[:systest][:unity_helper]
92 return if unity_helper.nil?
93
94 write_header_file(GENERATED_PATH + namix + UNITY_HELPER_H, namix.upcase + 'UNITY_HELPER_H', [namix + TYPES_H]) do |out|
95 out.puts(unity_helper[:header])
96 end
97
98 write_source_file(GENERATED_PATH + namix + UNITY_HELPER_C, ["unity.h", namix + UNITY_HELPER_H]) do |out|
99 out.puts(unity_helper[:code])
100 end
101 end
102
103 def generate_test_file(yaml_hash, namix, name)
104 tests = yaml_hash[:systest][:tests]
105 return if tests.nil?
106
107 includes = [UNITY_H, CMOCK_H]
108 includes << [MOCK_PREFIX + namix + MOCKABLE_H]
109 includes << [name + H_EXTENSION]
110
111 write_source_file(GENERATED_PATH + TEST_PREFIX + name + C_EXTENSION, includes.flatten) do |out|
112 out.puts(tests[:common])
113 out.puts('')
114
115 tests[:units].each_with_index do |test, index|
116 out.puts('// should ' + test[:should])
117 out.puts(test[:code].gsub!(/test\(\)/, "void test#{index+1}(void)"))
118 out.puts('')
119 end
120 end
121 end
122
123 def generate_source_file(yaml_hash, namix, name)
124 source = yaml_hash[:systest][:source]
125 return if source.nil?
126
127 header_file = name + H_EXTENSION
128
129 includes = yaml_hash[:systest][:types].nil? ? [] : [(namix + TYPES_H)]
130
131 write_header_file(GENERATED_PATH + header_file, name.upcase, includes) do |out|
132 out.puts(source[:header])
133 end
134
135 includes = []
136 includes << (namix + TYPES_H) if not yaml_hash[:systest][:types].nil?
137 includes << (namix + MOCKABLE_H) if not yaml_hash[:systest][:mockable].nil?
138 includes << header_file
139
140 unless (source[:code].nil?)
141 write_source_file(GENERATED_PATH + name + C_EXTENSION, includes.flatten) do |out|
142 out.puts(source[:code])
143 end
144 end
145 end
146
147 def generate_skeleton_file(yaml_hash, namix, name)
148 source = yaml_hash[:systest][:skeleton]
149 return if source.nil?
150
151 require 'cmock.rb'
152 cmock = CMock.new(GENERATED_PATH + namix + 'cmock' + YAML_EXTENSION)
153 cmock.setup_skeletons("#{$cfg['compiler']['source_path']}#{name}.h")
154 end
155
156 def write_header_file(filename, upcase_name, include_list=[])
157 File.open(filename, 'w') do |out|
158 out.puts("#ifndef _#{upcase_name}")
159 out.puts("#define _#{upcase_name}")
160 out.puts('')
161 include_list.each do |include|
162 out.puts("#include \"#{include}\"")
163 end
164 out.puts('')
165 out.puts("#if defined(__GNUC__) && !defined(__ICC)")
166 out.puts("#if !defined(__clang__)")
167 out.puts("#pragma GCC diagnostic ignored \"-Wpragmas\"")
168 out.puts("#endif")
169 out.puts('#pragma GCC diagnostic ignored "-Wunknown-pragmas"')
170 out.puts('#pragma GCC diagnostic ignored "-Wduplicate-decl-specifier"')
171 out.puts("#endif")
172 out.puts('')
173 yield(out)
174 out.puts('')
175 out.puts("#endif // _#{upcase_name}")
176 out.puts('')
177 end
178 end
179
180 def write_source_file(filename, include_list=[])
181 File.open(filename, 'w') do |out|
182 include_list.each do |include|
183 out.puts("#include \"#{include}\"")
184 end
185 out.puts('')
186 out.puts("#if defined(__GNUC__) && !defined(__ICC)")
187 out.puts("#if !defined(__clang__)")
188 out.puts("#pragma GCC diagnostic ignored \"-Wpragmas\"")
189 out.puts("#endif")
190 out.puts('#pragma GCC diagnostic ignored "-Wunknown-pragmas"')
191 out.puts('#pragma GCC diagnostic ignored "-Wduplicate-decl-specifier"')
192 out.puts("#endif")
193 out.puts('')
194 yield(out)
195 out.puts('')
196 end
197 end
198
199 end
200
201
202 if ($0 == __FILE__)
203 SystemTestGenerator.new.generate_files(Dir[CASES_PATH + "*#{YAML_EXTENSION}"])
204 end
205