Skip to content

Commit

Permalink
✅ More robust & simpler unit test case
Browse files Browse the repository at this point in the history
A previous refactoring provided a better wrapper method to access line-at-a-time code cleaning.
  • Loading branch information
mkarlesky committed Nov 28, 2024
1 parent 3d5680a commit 4b0fdb5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 55 deletions.
92 changes: 46 additions & 46 deletions lib/ceedling/test_context_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,45 +234,14 @@ def ingest_includes(filepath, includes)
end
end

# Exposed for testing (called from private `code_lines()`)
def clean_code_line(line, comment_block)
_line = sanitize_encoding( line )

# Remove line comments
_line.gsub!(/\/\/.*$/, '')

# Handle end of previously begun comment block
if comment_block
if _line.include?( '*/' )
# Turn off comment block handling state
comment_block = false

# Remove everything up to end of comment block
_line.gsub!(/^.*\*\//, '')
else
# Ignore contents of the line if its entirely within a comment block
return '', comment_block
end

end

# Block comments inside a C string are valid C, but we remove to simplify other parsing.
# No code we care about will be inside a C string.
# Note that we're not attempting the complex case of multiline string enclosed comment blocks
_line.gsub!(/"\s*\/\*.*"/, '')

# Remove single-line block comments
_line.gsub!(/\/\*.*\*\//, '')

# Handle beginning of any remaining multiline comment block
if _line.include?( '/*' )
comment_block = true

# Remove beginning of block comment
_line.gsub!(/\/\*.*/, '')
end

return _line, comment_block
# Exposed for testing
def code_lines(input)
comment_block = false
# Far more memory efficient and faster (for large files) than slurping entire file into memory
input.each_line do |line|
_line, comment_block = clean_code_line( line, comment_block )
yield( _line )
end
end

private #################################
Expand Down Expand Up @@ -401,13 +370,44 @@ def form_file_key( filepath )
return filepath.to_s.to_sym
end

def code_lines(input)
comment_block = false
# Far more memory efficient and faster (for large files) than slurping entire file into memory
input.each_line do |line|
_line, comment_block = clean_code_line( line, comment_block )
yield( _line )
end
def clean_code_line(line, comment_block)
_line = sanitize_encoding( line )

# Remove line comments
_line.gsub!(/\/\/.*$/, '')

# Handle end of previously begun comment block
if comment_block
if _line.include?( '*/' )
# Turn off comment block handling state
comment_block = false

# Remove everything up to end of comment block
_line.gsub!(/^.*\*\//, '')
else
# Ignore contents of the line if its entirely within a comment block
return '', comment_block
end

end

# Block comments inside a C string are valid C, but we remove to simplify other parsing.
# No code we care about will be inside a C string.
# Note that we're not attempting the complex case of multiline string enclosed comment blocks
_line.gsub!(/"\s*\/\*.*"/, '')

# Remove single-line block comments
_line.gsub!(/\/\*.*\*\//, '')

# Handle beginning of any remaining multiline comment block
if _line.include?( '/*' )
comment_block = true

# Remove beginning of block comment
_line.gsub!(/\/\*.*/, '')
end

return _line, comment_block
end

# Note: This method modifies encoding in place (encode!) in an attempt to reduce long string copies
Expand Down
14 changes: 5 additions & 9 deletions spec/test_context_extractor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
end
end

context "#clean_code_line" do
it "should clean code of encoding problems and comments a line at a time" do
context "#code_lines" do
it "should clean code of encoding problems and comments" do
file_contents = <<~CONTENTS
/* TEST_SOURCE_FILE("foo.c") */ // Eliminate single line comment block
// TEST_SOURCE_FILE("bar.c") // Eliminate single line comment
Expand All @@ -109,16 +109,12 @@
CONTENTS

got = []
comment_block = false

file_contents.split( "\n" ).each do |line|
_line, comment_block = @extractor.clean_code_line( line, comment_block )
_line.strip!
got << _line if !_line.empty?
@extractor.code_lines( StringIO.new( file_contents ) ) do |line|
line.strip!
got << line if !line.empty?
end

got.compact!

expected = [
'Some text', # ⛔️ removed with encoding sanitizing
'More text',
Expand Down

0 comments on commit 4b0fdb5

Please sign in to comment.