From 0319209b21a111099a14bbc22d4221b10992dd9d Mon Sep 17 00:00:00 2001 From: jf-home Date: Wed, 28 Aug 2024 00:57:04 +0800 Subject: [PATCH] =?UTF-8?q?=E9=92=88=E5=AF=B9=E5=A4=9A=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=B7=AF=E5=BE=84=E6=98=BE=E7=A4=BA=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E5=81=9A=E4=BA=86=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdparty/unicstl-unity/README.md | 16 +++++++ 3rdparty/unicstl-unity/src/unity.c | 48 ++++++++++++++++++++ 3rdparty/unicstl-unity/src/unity_config.h | 2 + 3rdparty/unicstl-unity/src/unity_internals.h | 16 ++++++- test/test_stack.c | 4 -- 5 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 3rdparty/unicstl-unity/README.md diff --git a/3rdparty/unicstl-unity/README.md b/3rdparty/unicstl-unity/README.md new file mode 100644 index 0000000..153e6b8 --- /dev/null +++ b/3rdparty/unicstl-unity/README.md @@ -0,0 +1,16 @@ + +# 问题和解决方案 + +## 1. 多文件打印测试文件路径不合理的问题 +新增宏定义,可放于unity_config.h中,用来让测试文件路径显示为当前测试文件,而不是主test.c文件。 +方便针对多文件测试,快速定位错误。 +```c +#define RUN_TEST_WITH_CURRENT_FILE 1 +``` + +另外,修改了两个函数,新增文件路径参数。 +```c +void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum, const char* FileName); + +void UnityConcludeTest(const char *FileName); +``` diff --git a/3rdparty/unicstl-unity/src/unity.c b/3rdparty/unicstl-unity/src/unity.c index 04d716d..3b6e7fc 100644 --- a/3rdparty/unicstl-unity/src/unity.c +++ b/3rdparty/unicstl-unity/src/unity.c @@ -543,6 +543,7 @@ static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line) } /*-----------------------------------------------*/ +#ifndef RUN_TEST_WITH_CURRENT_FILE void UnityConcludeTest(void) { if (Unity.CurrentTestIgnored) @@ -565,6 +566,31 @@ void UnityConcludeTest(void) UNITY_PRINT_EOL(); UNITY_FLUSH_CALL(); } +#else +void UnityConcludeTest(const char *FileName) +{ + if (Unity.CurrentTestIgnored) + { + Unity.TestIgnores++; + } + else if (!Unity.CurrentTestFailed) + { + UnityTestResultsBegin(FileName, Unity.CurrentTestLineNumber); + UnityPrint(UnityStrPass); + } + else + { + Unity.TestFailures++; + } + + Unity.CurrentTestFailed = 0; + Unity.CurrentTestIgnored = 0; + UNITY_PRINT_EXEC_TIME(); + UNITY_PRINT_EOL(); + UNITY_FLUSH_CALL(); +} +#endif + /*-----------------------------------------------*/ static void UnityAddMsgIfSpecified(const char* msg) @@ -2190,6 +2216,7 @@ void UnityMessage(const char* msg, const UNITY_LINE_TYPE line) /*-----------------------------------------------*/ /* If we have not defined our own test runner, then include our default test runner to make life easier */ #ifndef UNITY_SKIP_DEFAULT_RUNNER +#ifndef RUN_TEST_WITH_CURRENT_FILE void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum) { Unity.CurrentTestName = FuncName; @@ -2209,6 +2236,27 @@ void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int UNITY_EXEC_TIME_STOP(); UnityConcludeTest(); } +#else +void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum, const char* FileName) +{ + Unity.CurrentTestName = FuncName; + Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum; + Unity.NumberOfTests++; + UNITY_CLR_DETAILS(); + UNITY_EXEC_TIME_START(); + if (TEST_PROTECT()) + { + setUp(); + Func(); + } + if (TEST_PROTECT()) + { + tearDown(); + } + UNITY_EXEC_TIME_STOP(); + UnityConcludeTest(FileName); +} +#endif #endif /*-----------------------------------------------*/ diff --git a/3rdparty/unicstl-unity/src/unity_config.h b/3rdparty/unicstl-unity/src/unity_config.h index 70df6a4..8f53f2f 100644 --- a/3rdparty/unicstl-unity/src/unity_config.h +++ b/3rdparty/unicstl-unity/src/unity_config.h @@ -5,4 +5,6 @@ #define UNITY_OUTPUT_COLOR 1 #define UNITY_USE_FLUSH_STDOUT 1 +#define RUN_TEST_WITH_CURRENT_FILE 1 + #endif diff --git a/3rdparty/unicstl-unity/src/unity_internals.h b/3rdparty/unicstl-unity/src/unity_internals.h index 74a63b2..8a0527d 100644 --- a/3rdparty/unicstl-unity/src/unity_internals.h +++ b/3rdparty/unicstl-unity/src/unity_internals.h @@ -546,11 +546,19 @@ extern struct UNITY_STORAGE_T Unity; void UnityBegin(const char* filename); int UnityEnd(void); void UnitySetTestFile(const char* filename); +#ifndef RUN_TEST_WITH_CURRENT_FILE void UnityConcludeTest(void); +#else +void UnityConcludeTest(const char *FileName); +#endif #ifndef RUN_TEST +#ifndef RUN_TEST_WITH_CURRENT_FILE void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum); #else +void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum, const char* FileName); +#endif +#else #define UNITY_SKIP_DEFAULT_RUNNER #endif @@ -817,9 +825,13 @@ extern const char UnityStrErrShorthand[]; #ifndef RUN_TEST #ifdef UNITY_SUPPORT_VARIADIC_MACROS #define RUN_TEST(...) RUN_TEST_AT_LINE(__VA_ARGS__, __LINE__, throwaway) +#ifndef RUN_TEST_WITH_CURRENT_FILE #define RUN_TEST_AT_LINE(func, line, ...) UnityDefaultTestRun(func, #func, line) -#endif -#endif +#else +#define RUN_TEST_AT_LINE(func, line, ...) UnityDefaultTestRun(func, #func, line, __FILE__) +#endif // RUN_TEST_WITH_CURRENT_FILE +#endif // UNITY_SUPPORT_VARIADIC_MACROS +#endif // RUN_TEST /* Enable default macros for masking param tests test cases */ #ifdef UNITY_SUPPORT_TEST_CASES diff --git a/test/test_stack.c b/test/test_stack.c index e275889..418360c 100644 --- a/test/test_stack.c +++ b/test/test_stack.c @@ -48,9 +48,5 @@ static void test_stack_num(void) void test_stack(void) { - // UNITY_BEGIN(); - RUN_TEST(test_stack_num); - - // UNITY_END(); }