Skip to content

Commit

Permalink
Merge pull request #36 from approvals/existing_catch_main
Browse files Browse the repository at this point in the history
Support Existing catch main
  • Loading branch information
claremacrae committed Oct 14, 2019
2 parents 0885399 + 902742b commit a48dde3
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 9 deletions.
8 changes: 7 additions & 1 deletion ApprovalTests/integrations/catch/Catch2Approvals.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
#include "ApprovalTests/namers/ApprovalTestNamer.h"

// <SingleHpp unalterable>
#if defined(APPROVALS_CATCH_EXISTING_MAIN)
#define APPROVALS_CATCH
#define CATCH_CONFIG_RUNNER
#elif defined(APPROVALS_CATCH)
#define CATCH_CONFIG_MAIN
#endif

#ifdef APPROVALS_CATCH
#define CATCH_CONFIG_MAIN

#include <Catch.hpp>

Expand Down
2 changes: 1 addition & 1 deletion build/relnotes_X.X.X.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
* **Breaking changes**
* None
* **New features**
* None
* Support for [Existing Catch Project - with your main()](/doc/UsingCatch.md#existing-project---with-your-main)
* **Bug fixes**
* None
* **Other changes**
Expand Down
5 changes: 5 additions & 0 deletions doc/Features.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ To change this file edit the source file and then execute ./run_markdown_templat
## Contents

* [vNext](#vnext)
* [Existing Catch Project - with your main()](#existing-catch-project---with-your-main)
* [v.5.1.0](#v510)
* [Continuous Integration Builds](#continuous-integration-builds)
* [Approving multiple files in one test](#approving-multiple-files-in-one-test)
Expand All @@ -36,6 +37,10 @@ To change this file edit the source file and then execute ./run_markdown_templat

## vNext

### Existing Catch Project - with your main()

See [Existing Project - with your main()](/doc/UsingCatch.md#existing-project---with-your-main).

## v.5.1.0

### Continuous Integration Builds
Expand Down
22 changes: 19 additions & 3 deletions doc/UsingCatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,27 @@ If you have a Catch (1 or 2) project with your own `main.cpp` that contains the

If you have [supplied your own `main()` for Catch](https://github.com/catchorg/Catch2/blob/master/docs/own-main.md#top), you will need to teach it how to supply test names to Approval Tests.

There is not yet a streamlined way of doing this.
You should make the following additions to your own source file that contains `main()`.

For now, please see the code in [ApprovalTests/Catch2Approvals.h](/ApprovalTests/integrations/catch/Catch2Approvals.h) for the code you will need to add to your `main.cpp`.
<!-- snippet: catch_existing_main -->
<a id='snippet-catch_existing_main'/></a>
```cpp
// main.cpp:

// Add these two lines to your main:
#define APPROVALS_CATCH_EXISTING_MAIN
#include "ApprovalTests.hpp"

If it would be helpful for us to provide an easier way to do this, please let us know, via the contact details in [Contributing to ApprovalTests.cpp](/doc/Contributing.md#top).
int main( int argc, char* argv[] )
{
// your existing setup...
int result = Catch::Session().run( argc, argv );
// your existing clean-up...
return result;
}
```
<sup>[snippet source](/examples/catch2_existing_main/main.cpp#L1-L15) / [anchor](#snippet-catch_existing_main)</sup>
<!-- endsnippet -->
---
Expand Down
4 changes: 4 additions & 0 deletions doc/mdsource/Features.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ toc

## vNext

### Existing Catch Project - with your main()

See [Existing Project - with your main()](/doc/UsingCatch.md#existing-project---with-your-main).

## v.5.1.0

### Continuous Integration Builds
Expand Down
6 changes: 2 additions & 4 deletions doc/mdsource/UsingCatch.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ If you have a Catch (1 or 2) project with your own `main.cpp` that contains the
If you have [supplied your own `main()` for Catch](https://github.com/catchorg/Catch2/blob/master/docs/own-main.md#top), you will need to teach it how to supply test names to Approval Tests.
There is not yet a streamlined way of doing this.
You should make the following additions to your own source file that contains `main()`.
For now, please see the code in [ApprovalTests/Catch2Approvals.h](/ApprovalTests/integrations/catch/Catch2Approvals.h) for the code you will need to add to your `main.cpp`.
If it would be helpful for us to provide an easier way to do this, please let us know, via the contact details in [Contributing to ApprovalTests.cpp](/doc/Contributing.md#top).
snippet: catch_existing_main
---
Expand Down
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(catch2_existing_main)
add_subdirectory(googletest_existing_main)
11 changes: 11 additions & 0 deletions examples/catch2_existing_main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
project(catch2_existing_main)
set(CMAKE_CXX_STANDARD 11)
add_executable(${PROJECT_NAME}
main.cpp
Catch2ApprovalsTests.cpp
)
target_link_libraries(${PROJECT_NAME} ApprovalTests catch2)
target_include_directories(${PROJECT_NAME}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../tests/Catch2_Tests)

add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
9 changes: 9 additions & 0 deletions examples/catch2_existing_main/Catch2ApprovalsTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "Catch.hpp"
#include "ApprovalTests/Approvals.h"

using namespace ApprovalTests;

TEST_CASE("TestStreamableObject")
{
Approvals::verify(42);
}
15 changes: 15 additions & 0 deletions examples/catch2_existing_main/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// begin-snippet: catch_existing_main
// main.cpp:

// Add these two lines to your main:
#define APPROVALS_CATCH_EXISTING_MAIN
#include "ApprovalTests.hpp"

int main( int argc, char* argv[] )
{
// your existing setup...
int result = Catch::Session().run( argc, argv );
// your existing clean-up...
return result;
}
// end-snippet

0 comments on commit a48dde3

Please sign in to comment.