From 7b3afcaf77f96e7d62f6cd1623ead7f17512d79f Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sat, 24 Feb 2024 22:47:46 +1100 Subject: repo init. partial port of existing code --- deps/Unity/extras/bdd/readme.md | 40 +++++++++++ deps/Unity/extras/bdd/src/unity_bdd.h | 44 ++++++++++++ deps/Unity/extras/bdd/test/meson.build | 9 +++ deps/Unity/extras/bdd/test/test_bdd.c | 128 +++++++++++++++++++++++++++++++++ 4 files changed, 221 insertions(+) create mode 100644 deps/Unity/extras/bdd/readme.md create mode 100644 deps/Unity/extras/bdd/src/unity_bdd.h create mode 100644 deps/Unity/extras/bdd/test/meson.build create mode 100644 deps/Unity/extras/bdd/test/test_bdd.c (limited to 'deps/Unity/extras/bdd') diff --git a/deps/Unity/extras/bdd/readme.md b/deps/Unity/extras/bdd/readme.md new file mode 100644 index 0000000..e703588 --- /dev/null +++ b/deps/Unity/extras/bdd/readme.md @@ -0,0 +1,40 @@ +# Unity Project - BDD Feature + +Unity's Behavior-Driven Development (BDD) test feature. It allows developers to structure and describe various phases (Given, When, Then) of a test scenario in a BDD-style format. + +## Introduction + +This project is based on the Unity framework originally created by Mike Karlesky, Mark VanderVoord, and Greg Williams in 2007. The project extends Unity by providing macros to define BDD structures with descriptive elements. Feature added by Michael Gene Brockus (Dreamer). + +## License + +This project is distributed under the MIT License. See the [license.txt](license.txt) file for more information. + +## Usage + +### BDD Macros + +The provided BDD macros allow you to structure your test scenarios in a descriptive manner. These macros are for descriptive purposes only and do not have functional behavior. + +- `GIVEN(description)`: Describes the "Given" phase of a test scenario. +- `WHEN(description)`: Describes the "When" phase of a test scenario. +- `THEN(description)`: Describes the "Then" phase of a test scenario. + +Example usage: + +```c +GIVEN("a valid input") { + // Test setup and context + // ... + + WHEN("the input is processed") { + // Perform the action + // ... + + THEN("the expected outcome occurs") { + // Assert the outcome + // ... + } + } +} +``` diff --git a/deps/Unity/extras/bdd/src/unity_bdd.h b/deps/Unity/extras/bdd/src/unity_bdd.h new file mode 100644 index 0000000..d91b3f1 --- /dev/null +++ b/deps/Unity/extras/bdd/src/unity_bdd.h @@ -0,0 +1,44 @@ +/* Copyright (c) 2023 Michael Gene Brockus (Dreamer) and Contributed to Unity Project + * ========================================== + * Unity Project - A Test Framework for C + * Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams + * [Released under MIT License. Please refer to license.txt for details] + * ========================================== */ + +#ifndef UNITY_BDD_TEST_H_ +#define UNITY_BDD_TEST_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include + +/** + * @brief Macros for defining a Behavior-Driven Development (BDD) structure with descriptions. + * + * These macros provide a way to structure and describe different phases (Given, When, Then) of a + * test scenario in a BDD-style format. However, they don't have functional behavior by themselves + * and are used for descriptive purposes. + */ +#define GIVEN(description) \ + if (0) { \ + printf("Given %s\n", description); \ + } else + +#define WHEN(description) \ + if (0) { \ + printf("When %s\n", description); \ + } else + +#define THEN(description) \ + if (0) { \ + printf("Then %s\n", description); \ + } else + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/deps/Unity/extras/bdd/test/meson.build b/deps/Unity/extras/bdd/test/meson.build new file mode 100644 index 0000000..fcdafff --- /dev/null +++ b/deps/Unity/extras/bdd/test/meson.build @@ -0,0 +1,9 @@ +project('BDD Tester', 'c') + +# Add Unity as a dependency +unity_dep = dependency('unity') + +# Define your source files +sources = files('test_bdd.c') + +executable('tester', sources, dependencies : unity_dep) diff --git a/deps/Unity/extras/bdd/test/test_bdd.c b/deps/Unity/extras/bdd/test/test_bdd.c new file mode 100644 index 0000000..4f41585 --- /dev/null +++ b/deps/Unity/extras/bdd/test/test_bdd.c @@ -0,0 +1,128 @@ +/* ========================================== + * Unity Project - A Test Framework for C + * Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams + * [Released under MIT License. Please refer to license.txt for details] + * ========================================== */ + +#include "unity.h" +#include "unity_bdd.h" + +void test_bdd_logic_test(void) { + GIVEN("a valid statement is passed") + { + // Set up the context + bool givenExecuted = true; + + WHEN("a statement is true") + { + // Perform the login action + bool whenExecuted = true; + + THEN("we validate everything was worked") + { + // Check the expected outcome + bool thenExecuted = true; + + TEST_ASSERT_TRUE(givenExecuted); + TEST_ASSERT_TRUE(whenExecuted); + TEST_ASSERT_TRUE(thenExecuted); + } + } + } +} // end of case + +void test_bdd_user_account(void) { + GIVEN("a user's account with sufficient balance") + { + // Set up the context + float accountBalance = 500.0; + float withdrawalAmount = 200.0; + + WHEN("the user requests a withdrawal of $200") + { + // Perform the withdrawal action + if (accountBalance >= withdrawalAmount) + { + accountBalance -= withdrawalAmount; + } // end if + THEN("the withdrawal amount should be deducted from the account balance") + { + // Check the expected outcome + + // Simulate the scenario + float compareBalance = 500.0; + TEST_ASSERT_LESS_THAN_FLOAT(accountBalance, compareBalance); + } + } + } +} // end of case + +void test_bdd_empty_cart(void) { + GIVEN("a user with an empty shopping cart") + { + // Set up the context + int cartItemCount = 0; + + WHEN("the user adds a product to the cart") + { + // Perform the action of adding a product + + THEN("the cart item count should increase by 1") + { + // Check the expected outcome + cartItemCount++; + + TEST_ASSERT_EQUAL_INT(cartItemCount, 1); + } + } + } +} // end of case + +void test_bdd_valid_login(void) { + GIVEN("a registered user with valid credentials") + { + // Set up the context + const char* validUsername = "user123"; + const char* validPassword = "pass456"; + + WHEN("the user provides correct username and password") + { + // Perform the action of user login + const char* inputUsername = "user123"; + const char* inputPassword = "pass456"; + + THEN("the login should be successful") + { + // Check the expected outcome + // Simulate login validation + TEST_ASSERT_EQUAL_STRING(inputUsername, validUsername); + TEST_ASSERT_EQUAL_STRING(inputPassword, validPassword); + } + } + + WHEN("the user provides incorrect password") + { + // Perform the action of user login + const char* inputUsername = "user123"; + const char* inputPassword = "wrongpass"; + + THEN("the login should fail with an error message") + { + // Check the expected outcome + // Simulate login validation + TEST_ASSERT_EQUAL_STRING(inputUsername, validUsername); + // TEST_ASSERT_NOT_EQUAL_STRING(inputPassword, validPassword); + } + } + } +} // end of case + +int main(void) +{ + UnityBegin("test_bdd.c"); + RUN_TEST(test_bdd_logic_test); + RUN_TEST(test_bdd_user_account); + RUN_TEST(test_bdd_empty_cart); + RUN_TEST(test_bdd_valid_login); + return UnityEnd(); +} -- cgit v1.2.3-70-g09d2