Skip to content

Commit 917c876

Browse files
committed
feat: add tests for Strutils::getCNPJNumbers
fix: update regex in Strutils::getCNPJNumbers to keep letters and digits chore: update CMakeLists.txt to handle test options more flexibly chore: update CI/CD scripts to use new test option chore: update security analysis workflow to use new test option
1 parent a508c15 commit 917c876

6 files changed

Lines changed: 60 additions & 10 deletions

File tree

.github/workflows/codeql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
cmake .. -G Ninja \
5353
-DCMAKE_BUILD_TYPE=Release \
5454
-DCMAKE_CXX_FLAGS="-Wall -Wextra -fstack-protector-strong -D_FORTIFY_SOURCE=3" \
55-
-DCompileTestsApiFramework=OFF
55+
-Dcppapiframework_BUILD_TESTS=OFF
5656
5757
- name: Build Project
5858
run: |

.github/workflows/security-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
-DCMAKE_BUILD_TYPE=Release \
5151
-DCMAKE_CXX_FLAGS="-Wall -Wextra -fstack-protector-strong -D_FORTIFY_SOURCE=3 -fPIE -Wformat -Wformat-security" \
5252
-DCMAKE_EXE_LINKER_FLAGS="-pie -Wl,-z,relro,-z,now -Wl,-z,noexecstack" \
53-
-DCompileTestsApiFramework=OFF
53+
-Dcppapiframework_BUILD_TESTS=OFF
5454
5555
- name: Build Project
5656
run: |

CMakeLists.txt

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,32 @@ set(projectname cppapiframework)
33
cmake_minimum_required(VERSION 3.6.0)
44
project(${projectname} VERSION 0.1.0)
55

6+
if ("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
7+
message(STATUS "Configuring ${projectname} as a standalone project")
8+
# Quando usado como projeto standalone, ativar testes por padrão
9+
set(_DEFAULT_PROJECT_BUILD_TESTS ON)
10+
else()
11+
set(_DEFAULT_PROJECT_BUILD_TESTS OFF)
12+
endif()
13+
14+
# --- Test options: nova e legada (retrocompatibilidade) --------------------
15+
# opção legada para compatibilidade com projetos que ainda usam o nome antigo
16+
option(CompileTestsApiFramework "Legacy: build tests for ${projectname} (deprecated)" OFF)
17+
18+
# opção nomeada pelo projeto, recomendada para controle a partir do CMake superior
19+
# o valor padrão depende se estamos no topo (standalone) ou como subprojeto
20+
option(${projectname}_BUILD_TESTS "Build tests for ${projectname}" ${_DEFAULT_PROJECT_BUILD_TESTS})
21+
22+
# Somente inclua CTest se ainda não estiver definido e se alguma opção de teste for requerida
23+
if(NOT DEFINED BUILD_TESTING)
24+
if(CompileTestsApiFramework OR ${${projectname}_BUILD_TESTS})
25+
include(CTest)
26+
endif()
27+
else()
28+
# BUILD_TESTING já foi definido pelo projeto superior; nada a fazer
29+
endif()
30+
31+
632
if (NOT DEFINED CPPAPIFRAMEWORK_APPLY_FLAGS)
733
message("Applying flags")
834
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
@@ -13,14 +39,12 @@ if (NOT DEFINED CPPAPIFRAMEWORK_APPLY_FLAGS)
1339
set(CMAKE_C_EXTENSIONS ON)
1440
endif()
1541

16-
option(CompileTestsApiFramework "CompileTestsApiFramework" ON)
17-
1842
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
1943
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
2044
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
2145

2246
if(NOT DEFINED DISABLE_MANUAL_FIND_PACKAGES)
23-
if ("${CompileTestsApiFramework}" STREQUAL "ON")
47+
if (BUILD_TESTING AND cppapiframework_BUILD_TESTS)
2448
find_package(GTest REQUIRED)
2549
include(GoogleTest)
2650
endif()
@@ -138,7 +162,8 @@ endif()
138162

139163
target_link_libraries(${projectname} ${MYSQLCPPCONN_LIBRARY})
140164

141-
if ("${CompileTestsApiFramework}" STREQUAL "ON")
142-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tests)
165+
if (BUILD_TESTING AND cppapiframework_BUILD_TESTS)
166+
message("-- Compilando testes para ${projectname}")
167+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tests)
143168
endif()
144169

security-scan.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ cmake .. -G Ninja \
4343
-DCMAKE_BUILD_TYPE=Release \
4444
-DCMAKE_CXX_FLAGS="-Wall -Wextra -fstack-protector-strong -D_FORTIFY_SOURCE=3 -fPIE" \
4545
-DCMAKE_EXE_LINKER_FLAGS="-pie -Wl,-z,relro,-z,now" \
46-
-DCompileTestsApiFramework=OFF > ../security-reports/build-config.log 2>&1
46+
-Dcppapiframework_BUILD_TESTS=OFF > ../security-reports/build-config.log 2>&1
4747

4848
cmake --build . --config Release --target cppapiframework -j $(nproc) > ../security-reports/build.log 2>&1
4949

src/utils/Strutils.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ class Strutils {
6060
}
6161

6262
static inline auto getCNPJNumbers(const std::string &cnpj) -> std::string {
63-
return std::regex_replace(cnpj, std::regex("[^0-9]*"),
64-
std::string("$1"));
63+
return std::regex_replace(cnpj, std::regex("[^A-Za-z0-9]*"), "");
6564
}
6665

6766
static inline auto join(const std::span<const std::string> &vec,

tests/test_strutils_cnpj.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "utils/Strutils.hpp"
2+
#include <gtest/gtest.h>
3+
4+
TEST(StrutilsCNPJ, RemovesPunctuationAndKeepsDigits) {
5+
const std::string input = "12.345.678/0001-95";
6+
const std::string out = Strutils::getCNPJNumbers(input);
7+
EXPECT_EQ(out, "12345678000195");
8+
}
9+
10+
TEST(StrutilsCNPJ, KeepsLettersAndDigitsForFutureFormat) {
11+
const std::string input = "AB12.CD34/EF56-78";
12+
const std::string out = Strutils::getCNPJNumbers(input);
13+
EXPECT_EQ(out, "AB12CD34EF5678");
14+
}
15+
16+
TEST(StrutilsCNPJ, RemovesSpacesAndSymbols) {
17+
const std::string input = " A B . 1 2 - 3 4 ";
18+
const std::string out = Strutils::getCNPJNumbers(input);
19+
EXPECT_EQ(out, "AB1234");
20+
}
21+
22+
TEST(StrutilsCNPJ, EmptyStringReturnsEmpty) {
23+
const std::string input;
24+
const std::string out = Strutils::getCNPJNumbers(input);
25+
EXPECT_EQ(out, "");
26+
}

0 commit comments

Comments
 (0)