diff --git a/lib/cmock_unityhelper_parser.rb b/lib/cmock_unityhelper_parser.rb new file mode 100644 index 0000000..abe6b64 --- /dev/null +++ b/lib/cmock_unityhelper_parser.rb @@ -0,0 +1,45 @@ +class CMockUnityHelperParser + + attr_accessor :c_types + + def initialize(config) + @c_types = map_C_types(config.treat_as).merge(import_source(config.load_unity_helper)) + end + + def get_helper(ctype) + lookup = ctype.gsub('const','').strip.gsub(/\s+/,'_') + return @c_types[lookup] if (@c_types[lookup]) + return 'TEST_ASSERT_EQUAL_MEMORY_MESSAGE' + end + + private ########################### + + def map_C_types(treat_as={}) + c_types = {} + treat_as.each_pair do |expect, ctypes| + ctypes.each {|ctype| c_types[ctype] = "TEST_ASSERT_EQUAL_#{expect}_MESSAGE"} + end + c_types + end + + def import_source(source=nil) + return {} if source.nil? + + a = [] + source = source.gsub(/\/\/.*$/, '') #remove line comments + source = source.gsub(/\/\*.*?\*\//m, '') #remove block comments + + #scan for comparison helpers + m = Regexp.new('^\s*#define\s+(TEST_ASSERT_EQUAL_(\w+)_MESSAGE|TEST_ASSERT_EQUAL_(\w+))\s*\(' + Array.new(2,'\s*\w+\s*').join(',') + '\)') + a += source.scan(m).flatten.compact.reject {|helper| helper.include? "_ARRAY"} + + #scan for array variants of those helpers + m = Regexp.new('^\s*#define\s+(TEST_ASSERT_EQUAL_(\w+)_ARRAY_MESSAGE|TEST_ASSERT_EQUAL_(\w+)_ARRAY)\s*\(' + Array.new(3,'\s*\w+\s*').join(',') + '\)') + a += source.scan(m).flatten.compact + + #add to c_types + c_types = {} + a.each_slice(2) {|expect, ctype| c_types[ctype] = expect} + c_types + end +end diff --git a/test/unit/cmock_unityhelper_parser_test.rb b/test/unit/cmock_unityhelper_parser_test.rb new file mode 100644 index 0000000..7858566 --- /dev/null +++ b/test/unit/cmock_unityhelper_parser_test.rb @@ -0,0 +1,90 @@ +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + "/../../lib/cmock_unityhelper_parser" + +class CMockUnityHelperParserTest < Test::Unit::TestCase + + def setup + create_mocks :config + end + + def teardown + end + + should "ignore lines that are commented out" do + source = + " abcd;\n" + + "// #define TEST_ASSERT_EQUAL_CHICKENS(a,b) {...};\n" + + "or maybe // #define TEST_ASSERT_EQUAL_CHICKENS(a,b) {...};\n\n" + @config.expects.treat_as.returns({}) + @config.expects.load_unity_helper.returns(source) + @parser = CMockUnityHelperParser.new(@config) + expected = {} + + assert_equal(expected, @parser.c_types) + end + + should "ignore stuff in block comments" do + source = + " abcd; /*\n" + + "#define TEST_ASSERT_EQUAL_CHICKENS(a,b) {...};\n" + + "#define TEST_ASSERT_EQUAL_CHICKENS(a,b) {...};\n */\n" + @config.expects.treat_as.returns({}) + @config.expect.load_unity_helper.returns(source) + @parser = CMockUnityHelperParser.new(@config) + expected = {} + + assert_equal(expected, @parser.c_types) + end + + should "notice equal helpers in the proper form and ignore others" do + source = + "abcd;\n" + + "#define TEST_ASSERT_EQUAL_TURKEYS_T(a,b) {...};\n" + + "abcd;\n" + + "#define TEST_ASSERT_EQUAL_WRONG_NUM_ARGS(a,b,c) {...};\n" + + "#define TEST_ASSERT_WRONG_NAME_EQUAL(a,b) {...};\n" + + "#define TEST_ASSERT_EQUAL_unsigned_funky_rabbits(a,b) {...};\n" + + "abcd;\n" + @config.expects.treat_as.returns({}) + @config.expect.load_unity_helper.returns(source) + @parser = CMockUnityHelperParser.new(@config) + expected = { + 'TURKEYS_T' => "TEST_ASSERT_EQUAL_TURKEYS_T", + 'unsigned_funky_rabbits' => "TEST_ASSERT_EQUAL_unsigned_funky_rabbits" + } + + assert_equal(expected, @parser.c_types) + end + + should "be able to fetch helpers on my list" do + @config.expects.treat_as.returns({}) + @config.expect.load_unity_helper.returns("") + @parser = CMockUnityHelperParser.new(@config) + @parser.c_types = { + 'UINT8' => "TEST_ASSERT_EQUAL_UINT8_MESSAGE", + 'UINT16*' => "TEST_ASSERT_EQUAL_UINT16_ARRAY", + 'SPINACH' => "TEST_ASSERT_EQUAL_SPINACH", + } + + assert_equal("TEST_ASSERT_EQUAL_UINT8_MESSAGE", @parser.get_helper("UINT8")) + assert_equal("TEST_ASSERT_EQUAL_UINT16_ARRAY", @parser.get_helper("UINT16*")) + assert_equal("TEST_ASSERT_EQUAL_SPINACH", @parser.get_helper("SPINACH")) + end + + should "return memory comparison when asked to fetch helper of types not on my list" do + @config.expects.treat_as.returns({}) + @config.expect.load_unity_helper.returns("") + @parser = CMockUnityHelperParser.new(@config) + @parser.c_types = { + 'UINT8' => "TEST_ASSERT_EQUAL_UINT8_MESSAGE", + 'UINT16*' => "TEST_ASSERT_EQUAL_UINT16_ARRAY", + 'SPINACH' => "TEST_ASSERT_EQUAL_SPINACH", + } + + assert_equal("TEST_ASSERT_EQUAL_MEMORY_MESSAGE", @parser.get_helper("UINT16")) + assert_equal("TEST_ASSERT_EQUAL_MEMORY_MESSAGE", @parser.get_helper("UINT8*")) + assert_equal("TEST_ASSERT_EQUAL_MEMORY_MESSAGE", @parser.get_helper("SPINACH_T")) + assert_equal("TEST_ASSERT_EQUAL_MEMORY_MESSAGE", @parser.get_helper("SALAD")) + assert_equal("TEST_ASSERT_EQUAL_MEMORY_MESSAGE", @parser.get_helper("PINEAPPLE")) + end +end