Skip to content

Commit 55b3b82

Browse files
add test cases for mockups
1 parent 3de8f5b commit 55b3b82

File tree

3 files changed

+356
-1
lines changed

3 files changed

+356
-1
lines changed

code/tests/cases/test_mock.c

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
* -----------------------------------------------------------------------------
3+
* Project: Fossil Logic
4+
*
5+
* This file is part of the Fossil Logic project, which aims to develop high-
6+
* performance, cross-platform applications and libraries. The code contained
7+
* herein is subject to the terms and conditions defined in the project license.
8+
*
9+
* Author: Michael Gene Brockus (Dreamer)
10+
* Date: 07/01/2024
11+
*
12+
* Copyright (C) 2024 Fossil Logic. All rights reserved.
13+
* -----------------------------------------------------------------------------
14+
*/
15+
#include <fossil/test/framework.h>
16+
#include <string.h>
17+
18+
19+
// * * * * * * * * * * * * * * * * * * * * * * * *
20+
// * Fossil Logic Test Utilites
21+
// * * * * * * * * * * * * * * * * * * * * * * * *
22+
// Setup steps for things like test fixtures and
23+
// mock objects are set here.
24+
// * * * * * * * * * * * * * * * * * * * * * * * *
25+
26+
// Define the test suite and add test cases
27+
FOSSIL_TEST_SUITE(c_mock_suite);
28+
29+
// Setup function for the test suite
30+
FOSSIL_SETUP(c_mock_suite) {
31+
// Setup code here
32+
}
33+
34+
// Teardown function for the test suite
35+
FOSSIL_TEARDOWN(c_mock_suite) {
36+
// Teardown code here
37+
}
38+
39+
FOSSIL_MOCK_ALIAS(MockInt, int);
40+
41+
// Example of creating a mock struct using the macro
42+
FOSSIL_MOCK_STRUCT(MockStruct) {
43+
int a;
44+
char b;
45+
} MockStruct;
46+
47+
// Example of creating a mock function using the macro
48+
FOSSIL_MOCK_FUNC(int, mock_function, int a, int b) {
49+
return a + b;
50+
}
51+
52+
// * * * * * * * * * * * * * * * * * * * * * * * *
53+
// * Fossil Logic Test Cases
54+
// * * * * * * * * * * * * * * * * * * * * * * * *
55+
// The test cases below are provided as samples, showcasing
56+
// Domain-Driven Design (DDD) usage in the Fossil Logic project.
57+
// * * * * * * * * * * * * * * * * * * * * * * * *
58+
59+
FOSSIL_TEST_CASE(c_mock_call_list_initialization) {
60+
// Example of initializing a MockCallList
61+
MockCallList list;
62+
fossil_mock_init(&list);
63+
64+
// Test cases
65+
FOSSIL_TEST_ASSUME(list.head == NULL, "MockCallList head should be NULL after initialization");
66+
FOSSIL_TEST_ASSUME(list.tail == NULL, "MockCallList tail should be NULL after initialization");
67+
FOSSIL_TEST_ASSUME(list.size == 0, "MockCallList size should be 0 after initialization");
68+
} // end case
69+
70+
FOSSIL_TEST_CASE(c_mock_call_list_addition) {
71+
// Example of adding a MockCall to a MockCallList
72+
MockCallList list;
73+
fossil_mock_init(&list);
74+
char *args[] = {"arg1", "arg2"};
75+
fossil_mock_add_call(&list, "test_function", args, 2);
76+
77+
// Test cases
78+
FOSSIL_TEST_ASSUME(list.size == 1, "MockCallList size should be 1 after adding a call");
79+
FOSSIL_TEST_ASSUME(strcmp(list.head->function_name, "test_function") == 0, "Function name should be 'test_function'");
80+
FOSSIL_TEST_ASSUME(list.head->num_args == 2, "Number of arguments should be 2");
81+
FOSSIL_TEST_ASSUME(strcmp(list.head->arguments[0], "arg1") == 0, "First argument should be 'arg1'");
82+
FOSSIL_TEST_ASSUME(strcmp(list.head->arguments[1], "arg2") == 0, "Second argument should be 'arg2'");
83+
} // end case
84+
85+
FOSSIL_TEST_CASE(c_mock_call_list_destruction) {
86+
// Example of destroying a MockCallList
87+
MockCallList list;
88+
fossil_mock_init(&list);
89+
char *args[] = {"arg1", "arg2"};
90+
fossil_mock_add_call(&list, "test_function", args, 2);
91+
fossil_mock_destroy(&list);
92+
93+
// Test cases
94+
FOSSIL_TEST_ASSUME(list.head == NULL, "MockCallList head should be NULL after destruction");
95+
FOSSIL_TEST_ASSUME(list.tail == NULL, "MockCallList tail should be NULL after destruction");
96+
FOSSIL_TEST_ASSUME(list.size == 0, "MockCallList size should be 0 after destruction");
97+
} // end case
98+
99+
FOSSIL_TEST_CASE(c_mock_call_list_initialization_macro) {
100+
// Example of initializing a MockCallList using the macro
101+
MockCallList list;
102+
MOCK_INIT(list);
103+
104+
// Test cases
105+
FOSSIL_TEST_ASSUME(list.head == NULL, "MockCallList head should be NULL after initialization");
106+
FOSSIL_TEST_ASSUME(list.tail == NULL, "MockCallList tail should be NULL after initialization");
107+
FOSSIL_TEST_ASSUME(list.size == 0, "MockCallList size should be 0 after initialization");
108+
} // end case
109+
110+
FOSSIL_TEST_CASE(c_mock_call_list_addition_macro) {
111+
// Example of adding a MockCall to a MockCallList using the macro
112+
MockCallList list;
113+
MOCK_INIT(list);
114+
char *args[] = {"arg1", "arg2"};
115+
MOCK_ADD_CALL(list, "test_function", args, 2);
116+
117+
// Test cases
118+
FOSSIL_TEST_ASSUME(list.size == 1, "MockCallList size should be 1 after adding a call");
119+
FOSSIL_TEST_ASSUME(strcmp(list.head->function_name, "test_function") == 0, "Function name should be 'test_function'");
120+
FOSSIL_TEST_ASSUME(list.head->num_args == 2, "Number of arguments should be 2");
121+
FOSSIL_TEST_ASSUME(strcmp(list.head->arguments[0], "arg1") == 0, "First argument should be 'arg1'");
122+
FOSSIL_TEST_ASSUME(strcmp(list.head->arguments[1], "arg2") == 0, "Second argument should be 'arg2'");
123+
} // end case
124+
125+
FOSSIL_TEST_CASE(c_mock_call_list_destruction_macro) {
126+
// Example of destroying a MockCallList using the macro
127+
MockCallList list;
128+
MOCK_INIT(list);
129+
char *args[] = {"arg1", "arg2"};
130+
MOCK_ADD_CALL(list, "test_function", args, 2);
131+
MOCK_DESTROY(list);
132+
133+
// Test cases
134+
FOSSIL_TEST_ASSUME(list.head == NULL, "MockCallList head should be NULL after destruction");
135+
FOSSIL_TEST_ASSUME(list.tail == NULL, "MockCallList tail should be NULL after destruction");
136+
FOSSIL_TEST_ASSUME(list.size == 0, "MockCallList size should be 0 after destruction");
137+
} // end case
138+
139+
FOSSIL_TEST_CASE(c_mock_function_creation) {
140+
// Test cases
141+
FOSSIL_TEST_ASSUME(fossil_mockup_mock_function(2, 3) == 5, "Mock function should return the sum of its arguments");
142+
} // end case
143+
144+
FOSSIL_TEST_CASE(c_mock_alias_creation) {
145+
// Example of creating a type alias using the macro
146+
147+
148+
// Test cases
149+
MockInt x = 10;
150+
FOSSIL_TEST_ASSUME(x == 10, "Mock alias should behave like the original type");
151+
} // end case
152+
153+
FOSSIL_TEST_CASE(c_mock_struct_creation) {
154+
// Test cases
155+
MockStruct instance;
156+
instance.a = 5;
157+
instance.b = 'c';
158+
FOSSIL_TEST_ASSUME(instance.a == 5, "Mock struct member 'a' should be 5");
159+
FOSSIL_TEST_ASSUME(instance.b == 'c', "Mock struct member 'b' should be 'c'");
160+
} // end case
161+
162+
// * * * * * * * * * * * * * * * * * * * * * * * *
163+
// * Fossil Logic Test Pool
164+
// * * * * * * * * * * * * * * * * * * * * * * * *
165+
FOSSIL_TEST_GROUP(c_mock_test_cases) {
166+
FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_initialization);
167+
FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_addition);
168+
// FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_destruction);
169+
FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_initialization_macro);
170+
FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_addition_macro);
171+
// FOSSIL_TEST_ADD(c_mock_suite, c_mock_call_list_destruction_macro);
172+
FOSSIL_TEST_ADD(c_mock_suite, c_mock_function_creation);
173+
FOSSIL_TEST_ADD(c_mock_suite, c_mock_alias_creation);
174+
FOSSIL_TEST_ADD(c_mock_suite, c_mock_struct_creation);
175+
176+
FOSSIL_TEST_REGISTER(c_mock_suite);
177+
} // end of group

