Skip to content

Commit f3b7880

Browse files
committed
Add unit tests for CommonArgParser and DataReader
- test_common_arg_parser.cpp: 27 tests covering all parsing options - test_data_reader.cpp: 14 tests for file/stdin processing Bug fix in DataReader: - processFile() now respects inputFormat parameter instead of only using file extension detection. This allows -if csv to work on files without .csv extension.
1 parent 0fed7a1 commit f3b7880

File tree

4 files changed

+745
-3
lines changed

4 files changed

+745
-3
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ endif
1919
# Source files
2020
SOURCES = src/sensor-data.cpp
2121
LIB_SOURCES = src/csv_parser.cpp src/json_parser.cpp src/error_detector.cpp src/file_utils.cpp src/sensor_data_transformer.cpp src/data_counter.cpp src/error_lister.cpp src/error_summarizer.cpp src/stats_analyser.cpp src/latest_finder.cpp
22-
TEST_SOURCES = tests/test_csv_parser.cpp tests/test_json_parser.cpp tests/test_error_detector.cpp tests/test_file_utils.cpp tests/test_date_utils.cpp
22+
TEST_SOURCES = tests/test_csv_parser.cpp tests/test_json_parser.cpp tests/test_error_detector.cpp tests/test_file_utils.cpp tests/test_date_utils.cpp tests/test_common_arg_parser.cpp tests/test_data_reader.cpp
2323

2424
# Object files
2525
LIB_OBJECTS = $(LIB_SOURCES:.cpp=.o)
26-
TEST_EXECUTABLES = test_csv_parser test_json_parser test_error_detector test_file_utils test_date_utils
26+
TEST_EXECUTABLES = test_csv_parser test_json_parser test_error_detector test_file_utils test_date_utils test_common_arg_parser test_data_reader
2727

2828
TARGET = sensor-data
2929

@@ -45,6 +45,8 @@ test: $(LIB_OBJECTS)
4545
@$(CXX) $(CPPFLAGS) $(CXXFLAGS) tests/test_error_detector.cpp src/error_detector.o -o test_error_detector $(LDFLAGS) && ./test_error_detector
4646
@$(CXX) $(CPPFLAGS) $(CXXFLAGS) tests/test_file_utils.cpp src/file_utils.o -o test_file_utils $(LDFLAGS) && ./test_file_utils
4747
@$(CXX) $(CPPFLAGS) $(CXXFLAGS) tests/test_date_utils.cpp -o test_date_utils $(LDFLAGS) && ./test_date_utils
48+
@$(CXX) $(CPPFLAGS) $(CXXFLAGS) tests/test_common_arg_parser.cpp src/file_utils.o -o test_common_arg_parser $(LDFLAGS) && ./test_common_arg_parser
49+
@$(CXX) $(CPPFLAGS) $(CXXFLAGS) tests/test_data_reader.cpp src/csv_parser.o src/json_parser.o src/file_utils.o -o test_data_reader $(LDFLAGS) && ./test_data_reader
4850
@echo "All unit tests passed!"
4951

5052
# Run integration tests (requires bash)
@@ -58,6 +60,7 @@ test-integration: $(TARGET)
5860
@bash tests/test_stats.sh
5961
@bash tests/test_csv_input.sh
6062
@bash tests/test_error_handling.sh
63+
@bash tests/test_latest.sh
6164
@echo "All integration tests passed!"
6265

6366
# Run all tests

include/data_reader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ class DataReader {
101101
}
102102
}
103103

104-
bool isCSV = FileUtils::isCsvFile(filename);
104+
// Determine format: explicit inputFormat takes precedence, otherwise detect from extension
105+
bool isCSV = (inputFormat == "csv") || (inputFormat == "json" ? false : FileUtils::isCsvFile(filename));
105106

106107
if (tailLines > 0) {
107108
// Use tail mode

0 commit comments

Comments
 (0)