Skip to content

Commit

Permalink
✅ Added tests for preprocessing feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarlesky committed Jul 29, 2024
1 parent 4d6e750 commit c242ca1
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 0 deletions.
17 changes: 17 additions & 0 deletions assets/tests_with_preprocessing/src/adc_hardwareA.c
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions assets/tests_with_preprocessing/src/adc_hardwareA.h
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions assets/tests_with_preprocessing/src/adc_hardwareB.c
Original file line number Diff line number Diff line change
@@ -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();
}
13 changes: 13 additions & 0 deletions assets/tests_with_preprocessing/src/adc_hardwareB.h
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions assets/tests_with_preprocessing/src/adc_hardwareC.c
Original file line number Diff line number Diff line change
@@ -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();
}
13 changes: 13 additions & 0 deletions assets/tests_with_preprocessing/src/adc_hardwareC.h
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions assets/tests_with_preprocessing/src/adc_hardware_configuratorA.h
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions assets/tests_with_preprocessing/src/adc_hardware_configuratorB.h
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions assets/tests_with_preprocessing/src/adc_hardware_configuratorC.h
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions assets/tests_with_preprocessing/test/test_adc_hardwareA.c
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions assets/tests_with_preprocessing/test/test_adc_hardwareB.c
Original file line number Diff line number Diff line change
@@ -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();
}
29 changes: 29 additions & 0 deletions assets/tests_with_preprocessing/test/test_adc_hardwareC.c
Original file line number Diff line number Diff line change
@@ -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
107 changes: 107 additions & 0 deletions spec/spec_system_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions spec/system/deployment_as_gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
10 changes: 10 additions & 0 deletions spec/system/deployment_as_vendor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 }
Expand Down

0 comments on commit c242ca1

Please sign in to comment.