Skip to content

Commit

Permalink
Add line breaks after each stitched file if missing. (#46)
Browse files Browse the repository at this point in the history
Breaking change with existing behavior.
  • Loading branch information
Viatorus authored Aug 15, 2021
1 parent 4633b5a commit 5f2aa90
Showing 3 changed files with 118 additions and 11 deletions.
25 changes: 16 additions & 9 deletions src/quom/quom.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import re
from pathlib import Path
from queue import Queue
@@ -46,6 +47,7 @@ def __init__(self, src_file_path: Union[Path, str], dst: TextIO, stitch_format:
self.__processed_files = set()
self.__source_files = Queue()
self.__cont_lb = CONTINUOUS_LINE_BREAK_START
self.__used_linesep = None
self.__prev_token = EmptyToken()

self.__process_file(Path(), src_file_path, False, True)
@@ -56,12 +58,7 @@ def __init__(self, src_file_path: Union[Path, str], dst: TextIO, stitch_format:
.format(stitch_format))
while not self.__source_files.empty():
self.__process_file(Path(), self.__source_files.get(), True)
# Write last token.
self.__write_token(self.__prev_token, True)
elif self.__cont_lb == CONTINUOUS_LINE_BREAK_START or not isinstance(self.__prev_token,
LinebreakWhitespaceToken):
# Write last token, if not a continuous line break.
self.__write_token(self.__prev_token, True)
self.__write_line_break_if_missing()

def __process_file(self, relative_path: Path, include_path: Path, is_source_file: bool,
is_main_header=False):
@@ -109,9 +106,8 @@ def __write_token(self, token: Token, is_main_header: bool):
if self.__is_cont_line_break(token):
return

# Write previous token, store current.
if self.__prev_token:
self.__dst.write(str(self.__prev_token.raw))
# Write token and store.
self.__dst.write(str(token.raw))
self.__prev_token = token

@staticmethod
@@ -171,10 +167,15 @@ def __scan_for_source_files_stitch(self, token: Token) -> bool:

while not self.__source_files.empty():
self.__process_file(Path(), self.__source_files.get(), True)
self.__write_line_break_if_missing()

return True

def __is_cont_line_break(self, token: Token) -> bool:
# Save a used line break for later.
if self.__used_linesep is None and isinstance(token, LinebreakWhitespaceToken):
self.__used_linesep = token.raw

if not self.__trim:
return False

@@ -187,3 +188,9 @@ def __is_cont_line_break(self, token: Token) -> bool:
self.__cont_lb = CONTINUOUS_LINE_BREAK_START

return self.__cont_lb >= CONTINUOUS_BREAK_REACHED

def __write_line_break_if_missing(self):
if not isinstance(self.__prev_token, LinebreakWhitespaceToken):
if self.__used_linesep is None:
self.__used_linesep = os.linesep # fallback
self.__dst.write(self.__used_linesep)
6 changes: 4 additions & 2 deletions tests/test_quom/test_last_source_file.py
Original file line number Diff line number Diff line change
@@ -53,7 +53,8 @@
#include <algorithm>
int foo = 42;"""
int foo = 42;
"""

RESULT_NORMAL_WITHOUT_TRIM = """\
#pragma once
@@ -78,7 +79,8 @@
#include <algorithm>
int foo = 42;"""
int foo = 42;
"""


def init():
98 changes: 98 additions & 0 deletions tests/test_quom/test_line_breaks_when_stitching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from io import StringIO
from pathlib import Path

from quom import Quom

FILE_MAIN_CPP = """\
#include "a.hpp"
int main() {
return 0;
}
// Stitch Begin
// End
"""

FILE_A_HPP = 'int a;'

FILE_A_CPP = """\
#include "b.hpp"
#include "c.hpp"
void mid() {}"""

FILE_B_HPP = 'int b;'
FILE_C_HPP = 'int c;'

FILE_B_CPP = """\
#include <b>"""

FILE_C_CPP = """\
#include <c>"""

RESULT = """\
int a;
int main() {
return 0;
}
// Stitch Begin
// End
int b;
int c;
void mid() {}
#include <b>
#include <c>
"""

RESULT_STITCH = """\
int a;
int main() {
return 0;
}
int b;
int c;
void mid() {}
#include <b>
#include <c>
// End
"""


def init():
with open('main.hpp', 'w+', encoding='utf-8') as file:
file.write(FILE_MAIN_CPP)

with open('a.hpp', 'w+', encoding='utf-8') as file:
file.write(FILE_A_HPP)

with open('a.cpp', 'w+', encoding='utf-8') as file:
file.write(FILE_A_CPP)

with open('b.hpp', 'w+', encoding='utf-8') as file:
file.write(FILE_B_HPP)

with open('b.cpp', 'w+', encoding='utf-8') as file:
file.write(FILE_B_CPP)

with open('c.hpp', 'w+', encoding='utf-8') as file:
file.write(FILE_C_HPP)

with open('c.cpp', 'w+', encoding='utf-8') as file:
file.write(FILE_C_CPP)


def test_add_line_break_in_stitched_files_if_missing(fs):
init()

dst = StringIO()
Quom(Path('main.hpp'), dst)

assert dst.getvalue() == RESULT


def test_add_line_break_in_stitched_files_if_missing_at_stitch_location(fs):
init()

dst = StringIO()
Quom(Path('main.hpp'), dst, stitch_format='Stitch Begin')

assert dst.getvalue() == RESULT_STITCH

0 comments on commit 5f2aa90

Please sign in to comment.