2016-01-07 14:11:52 -05:00
|
|
|
# ==========================================
|
|
|
|
# CMock Project - Automatic Mock Generation for C
|
|
|
|
# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
|
|
|
|
# [Released under MIT License. Please refer to license.txt for details]
|
2020-03-18 19:16:58 -04:00
|
|
|
# ==========================================
|
2016-01-07 14:11:52 -05:00
|
|
|
|
|
|
|
class CMockUnityHelperParser
|
|
|
|
attr_accessor :c_types
|
2020-03-18 19:16:58 -04:00
|
|
|
|
2016-01-07 14:11:52 -05:00
|
|
|
def initialize(config)
|
|
|
|
@config = config
|
|
|
|
@fallback = @config.plugins.include?(:array) ? 'UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY' : 'UNITY_TEST_ASSERT_EQUAL_MEMORY'
|
2020-03-19 10:00:12 -04:00
|
|
|
@c_types = map_c_types.merge(import_source)
|
2016-01-07 14:11:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_helper(ctype)
|
2020-03-18 19:16:58 -04:00
|
|
|
lookup = ctype.gsub(/(?:^|(\S?)(\s*)|(\W))const(?:$|(\s*)(\S)|(\W))/, '\1\3\5\6').strip.gsub(/\s+/, '_')
|
|
|
|
return [@c_types[lookup], ''] if @c_types[lookup]
|
|
|
|
|
|
|
|
if lookup =~ /\*$/
|
|
|
|
lookup = lookup.gsub(/\*$/, '')
|
|
|
|
return [@c_types[lookup], '*'] if @c_types[lookup]
|
2016-01-07 14:11:52 -05:00
|
|
|
else
|
2020-03-18 19:16:58 -04:00
|
|
|
lookup += '*'
|
|
|
|
return [@c_types[lookup], '&'] if @c_types[lookup]
|
2016-01-07 14:11:52 -05:00
|
|
|
end
|
2020-03-18 19:16:58 -04:00
|
|
|
return ['UNITY_TEST_ASSERT_EQUAL_PTR', ''] if ctype =~ /cmock_\w+_ptr\d+/
|
2016-01-07 14:11:52 -05:00
|
|
|
raise("Don't know how to test #{ctype} and memory tests are disabled!") unless @config.memcmp_if_unknown
|
2020-03-18 19:16:58 -04:00
|
|
|
|
|
|
|
lookup =~ /\*$/ ? [@fallback, '&'] : [@fallback, '']
|
2016-01-07 14:11:52 -05:00
|
|
|
end
|
2020-03-18 19:16:58 -04:00
|
|
|
|
2016-01-07 14:11:52 -05:00
|
|
|
private ###########################
|
2020-03-18 19:16:58 -04:00
|
|
|
|
2020-03-19 10:00:12 -04:00
|
|
|
def map_c_types
|
2016-01-07 14:11:52 -05:00
|
|
|
c_types = {}
|
|
|
|
@config.treat_as.each_pair do |ctype, expecttype|
|
2020-03-18 19:16:58 -04:00
|
|
|
c_type = ctype.gsub(/\s+/, '_')
|
|
|
|
if expecttype =~ /\*/
|
2020-03-19 11:29:17 -04:00
|
|
|
c_types[c_type] = "UNITY_TEST_ASSERT_EQUAL_#{expecttype.delete('*')}_ARRAY"
|
2016-01-07 14:11:52 -05:00
|
|
|
else
|
|
|
|
c_types[c_type] = "UNITY_TEST_ASSERT_EQUAL_#{expecttype}"
|
2020-03-18 19:16:58 -04:00
|
|
|
c_types[c_type + '*'] ||= "UNITY_TEST_ASSERT_EQUAL_#{expecttype}_ARRAY"
|
2016-01-07 14:11:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
c_types
|
|
|
|
end
|
2020-03-18 19:16:58 -04:00
|
|
|
|
2016-01-07 14:11:52 -05:00
|
|
|
def import_source
|
|
|
|
source = @config.load_unity_helper
|
|
|
|
return {} if source.nil?
|
2020-03-18 19:16:58 -04:00
|
|
|
|
2016-01-07 14:11:52 -05:00
|
|
|
c_types = {}
|
2020-03-18 19:16:58 -04:00
|
|
|
source = source.gsub(/\/\/.*$/, '') # remove line comments
|
|
|
|
source = source.gsub(/\/\*.*?\*\//m, '') # remove block comments
|
|
|
|
|
|
|
|
# scan for comparison helpers
|
|
|
|
match_regex = Regexp.new('^\s*#define\s+(UNITY_TEST_ASSERT_EQUAL_(\w+))\s*\(' + Array.new(4, '\s*\w+\s*').join(',') + '\)')
|
2016-01-07 14:11:52 -05:00
|
|
|
pairs = source.scan(match_regex).flatten.compact
|
2020-03-18 19:16:58 -04:00
|
|
|
(pairs.size / 2).times do |i|
|
|
|
|
expect = pairs[i * 2]
|
|
|
|
ctype = pairs[(i * 2) + 1]
|
|
|
|
c_types[ctype] = expect unless expect.include?('_ARRAY')
|
2016-01-07 14:11:52 -05:00
|
|
|
end
|
2020-03-18 19:16:58 -04:00
|
|
|
|
|
|
|
# scan for array variants of those helpers
|
|
|
|
match_regex = Regexp.new('^\s*#define\s+(UNITY_TEST_ASSERT_EQUAL_(\w+_ARRAY))\s*\(' + Array.new(5, '\s*\w+\s*').join(',') + '\)')
|
2016-01-07 14:11:52 -05:00
|
|
|
pairs = source.scan(match_regex).flatten.compact
|
2020-03-18 19:16:58 -04:00
|
|
|
(pairs.size / 2).times do |i|
|
|
|
|
expect = pairs[i * 2]
|
|
|
|
ctype = pairs[(i * 2) + 1]
|
|
|
|
c_types[ctype.gsub('_ARRAY', '*')] = expect
|
2016-01-07 14:11:52 -05:00
|
|
|
end
|
2020-03-18 19:16:58 -04:00
|
|
|
|
2016-01-07 14:11:52 -05:00
|
|
|
c_types
|
|
|
|
end
|
|
|
|
end
|