Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function to negative_tests to get a call description (using stringify) #333

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions devdoc/umock_c_negative_tests_requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,16 @@ XX**SRS_UMOCK_C_NEGATIVE_TESTS_01_021: [** `umock_c_negative_tests_call_count` s

**SRS_UMOCK_C_NEGATIVE_TESTS_31_029: [** If `umockcallrecorder_can_call_fail` fails, `umock_c_negative_tests_fail_call` shall indicate the error via the umock error callback with error code `UMOCK_C_ERROR` and return 1. **]**

## umock_c_negative_tests_get_call_description

```c
char* umock_c_negative_tests_get_call_description(size_t index);
```

**SRS_UMOCK_C_NEGATIVE_TESTS_09_001: [** If the module was not previously initialized, `umock_c_negative_tests_get_call_description` shall indicate the error via the umock error callback with error code `UMOCK_C_ERROR`. **]**

**SRS_UMOCK_C_NEGATIVE_TESTS_09_002: [** If `umock_c_get_call_recorder` fails, `umock_c_negative_tests_get_call_description` shall indicate the error via the umock error callback with error code `UMOCK_C_ERROR`. **]**

**SRS_UMOCK_C_NEGATIVE_TESTS_09_003: [** If any failure occurs `umock_c_negative_tests_get_call_description` shall return NULL. **]**

**SRS_UMOCK_C_NEGATIVE_TESTS_09_004: [** Otherwise `umock_c_negative_tests_get_call_description` shall return the result of `umockcallrecorder_get_expected_call_string`. **]**
10 changes: 10 additions & 0 deletions devdoc/umockcallrecorder_requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
int umockcallrecorder_get_expected_call_count(UMOCKCALLRECORDER_HANDLE umock_call_recorder, size_t* expected_call_count);
int umockcallrecorder_fail_call(UMOCKCALLRECORDER_HANDLE umock_call_recorder, size_t index);
int umockcallrecorder_can_call_fail(UMOCKCALLRECORDER_HANDLE umock_call_recorder, size_t index, int* can_call_fail);
char* umockcallrecorder_get_expected_call_string(UMOCKCALLRECORDER_HANDLE umock_call_recorder, size_t index);
```

### umockcallrecorder_create
Expand Down Expand Up @@ -297,3 +298,12 @@ int umockcallrecorder_can_call_fail(UMOCKCALLRECORDER_HANDLE umock_call_recorder
**SRS_UMOCKCALLRECORDER_01_094: [** If a lock was created for the call recorder, `umockcallrecorder_can_call_fail` shall release the exclusive lock. **]**

**SRS_UMOCKCALLRECORDER_31_060: [** On success `umockcallrecorder_can_call_fail` shall return 0. **]**

### umockcallrecorder_get_expected_call_string

```c
char* umockcallrecorder_get_expected_call_string(UMOCKCALLRECORDER_HANDLE umock_call_recorder, size_t index);
```
**SRS_UMOCKCALLRECORDER_09_001: [** If `umock_call_recorder` or `index` is >= `expected_call_count`, `umockcallrecorder_can_call_fail` shall return NULL. **]**

**SRS_UMOCKCALLRECORDER_09_002: [** Otherwise `umockcallrecorder_can_call_fail` shall call `umockcall_stringify` over `expected_calls[index].umockcall` and return the resulting string. **]**
1 change: 1 addition & 0 deletions inc/umock_c/umock_c_negative_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern "C" {
void umock_c_negative_tests_fail_call(size_t index);
size_t umock_c_negative_tests_call_count(void);
int umock_c_negative_tests_can_call_fail(size_t index);
char* umock_c_negative_tests_get_call_description(size_t index);

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions inc/umock_c/umockcallrecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extern "C" {
int umockcallrecorder_get_expected_call_count(UMOCKCALLRECORDER_HANDLE umock_call_recorder, size_t* expected_call_count);
int umockcallrecorder_fail_call(UMOCKCALLRECORDER_HANDLE umock_call_recorder, size_t index);
int umockcallrecorder_can_call_fail(UMOCKCALLRECORDER_HANDLE umock_call_recorder, size_t index, int* can_call_fail);
char* umockcallrecorder_get_expected_call_string(UMOCKCALLRECORDER_HANDLE umock_call_recorder, size_t index);

#ifdef __cplusplus
}
Expand Down
29 changes: 29 additions & 0 deletions src/umock_c_negative_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,33 @@ int umock_c_negative_tests_can_call_fail(size_t index)
return can_call_fail;
}

char* umock_c_negative_tests_get_call_description(size_t index)
{
/* Codes_SRS_UMOCK_C_NEGATIVE_TESTS_09_003: [ If any failure occurs `umock_c_negative_tests_get_call_description` shall return NULL. ]*/
char* result = NULL;

/* Codes_SRS_UMOCK_C_NEGATIVE_TESTS_09_001: [ If the module was not previously initialized, `umock_c_negative_tests_get_call_description` shall indicate the error via the umock error callback with error code `UMOCK_C_ERROR`. ]*/
if (umock_c_negative_tests_state != UMOCK_C_NEGATIVE_TESTS_STATE_INITIALIZED)
{

UMOCK_LOG("umock_c_negative_tests_fail_call: Not initialized.");
umock_c_indicate_error(UMOCK_C_ERROR);
}
else
{
/* Codes_SRS_UMOCK_C_NEGATIVE_TESTS_09_002: [ If `umock_c_get_call_recorder` fails, `umock_c_negative_tests_get_call_description` shall indicate the error via the umock error callback with error code `UMOCK_C_ERROR`. ]*/
UMOCKCALLRECORDER_HANDLE call_recorder = umock_c_get_call_recorder();
if (call_recorder == NULL)
{
UMOCK_LOG("umock_c_negative_tests_fail_call: Cannot get call recorder.");
umock_c_indicate_error(UMOCK_C_ERROR);
}
else
{
/* Codes_SRS_UMOCK_C_NEGATIVE_TESTS_09_004: [ Otherwise `umock_c_negative_tests_get_call_description` shall return the result of `umockcallrecorder_get_expected_call_string`. ]*/
result = umockcallrecorder_get_expected_call_string(call_recorder, index);
}
}

return result;
}
20 changes: 20 additions & 0 deletions src/umockcallrecorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,3 +829,23 @@ int umockcallrecorder_can_call_fail(UMOCKCALLRECORDER_HANDLE umock_call_recorder

return result;
}

char* umockcallrecorder_get_expected_call_string(UMOCKCALLRECORDER_HANDLE umock_call_recorder, size_t index)
{
char* result = NULL;

/* Codes_SRS_UMOCKCALLRECORDER_09_001: [ If `umock_call_recorder` or `index` is >= `expected_call_count`, `umockcallrecorder_can_call_fail` shall return NULL. ]*/
if ((umock_call_recorder == NULL) ||
(index >= umock_call_recorder->expected_call_count))
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, missing .md specs update, which should be used to write also the tests for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed most of the issues. I'm having this error though, need to understand why.

[ 93%] Linking C executable ../../../Debug/umock_c_negt_noint_tests_exe_umock_c/umock_c_negt_noint_tests_exe_umock_c
/usr/bin/ld: ../../../libumock_c.a(umockcallrecorder.c.o): in function `umockcallrecorder_destroy':
/home/ewertons/code/s2/umock-c/src/umockcallrecorder.c:150: multiple definition of `umockcallrecorder_destroy'; libumock_c_negt_noint_tests_lib_umock_c.a(umock_c_negt_noint_tests.c.o):/home/ewertons/code/s2/umock-c/tests/umock_c_negt_noini_ut/umock_c_negt_noint_tests.c:155: first defined here
/usr/bin/ld: ../../../libumock_c.a(umockcallrecorder.c.o): in function `umockcallrecorder_clone':
/home/ewertons/code/s2/umock-c/src/umockcallrecorder.c:595: multiple definition of `umockcallrecorder_clone'; libumock_c_negt_noint_tests_lib_umock_c.a(umock_c_negt_noint_tests.c.o):/home/ewertons/code/s2/umock-c/tests/umock_c_negt_noini_ut/umock_c_negt_noint_tests.c:93: first defined here
/usr/bin/ld: ../../../libumock_c.a(umockcallrecorder.c.o): in function `umockcallrecorder_get_expected_call_count':
/home/ewertons/code/s2/umock-c/src/umockcallrecorder.c:722: multiple definition of `umockcallrecorder_get_expected_call_count'; libumock_c_negt_noint_tests_lib_umock_c.a(umock_c_negt_noint_tests.c.o):/home/ewertons/code/s2/umock-c/tests/umock_c_negt_noini_ut/umock_c_negt_noint_tests.c:106: first defined here
/usr/bin/ld: ../../../libumock_c.a(umockcallrecorder.c.o): in function `umockcallrecorder_fail_call':
/home/ewertons/code/s2/umock-c/src/umockcallrecorder.c:752: multiple definition of `umockcallrecorder_fail_call'; libumock_c_negt_noint_tests_lib_umock_c.a(umock_c_negt_noint_tests.c.o):/home/ewertons/code/s2/umock-c/tests/umock_c_negt_noini_ut/umock_c_negt_noint_tests.c:120: first defined here
/usr/bin/ld: ../../../libumock_c.a(umockcallrecorder.c.o): in function `umockcallrecorder_can_call_fail':
/home/ewertons/code/s2/umock-c/src/umockcallrecorder.c:791: multiple definition of `umockcallrecorder_can_call_fail'; libumock_c_negt_noint_tests_lib_umock_c.a(umock_c_negt_noint_tests.c.o):/home/ewertons/code/s2/umock-c/tests/umock_c_negt_noini_ut/umock_c_negt_noint_tests.c:147: first defined here
/usr/bin/ld: ../../../libumock_c.a(umockalloc.c.o): in function `umockalloc_malloc':
/home/ewertons/code/s2/umock-c/src/umockalloc.c:10: multiple definition of `umockalloc_malloc'; libumock_c_negt_noint_tests_lib_umock_c.a(umock_c_negt_noint_tests.c.o):/home/ewertons/code/s2/umock-c/tests/umock_c_negt_noini_ut/umock_c_negt_noint_tests.c:229: first defined here
/usr/bin/ld: ../../../libumock_c.a(umockalloc.c.o): in function `umockalloc_free':
/home/ewertons/code/s2/umock-c/src/umockalloc.c:31: multiple definition of `umockalloc_free'; libumock_c_negt_noint_tests_lib_umock_c.a(umock_c_negt_noint_tests.c.o):/home/ewertons/code/s2/umock-c/tests/umock_c_negt_noini_ut/umock_c_negt_noint_tests.c:234: first defined here
collect2: error: ld returned 1 exit status
make[2]: *** [tests/umock_c_negt_noini_ut/exe/CMakeFiles/umock_c_negt_noint_tests_exe_umock_c.dir/build.make:104: Debug/umock_c_negt_noint_tests_exe_umock_c/umock_c_negt_noint_tests_exe_umock_c] Error 1
make[1]: *** [CMakeFiles/Makefile2:2657: tests/umock_c_negt_noini_ut/exe/CMakeFiles/umock_c_negt_noint_tests_exe_umock_c.dir/all] Error 2
make: *** [Makefile:146: all] Error 2

Apparently the build is not happy that I introduced a new function in umockcallrecorder.c

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I took the dependency on this new function off the PR I was actually working on so it gets unblocked. I'll come back to this once I get the other task done.

UMOCK_LOG("umockcallrecorder_get_expected_call_string: NULL Invalid arguments, umock_call_recorder = %p, index = %zu",
umock_call_recorder, index);
}
else
{
/* Codes_SRS_UMOCKCALLRECORDER_09_002: [ Otherwise `umockcallrecorder_can_call_fail` shall call `umockcall_stringify` over `expected_calls[index].umockcall` and return the resulting string. ]*/
result = umockcall_stringify(umock_call_recorder->expected_calls[index].umockcall);
}

return result;
}
57 changes: 57 additions & 0 deletions tests/umockcallrecorder_ut/umockcallrecorder_ut.c
Original file line number Diff line number Diff line change
Expand Up @@ -2793,4 +2793,61 @@ TEST_FUNCTION(umockcallrecorder_can_call_fail_with_lock_functions_setup_locks_an
umockcallrecorder_destroy(call_recorder);
}


/* Tests_SRS_UMOCKCALLRECORDER_09_002: [ Otherwise `umockcallrecorder_can_call_fail` shall call `umockcall_stringify` over `expected_calls[index].umockcall` and return the resulting string. ]*/
TEST_FUNCTION(umockcallrecorder_get_expected_call_string_succeeds)
{
// arrange
char* result;
UMOCKCALLRECORDER_HANDLE call_recorder = umockcallrecorder_create(NULL, NULL);
(void)umockcallrecorder_add_expected_call(call_recorder, test_expected_umockcall_1);

reset_all_calls();
// act
result = umockcallrecorder_get_expected_call_string(call_recorder, 0);

// assert
ASSERT_ARE_EQUAL(size_t, 1, mocked_call_count);
ASSERT_ARE_EQUAL(void_ptr, test_expected_umockcall_1, mocked_calls[0].u.umockcall_get_fail_call.umockcall);
ASSERT_ARE_EQUAL(char_ptr, umockcall_stringify_call_result, result);

// cleanup
umockcallrecorder_destroy(call_recorder);
}

/* Tests_SRS_UMOCKCALLRECORDER_09_001: [ If `umock_call_recorder` or `index` is >= `expected_call_count`, `umockcallrecorder_can_call_fail` shall return NULL. ]*/
TEST_FUNCTION(umockcallrecorder_get_expected_call_string_NULL_handle_fails)
{
// arrange
char* result;

reset_all_calls();
// act
result = umockcallrecorder_get_expected_call_string(NULL, 0);

// assert
ASSERT_IS_NULL(result);

// cleanup
}

/* Tests_SRS_UMOCKCALLRECORDER_09_001: [ If `umock_call_recorder` or `index` is >= `expected_call_count`, `umockcallrecorder_can_call_fail` shall return NULL. ]*/
TEST_FUNCTION(umockcallrecorder_get_expected_call_string_index_out_of_bounds_fails)
{
// arrange
char* result;
UMOCKCALLRECORDER_HANDLE call_recorder = umockcallrecorder_create(NULL, NULL);
(void)umockcallrecorder_add_expected_call(call_recorder, test_expected_umockcall_1);

reset_all_calls();
// act
result = umockcallrecorder_get_expected_call_string(call_recorder, 1);

// assert
ASSERT_IS_NULL(result);

// cleanup
umockcallrecorder_destroy(call_recorder);
}

END_TEST_SUITE(TEST_SUITE_NAME_FROM_CMAKE)