From c242ca166568800cd10188b360cacd03095091ef Mon Sep 17 00:00:00 2001 From: Michael Karlesky Date: Mon, 29 Jul 2024 15:10:41 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Added=20tests=20for=20preprocessing?= =?UTF-8?q?=20feature?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/adc_hardwareA.c | 17 +++ .../src/adc_hardwareA.h | 13 +++ .../src/adc_hardwareB.c | 14 +++ .../src/adc_hardwareB.h | 13 +++ .../src/adc_hardwareC.c | 14 +++ .../src/adc_hardwareC.h | 13 +++ .../src/adc_hardware_configuratorA.h | 13 +++ .../src/adc_hardware_configuratorB.h | 15 +++ .../src/adc_hardware_configuratorC.h | 15 +++ .../test/test_adc_hardwareA.c | 41 +++++++ .../test/test_adc_hardwareB.c | 25 ++++ .../test/test_adc_hardwareC.c | 29 +++++ spec/spec_system_helper.rb | 107 ++++++++++++++++++ spec/system/deployment_as_gem_spec.rb | 5 + spec/system/deployment_as_vendor_spec.rb | 10 ++ 15 files changed, 344 insertions(+) create mode 100644 assets/tests_with_preprocessing/src/adc_hardwareA.c create mode 100644 assets/tests_with_preprocessing/src/adc_hardwareA.h create mode 100644 assets/tests_with_preprocessing/src/adc_hardwareB.c create mode 100644 assets/tests_with_preprocessing/src/adc_hardwareB.h create mode 100644 assets/tests_with_preprocessing/src/adc_hardwareC.c create mode 100644 assets/tests_with_preprocessing/src/adc_hardwareC.h create mode 100644 assets/tests_with_preprocessing/src/adc_hardware_configuratorA.h create mode 100644 assets/tests_with_preprocessing/src/adc_hardware_configuratorB.h create mode 100644 assets/tests_with_preprocessing/src/adc_hardware_configuratorC.h create mode 100644 assets/tests_with_preprocessing/test/test_adc_hardwareA.c create mode 100644 assets/tests_with_preprocessing/test/test_adc_hardwareB.c create mode 100644 assets/tests_with_preprocessing/test/test_adc_hardwareC.c diff --git a/assets/tests_with_preprocessing/src/adc_hardwareA.c b/assets/tests_with_preprocessing/src/adc_hardwareA.c new file mode 100644 index 000000000..4a28bbe49 --- /dev/null +++ b/assets/tests_with_preprocessing/src/adc_hardwareA.c @@ -0,0 +1,17 @@ +/* ========================================================================= + Ceedling - Test-Centered Build System for C + ThrowTheSwitch.org + Copyright (c) 2010-24 Mike Karlesky, Mark VanderVoord, & Greg Williams + SPDX-License-Identifier: MIT +========================================================================= */ + +#include "adc_hardwareA.h" +#include "adc_hardware_configuratorA.h" + +void AdcHardware_Init(void) +{ + // Reusing outer test file preprocessing symbol to prevent linking failure + #ifdef PREPROCESSING_TESTS + Adc_Reset(); + #endif +} diff --git a/assets/tests_with_preprocessing/src/adc_hardwareA.h b/assets/tests_with_preprocessing/src/adc_hardwareA.h new file mode 100644 index 000000000..7483ac99a --- /dev/null +++ b/assets/tests_with_preprocessing/src/adc_hardwareA.h @@ -0,0 +1,13 @@ +/* ========================================================================= + Ceedling - Test-Centered Build System for C + ThrowTheSwitch.org + Copyright (c) 2010-24 Mike Karlesky, Mark VanderVoord, & Greg Williams + SPDX-License-Identifier: MIT +========================================================================= */ + +#ifndef _ADCHARDWARE_H +#define _ADCHARDWARE_H + +void AdcHardware_Init(void); + +#endif // _ADCHARDWARE_H diff --git a/assets/tests_with_preprocessing/src/adc_hardwareB.c b/assets/tests_with_preprocessing/src/adc_hardwareB.c new file mode 100644 index 000000000..0d3e1b2d3 --- /dev/null +++ b/assets/tests_with_preprocessing/src/adc_hardwareB.c @@ -0,0 +1,14 @@ +/* ========================================================================= + Ceedling - Test-Centered Build System for C + ThrowTheSwitch.org + Copyright (c) 2010-24 Mike Karlesky, Mark VanderVoord, & Greg Williams + SPDX-License-Identifier: MIT +========================================================================= */ + +#include "adc_hardwareB.h" +#include "adc_hardware_configuratorB.h" + +void AdcHardware_Init(void) +{ + Adc_Reset(); +} diff --git a/assets/tests_with_preprocessing/src/adc_hardwareB.h b/assets/tests_with_preprocessing/src/adc_hardwareB.h new file mode 100644 index 000000000..7483ac99a --- /dev/null +++ b/assets/tests_with_preprocessing/src/adc_hardwareB.h @@ -0,0 +1,13 @@ +/* ========================================================================= + Ceedling - Test-Centered Build System for C + ThrowTheSwitch.org + Copyright (c) 2010-24 Mike Karlesky, Mark VanderVoord, & Greg Williams + SPDX-License-Identifier: MIT +========================================================================= */ + +#ifndef _ADCHARDWARE_H +#define _ADCHARDWARE_H + +void AdcHardware_Init(void); + +#endif // _ADCHARDWARE_H diff --git a/assets/tests_with_preprocessing/src/adc_hardwareC.c b/assets/tests_with_preprocessing/src/adc_hardwareC.c new file mode 100644 index 000000000..66f811595 --- /dev/null +++ b/assets/tests_with_preprocessing/src/adc_hardwareC.c @@ -0,0 +1,14 @@ +/* ========================================================================= + Ceedling - Test-Centered Build System for C + ThrowTheSwitch.org + Copyright (c) 2010-24 Mike Karlesky, Mark VanderVoord, & Greg Williams + SPDX-License-Identifier: MIT +========================================================================= */ + +#include "adc_hardwareC.h" +#include "adc_hardware_configuratorC.h" + +void AdcHardware_Init(void) +{ + Adc_Reset(); +} diff --git a/assets/tests_with_preprocessing/src/adc_hardwareC.h b/assets/tests_with_preprocessing/src/adc_hardwareC.h new file mode 100644 index 000000000..7483ac99a --- /dev/null +++ b/assets/tests_with_preprocessing/src/adc_hardwareC.h @@ -0,0 +1,13 @@ +/* ========================================================================= + Ceedling - Test-Centered Build System for C + ThrowTheSwitch.org + Copyright (c) 2010-24 Mike Karlesky, Mark VanderVoord, & Greg Williams + SPDX-License-Identifier: MIT +========================================================================= */ + +#ifndef _ADCHARDWARE_H +#define _ADCHARDWARE_H + +void AdcHardware_Init(void); + +#endif // _ADCHARDWARE_H diff --git a/assets/tests_with_preprocessing/src/adc_hardware_configuratorA.h b/assets/tests_with_preprocessing/src/adc_hardware_configuratorA.h new file mode 100644 index 000000000..d474d0799 --- /dev/null +++ b/assets/tests_with_preprocessing/src/adc_hardware_configuratorA.h @@ -0,0 +1,13 @@ +/* ========================================================================= + Ceedling - Test-Centered Build System for C + ThrowTheSwitch.org + Copyright (c) 2010-24 Mike Karlesky, Mark VanderVoord, & Greg Williams + SPDX-License-Identifier: MIT +========================================================================= */ + +#ifndef _ADCHARDWARECONFIGURATOR_H +#define _ADCHARDWARECONFIGURATOR_H + +void Adc_Reset(void); + +#endif // _ADCHARDWARECONFIGURATOR_H diff --git a/assets/tests_with_preprocessing/src/adc_hardware_configuratorB.h b/assets/tests_with_preprocessing/src/adc_hardware_configuratorB.h new file mode 100644 index 000000000..4b3ef81a3 --- /dev/null +++ b/assets/tests_with_preprocessing/src/adc_hardware_configuratorB.h @@ -0,0 +1,15 @@ +/* ========================================================================= + Ceedling - Test-Centered Build System for C + ThrowTheSwitch.org + Copyright (c) 2010-24 Mike Karlesky, Mark VanderVoord, & Greg Williams + SPDX-License-Identifier: MIT +========================================================================= */ + +#ifndef _ADCHARDWARECONFIGURATOR_H +#define _ADCHARDWARECONFIGURATOR_H + +#ifdef PREPROCESSING_MOCKS +void Adc_Reset(void); +#endif + +#endif // _ADCHARDWARECONFIGURATOR_H diff --git a/assets/tests_with_preprocessing/src/adc_hardware_configuratorC.h b/assets/tests_with_preprocessing/src/adc_hardware_configuratorC.h new file mode 100644 index 000000000..4b3ef81a3 --- /dev/null +++ b/assets/tests_with_preprocessing/src/adc_hardware_configuratorC.h @@ -0,0 +1,15 @@ +/* ========================================================================= + Ceedling - Test-Centered Build System for C + ThrowTheSwitch.org + Copyright (c) 2010-24 Mike Karlesky, Mark VanderVoord, & Greg Williams + SPDX-License-Identifier: MIT +========================================================================= */ + +#ifndef _ADCHARDWARECONFIGURATOR_H +#define _ADCHARDWARECONFIGURATOR_H + +#ifdef PREPROCESSING_MOCKS +void Adc_Reset(void); +#endif + +#endif // _ADCHARDWARECONFIGURATOR_H diff --git a/assets/tests_with_preprocessing/test/test_adc_hardwareA.c b/assets/tests_with_preprocessing/test/test_adc_hardwareA.c new file mode 100644 index 000000000..0d2cac8c7 --- /dev/null +++ b/assets/tests_with_preprocessing/test/test_adc_hardwareA.c @@ -0,0 +1,41 @@ +/* ========================================================================= + Ceedling - Test-Centered Build System for C + ThrowTheSwitch.org + Copyright (c) 2010-24 Mike Karlesky, Mark VanderVoord, & Greg Williams + SPDX-License-Identifier: MIT +========================================================================= */ + +#include "unity.h" +#include "adc_hardwareA.h" +#ifdef PREPROCESSING_TESTS +#include "mock_adc_hardware_configuratorA.h" +#endif + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +#ifdef PREPROCESSING_TESTS +void test_init_should_call_adc_reset(void) +{ + Adc_Reset_Expect(); + + AdcHardware_Init(); +} +#endif + +#ifndef PREPROCESSING_TESTS +void test_caseA_should_fail(void) +{ + TEST_FAIL_MESSAGE("Intentional failure"); +} + +void test_caseB_should_fail(void) +{ + TEST_FAIL_MESSAGE("Intentional failure"); +} +#endif \ No newline at end of file diff --git a/assets/tests_with_preprocessing/test/test_adc_hardwareB.c b/assets/tests_with_preprocessing/test/test_adc_hardwareB.c new file mode 100644 index 000000000..fcd4183a7 --- /dev/null +++ b/assets/tests_with_preprocessing/test/test_adc_hardwareB.c @@ -0,0 +1,25 @@ +/* ========================================================================= + Ceedling - Test-Centered Build System for C + ThrowTheSwitch.org + Copyright (c) 2010-24 Mike Karlesky, Mark VanderVoord, & Greg Williams + SPDX-License-Identifier: MIT +========================================================================= */ + +#include "unity.h" +#include "adc_hardwareB.h" +#include "mock_adc_hardware_configuratorB.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_init_should_call_adc_reset(void) +{ + Adc_Reset_Expect(); + + AdcHardware_Init(); +} diff --git a/assets/tests_with_preprocessing/test/test_adc_hardwareC.c b/assets/tests_with_preprocessing/test/test_adc_hardwareC.c new file mode 100644 index 000000000..e38dab62b --- /dev/null +++ b/assets/tests_with_preprocessing/test/test_adc_hardwareC.c @@ -0,0 +1,29 @@ +/* ========================================================================= + Ceedling - Test-Centered Build System for C + ThrowTheSwitch.org + Copyright (c) 2010-24 Mike Karlesky, Mark VanderVoord, & Greg Williams + SPDX-License-Identifier: MIT +========================================================================= */ + +#include "unity.h" +#include "adc_hardwareC.h" +#ifdef PREPROCESSING_TESTS +#include "mock_adc_hardware_configuratorC.h" +#endif + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +#ifdef PREPROCESSING_TESTS +void test_init_should_call_adc_reset(void) +{ + Adc_Reset_Expect(); + + AdcHardware_Init(); +} +#endif \ No newline at end of file diff --git a/spec/spec_system_helper.rb b/spec/spec_system_helper.rb index db3ed2248..318c7599f 100644 --- a/spec/spec_system_helper.rb +++ b/spec/spec_system_helper.rb @@ -440,6 +440,113 @@ def can_test_projects_unity_parameterized_test_cases_with_success end end + def can_test_projects_with_preprocessing_for_test_files_symbols_undefined + @c.with_context do + Dir.chdir @proj_name do + FileUtils.cp test_asset_path("tests_with_preprocessing/test/test_adc_hardwareA.c"), 'test/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardwareA.c"), 'src/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardwareA.h"), 'src/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardware_configuratorA.h"), 'src/' + # Rely on undefined symbols in our C files + # 2 enabled intentionally failing test cases (no mocks generated) + settings = { :project => { :use_test_preprocessor => :tests }, + } + @c.merge_project_yml_for_test(settings) + + output = `bundle exec ruby -S ceedling test:adc_hardwareA 2>&1` + expect($?.exitstatus).to match(1) # Intentional test failure in successful build + expect(output).to match(/TESTED:\s+2/) + expect(output).to match(/PASSED:\s+0/) + expect(output).to match(/FAILED:\s+2/) + end + end + end + + def can_test_projects_with_preprocessing_for_test_files_symbols_defined + @c.with_context do + Dir.chdir @proj_name do + FileUtils.cp test_asset_path("tests_with_preprocessing/test/test_adc_hardwareA.c"), 'test/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardwareA.c"), 'src/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardwareA.h"), 'src/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardware_configuratorA.h"), 'src/' + # 1 enabled passing test case with 1 mock used + settings = { :project => { :use_test_preprocessor => :tests }, + :defines => { :test => ['PREPROCESSING_TESTS'] } + } + @c.merge_project_yml_for_test(settings) + + output = `bundle exec ruby -S ceedling test:adc_hardwareA 2>&1` + expect($?.exitstatus).to match(0) # Successful build and tests + expect(output).to match(/TESTED:\s+1/) + expect(output).to match(/PASSED:\s+1/) + expect(output).to match(/FAILED:\s+0/) + end + end + end + + def can_test_projects_with_preprocessing_for_mocks_success + @c.with_context do + Dir.chdir @proj_name do + FileUtils.cp test_asset_path("tests_with_preprocessing/test/test_adc_hardwareB.c"), 'test/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardwareB.c"), 'src/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardwareB.h"), 'src/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardware_configuratorB.h"), 'src/' + # 1 test case with 1 mocked function + settings = { :project => { :use_test_preprocessor => :mocks }, + :defines => { :test => ['PREPROCESSING_MOCKS'] } + } + @c.merge_project_yml_for_test(settings) + + output = `bundle exec ruby -S ceedling test:adc_hardwareB 2>&1` + expect($?.exitstatus).to match(0) # Successful build and tests + expect(output).to match(/TESTED:\s+1/) + expect(output).to match(/PASSED:\s+1/) + expect(output).to match(/FAILED:\s+0/) + end + end + end + + def can_test_projects_with_preprocessing_for_mocks_intentional_build_failure + @c.with_context do + Dir.chdir @proj_name do + FileUtils.cp test_asset_path("tests_with_preprocessing/test/test_adc_hardwareB.c"), 'test/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardwareB.c"), 'src/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardwareB.h"), 'src/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardware_configuratorB.h"), 'src/' + # 1 test case with a missing mocked function + settings = { :project => { :use_test_preprocessor => :mocks } + } + @c.merge_project_yml_for_test(settings) + + output = `bundle exec ruby -S ceedling test:adc_hardwareB 2>&1` + expect($?.exitstatus).to match(1) # Failing build because of missing mock + expect(output).to match(/undefined reference to `Adc_Reset_Expect'/) + end + end + end + + def can_test_projects_with_preprocessing_all + @c.with_context do + Dir.chdir @proj_name do + FileUtils.cp test_asset_path("tests_with_preprocessing/test/test_adc_hardwareC.c"), 'test/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardwareC.c"), 'src/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardwareC.h"), 'src/' + FileUtils.cp test_asset_path("tests_with_preprocessing/src/adc_hardware_configuratorC.h"), 'src/' + # 1 test case using 1 mock + settings = { :project => { :use_test_preprocessor => :all }, + :defines => { :test => ['PREPROCESSING_TESTS', 'PREPROCESSING_MOCKS'] } + } + @c.merge_project_yml_for_test(settings) + + output = `bundle exec ruby -S ceedling test:adc_hardwareC 2>&1` + expect($?.exitstatus).to match(0) # Successful build and tests + expect(output).to match(/TESTED:\s+1/) + expect(output).to match(/PASSED:\s+1/) + expect(output).to match(/FAILED:\s+0/) + end + end + end + def can_test_projects_with_fail @c.with_context do Dir.chdir @proj_name do diff --git a/spec/system/deployment_as_gem_spec.rb b/spec/system/deployment_as_gem_spec.rb index f5ab15864..446f2fa61 100644 --- a/spec/system/deployment_as_gem_spec.rb +++ b/spec/system/deployment_as_gem_spec.rb @@ -37,6 +37,11 @@ it { can_test_projects_with_success_test_alias } it { can_test_projects_with_test_name_replaced_defines_with_success } it { can_test_projects_unity_parameterized_test_cases_with_success } + it { can_test_projects_with_preprocessing_for_test_files_symbols_undefined } + it { can_test_projects_with_preprocessing_for_test_files_symbols_defined } + it { can_test_projects_with_preprocessing_for_mocks_success } + it { can_test_projects_with_preprocessing_for_mocks_intentional_build_failure } + it { can_test_projects_with_preprocessing_all } it { can_test_projects_with_success_default } it { can_test_projects_with_unity_exec_time } it { can_test_projects_with_test_and_vendor_defines_with_success } diff --git a/spec/system/deployment_as_vendor_spec.rb b/spec/system/deployment_as_vendor_spec.rb index 5a8a05b03..7fff49135 100644 --- a/spec/system/deployment_as_vendor_spec.rb +++ b/spec/system/deployment_as_vendor_spec.rb @@ -38,6 +38,11 @@ it { can_test_projects_with_success_test_alias } it { can_test_projects_with_test_name_replaced_defines_with_success } it { can_test_projects_unity_parameterized_test_cases_with_success } + it { can_test_projects_with_preprocessing_for_test_files_symbols_undefined } + it { can_test_projects_with_preprocessing_for_test_files_symbols_defined } + it { can_test_projects_with_preprocessing_for_mocks_success } + it { can_test_projects_with_preprocessing_for_mocks_intentional_build_failure } + it { can_test_projects_with_preprocessing_all } it { can_test_projects_with_success_default } it { can_test_projects_with_unity_exec_time } it { can_test_projects_with_test_and_vendor_defines_with_success } @@ -94,6 +99,11 @@ it { can_test_projects_with_success_test_alias } it { can_test_projects_with_test_name_replaced_defines_with_success } it { can_test_projects_unity_parameterized_test_cases_with_success } + it { can_test_projects_with_preprocessing_for_test_files_symbols_undefined } + it { can_test_projects_with_preprocessing_for_test_files_symbols_defined } + it { can_test_projects_with_preprocessing_for_mocks_success } + it { can_test_projects_with_preprocessing_for_mocks_intentional_build_failure } + it { can_test_projects_with_preprocessing_all } it { can_test_projects_with_success_default } it { can_test_projects_with_unity_exec_time } it { can_test_projects_with_test_and_vendor_defines_with_success }