Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion auto/generate_test_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def self.default_options
test_prefix: 'test|spec|should',
mock_prefix: 'Mock',
mock_suffix: '',
mock_name_include_folder: false,
setup_name: 'setUp',
teardown_name: 'tearDown',
test_reset_name: 'resetTest',
Expand Down Expand Up @@ -339,7 +340,10 @@ def create_mock_management(output, mock_headers)
output.puts(' GlobalOrderError = NULL;')
end

mocks = mock_headers.map { |mock| File.basename(mock, '.*') }
# optionally keep the folder a mock is included from to match cmock's naming
mocks = mock_headers.map do |mock|
@options[:mock_name_include_folder] ? mock.sub(/\A(?:\.[\/\\])+/, '').sub(/\.[^.\/\\]*\z/, '') : File.basename(mock, '.*')
end
mocks.each do |mock|
mock_clean = TypeSanitizer.sanitize_c_identifier(mock)
output.puts(" #{mock_clean}_Init();")
Expand Down
9 changes: 9 additions & 0 deletions docs/UnityHelperScriptsGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ This option can also be specified at the command prompt as `--externc`
Unity automatically generates calls to Init, Verify and Destroy for every file included in the main test file that starts with the given mock prefix and ends with the given mock suffix, file extension not included.
By default, Unity assumes a `Mock` prefix and no suffix.

##### `:mock_name_include_folder`

This option names each mock's Init, Verify and Destroy calls by the folder a mock is included from as well as its filename.
A mock included as `uart/MockConfig.h` gets a call to `uart_MockConfig_Init()` rather than `MockConfig_Init()`.
This keeps two mocks of the same filename in different folders distinct within one test.
CMock offers an option of the same name that names its generated functions the same way, and the two must agree.
If you provide the same YAML to the generator as used in CMock's configuration, you've already configured the generator properly.
By default, Unity names mocks by filename alone.

##### `:plugins`

This option specifies an array of plugins to be used (of course, the array can contain only a single plugin).
Expand Down
19 changes: 19 additions & 0 deletions test/testdata/spi/MockConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* =========================================================================
Unity - A Test Framework for C
ThrowTheSwitch.org
Copyright (c) 2007-26 Mike Karlesky, Mark VanderVoord, & Greg Williams
SPDX-License-Identifier: MIT
========================================================================= */

#ifndef SPI_MOCK_CONFIG_H
#define SPI_MOCK_CONFIG_H

extern int spi_MockConfig_Init_Counter;
extern int spi_MockConfig_Verify_Counter;
extern int spi_MockConfig_Destroy_Counter;

void spi_MockConfig_Init(void);
void spi_MockConfig_Verify(void);
void spi_MockConfig_Destroy(void);

#endif
42 changes: 42 additions & 0 deletions test/testdata/testRunnerGeneratorWithSameNamedMocks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* =========================================================================
Unity - A Test Framework for C
ThrowTheSwitch.org
Copyright (c) 2007-26 Mike Karlesky, Mark VanderVoord, & Greg Williams
SPDX-License-Identifier: MIT
========================================================================= */

#include "unity.h"
#include "uart/MockConfig.h"
#include "spi/MockConfig.h"

/* Stand-ins for two mocks sharing a filename, named by the folder each is included by */
int uart_MockConfig_Init_Counter = 0;
int uart_MockConfig_Verify_Counter = 0;
int uart_MockConfig_Destroy_Counter = 0;
int spi_MockConfig_Init_Counter = 0;
int spi_MockConfig_Verify_Counter = 0;
int spi_MockConfig_Destroy_Counter = 0;

void uart_MockConfig_Init(void) { uart_MockConfig_Init_Counter++; }
void uart_MockConfig_Verify(void) { uart_MockConfig_Verify_Counter++; }
void uart_MockConfig_Destroy(void) { uart_MockConfig_Destroy_Counter++; }
void spi_MockConfig_Init(void) { spi_MockConfig_Init_Counter++; }
void spi_MockConfig_Verify(void) { spi_MockConfig_Verify_Counter++; }
void spi_MockConfig_Destroy(void) { spi_MockConfig_Destroy_Counter++; }

void setUp(void) {}
void tearDown(void) {}

void test_ShouldInitEachSameNamedMock(void)
{
TEST_ASSERT_EQUAL_MESSAGE(1, uart_MockConfig_Init_Counter, "First Mock Init Should Be Called");
TEST_ASSERT_EQUAL_MESSAGE(1, spi_MockConfig_Init_Counter, "Second Mock Init Should Be Called");
}

void test_ShouldVerifyAndDestroyEachSameNamedMock(void)
{
TEST_ASSERT_EQUAL_MESSAGE(1, uart_MockConfig_Verify_Counter, "First Mock Verify Should Be Called");
TEST_ASSERT_EQUAL_MESSAGE(1, spi_MockConfig_Verify_Counter, "Second Mock Verify Should Be Called");
TEST_ASSERT_EQUAL_MESSAGE(1, uart_MockConfig_Destroy_Counter, "First Mock Destroy Should Be Called");
TEST_ASSERT_EQUAL_MESSAGE(1, spi_MockConfig_Destroy_Counter, "Second Mock Destroy Should Be Called");
}
19 changes: 19 additions & 0 deletions test/testdata/uart/MockConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* =========================================================================
Unity - A Test Framework for C
ThrowTheSwitch.org
Copyright (c) 2007-26 Mike Karlesky, Mark VanderVoord, & Greg Williams
SPDX-License-Identifier: MIT
========================================================================= */

#ifndef UART_MOCK_CONFIG_H
#define UART_MOCK_CONFIG_H

extern int uart_MockConfig_Init_Counter;
extern int uart_MockConfig_Verify_Counter;
extern int uart_MockConfig_Destroy_Counter;

void uart_MockConfig_Init(void);
void uart_MockConfig_Verify(void);
void uart_MockConfig_Destroy(void);

#endif
29 changes: 29 additions & 0 deletions test/tests/test_generate_test_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@
}
},

{ :name => 'SameNamedMocksByFolder',
:testfile => 'testdata/testRunnerGeneratorWithSameNamedMocks.c',
:testdefines => ['TEST'],
:options => {
:mock_name_include_folder => true,
},
:expected => {
:to_pass => [ 'test_ShouldInitEachSameNamedMock',
'test_ShouldVerifyAndDestroyEachSameNamedMock',
],
:to_fail => [ ],
:to_ignore => [ ],
}
},

{ :name => 'ShorterFilterOfJustTest',
:testfile => 'testdata/testRunnerGenerator.c',
:testdefines => ['TEST'],
Expand Down Expand Up @@ -1383,6 +1398,20 @@ def verify_number(expected, expression, output)
$generate_test_runner_tests += 1
end

should 'NameMocksByBasenameWithoutFolderOption' do
runner_name = OUT_FILE + 'SameNamedMocksByBasename_runner.c'
UnityTestRunnerGenerator.new({}).run('testdata/testRunnerGeneratorWithSameNamedMocks.c', runner_name)

runner = File.read(runner_name)
if runner.scan(' MockConfig_Init();').length == 2 && !runner.include?('uart_MockConfig_Init();')
report 'Runner_NameMocksByBasenameWithoutFolderOption:PASS'
else
report 'Runner_NameMocksByBasenameWithoutFolderOption:FAIL'
$generate_test_runner_failures += 1
end
$generate_test_runner_tests += 1
end

RUNNER_TESTS.each do |testset|
basename = File.basename(testset[:testfile], C_EXTENSION)
testset_name = "Runner_#{basename}_#{testset[:name]}"
Expand Down
Loading