example: wrap trick
[platform/upstream/cmocka.git] / example / customer_database_test.c
1 /*
2  * Copyright 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <stdarg.h>
17 #include <stddef.h>
18 #include <setjmp.h>
19 #include <cmocka.h>
20 #include <database.h>
21
22 extern DatabaseConnection* connect_to_customer_database();
23 extern unsigned int get_customer_id_by_name(
24     DatabaseConnection * const connection, const char * const customer_name);
25
26 /* Mock query database function. */
27 static unsigned int mock_query_database(DatabaseConnection* const connection,
28                                         const char * const query_string,
29                                         void *** const results) {
30     (void) connection; /* unused */
31     (void) query_string; /* unused */
32
33     *results = (void**)((size_t)mock());
34     return (unsigned int)mock();
35 }
36
37 /* Mock of the connect to database function. */
38 DatabaseConnection* connect_to_database(const char * const database_url,
39                                         const unsigned int port) {
40     (void) database_url; /* unused */
41     (void) port; /* unused */
42
43     return (DatabaseConnection*)((size_t)mock());
44 }
45
46 static void test_connect_to_customer_database(void **state) {
47     (void) state; /* unused */
48
49     will_return(connect_to_database, 0x0DA7ABA53);
50
51     assert_int_equal((size_t)connect_to_customer_database(), 0x0DA7ABA53);
52 }
53
54 /* This test fails as the mock function connect_to_database() will have no
55  * value to return. */
56 #if 0
57 static void fail_connect_to_customer_database(void **state) {
58     (void) state; /* unused */
59
60     assert_true(connect_to_customer_database() ==
61                 (DatabaseConnection*)0x0DA7ABA53);
62 }
63 #endif
64
65 static void test_get_customer_id_by_name(void **state) {
66     DatabaseConnection connection = {
67         "somedatabase.somewhere.com", 12345678, mock_query_database
68     };
69     /* Return a single customer ID when mock_query_database() is called. */
70     int customer_ids = 543;
71
72     (void) state; /* unused */
73
74     will_return(mock_query_database, &customer_ids);
75     will_return(mock_query_database, 1);
76     assert_int_equal(get_customer_id_by_name(&connection, "john doe"), 543);
77 }
78
79 int main(void) {
80     const UnitTest tests[] = {
81         unit_test(test_connect_to_customer_database),
82         unit_test(test_get_customer_id_by_name),
83     };
84     return run_tests(tests);
85 }