Class: TestContextExtractor

Inherits:
Object show all
Defined in:
lib/ceedling/test_context_extractor.rb

Instance Method Summary collapse

Instance Method Details

#collect_simple_context(filepath, input, *args) ⇒ Object

‘input` must have the interface of IO – StringIO for testing or File in typical use



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ceedling/test_context_extractor.rb', line 34

def collect_simple_context( filepath, input, *args )
  all_options = [
    :build_directive_include_paths,
    :build_directive_source_files,
    :includes,
    :test_runner_details
  ]

  # Code error check--bad context symbol argument
  args.each do |context|
    next if context == :all
    msg = "Unrecognized test context for collection :#{context}"
    raise CeedlingException.new( msg ) if !all_options.include?( context )
  end

  # Handle the :all shortcut to redefine list to include all contexts
  args = all_options if args.include?( :all )

  include_paths = []
  source_extras = []
  includes = []

  @parsing_parcels.code_lines( input ) do |line|
    if args.include?( :build_directive_include_paths )
      # Scan for build directives: TEST_INCLUDE_PATH()
      include_paths += extract_build_directive_include_paths( line )
    end

    if args.include?( :build_directive_source_files )
      # Scan for build directives: TEST_SOURCE_FILE()
      source_extras += extract_build_directive_source_files( line )
    end

    if args.include?( :includes )
      # Scan for contents of #include directives
      includes += _extract_includes( line )
    end
  end

  collect_build_directive_include_paths( filepath, include_paths ) if args.include?( :build_directive_include_paths )
  collect_build_directive_source_files( filepath, source_extras ) if args.include?( :build_directive_source_files )
  collect_includes( filepath, includes ) if args.include?( :includes )

  # Different code processing pattern for test runner
  if args.include?( :test_runner_details )
    # Go back to beginning of IO object for a full string extraction
    input.rewind()

    # Ultimately, we rely on Unity's runner generator that processes file contents as a single string
    _collect_test_runner_details( filepath, input.read() )
  end
end

#collect_test_runner_details(test_filepath, input_filepath = nil) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/ceedling/test_context_extractor.rb', line 87

def collect_test_runner_details(test_filepath, input_filepath=nil)
  # Ultimately, we rely on Unity's runner generator that processes file contents as a single string
  _collect_test_runner_details(
    test_filepath,
    @file_wrapper.read( test_filepath ),
    input_filepath.nil? ? nil : @file_wrapper.read( input_filepath )
  )
end

#extract_includes(input) ⇒ Object

Scan for all includes. Unlike other extract() calls, extract_includes() is public to be called externally. ‘input` must have the interface of IO – StringIO for testing or File in typical use



99
100
101
102
103
104
105
# File 'lib/ceedling/test_context_extractor.rb', line 99

def extract_includes(input)
  includes = []

  @parsing_parcels.code_lines( input ) {|line| includes += _extract_includes( line ) }

  return includes.uniq
end

#ingest_includes(filepath, includes) ⇒ Object

Unlike other ingest() calls, ingest_includes() can be called externally.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/ceedling/test_context_extractor.rb', line 198

