Heather Lee Wilson | bdd62c5 | 2013-12-28 15:12:39 -0800 | [diff] [blame^] | 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 <cmockery.h> |
| 20 | #include <database.h> |
| 21 | |
| 22 | extern DatabaseConnection* connect_to_product_database(); |
| 23 | |
| 24 | /* Mock connect to database function. |
| 25 | * NOTE: This mock function is very general could be shared between tests |
| 26 | * that use the imaginary database.h module. */ |
| 27 | DatabaseConnection* connect_to_database(const char * const url, |
| 28 | const unsigned int port) { |
| 29 | check_expected(url); |
| 30 | check_expected(port); |
| 31 | return (DatabaseConnection*)mock(); |
| 32 | } |
| 33 | |
| 34 | void test_connect_to_product_database(void **state) { |
| 35 | expect_string(connect_to_database, url, "products.abcd.org"); |
| 36 | expect_value(connect_to_database, port, 322); |
| 37 | will_return(connect_to_database, 0xDA7ABA53); |
| 38 | assert_int_equal((int)connect_to_product_database(), 0xDA7ABA53); |
| 39 | } |
| 40 | |
| 41 | /* This test will fail since the expected URL is different to the URL that is |
| 42 | * passed to connect_to_database() by connect_to_product_database(). */ |
| 43 | void test_connect_to_product_database_bad_url(void **state) { |
| 44 | expect_string(connect_to_database, url, "products.abcd.com"); |
| 45 | expect_value(connect_to_database, port, 322); |
| 46 | will_return(connect_to_database, 0xDA7ABA53); |
| 47 | assert_int_equal((int)connect_to_product_database(), 0xDA7ABA53); |
| 48 | } |
| 49 | |
| 50 | /* This test will fail since the mock connect_to_database() will attempt to |
| 51 | * retrieve a value for the parameter port which isn't specified by this |
| 52 | * test function. */ |
| 53 | void test_connect_to_product_database_missing_parameter(void **state) { |
| 54 | expect_string(connect_to_database, url, "products.abcd.org"); |
| 55 | will_return(connect_to_database, 0xDA7ABA53); |
| 56 | assert_int_equal((int)connect_to_product_database(), 0xDA7ABA53); |
| 57 | } |
| 58 | |
| 59 | int main(int argc, char* argv[]) { |
| 60 | const UnitTest tests[] = { |
| 61 | unit_test(test_connect_to_product_database), |
| 62 | unit_test(test_connect_to_product_database_bad_url), |
| 63 | unit_test(test_connect_to_product_database_missing_parameter), |
| 64 | }; |
| 65 | return run_tests(tests); |
| 66 | } |