Debian Codegraph @

lib/cmock_unityhelper_parser.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 class CMockUnityHelperParser
8 attr_accessor :c_types
9
10 def initialize(config)
11 @config = config
12 @fallback = @config.plugins.include?(:array) ? 'UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY' : 'UNITY_TEST_ASSERT_EQUAL_MEMORY'
13 @c_types = map_c_types.merge(import_source)
14 end
15
16 def get_helper(ctype)
17 lookup = ctype.gsub(/(?:^|(\S?)(\s*)|(\W))const(?:$|(\s*)(\S)|(\W))/, '\1\3\5\6').strip.gsub(/\s+/, '_')
18 return [@c_types[lookup], ''] if @c_types[lookup]
19
20 if lookup =~ /\*$/
21 lookup = lookup.gsub(/\*$/, '')
22 return [@c_types[lookup], '*'] if @c_types[lookup]
23 else
24 lookup += '*'
25 return [@c_types[lookup], '&'] if @c_types[lookup]
26 end
27 return ['UNITY_TEST_ASSERT_EQUAL_PTR', ''] if ctype =~ /cmock_\w+_ptr\d+/
28 raise("Don't know how to test #{ctype} and memory tests are disabled!") unless @config.memcmp_if_unknown
29
30 lookup =~ /\*$/ ? [@fallback, '&'] : [@fallback, '']
31 end
32
33 private ###########################
34
35 def map_c_types
36 c_types = {}
37 @config.treat_as.each_pair do |ctype, expecttype|
38 c_type = ctype.gsub(/\s+/, '_')
39 if expecttype =~ /\*/
40 c_types[c_type] = "UNITY_TEST_ASSERT_EQUAL_#{expecttype.delete('*')}_ARRAY"
41 else
42 c_types[c_type] = "UNITY_TEST_ASSERT_EQUAL_#{expecttype}"
43 c_types[c_type + '*'] ||= "UNITY_TEST_ASSERT_EQUAL_#{expecttype}_ARRAY"
44 end
45 end
46 c_types
47 end
48
49 def import_source
50 source = @config.load_unity_helper
51 return {} if source.nil?
52
53 c_types = {}
54 source = source.gsub(/\/\/.*$/, '') # remove line comments
55 source = source.gsub(/\/\*.*?\*\//m, '') # remove block comments
56
57 # scan for comparison helpers
58 match_regex = Regexp.new('^\s*#define\s+(UNITY_TEST_ASSERT_EQUAL_(\w+))\s*\(' + Array.new(4, '\s*\w+\s*').join(',') + '\)')
59 pairs = source.scan(match_regex).flatten.compact
60 (pairs.size / 2).times do |i|
61 expect = pairs[i * 2]
62 ctype = pairs[(i * 2) + 1]
63 c_types[ctype] = expect unless expect.include?('_ARRAY')
64 end
65
66 # scan for array variants of those helpers
67 match_regex = Regexp.new('^\s*#define\s+(UNITY_TEST_ASSERT_EQUAL_(\w+_ARRAY))\s*\(' + Array.new(5, '\s*\w+\s*').join(',') + '\)')
68 pairs = source.scan(match_regex).flatten.compact
69 (pairs.size / 2).times do |i|
70 expect = pairs[i * 2]
71 ctype = pairs[(i * 2) + 1]
72 c_types[ctype.gsub('_ARRAY', '*')] = expect
73 end
74
75 c_types
76 end
77 end