code/tests/cases/test_mock.cpp

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* -----------------------------------------------------------------------------
3+
* Project: Fossil Logic
4+
*
5+
* This file is part of the Fossil Logic project, which aims to develop high-
6+
* performance, cross-platform applications and libraries. The code contained
7+
* herein is subject to the terms and conditions defined in the project license.
8+
*
9+
* Author: Michael Gene Brockus (Dreamer)
10+
* Date: 07/01/2024
11+
*
12+
* Copyright (C) 2024 Fossil Logic. All rights reserved.
13+
* -----------------------------------------------------------------------------
14+
*/
15+
#include <fossil/test/framework.h>
16+
#include <string>
17+
#include <cstring>
18+
19+
20+
// * * * * * * * * * * * * * * * * * * * * * * * *
21+
// * Fossil Logic Test Utilites
22+
// * * * * * * * * * * * * * * * * * * * * * * * *
23+
// Setup steps for things like test fixtures and
24+
// mock objects are set here.
25+
// * * * * * * * * * * * * * * * * * * * * * * * *
26+
27+
// Define the test suite and add test cases
28+
FOSSIL_TEST_SUITE(cpp_mock_suite);
29+
30+
// Setup function for the test suite
31+
FOSSIL_SETUP(cpp_mock_suite) {
32+
// Setup code here
33+
}
34+
35+
// Teardown function for the test suite
36+
FOSSIL_TEARDOWN(cpp_mock_suite) {
37+
// Teardown code here
38+
}
39+
40+
FOSSIL_MOCK_ALIAS(MockInt, int);
41+
42+
// Example of creating a mock struct using the macro
43+
FOSSIL_MOCK_STRUCT(MockStruct) {
44+
int a;
45+
char b;
46+
};
47+
48+
// Example of creating a mock function using the macro
49+
FOSSIL_MOCK_FUNC(int, mock_function, int a, int b) {
50+
return a + b;
51+
}
52+
53+
// * * * * * * * * * * * * * * * * * * * * * * * *
54+
// * Fossil Logic Test Cases
55+
// * * * * * * * * * * * * * * * * * * * * * * * *
56+
// The test cases below are provided as samples, showcasing
57+
// Domain-Driven Design (DDD) usage in the Fossil Logic project.
58+
// * * * * * * * * * * * * * * * * * * * * * * * *
59+
60+
FOSSIL_TEST_CASE(cpp_mock_call_list_initialization) {
61+
// Example of initializing a MockCallList
62+
MockCallList list;
63+
fossil_mock_init(&list);
64+
65+
// Test cases
66+
FOSSIL_TEST_ASSUME(list.head == NULL, "MockCallList head should be NULL after initialization");
67+
FOSSIL_TEST_ASSUME(list.tail == NULL, "MockCallList tail should be NULL after initialization");
68+
FOSSIL_TEST_ASSUME(list.size == 0, "MockCallList size should be 0 after initialization");
69+
} // end case
70+
71+
FOSSIL_TEST_CASE(cpp_mock_call_list_addition) {
72+
// Example of adding a MockCall to a MockCallList
73+
MockCallList list;
74+
fossil_mock_init(&list);
75+
const char* args[] = {"arg1", "arg2"};
76+
fossil_mock_add_call(&list, "test_function", (char**)args, 2);
77+
78+
// Test cases
79+
FOSSIL_TEST_ASSUME(list.size == 1, "MockCallList size should be 1 after adding a call");
80+
//FOSSIL_TEST_ASSUME(list.head->function_name == "test_function", "Function name should be 'test_function'");
81+
FOSSIL_TEST_ASSUME(list.head->num_args == 2, "Number of arguments should be 2");
82+
FOSSIL_TEST_ASSUME(std::strcmp(list.head->arguments[0], "arg1") == 0, "First argument should be 'arg1'");
83+
FOSSIL_TEST_ASSUME(std::strcmp(list.head->arguments[1], "arg2") == 0, "Second argument should be 'arg2'");
84+
} // end case
85+
86+
FOSSIL_TEST_CASE(cpp_mock_call_list_destruction) {
87+
// Example of destroying a MockCallList
88+
MockCallList list;
89+
fossil_mock_init(&list);
90+
const char* args[] = {"arg1", "arg2"};
91+
fossil_mock_add_call(&list, "test_function", (char**)args, 2);
92+
fossil_mock_destroy(&list);
93+
94+
// Test cases
95+
FOSSIL_TEST_ASSUME(list.head == NULL, "MockCallList head should be NULL after destruction");
96+
FOSSIL_TEST_ASSUME(list.tail == NULL, "MockCallList tail should be NULL after destruction");
97+
FOSSIL_TEST_ASSUME(list.size == 0, "MockCallList size should be 0 after destruction");
98+
} // end case
99+
100+
FOSSIL_TEST_CASE(cpp_mock_call_list_initialization_macro) {
101+
// Example of initializing a MockCallList using the macro
102+
MockCallList list;
103+
MOCK_INIT(list);
104+
105+
// Test cases
106+
FOSSIL_TEST_ASSUME(list.head == NULL, "MockCallList head should be NULL after initialization");
107+
FOSSIL_TEST_ASSUME(list.tail == NULL, "MockCallList tail should be NULL after initialization");
108+
FOSSIL_TEST_ASSUME(list.size == 0, "MockCallList size should be 0 after initialization");
109+
} // end case
110+
111+
FOSSIL_TEST_CASE(cpp_mock_call_list_addition_macro) {
112+
// Example of adding a MockCall to a MockCallList using the macro
113+
MockCallList list;
114+
MOCK_INIT(list);
115+
const char* args[] = {"arg1", "arg2"};
116+
MOCK_ADD_CALL(list, "test_function", (char**)args, 2);
117+
118+
// Test cases
119+
FOSSIL_TEST_ASSUME(list.size == 1, "MockCallList size should be 1 after adding a call");
120+
//FOSSIL_TEST_ASSUME(list.head->function_name == "test_function", "Function name should be 'test_function'");
121+
FOSSIL_TEST_ASSUME(list.head->num_args == 2, "Number of arguments should be 2");
122+
FOSSIL_TEST_ASSUME(std::strcmp(list.head->arguments[0], "arg1") == 0, "First argument should be 'arg1'");
123+
FOSSIL_TEST_ASSUME(std::strcmp(list.head->arguments[1], "arg2") == 0, "Second argument should be 'arg2'");
124+
} // end case
125+
126+
FOSSIL_TEST_CASE(cpp_mock_call_list_destruction_macro) {
127+
// Example of destroying a MockCallList using the macro
128+
MockCallList list;
129+
MOCK_INIT(list);
130+
const char* args[] = {"arg1", "arg2"};
131+
MOCK_ADD_CALL(list, "test_function", (char**)args, 2);
132+
MOCK_DESTROY(list);
133+
134+
// Test cases
135+
FOSSIL_TEST_ASSUME(list.head == NULL, "MockCallList head should be NULL after destruction");
136+
FOSSIL_TEST_ASSUME(list.tail == NULL, "MockCallList tail should be NULL after destruction");
137+
FOSSIL_TEST_ASSUME(list.size == 0, "MockCallList size should be 0 after destruction");
138+
} // end case
139+
140+
FOSSIL_TEST_CASE(cpp_mock_function_creation) {
141+
// Test cases
142+
FOSSIL_TEST_ASSUME(fossil_mockup_mock_function(2, 3) == 5, "Mock function should return the sum of its arguments");
143+
} // end case
144+
145+
FOSSIL_TEST_CASE(cpp_mock_alias_creation) {
146+
// Example of creating a type alias using the macro
147+
148+
149+
// Test cases
150+
MockInt x = 10;
151+
FOSSIL_TEST_ASSUME(x == 10, "Mock alias should behave like the original type");
152+
} // end case
153+
154+
FOSSIL_TEST_CASE(cpp_mock_struct_creation) {
155+
// Test cases
156+
MockStruct instance;
157+
instance.a = 5;
158+
instance.b = 'c';
159+
FOSSIL_TEST_ASSUME(instance.a == 5, "Mock struct member 'a' should be 5");
160+
FOSSIL_TEST_ASSUME(instance.b == 'c', "Mock struct member 'b' should be 'c'");
161+
} // end case
162+
163+
// * * * * * * * * * * * * * * * * * * * * * * * *
164+
// * Fossil Logic Test Pool
165+
// * * * * * * * * * * * * * * * * * * * * * * * *
166+
FOSSIL_TEST_GROUP(cpp_mock_test_cases) {
167+
FOSSIL_TEST_ADD(cpp_mock_suite, cpp_mock_call_list_initialization);
168+
FOSSIL_TEST_ADD(cpp_mock_suite, cpp_mock_call_list_addition);
169+
// FOSSIL_TEST_ADD(cpp_mock_suite, cpp_mock_call_list_destruction);
170+
FOSSIL_TEST_ADD(cpp_mock_suite, cpp_mock_call_list_initialization_macro);
171+
FOSSIL_TEST_ADD(cpp_mock_suite, cpp_mock_call_list_addition_macro);
172+
// FOSSIL_TEST_ADD(cpp_mock_suite, cpp_mock_call_list_destruction_macro);
173+
FOSSIL_TEST_ADD(cpp_mock_suite, cpp_mock_function_creation);
174+
FOSSIL_TEST_ADD(cpp_mock_suite, cpp_mock_alias_creation);
175+
FOSSIL_TEST_ADD(cpp_mock_suite, cpp_mock_struct_creation);
176+
177+
FOSSIL_TEST_REGISTER(cpp_mock_suite);
178+
} // end of group

code/tests/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ if get_option('with_test').enabled()
22
run_command(['python3', 'tools' / 'generate-runner.py'], check: true)
33

44
test_c = ['unit_runner.c']
5-
test_cases = ['sample', 'bdd', 'tdd', 'ddd', 'mark']
5+
test_cases = ['sample', 'bdd', 'tdd', 'ddd', 'mark', 'mock']
66

77
foreach cases : test_cases
88
test_c += ['cases' / 'test_' + cases + '.c']

0 commit comments

Comments
 (0)