winprefs v0.3.2
A registry exporter for programmers.
Loading...
Searching...
No Matches
test_get_git_branch.c
1#include "git_branch.h"
2
3void test_get_git_branch_CreatePipe_fails(void **state) {
4 will_return(__wrap_CreatePipe, false);
5 assert_null(get_git_branch(nullptr, 0, nullptr, 0));
6}
7
8void test_get_git_branch_CreateProcess_fails(void **state) {
9 will_return(__wrap_CreatePipe, true);
10 will_return(__wrap_CreateProcess, false);
11 will_return_always(__wrap_CloseHandle, true);
12 assert_null(get_git_branch(nullptr, 0, nullptr, 0));
13}
14
15void test_get_git_branch(void **state) {
16 char *master_nl = malloc(8);
17 master_nl[7] = '\0';
18 memcpy(master_nl, "master\n", 7);
19 wchar_t *w_master = calloc(7, sizeof(wchar_t));
20 w_master[6] = L'\0';
21 wmemcpy(w_master, L"master", 6);
22
23 will_return(__wrap_CreatePipe, true);
24 will_return(__wrap_CreateProcess, true);
25 will_return_always(__wrap_CloseHandle, true);
26 will_return(__wrap_WaitForSingleObject, WAIT_OBJECT_0);
27 will_return(__wrap_PeekNamedPipe, 8);
28 will_return(__wrap_PeekNamedPipe, true);
29 will_return(__wrap_ReadFile, master_nl);
30 will_return(__wrap_ReadFile, 7);
31 will_return(__wrap_ReadFile, true);
32 will_return(__wrap_PeekNamedPipe, 0);
33 will_return(__wrap_PeekNamedPipe, true);
34 expect_value(__wrap_MultiByteToWideChar, cchWideChar, 0);
35 will_return(__wrap_MultiByteToWideChar, 0);
36 will_return(__wrap_MultiByteToWideChar, 6);
37 expect_value(__wrap_MultiByteToWideChar, cchWideChar, 6);
38 will_return(__wrap_MultiByteToWideChar, w_master);
39 will_return(__wrap_MultiByteToWideChar, 6);
40
41 wchar_t *ret = get_git_branch(nullptr, 0, nullptr, 0);
42 assert_memory_equal(ret, L"master", 12);
43
44 free(master_nl);
45 free(ret);
46 free(w_master);
47}
48
49const struct CMUnitTest powershell_tests[] = {
50 cmocka_unit_test(test_get_git_branch),
51 cmocka_unit_test(test_get_git_branch_CreatePipe_fails),
52 cmocka_unit_test(test_get_git_branch_CreateProcess_fails),
53};
54
55int main(int argc, char *argv[]) {
56 return cmocka_run_group_tests(powershell_tests, nullptr, nullptr);
57}