test_includes_extractor.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. class TestIncludesExtractor
  2. constructor :configurator, :yaml_wrapper, :file_wrapper
  3. def setup
  4. @includes = {}
  5. @mocks = {}
  6. end
  7. # for includes_list file, slurp up array from yaml file and sort & store includes
  8. def parse_includes_list(includes_list)
  9. gather_and_store_includes( includes_list, @yaml_wrapper.load(includes_list) )
  10. end
  11. # open, scan for, and sort & store includes of test file
  12. def parse_test_file(test)
  13. gather_and_store_includes( test, extract_from_file(test) )
  14. end
  15. # mocks with no file extension
  16. def lookup_raw_mock_list(test)
  17. file_key = form_file_key(test)
  18. return [] if @mocks[file_key].nil?
  19. return @mocks[file_key]
  20. end
  21. # includes with file extension
  22. def lookup_includes_list(file)
  23. file_key = form_file_key(file)
  24. return [] if (@includes[file_key]).nil?
  25. return @includes[file_key]
  26. end
  27. private #################################
  28. def form_file_key(filepath)
  29. return File.basename(filepath).to_sym
  30. end
  31. def extract_from_file(file)
  32. includes = []
  33. header_extension = @configurator.extension_header
  34. contents = @file_wrapper.read(file)
  35. # remove line comments
  36. contents = contents.gsub(/\/\/.*$/, '')
  37. # remove block comments
  38. contents = contents.gsub(/\/\*.*?\*\//m, '')
  39. contents.split("\n").each do |line|
  40. # look for include statement
  41. scan_results = line.scan(/#include\s+\"\s*(.+#{'\\'+header_extension})\s*\"/)
  42. includes << scan_results[0][0] if (scan_results.size > 0)
  43. end
  44. return includes.uniq
  45. end
  46. def gather_and_store_includes(file, includes)
  47. mock_prefix = @configurator.cmock_mock_prefix
  48. header_extension = @configurator.extension_header
  49. file_key = form_file_key(file)
  50. @mocks[file_key] = []
  51. # add includes to lookup hash
  52. @includes[file_key] = includes
  53. includes.each do |include_file|
  54. # check if include is a mock
  55. scan_results = include_file.scan(/(#{mock_prefix}.+)#{'\\'+header_extension}/)
  56. # add mock to lookup hash
  57. @mocks[file_key] << scan_results[0][0] if (scan_results.size > 0)
  58. end
  59. end
  60. end