mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2025-06-26 20:38:48 +08:00
Merge pull request #108 from eivindt/test-equal-string-len
New asserter: TEST_ASSERT_EQUAL_STRING_LEN
This commit is contained in:
@ -169,10 +169,18 @@ String Assertions
|
||||
|
||||
Compare two null-terminate strings. Fail if any character is different or if the lengths are different.
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len)
|
||||
|
||||
Compare two strings. Fail if any character is different, stop comparing after len characters.
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message)
|
||||
|
||||
Compare two null-terminate strings. Fail if any character is different or if the lengths are different. Output a custom message on failure.
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message)
|
||||
|
||||
Compare two strings. Fail if any character is different, stop comparing after len characters. Output a custom message on failure.
|
||||
|
||||
Pointer Assertions
|
||||
------------------
|
||||
|
||||
|
@ -105,6 +105,8 @@ class ParseOutput
|
||||
else
|
||||
@className = 0
|
||||
end
|
||||
else
|
||||
@className = 0
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -178,10 +178,18 @@ TEST_ASSERT_EQUAL_STRING(expected, actual)
|
||||
|
||||
Compare two null-terminate strings. Fail if any character is different or if the lengths are different.
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len)
|
||||
|
||||
Compare two strings. Fail if any character is different, stop comparing after len characters.
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message)
|
||||
|
||||
Compare two null-terminate strings. Fail if any character is different or if the lengths are different. Output a custom message on failure.
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message)
|
||||
|
||||
Compare two strings. Fail if any character is different, stop comparing after len characters. Output a custom message on failure.
|
||||
|
||||
------------------
|
||||
Pointer Assertions
|
||||
------------------
|
||||
|
106
src/unity.c
106
src/unity.c
@ -110,6 +110,42 @@ void UnityPrint(const char* string)
|
||||
}
|
||||
}
|
||||
|
||||
void UnityPrintLen(const char* string, const _UU32 length)
|
||||
{
|
||||
const char* pch = string;
|
||||
|
||||
if (pch != NULL)
|
||||
{
|
||||
while (*pch && (pch - string) < length)
|
||||
{
|
||||
// printable characters plus CR & LF are printed
|
||||
if ((*pch <= 126) && (*pch >= 32))
|
||||
{
|
||||
UNITY_OUTPUT_CHAR(*pch);
|
||||
}
|
||||
//write escaped carriage returns
|
||||
else if (*pch == 13)
|
||||
{
|
||||
UNITY_OUTPUT_CHAR('\\');
|
||||
UNITY_OUTPUT_CHAR('r');
|
||||
}
|
||||
//write escaped line feeds
|
||||
else if (*pch == 10)
|
||||
{
|
||||
UNITY_OUTPUT_CHAR('\\');
|
||||
UNITY_OUTPUT_CHAR('n');
|
||||
}
|
||||
// unprintable characters are shown as codes
|
||||
else
|
||||
{
|
||||
UNITY_OUTPUT_CHAR('\\');
|
||||
UnityPrintNumberHex((_U_UINT)*pch, 2);
|
||||
}
|
||||
pch++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style)
|
||||
{
|
||||
@ -363,6 +399,35 @@ static void UnityPrintExpectedAndActualStrings(const char* expected, const char*
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
static void UnityPrintExpectedAndActualStringsLen(const char* expected, const char* actual, const _UU32 length)
|
||||
{
|
||||
UnityPrint(UnityStrExpected);
|
||||
if (expected != NULL)
|
||||
{
|
||||
UNITY_OUTPUT_CHAR('\'');
|
||||
UnityPrintLen(expected, length);
|
||||
UNITY_OUTPUT_CHAR('\'');
|
||||
}
|
||||
else
|
||||
{
|
||||
UnityPrint(UnityStrNull);
|
||||
}
|
||||
UnityPrint(UnityStrWas);
|
||||
if (actual != NULL)
|
||||
{
|
||||
UNITY_OUTPUT_CHAR('\'');
|
||||
UnityPrintLen(actual, length);
|
||||
UNITY_OUTPUT_CHAR('\'');
|
||||
}
|
||||
else
|
||||
{
|
||||
UnityPrint(UnityStrNull);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------
|
||||
// Assertion & Control Helpers
|
||||
//-----------------------------------------------
|
||||
@ -959,6 +1024,47 @@ void UnityAssertEqualString(const char* expected,
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityAssertEqualStringLen(const char* expected,
|
||||
const char* actual,
|
||||
const _UU32 length,
|
||||
const char* msg,
|
||||
const UNITY_LINE_TYPE lineNumber)
|
||||
{
|
||||
_UU32 i;
|
||||
|
||||
UNITY_SKIP_EXECUTION;
|
||||
|
||||
// if both pointers not null compare the strings
|
||||
if (expected && actual)
|
||||
{
|
||||
for (i = 0; (expected[i] || actual[i]) && i < length; i++)
|
||||
{
|
||||
if (expected[i] != actual[i])
|
||||
{
|
||||
Unity.CurrentTestFailed = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // handle case of one pointers being null (if both null, test should pass)
|
||||
if (expected != actual)
|
||||
{
|
||||
Unity.CurrentTestFailed = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (Unity.CurrentTestFailed)
|
||||
{
|
||||
UnityTestResultsFailBegin(lineNumber);
|
||||
UnityPrintExpectedAndActualStringsLen(expected, actual, length);
|
||||
UnityAddMsgIfSpecified(msg);
|
||||
UNITY_FAIL_AND_BAIL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityAssertEqualStringArray( const char** expected,
|
||||
const char** actual,
|
||||
|
@ -116,6 +116,7 @@
|
||||
//Structs and Strings
|
||||
#define TEST_ASSERT_EQUAL_PTR(expected, actual) UNITY_TEST_ASSERT_EQUAL_PTR((expected), (actual), __LINE__, NULL)
|
||||
#define TEST_ASSERT_EQUAL_STRING(expected, actual) UNITY_TEST_ASSERT_EQUAL_STRING(expected, actual, __LINE__, NULL)
|
||||
#define TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len) UNITY_TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len, __LINE__, NULL)
|
||||
#define TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) UNITY_TEST_ASSERT_EQUAL_MEMORY(expected, actual, len, __LINE__, NULL)
|
||||
|
||||
//Arrays
|
||||
@ -219,7 +220,7 @@
|
||||
|
||||
//Structs and Strings
|
||||
#define TEST_ASSERT_EQUAL_PTR_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, __LINE__, message)
|
||||
#define TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_STRING(expected, actual, __LINE__, message)
|
||||
#define TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message) UNITY_TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len, __LINE__, message)
|
||||
#define TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, message) UNITY_TEST_ASSERT_EQUAL_MEMORY(expected, actual, len, __LINE__, message)
|
||||
|
||||
//Arrays
|
||||
|
@ -440,6 +440,12 @@ void UnityAssertEqualString(const char* expected,
|
||||
const char* msg,
|
||||
const UNITY_LINE_TYPE lineNumber);
|
||||
|
||||
void UnityAssertEqualStringLen(const char* expected,
|
||||
const char* actual,
|
||||
const _UU32 length,
|
||||
const char* msg,
|
||||
const UNITY_LINE_TYPE lineNumber);
|
||||
|
||||
void UnityAssertEqualStringArray( const char** expected,
|
||||
const char** actual,
|
||||
const _UU32 num_elements,
|
||||
@ -599,6 +605,7 @@ extern const char UnityStrErr64[];
|
||||
|
||||
#define UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(_UP)(expected), (_U_SINT)(_UP)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_POINTER)
|
||||
#define UNITY_TEST_ASSERT_EQUAL_STRING(expected, actual, line, message) UnityAssertEqualString((const char*)(expected), (const char*)(actual), (message), (UNITY_LINE_TYPE)line)
|
||||
#define UNITY_TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len, line, message) UnityAssertEqualStringLen((const char*)(expected), (const char*)(actual), (_UU32)(len), (message), (UNITY_LINE_TYPE)line)
|
||||
#define UNITY_TEST_ASSERT_EQUAL_MEMORY(expected, actual, len, line, message) UnityAssertEqualMemory((UNITY_PTR_ATTRIBUTE void*)(expected), (UNITY_PTR_ATTRIBUTE void*)(actual), (_UU32)(len), 1, (message), (UNITY_LINE_TYPE)line)
|
||||
|
||||
#define UNITY_TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_PTR_ATTRIBUTE const void*)(expected), (UNITY_PTR_ATTRIBUTE const void*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_INT)
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <setjmp.h>
|
||||
#include "unity.h"
|
||||
#include <string.h>
|
||||
|
||||
// Dividing by these constants produces +/- infinity.
|
||||
// The rationale is given in UnityAssertFloatIsInf's body.
|
||||
@ -1265,6 +1266,16 @@ void testEqualStrings(void)
|
||||
TEST_ASSERT_EQUAL_STRING("", "");
|
||||
}
|
||||
|
||||
void testEqualStringsLen(void)
|
||||
{
|
||||
const char *testString = "foobar";
|
||||
TEST_ASSERT_EQUAL_STRING_LEN(testString, testString, strlen(testString));
|
||||
TEST_ASSERT_EQUAL_STRING_LEN("foobar", "foobaz", 5);
|
||||
TEST_ASSERT_EQUAL_STRING_LEN("foo", testString, 3);
|
||||
TEST_ASSERT_EQUAL_STRING_LEN(testString, "foo", 3);
|
||||
TEST_ASSERT_EQUAL_STRING_LEN("", "", 3);
|
||||
}
|
||||
|
||||
void testEqualStringsWithCarriageReturnsAndLineFeeds(void)
|
||||
{
|
||||
const char *testString = "foo\r\nbar";
|
||||
@ -1283,6 +1294,13 @@ void testNotEqualString1(void)
|
||||
VERIFY_FAILS_END
|
||||
}
|
||||
|
||||
void testNotEqualStringLen1(void)
|
||||
{
|
||||
EXPECT_ABORT_BEGIN
|
||||
TEST_ASSERT_EQUAL_STRING_LEN("foobar", "foobaz", 6);
|
||||
VERIFY_FAILS_END
|
||||
}
|
||||
|
||||
void testNotEqualString2(void)
|
||||
{
|
||||
EXPECT_ABORT_BEGIN
|
||||
@ -1290,6 +1308,13 @@ void testNotEqualString2(void)
|
||||
VERIFY_FAILS_END
|
||||
}
|
||||
|
||||
void testNotEqualStringLen2(void)
|
||||
{
|
||||
EXPECT_ABORT_BEGIN
|
||||
TEST_ASSERT_EQUAL_STRING_LEN("foo", "", 3);
|
||||
VERIFY_FAILS_END
|
||||
}
|
||||
|
||||
void testNotEqualString3(void)
|
||||
{
|
||||
EXPECT_ABORT_BEGIN
|
||||
@ -1297,6 +1322,13 @@ void testNotEqualString3(void)
|
||||
VERIFY_FAILS_END
|
||||
}
|
||||
|
||||
void testNotEqualStringLen3(void)
|
||||
{
|
||||
EXPECT_ABORT_BEGIN
|
||||
TEST_ASSERT_EQUAL_STRING_LEN("", "bar", 3);
|
||||
VERIFY_FAILS_END
|
||||
}
|
||||
|
||||
void testNotEqualString4(void)
|
||||
{
|
||||
EXPECT_ABORT_BEGIN
|
||||
@ -1304,6 +1336,13 @@ void testNotEqualString4(void)
|
||||
VERIFY_FAILS_END
|
||||
}
|
||||
|
||||
void testNotEqualStringLen4(void)
|
||||
{
|
||||
EXPECT_ABORT_BEGIN
|
||||
TEST_ASSERT_EQUAL_STRING_LEN("bar\r", "bar\n", 4);
|
||||
VERIFY_FAILS_END
|
||||
}
|
||||
|
||||
void testNotEqualString5(void)
|
||||
{
|
||||
const char str1[] = { 0x41, 0x42, 0x03, 0x00 };
|
||||
|
Reference in New Issue
Block a user