def ingest_includes(filepath, includes)
  mock_prefix = @configurator.cmock_mock_prefix
  file_key    = form_file_key( filepath )
  
  mocks       = []
  all_headers = []
  headers     = []
  sources     = []

  includes.each do |include|
    # <*.h>
    if include =~ /#{Regexp.escape(@configurator.extension_header)}$/
      # Check if include is a mock with regex match that extracts only mock name (no .h)
      scan_results = include.scan(/(#{mock_prefix}.+)#{Regexp.escape(@configurator.extension_header)}/)
      
      if (scan_results.size > 0)
        # Collect mock name
        mocks << scan_results[0][0]
      else
        # If not a mock or framework file, collect tailored header filename
        headers << include unless VENDORS_FILES.include?( include.ext('') )
      end

      # Add to .h includes list
      all_headers << include
    # <*.c>
    elsif include =~ /#{Regexp.escape(@configurator.extension_source)}$/
      # Add to .c includes list
      sources << include
    end
  end

  @lock.synchronize do
    @mocks[file_key] = mocks
    @all_header_includes[file_key] = all_headers
    @header_includes[file_key] = headers
    @source_includes[file_key] = sources
  end
end

#inspect_include_pathsObject



191
192
193
194
195
# File 'lib/ceedling/test_context_extractor.rb', line 191

def inspect_include_paths
  @lock.synchronize do
    @include_paths.each { |test, paths| yield test, paths }
  end
end

#lookup_all_include_pathsObject



183
184
185
186
187
188
189
# File 'lib/ceedling/test_context_extractor.rb', line 183

def lookup_all_include_paths
  val = nil
  @lock.synchronize do
    val = @all_include_paths.uniq
  end
  return val
end

#lookup_build_directive_sources_list(filepath) ⇒ Object

Source extras via TEST_SOURCE_FILE() within test file



144
145
146
147
148
149
150
# File 'lib/ceedling/test_context_extractor.rb', line 144

def lookup_build_directive_sources_list(filepath)
  val = nil
  @lock.synchronize do
    val = @source_extras[form_file_key( filepath )] || []
  end
  return val
end

#lookup_full_header_includes_list(filepath) ⇒ Object

All header includes .h of test file



108
109
110
111
112
113
114
# File 'lib/ceedling/test_context_extractor.rb', line 108

def lookup_full_header_includes_list(filepath)
  val = nil
  @lock.synchronize do
    val = @all_header_includes[form_file_key( filepath )] || []
  end
  return val
end

#lookup_header_includes_list(filepath) ⇒ Object

Header includes .h (minus mocks & framework headers) in test file



117
118
119
120
121
122
123
# File 'lib/ceedling/test_context_extractor.rb', line 117

def lookup_header_includes_list(filepath)
  val = nil
  @lock.synchronize do
    val = @header_includes[form_file_key( filepath )] || []
  end
  return val
end

#lookup_include_paths_list(filepath) ⇒ Object

Include paths of test file specified with TEST_INCLUDE_PATH()



126
127
128
129
130
131
132
# File 'lib/ceedling/test_context_extractor.rb', line 126

def lookup_include_paths_list(filepath)
  val = nil
  @lock.synchronize do
    val = @include_paths[form_file_key( filepath )] || []
  end
  return val
end

#lookup_raw_mock_list(filepath) ⇒ Object

Mocks within test file with no file extension



175
176
177
178
179
180
181
# File 'lib/ceedling/test_context_extractor.rb', line 175

def lookup_raw_mock_list(filepath)
  val = nil
  @lock.synchronize do
    val = @mocks[form_file_key( filepath )] || []
  end
  return val
end

#lookup_source_includes_list(filepath) ⇒ Object

Source C includes within test file



135
136
137
138
139
140
141
# File 'lib/ceedling/test_context_extractor.rb', line 135

def lookup_source_includes_list(filepath)
  val = nil
  @lock.synchronize do
    val = @source_includes[form_file_key( filepath )] || []
  end
  return val
end

#lookup_test_cases(filepath) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/ceedling/test_context_extractor.rb', line 152

def lookup_test_cases(filepath)
  val = []
  @lock.synchronize do
    details = @test_runner_details[form_file_key( filepath )]
    if !details.nil?
      val = details[:test_cases]
    end
  end
  return val
end

#lookup_test_runner_generator(filepath) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/ceedling/test_context_extractor.rb', line 163

def lookup_test_runner_generator(filepath)
  val = nil
  @lock.synchronize do
    details = @test_runner_details[form_file_key( filepath )]
    if !details.nil?
      val = details[:generator]
    end
  end
  return val
end

#setupObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ceedling/test_context_extractor.rb', line 17

def setup
  # Per test-file lookup hashes
  @all_header_includes = {} # Full list of all headers from test #include statements
  @header_includes     = {} # List of all headers minus mocks & framework files
  @source_includes     = {} # List of C files #include'd in a test file
  @source_extras       = {} # C source files outside of header convention added to test build by TEST_SOURCE_FILE()
  @test_runner_details = {} # Test case lists & Unity runner generator instances
  @mocks               = {} # List of mocks by name without header file extension
  @include_paths       = {} # Additional search paths added to a test build via TEST_INCLUDE_PATH()
  
  # Arrays
  @all_include_paths   = [] # List of all search paths added through individual test files using TEST_INCLUDE_PATH()

  @lock = Mutex.new
end