Skip to content

Commit 491fe70

Browse files
Merge pull request #29 from dreamer-coding/pr_cstr_assume_cpp
Pull request for C String assumptions to work with C++ STL string
2 parents 0401f8e + 4e67650 commit 491fe70

File tree

4 files changed

+55
-23
lines changed

4 files changed

+55
-23
lines changed

code/logic/fossil/test/assume.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,53 +2177,83 @@ extern "C" {
21772177
* @param actual The actual byte string.
21782178
* @param expected The expected byte string.
21792179
*/
2180+
#ifdef __cplusplus
2181+
#define ASSUME_ITS_EQUAL_BSTR(actual, expected) \
2182+
FOSSIL_TEST_ASSUME(strcmp((actual.c_str()), (expected.c_str())) == 0, "Expected byte string " #actual " to be equal to " #expected)
2183+
#else
21802184
#define ASSUME_ITS_EQUAL_BSTR(actual, expected) \
21812185
FOSSIL_TEST_ASSUME(strcmp((const char*)(actual), (const char*)(expected)) == 0, "Expected byte string " #actual " to be equal to " #expected)
2186+
#endif
21822187

21832188
/**
21842189
* @brief Assumes that the given byte strings are not equal.
21852190
*
21862191
* @param actual The actual byte string.
21872192
* @param expected The expected byte string.
21882193
*/
2194+
#ifdef __cplusplus
2195+
#define ASSUME_NOT_EQUAL_BSTR(actual, expected) \
2196+
FOSSIL_TEST_ASSUME(strcmp((actual.c_str()), (expected.c_str())) != 0, "Expected byte string " #actual " to not be equal to " #expected)
2197+
#else
21892198
#define ASSUME_NOT_EQUAL_BSTR(actual, expected) \
21902199
FOSSIL_TEST_ASSUME(strcmp((const char*)(actual), (const char*)(expected)) != 0, "Expected byte string " #actual " to not be equal to " #expected)
2200+
#endif
21912201

21922202
/**
21932203
* @brief Assumes that the length of the given byte string is equal to the expected length.
21942204
*
21952205
* @param actual The actual byte string.
21962206
* @param expected_len The expected length of the byte string.
21972207
*/
2208+
#ifdef __cplusplus
2209+
#define ASSUME_ITS_LENGTH_EQUAL_BSTR(actual, expected_len) \
2210+
FOSSIL_TEST_ASSUME(strlen((actual.c_str())) == (expected_len), "Expected length of byte string " #actual " to be equal to " #expected_len)
2211+
#else
21982212
#define ASSUME_ITS_LENGTH_EQUAL_BSTR(actual, expected_len) \
21992213
FOSSIL_TEST_ASSUME(strlen((const char*)(actual)) == (expected_len), "Expected length of byte string " #actual " to be equal to " #expected_len)
2214+
#endif
22002215

22012216
/**
22022217
* @brief Assumes that the given C strings are equal.
22032218
*
22042219
* @param actual The actual C string.
22052220
* @param expected The expected C string.
22062221
*/
2222+
#ifdef __cplusplus
2223+
#define ASSUME_ITS_EQUAL_CSTR(actual, expected) \
2224+
FOSSIL_TEST_ASSUME(strcmp((actual.c_str()), (expected.c_str())) == 0, "Expected C string " #actual " to be equal to " #expected)
2225+
#else
22072226
#define ASSUME_ITS_EQUAL_CSTR(actual, expected) \
22082227
FOSSIL_TEST_ASSUME(strcmp((actual), (expected)) == 0, "Expected C string " #actual " to be equal to " #expected)
2228+
#endif
22092229

22102230
/**
22112231
* @brief Assumes that the given C strings are not equal.
22122232
*
22132233
* @param actual The actual C string.
22142234
* @param expected The expected C string.
22152235
*/
2236+
#ifdef __cplusplus
2237+
#define ASSUME_NOT_EQUAL_CSTR(actual, expected) \
2238+
FOSSIL_TEST_ASSUME(strcmp((actual.c_str()), (expected.c_str())) != 0, "Expected C string " #actual " to not be equal to " #expected)
2239+
#else
22162240
#define ASSUME_NOT_EQUAL_CSTR(actual, expected) \
22172241
FOSSIL_TEST_ASSUME(strcmp((actual), (expected)) != 0, "Expected C string " #actual " to not be equal to " #expected)
2242+
#endif
22182243

22192244
/**
22202245
* @brief Assumes that the length of the given C string is equal to the expected length.
22212246
*
22222247
* @param actual The actual C string.
22232248
* @param expected_len The expected length of the C string.
22242249
*/
2250+
#ifdef __cplusplus
2251+
#define ASSUME_ITS_LENGTH_EQUAL_CSTR(actual, expected_len) \
2252+
FOSSIL_TEST_ASSUME(strlen((actual.c_str())) == (expected_len), "Expected length of C string " #actual " to be equal to " #expected_len)
2253+
#else
22252254
#define ASSUME_ITS_LENGTH_EQUAL_CSTR(actual, expected_len) \
22262255
FOSSIL_TEST_ASSUME(strlen((actual)) == (expected_len), "Expected length of C string " #actual " to be equal to " #expected_len)
2256+
#endif
22272257

22282258
#ifdef __cplusplus
22292259
}

code/tests/cases/test_bdd.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* -----------------------------------------------------------------------------
1414
*/
1515
#include <fossil/test/framework.h>
16+
#include <string>
1617

1718
// * * * * * * * * * * * * * * * * * * * * * * * *
1819
// * Fossil Logic Test Utilites
@@ -106,32 +107,32 @@ FOSSIL_TEST_CASE(cpp_xbdd_empty_cart) {
106107
FOSSIL_TEST_CASE(cpp_xbdd_valid_login) {
107108
GIVEN("a registered user with valid credentials") {
108109
// Set up the context
109-
const char* validUsername = "user123";
110-
const char* validPassword = "pass456";
110+
std::string validUsername = "user123";
111+
std::string validPassword = "pass456";
111112

112113
WHEN("the user provides correct username and password") {
113114
// Perform the action of user login
114-
const char* inputUsername = "user123";
115-
const char* inputPassword = "pass456";
115+
std::string inputUsername = "user123";
116+
std::string inputPassword = "pass456";
116117

117118
THEN("the login should be successful") {
118119
// Check the expected outcome
119120
// Simulate login validation
120-
FOSSIL_TEST_ASSUME(strcmp(inputUsername, validUsername) == 0, "Username should match");
121-
FOSSIL_TEST_ASSUME(strcmp(inputPassword, validPassword) == 0, "Password should match");
121+
FOSSIL_TEST_ASSUME(inputUsername == validUsername, "Username should match");
122+
FOSSIL_TEST_ASSUME(inputPassword == validPassword, "Password should match");
122123
}
123124
}
124125

125126
WHEN("the user provides incorrect password") {
126127
// Perform the action of user login
127-
const char* inputUsername = "user123";
128-
const char* inputPassword = "wrongpass";
128+
std::string inputUsername = "user123";
129+
std::string inputPassword = "wrongpass";
129130

130131
THEN("the login should fail with an error message") {
131132
// Check the expected outcome
132133
// Simulate login validation
133-
FOSSIL_TEST_ASSUME(strcmp(inputUsername, validUsername) == 0, "Username should match");
134-
FOSSIL_TEST_ASSUME(strcmp(inputPassword, validPassword) != 0, "Password should not match");
134+
FOSSIL_TEST_ASSUME(inputUsername == validUsername, "Username should match");
135+
FOSSIL_TEST_ASSUME(inputPassword != validPassword, "Password should not match");
135136
}
136137
}
137138
}
@@ -140,19 +141,19 @@ FOSSIL_TEST_CASE(cpp_xbdd_valid_login) {
140141
FOSSIL_TEST_CASE(cpp_xbdd_invalid_login) {
141142
GIVEN("a registered user with valid credentials") {
142143
// Set up the context
143-
const char* validUsername = "user123";
144-
const char* validPassword = "pass456";
144+
std::string validUsername = "user123";
145+
std::string validPassword = "pass456";
145146

146147
WHEN("the user provides incorrect username") {
147148
// Perform the action of user login
148-
const char* inputUsername = "wronguser";
149-
const char* inputPassword = "pass456";
149+
std::string inputUsername = "wronguser";
150+
std::string inputPassword = "pass456";
150151

151152
THEN("the login should fail with an error message") {
152153
// Check the expected outcome
153154
// Simulate login validation
154-
FOSSIL_TEST_ASSUME(strcmp(inputUsername, validUsername) != 0, "Username should not match");
155-
FOSSIL_TEST_ASSUME(strcmp(inputPassword, validPassword) == 0, "Password should match");
155+
FOSSIL_TEST_ASSUME(inputUsername != validUsername, "Username should not match");
156+
FOSSIL_TEST_ASSUME(inputPassword == validPassword, "Password should match");
156157
}
157158
}
158159
}

code/tests/cases/test_mark.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ FOSSIL_TEST_CASE(cpp_mark_start_and_stop) {
5050
MARK_BENCHMARK(stop_test);
5151
MARK_START(stop_test);
5252
MARK_STOP(stop_test);
53-
ASSUME_ITS_EQUAL_CSTR(benchmark_stop_test.name, benchmark_stop_test_name.c_str());
53+
ASSUME_ITS_EQUAL_CSTR(std::string(benchmark_stop_test.name), benchmark_stop_test_name);
5454
}
5555

5656
// * * * * * * * * * * * * * * * * * * * * * * * *

code/tests/cases/test_tdd.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* -----------------------------------------------------------------------------
1414
*/
1515
#include <fossil/test/framework.h>
16+
#include <string>
1617

1718
// * * * * * * * * * * * * * * * * * * * * * * * *
1819
// * Fossil Logic Test Utilites
@@ -686,9 +687,9 @@ FOSSIL_TEST_CASE(cpp_assume_run_of_wstr) {
686687
} // end case
687688

688689
FOSSIL_TEST_CASE(cpp_assume_run_of_bstr) {
689-
const char *str1 = (const char *)"Hello";
690-
const char *str2 = (const char *)"Hello";
691-
const char *str3 = (const char *)"World";
690+
std::string str1 = "Hello";
691+
std::string str2 = "Hello";
692+
std::string str3 = "World";
692693

693694
// Test cases
694695
ASSUME_ITS_EQUAL_BSTR(str1, str2);
@@ -697,9 +698,9 @@ FOSSIL_TEST_CASE(cpp_assume_run_of_bstr) {
697698
} // end case
698699

699700
FOSSIL_TEST_CASE(cpp_assume_run_of_cstr) {
700-
const char *str1 = "Hello";
701-
const char *str2 = "Hello";
702-
const char *str3 = "World";
701+
std::string str1 = "Hello";
702+
std::string str2 = "Hello";
703+
std::string str3 = "World";
703704

704705
// Test cases
705706
ASSUME_ITS_EQUAL_CSTR(str1, str2);

0 commit comments

Comments
 (0)