Skip to content

Commit 57dabca

Browse files
update test cases for mock
1 parent 9bea8cc commit 57dabca

File tree

2 files changed

+435
-97
lines changed

2 files changed

+435
-97
lines changed

code/tests/cases/test_mock.c

Lines changed: 213 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ FOSSIL_MOCK_STRUCT(MockStruct) {
4545
} MockStruct;
4646

4747
// Example of creating a mock function using the macro
48-
FOSSIL_MOCK_FUNC(int, mock_function, int a, int b) {
48+
FOSSIL_MOCK_FUNC(int, c_mock_function, int a, int b) {
4949
return a + b;
5050
}
5151

@@ -71,7 +71,23 @@ FOSSIL_TEST(c_mock_call_list_addition) {
7171
// Example of adding a fossil_mock_call_t to a fossil_mock_calllist_t
7272
fossil_mock_calllist_t list;
7373
fossil_mock_init(&list);
74-
char *args[] = {"arg1", "arg2"};
74+
75+
// Create mock arguments
76+
fossil_mock_pizza_t args[2];
77+
args[0].type = FOSSIL_MOCK_PIZZA_TYPE_CSTR;
78+
args[0].value.data = pizza_io_cstr_dup("arg1");
79+
args[0].value.mutable_flag = false;
80+
args[0].attribute.name = pizza_io_cstr_dup("arg1_name");
81+
args[0].attribute.description = pizza_io_cstr_dup("First argument");
82+
args[0].attribute.id = pizza_io_cstr_dup("1");
83+
84+
args[1].type = FOSSIL_MOCK_PIZZA_TYPE_CSTR;
85+
args[1].value.data = pizza_io_cstr_dup("arg2");
86+
args[1].value.mutable_flag = false;
87+
args[1].attribute.name = pizza_io_cstr_dup("arg2_name");
88+
args[1].attribute.description = pizza_io_cstr_dup("Second argument");
89+
args[1].attribute.id = pizza_io_cstr_dup("2");
90+
7591
fossil_mock_add_call(&list, "test_function", args, 2);
7692

7793
// Test cases
@@ -86,76 +102,225 @@ FOSSIL_TEST(c_mock_call_list_destruction) {
86102
// Example of destroying a fossil_mock_calllist_t
87103
fossil_mock_calllist_t list;
88104
fossil_mock_init(&list);
89-
char *args[] = {"arg1", "arg2"};
105+
106+
// Create mock arguments
107+
fossil_mock_pizza_t args[2];
108+
args[0].type = FOSSIL_MOCK_PIZZA_TYPE_CSTR;
109+
args[0].value.data = pizza_io_cstr_dup("arg1");
110+
args[0].value.mutable_flag = false;
111+
args[0].attribute.name = pizza_io_cstr_dup("arg1_name");
112+
args[0].attribute.description = pizza_io_cstr_dup("First argument");
113+
args[0].attribute.id = pizza_io_cstr_dup("1");
114+
115+
args[1].type = FOSSIL_MOCK_PIZZA_TYPE_CSTR;
116+
args[1].value.data = pizza_io_cstr_dup("arg2");
117+
args[1].value.mutable_flag = false;
118+
args[1].attribute.name = pizza_io_cstr_dup("arg2_name");
119+
args[1].attribute.description = pizza_io_cstr_dup("Second argument");
120+
args[1].attribute.id = pizza_io_cstr_dup("2");
121+
90122
fossil_mock_add_call(&list, "test_function", args, 2);
123+
91124
FOSSIL_TEST_ASSUME(list.size == 1, "fossil_mock_calllist_t size should be 1 after adding a call");
92125
FOSSIL_TEST_ASSUME(strcmp(list.head->function_name, "test_function") == 0, "Function name should be 'test_function'");
93126
FOSSIL_TEST_ASSUME(list.head->num_args == 2, "Number of arguments should be 2");
94-
95-
fossil_mock_destroy(&list); // not allowed to access due to the object being freed
127+
128+
fossil_mock_destroy(&list);
129+
} // end case
130+
131+
FOSSIL_TEST(c_mock_function_creation) {
132+
// Test cases
133+
FOSSIL_TEST_ASSUME(fossil_mockup_c_mock_function(2, 3) == 5, "Mock function should return the sum of its arguments");
134+
} // end case
135+
136+
FOSSIL_TEST(c_mock_alias_creation) {
137+
// Example of creating a type alias using the macro
138+
139+
// Test cases
140+
MockInt x = 10;
141+
FOSSIL_TEST_ASSUME(x == 10, "Mock alias should behave like the original type");
142+
} // end case
143+
144+
FOSSIL_TEST(c_mock_struct_creation) {
145+
// Test cases
146+
MockStruct instance;
147+
instance.a = 5;
148+
instance.b = 'c';
149+
FOSSIL_TEST_ASSUME(instance.a == 5, "Mock struct member 'a' should be 5");
150+
FOSSIL_TEST_ASSUME(instance.b == 'c', "Mock struct member 'b' should be 'c'");
96151
} // end case
97152

98-
FOSSIL_TEST(c_mock_call_list_initialization_macro) {
99-
// Example of initializing a fossil_mock_calllist_t using the macro
153+
FOSSIL_TEST(c_mock_call_list_type_handling) {
154+
// Initialize the mock call list
155+
fossil_mock_calllist_t list;
156+
fossil_mock_init(&list);
157+
158+
// Create mock arguments with various types
159+
fossil_mock_pizza_t args[3];
160+
args[0].type = FOSSIL_MOCK_PIZZA_TYPE_I32;
161+
args[0].value.data = pizza_io_cstr_dup("42");
162+
args[0].value.mutable_flag = false;
163+
args[0].attribute.name = pizza_io_cstr_dup("arg1");
164+
args[0].attribute.description = pizza_io_cstr_dup("Integer argument");
165+
args[0].attribute.id = pizza_io_cstr_dup("1");
166+
167+
args[1].type = FOSSIL_MOCK_PIZZA_TYPE_CSTR;
168+
args[1].value.data = pizza_io_cstr_dup("Hello");
169+
args[1].value.mutable_flag = true;
170+
args[1].attribute.name = pizza_io_cstr_dup("arg2");
171+
args[1].attribute.description = pizza_io_cstr_dup("String argument");
172+
args[1].attribute.id = pizza_io_cstr_dup("2");
173+
174+
args[2].type = FOSSIL_MOCK_PIZZA_TYPE_BOOL;
175+
args[2].value.data = pizza_io_cstr_dup("true");
176+
args[2].value.mutable_flag = false;
177+
args[2].attribute.name = pizza_io_cstr_dup("arg3");
178+
args[2].attribute.description = pizza_io_cstr_dup("Boolean argument");
179+
args[2].attribute.id = pizza_io_cstr_dup("3");
180+
181+
// Add a mock call with the arguments
182+
fossil_mock_add_call(&list, "test_function", args, 3);
183+
184+
// Test cases
185+
FOSSIL_TEST_ASSUME(list.size == 1, "fossil_mock_calllist_t size should be 1 after adding a call");
186+
FOSSIL_TEST_ASSUME(strcmp(list.head->function_name, "test_function") == 0, "Function name should be 'test_function'");
187+
FOSSIL_TEST_ASSUME(list.head->num_args == 3, "Number of arguments should be 3");
188+
189+
FOSSIL_TEST_ASSUME(list.head->arguments[0].type == FOSSIL_MOCK_PIZZA_TYPE_I32, "First argument type should be I32");
190+
FOSSIL_TEST_ASSUME(strcmp(list.head->arguments[0].value.data, "42") == 0, "First argument value should be '42'");
191+
FOSSIL_TEST_ASSUME(strcmp(list.head->arguments[0].attribute.name, "arg1") == 0, "First argument name should be 'arg1'");
192+
193+
FOSSIL_TEST_ASSUME(list.head->arguments[1].type == FOSSIL_MOCK_PIZZA_TYPE_CSTR, "Second argument type should be CSTR");
194+
FOSSIL_TEST_ASSUME(strcmp(list.head->arguments[1].value.data, "Hello") == 0, "Second argument value should be 'Hello'");
195+
FOSSIL_TEST_ASSUME(strcmp(list.head->arguments[1].attribute.name, "arg2") == 0, "Second argument name should be 'arg2'");
196+
197+
FOSSIL_TEST_ASSUME(list.head->arguments[2].type == FOSSIL_MOCK_PIZZA_TYPE_BOOL, "Third argument type should be BOOL");
198+
FOSSIL_TEST_ASSUME(strcmp(list.head->arguments[2].value.data, "true") == 0, "Third argument value should be 'true'");
199+
FOSSIL_TEST_ASSUME(strcmp(list.head->arguments[2].attribute.name, "arg3") == 0, "Third argument name should be 'arg3'");
200+
201+
// Clean up
202+
fossil_mock_destroy(&list);
203+
} // end case
204+
205+
FOSSIL_TEST(c_mock_call_list_edge_cases) {
206+
// Initialize the mock call list
207+
fossil_mock_calllist_t list;
208+
fossil_mock_init(&list);
209+
210+
// Add a call with no arguments
211+
fossil_mock_add_call(&list, "no_args_function", NULL, 0);
212+
213+
// Test cases
214+
FOSSIL_TEST_ASSUME(list.size == 1, "fossil_mock_calllist_t size should be 1 after adding a call with no arguments");
215+
FOSSIL_TEST_ASSUME(strcmp(list.head->function_name, "no_args_function") == 0, "Function name should be 'no_args_function'");
216+
FOSSIL_TEST_ASSUME(list.head->num_args == 0, "Number of arguments should be 0");
217+
218+
// Clean up
219+
fossil_mock_destroy(&list);
220+
} // end case
221+
222+
FOSSIL_TEST(c_mock_call_list_large_arguments) {
223+
// Initialize the mock call list
224+
fossil_mock_calllist_t list;
225+
fossil_mock_init(&list);
226+
227+
// Create a large number of mock arguments
228+
const int num_args = 100;
229+
fossil_mock_pizza_t args[num_args];
230+
for (int i = 0; i < num_args; ++i) {
231+
args[i].type = FOSSIL_MOCK_PIZZA_TYPE_I32;
232+
args[i].value.data = pizza_io_cstr_dup("42");
233+
args[i].value.mutable_flag = false;
234+
args[i].attribute.name = pizza_io_cstr_dup("arg");
235+
args[i].attribute.description = pizza_io_cstr_dup("Large argument test");
236+
args[i].attribute.id = pizza_io_cstr_dup("id");
237+
}
238+
239+
// Add a mock call with the large number of arguments
240+
fossil_mock_add_call(&list, "large_args_function", args, num_args);
241+
242+
// Test cases
243+
FOSSIL_TEST_ASSUME(list.size == 1, "fossil_mock_calllist_t size should be 1 after adding a call with large arguments");
244+
FOSSIL_TEST_ASSUME(list.head->num_args == num_args, "Number of arguments should match the large number");
245+
246+
// Clean up
247+
fossil_mock_destroy(&list);
248+
} // end case
249+
250+
FOSSIL_TEST(c_mock_macro_initialization) {
251+
// Initialize the mock call list using the macro
100252
fossil_mock_calllist_t list;
101253
MOCK_INIT(list);
102254

103255
// Test cases
104-
FOSSIL_TEST_ASSUME(list.head == NULL, "fossil_mock_calllist_t head should be NULL after initialization");
105-
FOSSIL_TEST_ASSUME(list.tail == NULL, "fossil_mock_calllist_t tail should be NULL after initialization");
106-
FOSSIL_TEST_ASSUME(list.size == 0, "fossil_mock_calllist_t size should be 0 after initialization");
256+
FOSSIL_TEST_ASSUME(list.head == NULL, "fossil_mock_calllist_t head should be NULL after initialization using macro");
257+
FOSSIL_TEST_ASSUME(list.tail == NULL, "fossil_mock_calllist_t tail should be NULL after initialization using macro");
258+
FOSSIL_TEST_ASSUME(list.size == 0, "fossil_mock_calllist_t size should be 0 after initialization using macro");
107259
} // end case
108260

109-
FOSSIL_TEST(c_mock_call_list_addition_macro) {
110-
// Example of adding a fossil_mock_call_t to a fossil_mock_calllist_t using the macro
261+
FOSSIL_TEST(c_mock_macro_addition) {
262+
// Initialize the mock call list using the macro
111263
fossil_mock_calllist_t list;
112264
MOCK_INIT(list);
113-
char *args[] = {"arg1", "arg2"};
265+
266+
// Create mock arguments
267+
fossil_mock_pizza_t args[2];
268+
args[0].type = FOSSIL_MOCK_PIZZA_TYPE_CSTR;
269+
args[0].value.data = pizza_io_cstr_dup("arg1");
270+
args[0].value.mutable_flag = false;
271+
args[0].attribute.name = pizza_io_cstr_dup("arg1_name");
272+
args[0].attribute.description = pizza_io_cstr_dup("First argument");
273+
args[0].attribute.id = pizza_io_cstr_dup("1");
274+
275+
args[1].type = FOSSIL_MOCK_PIZZA_TYPE_CSTR;
276+
args[1].value.data = pizza_io_cstr_dup("arg2");
277+
args[1].value.mutable_flag = false;
278+
args[1].attribute.name = pizza_io_cstr_dup("arg2_name");
279+
args[1].attribute.description = pizza_io_cstr_dup("Second argument");
280+
args[1].attribute.id = pizza_io_cstr_dup("2");
281+
282+
// Add a mock call using the macro
114283
MOCK_ADD_CALL(list, "test_function", args, 2);
115284

116285
// Test cases
117-
FOSSIL_TEST_ASSUME(list.size == 1, "fossil_mock_calllist_t size should be 1 after adding a call");
286+
FOSSIL_TEST_ASSUME(list.size == 1, "fossil_mock_calllist_t size should be 1 after adding a call using macro");
118287
FOSSIL_TEST_ASSUME(strcmp(list.head->function_name, "test_function") == 0, "Function name should be 'test_function'");
119288
FOSSIL_TEST_ASSUME(list.head->num_args == 2, "Number of arguments should be 2");
120289
FOSSIL_TEST_ASSUME(strcmp(list.head->arguments[0].value.data, "arg1") == 0, "First argument should be 'arg1'");
121290
FOSSIL_TEST_ASSUME(strcmp(list.head->arguments[1].value.data, "arg2") == 0, "Second argument should be 'arg2'");
122291
} // end case
123292

124-
FOSSIL_TEST(c_mock_call_list_destruction_macro) {
125-
// Example of destroying a fossil_mock_calllist_t using the macro
293+
FOSSIL_TEST(c_mock_macro_destruction) {
294+
// Initialize the mock call list using the macro
126295
fossil_mock_calllist_t list;
127296
MOCK_INIT(list);
128-
char *args[] = {"arg1", "arg2"};
129-
MOCK_ADD_CALL(list, "test_function", args, 2);
130-
131-
FOSSIL_TEST_ASSUME(list.size == 1, "fossil_mock_calllist_t size should be 1 after adding a call");
132-
FOSSIL_TEST_ASSUME(strcmp(list.head->function_name, "test_function") == 0, "Function name should be 'test_function'");
133-
FOSSIL_TEST_ASSUME(list.head->num_args == 2, "Number of arguments should be 2");
134297

135-
MOCK_DESTROY(list); // not allowed to access due to the object being freed
136-
} // end case
298+
// Create mock arguments
299+
fossil_mock_pizza_t args[2];
300+
args[0].type = FOSSIL_MOCK_PIZZA_TYPE_CSTR;
301+
args[0].value.data = pizza_io_cstr_dup("arg1");
302+
args[0].value.mutable_flag = false;
303+
args[0].attribute.name = pizza_io_cstr_dup("arg1_name");
304+
args[0].attribute.description = pizza_io_cstr_dup("First argument");
305+
args[0].attribute.id = pizza_io_cstr_dup("1");
137306

138-
FOSSIL_TEST(c_mock_function_creation) {
139-
// Test cases
140-
FOSSIL_TEST_ASSUME(fossil_mockup_mock_function(2, 3) == 5, "Mock function should return the sum of its arguments");
141-
} // end case
307+
args[1].type = FOSSIL_MOCK_PIZZA_TYPE_CSTR;
308+
args[1].value.data = pizza_io_cstr_dup("arg2");
309+
args[1].value.mutable_flag = false;
310+
args[1].attribute.name = pizza_io_cstr_dup("arg2_name");
311+
args[1].attribute.description = pizza_io_cstr_dup("Second argument");
312+
args[1].attribute.id = pizza_io_cstr_dup("2");
142313

143-
FOSSIL_TEST(c_mock_alias_creation) {
144-
// Example of creating a type alias using the macro
145-
314+
// Add a mock call using the macro
315+
MOCK_ADD_CALL(list, "test_function", args, 2);
146316

147-
// Test cases
148-
MockInt x = 10;
149-
FOSSIL_TEST_ASSUME(x == 10, "Mock alias should behave like the original type");
150-
} // end case
317+
// Destroy the mock call list using the macro
318+
MOCK_DESTROY(list);
151319

152-
FOSSIL_TEST(c_mock_struct_creation) {
153320
// Test cases
154-
MockStruct instance;
155-
instance.a = 5;
156-
instance.b = 'c';
157-
FOSSIL_TEST_ASSUME(instance.a == 5, "Mock struct member 'a' should be 5");
158-
FOSSIL_TEST_ASSUME(instance.b == 'c', "Mock struct member 'b' should be 'c'");
321+
FOSSIL_TEST_ASSUME(list.head == NULL, "fossil_mock_calllist_t head should be NULL after destruction using macro");
322+
FOSSIL_TEST_ASSUME(list.tail == NULL, "fossil_mock_calllist_t tail should be NULL after destruction using macro");
323+
FOSSIL_TEST_ASSUME(list.size == 0, "fossil_mock_calllist_t size should be 0 after destruction using macro");
159324
} // end case
160325

161326
// * * * * * * * * * * * * * * * * * * * * * * * *
@@ -165,12 +330,16 @@ FOSSIL_TEST_GROUP(c_mock_test_cases) {
165330
FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_initialization);
166331
FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_addition);
167332
FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_destruction);
168-
FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_initialization_macro);
169-
FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_addition_macro);
170-
FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_destruction_macro);
171333
FOSSIL_TEST_ADD(c_mock_suite, c_mock_function_creation);
172334
FOSSIL_TEST_ADD(c_mock_suite, c_mock_alias_creation);
173335
FOSSIL_TEST_ADD(c_mock_suite, c_mock_struct_creation);
336+
FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_type_handling);
337+
FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_edge_cases);
338+
FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_large_arguments);
339+
340+
FOSSIL_TEST_ADD(c_mock_suite, c_mock_macro_initialization);
341+
FOSSIL_TEST_ADD(c_mock_suite, c_mock_macro_addition);
342+
FOSSIL_TEST_ADD(c_mock_suite, c_mock_macro_destruction);
174343

175344
FOSSIL_TEST_REGISTER(c_mock_suite);
176345
} // end of group

0 commit comments

Comments
 (0)