|
| 1 | +/* ========================================== |
| 2 | + * Unity Project - A Test Framework for C |
| 3 | + * Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams |
| 4 | + * [Released under MIT License. Please refer to license.txt for details] |
| 5 | + * ========================================== */ |
| 6 | + |
| 7 | +#include "unity.h" |
| 8 | +#include "unity_bdd.h" |
| 9 | + |
| 10 | +void test_bdd_logic_test(void) { |
| 11 | + GIVEN("a valid statement is passed") |
| 12 | + { |
| 13 | + // Set up the context |
| 14 | + bool givenExecuted = true; |
| 15 | + |
| 16 | + WHEN("a statement is true") |
| 17 | + { |
| 18 | + // Perform the login action |
| 19 | + bool whenExecuted = true; |
| 20 | + |
| 21 | + THEN("we validate everything was worked") |
| 22 | + { |
| 23 | + // Check the expected outcome |
| 24 | + bool thenExecuted = true; |
| 25 | + |
| 26 | + TEST_ASSERT_TRUE(givenExecuted); |
| 27 | + TEST_ASSERT_TRUE(whenExecuted); |
| 28 | + TEST_ASSERT_TRUE(thenExecuted); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | +} // end of case |
| 33 | + |
| 34 | +void test_bdd_user_account(void) { |
| 35 | + GIVEN("a user's account with sufficient balance") |
| 36 | + { |
| 37 | + // Set up the context |
| 38 | + float accountBalance = 500.0; |
| 39 | + float withdrawalAmount = 200.0; |
| 40 | + |
| 41 | + WHEN("the user requests a withdrawal of $200") |
| 42 | + { |
| 43 | + // Perform the withdrawal action |
| 44 | + if (accountBalance >= withdrawalAmount) |
| 45 | + { |
| 46 | + accountBalance -= withdrawalAmount; |
| 47 | + } // end if |
| 48 | + THEN("the withdrawal amount should be deducted from the account balance") |
| 49 | + { |
| 50 | + // Check the expected outcome |
| 51 | + |
| 52 | + // Simulate the scenario |
| 53 | + float compareBalance = 500.0; |
| 54 | + TEST_ASSERT_LESS_THAN_FLOAT(accountBalance, compareBalance); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} // end of case |
| 59 | + |
| 60 | +void test_bdd_empty_cart(void) { |
| 61 | + GIVEN("a user with an empty shopping cart") |
| 62 | + { |
| 63 | + // Set up the context |
| 64 | + int cartItemCount = 0; |
| 65 | + |
| 66 | + WHEN("the user adds a product to the cart") |
| 67 | + { |
| 68 | + // Perform the action of adding a product |
| 69 | + |
| 70 | + THEN("the cart item count should increase by 1") |
| 71 | + { |
| 72 | + // Check the expected outcome |
| 73 | + cartItemCount++; |
| 74 | + |
| 75 | + TEST_ASSERT_EQUAL_INT(cartItemCount, 1); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} // end of case |
| 80 | + |
| 81 | +void test_bdd_valid_login(void) { |
| 82 | + GIVEN("a registered user with valid credentials") |
| 83 | + { |
| 84 | + // Set up the context |
| 85 | + const char* validUsername = "user123"; |
| 86 | + const char* validPassword = "pass456"; |
| 87 | + |
| 88 | + WHEN("the user provides correct username and password") |
| 89 | + { |
| 90 | + // Perform the action of user login |
| 91 | + const char* inputUsername = "user123"; |
| 92 | + const char* inputPassword = "pass456"; |
| 93 | + |
| 94 | + THEN("the login should be successful") |
| 95 | + { |
| 96 | + // Check the expected outcome |
| 97 | + // Simulate login validation |
| 98 | + TEST_ASSERT_EQUAL_STRING(inputUsername, validUsername); |
| 99 | + TEST_ASSERT_EQUAL_STRING(inputPassword, validPassword); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + WHEN("the user provides incorrect password") |
| 104 | + { |
| 105 | + // Perform the action of user login |
| 106 | + const char* inputUsername = "user123"; |
| 107 | + const char* inputPassword = "wrongpass"; |
| 108 | + |
| 109 | + THEN("the login should fail with an error message") |
| 110 | + { |
| 111 | + // Check the expected outcome |
| 112 | + // Simulate login validation |
| 113 | + TEST_ASSERT_EQUAL_STRING(inputUsername, validUsername); |
| 114 | + // TEST_ASSERT_NOT_EQUAL_STRING(inputPassword, validPassword); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} // end of case |
| 119 | + |
| 120 | +int main(void) |
| 121 | +{ |
| 122 | + UnityBegin("test_bdd.c"); |
| 123 | + RUN_TEST(test_bdd_logic_test); |
| 124 | + RUN_TEST(test_bdd_user_account); |
| 125 | + RUN_TEST(test_bdd_empty_cart); |
| 126 | + RUN_TEST(test_bdd_valid_login); |
| 127 | + return UnityEnd(); |
| 128 | +} |
0 commit comments