1
0
mirror of https://github.com/ThrowTheSwitch/CMock synced 2025-03-13 16:51:12 -04:00
CMock/lib/cmock_unityhelper_parser.rb

78 lines
2.7 KiB
Ruby
Raw Normal View History

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]
# ==========================================
2016-01-07 14:11:52 -05:00
class CMockUnityHelperParser
attr_accessor :c_types
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'
@c_types = map_c_types.merge(import_source)
2016-01-07 14:11:52 -05:00
end
def get_helper(ctype)
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
lookup += '*'
return [@c_types[lookup], '&'] if @c_types[lookup]
2016-01-07 14:11:52 -05:00
end
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
lookup =~ /\*$/ ? [@fallback, '&'] : [@fallback, '']
2016-01-07 14:11:52 -05:00
end
2016-01-07 14:11:52 -05:00
private ###########################
def map_c_types
2016-01-07 14:11:52 -05:00
c_types = {}
@config.treat_as.each_pair do |ctype, expecttype|
c_type = ctype.gsub(/\s+/, '_')
if expecttype =~ /\*/
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}"
c_types[c_type + '*'] ||= "UNITY_TEST_ASSERT_EQUAL_#{expecttype}_ARRAY"
2016-01-07 14:11:52 -05:00
end
end
c_types
end
2016-01-07 14:11:52 -05:00
def import_source
source = @config.load_unity_helper
return {} if source.nil?
2016-01-07 14:11:52 -05:00
c_types = {}
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
(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
# 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
(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
2016-01-07 14:11:52 -05:00
c_types
end
end