ThrowTheSwitch / Unity

Simple Unit Testing for C
ThrowTheSwitch.org
MIT License
3.98k stars 965 forks source link

New test suggestion: File Compare #274

Open dpostorivo opened 7 years ago

dpostorivo commented 7 years ago

I've been testing a lot of file IO recently and one thing that would be a nice to have would be a file compare inside of unity. For now I've been opening both files, putting them in a buffer, then comparing the resulting buffers and their size.

Obviously this would not be enabled by default especially in most embedded contexts, but having it will simplify tests for those who do work with file IO.

Thoughts on this? I can get started if others are interested.

mvandervoord commented 7 years ago

I think that's a really good idea. I agree that it would be something that is disabled by default. We have other things that fall into this category already, though. I think it makes a lot of sense.

Personally, I can imagine two useful versions:

TEST_ASSERT_EQUAL_TEXT_FILE - loads the files and then uses TEST_ASSERT_EQUAL_STRING_ARRAY or something? TEST_ASSERT_EQUAL_BINARY_FILE - loads the files and then uses TEST_ASSERT_EQUAL_MEMORY

:)

dpostorivo commented 7 years ago

So I've tried to start making the binary file first which is in #283 . If someone could review it that would be nice.

mvandervoord commented 4 years ago

While the last version got tangled up and discontinued, I'd like to revisit this feature. I still think it's a good one. At this point, I see it being in extras/files and called something like unity_files.h. It could then be included as needed.

Assertions it would add initially:

TEST_ASSERT_EQUAL_FILE_TEXT(exp_filename, act_filename)
TEST_ASSERT_EQUAL_FILE_BINARY(exp_filename, act_filename)
AndersonMartins1 commented 6 months ago

To resolve the problem described, you can follow the steps below:

Add unity_config.h to the Unity Include Directory:

Make sure the directory containing unity_config.h is added to Unity's include directories during compilation. This can be done by specifying -I in the build options. Configure UNITY_INCLUDE_CONFIG_H:

Define the UNITY_INCLUDE_CONFIG_H macro during compilation to enable the required doubles. This can be done using -DUNITY_INCLUDE_CONFIG_H in the build options. Update CMakeLists.txt:

In the CMakeLists.txt file in the testing directory, add the directory containing unity_config.h to the target unity include directories. This can be done using target_include_directories(unity PUBLIC ). Check Directory Hierarchy:

Verify that the directory hierarchy is correct and that unity_config.h is located in a location accessible to Unity during compilation. Test the Configuration:

After making the above changes, compile your project with CMake and check that Unity recognizes unity_config.h correctly and that doubles are enabled as expected. Additional Debugging:

If the problem persists, check that there are no typos or incorrect paths in the CMakeLists.txt settings and that there are no conflicts with other compilation settings or flags. Make sure you follow these steps carefully and check that the settings are correct at each step. If the problem persists, carefully review the error messages and try to identify the root cause of the problem to effectively resolve it.

AndersonMartins1 commented 6 months ago

Vou lhe fornecer um exemplo simples de como você pode configurar o arquivo CMakeLists.txt para incluir o diretório contendo unity_config.h durante a compilação do Unity. Vamos supor que o diretório que contém unity_config.h está localizado em project/testing.

Defina a versão mínima do CMake

cmake_minimum_required(VERSION 3.5)

Nome do seu projeto

project(MyProject)

Adicione o subdiretório contendo os testes Unity

add_subdirectory(testing)

Se houver outras bibliotecas ou arquivos fonte, adicione-os aqui

add_library( )

Se houver um executável, adicione-o aqui

add_executable( )

Agora, no arquivo testing/CMakeLists.txt, você pode incluir o diretório contendo unity_config.h usando target_include_directories

Adicione o diretório contendo unity_config.h aos diretórios de inclusão do target unity

target_include_directories(unity PUBLIC ${CMAKE_CURRENT_LIST_DIR})

Com essas configurações, o CMake irá garantir que o Unity tenha acesso ao arquivo unity_config.h durante a compilação. Certifique-se de adaptar os caminhos e nomes de arquivos de acordo com a estrutura do seu projeto.

AndersonMartins1 commented 6 months ago

Defina a versão mínima do CMake

cmake_minimum_required(VERSION 3.5)

Nome do seu projeto

project(MyProject)

Adicione o diretório de compilação dos testes

add_subdirectory(testing)

Se houver outras bibliotecas ou arquivos fonte, adicione-os aqui

add_library( )

Se houver um executável, adicione-o aqui

add_executable( )

Agora, no arquivo testing/CMakeLists.txt, adicione comentários explicativos e use uma variável para armazenar o caminho do diretório contendo unity_config.h.

Defina o diretório contendo unity_config.h

set(UNITY_CONFIG_DIR ${CMAKE_CURRENT_LIST_DIR})

Adicione o diretório contendo unity_config.h aos diretórios de inclusão do target unity

target_include_directories(unity PUBLIC ${UNITY_CONFIG_DIR})

Com essas melhorias, o código está mais organizado e os comentários fornecem uma explicação clara do que está sendo feito em cada parte do arquivo CMakeLists.txt.