// Copyright 2019 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[cpp_unit_test_plugin]]
= {cpp} Unit Test

The {cpp} Unit Test Plugin provides the tasks, configurations and conventions for integrating with a {cpp} executable-based testing framework, such as https://github.com/google/googletest[Google Test].

[[sec:cpp_unit_test_usage]]
== Usage

.Applying the {cpp} Unit Test Plugin
====
include::sample[dir="snippets/cpp/cppUnitTest/groovy",files="build.gradle[tags=apply-plugin]"]
include::sample[dir="snippets/cpp/cppUnitTest/kotlin",files="build.gradle.kts[tags=apply-plugin]"]
====

[[sec:cpp_unit_test_build_varants]]
== Build variants

The {cpp} Unit Test Plugin understands the following dimensions.
Read the <<building_cpp_projects.adoc#sec:introducing_build_variants-cpp,introduction to build variants>> for more information.

Target machines - defaults to the tested component (if present) or build host (otherwise)::
The target machine expresses which machines the application expects to run.
A target machine is identified by its operating system and architecture.
Gradle uses the target machine to decide which tool chain to choose based on availability on the host machine.

The target machine can be configured as follows:

.Configure application target machines
====
include::sample[dir="snippets/cpp/cppUnitTest/groovy",files="build.gradle[tags=configure-target-machines]"]
include::sample[dir="snippets/cpp/cppUnitTest/kotlin",files="build.gradle.kts[tags=configure-target-machines]"]
====

[[sec:cpp_unit_test_tasks]]
== Tasks

The following diagram shows the relationships between tasks added by this plugin.

.{cpp} Unit Test Plugin default task graph
image::cpp-unit-test-task-graph.png[]

[[sec:cpp_unit_test_task_variants]]
=== Variant-dependent Tasks

The {cpp} Unit Test Plugin creates tasks based on the variants of the unit test component.
Read the <<building_cpp_projects.adoc#sec:introducing_build_variants-cpp,introduction to build variants>> for more information.
The following diagrams show the relationship between variant-dependent tasks.

.{cpp} Unit Test Plugin variant-dependent task graph
image::cpp-unit-test-variant-task-graph.png[]

`compileTest__Variant__Cpp` (e.g. `compileTestCpp`)  - link:{groovyDslPath}/org.gradle.language.cpp.tasks.CppCompile.html[CppCompile]::
Depends on: All tasks that contribute source files to the compilation
::
Compiles {cpp} source files using the selected compiler.

`linkTest__Variant__` (e.g. `linkTest`) - link:{groovyDslPath}/org.gradle.nativeplatform.tasks.LinkExecutable.html[LinkExecutable]::
Depends on: All tasks which contribute to the link executable, including `link__Variant__` and `create__Variant__` tasks from projects that are resolved via project dependencies and tested component
::
Links executable from compiled object files using the selected linker.

`installTest__Variant__` (e.g. `installTest`) - link:{groovyDslPath}/org.gradle.nativeplatform.tasks.InstallExecutable.html[InstallExecutable]::
Depends on: `linkTest__Variant__` and all tasks which contribute to the runtime of the executable, including `link__Variant__` tasks from projects that are resolved via project dependencies
::
Installs the executable and all of it's runtime dependencies for easy execution.

`runTest__Variant__` (e.g. `runTest`) - link:{groovyDslPath}/org.gradle.nativeplatform.test.tasks.RunTestExecutable.html[RunTestExecutable]::
Depends on: `installTest__Variant__`
::
Run the installed executable.

[[sec:cpp_unit_test_lifecycle_tasks]]
=== Lifecycle Tasks

The {cpp} Unit Test Plugin attaches some of its tasks to the standard lifecycle tasks documented in the <<base_plugin.adoc#,Base Plugin chapter>> — which the {cpp} Unit Test Plugin applies automatically:

`assemble` - Task (lifecycle)::
Aggregate task that assembles the debug variant of the tested component for the current host (if present) in the project.
For example, the <<cpp_application_plugin.adoc#,{cpp} Application Plugin>> and <<cpp_library_plugin.adoc#,{cpp} Library Plugin>> attach their link and create tasks to this lifecycle task.
This task is added by the <<base_plugin.adoc#,Base Plugin>>.

`test` - Task (lifecycle)::
Depends on: `runTest__Variant__` that most closely matches the build host
::
Aggregate task of the variant that most closely match the build host for testing the component.

`check` - Task (lifecycle)::
Depends on: `test`
::
Aggregate task that performs verification tasks, such as running the tests.
Some plugins add their own verification task to `check`.
This task is added by the <<base_plugin.adoc#,Base Plugin>>.

`build` - Task (lifecycle)::
Depends on: `check`, `assemble`
::
Aggregate tasks that perform a full build of the project.
This task is added by the <<base_plugin.adoc#,Base Plugin>>.

`clean` - Delete::
Deletes the build directory and everything in it, i.e. the path specified by the `Project.getBuildDir()` project property.
This task is added by the <<base_plugin.adoc#,Base Plugin>>.

[[sec:cpp_unit_test_dependency_management]]
== Dependency management

Just like the tasks created by the {cpp} Unit Test Plugin, the configurations are created based on the variant of the application component.
Read the <<building_cpp_projects.adoc#sec:introducing_build_variants-cpp,introduction to build variants>> for more information.
The following graph describes the configurations added by the {cpp} Unit Test Plugin:

.{cpp} Unit Test Plugin configurations
image::cpp-unit-test-configurations.png[]

* The configurations in white are the ones a user should use to declare dependencies
* The configurations in blue, also known as resolvable denoted by \(R), are internal to the component, for its own use

The following configurations are used to declare dependencies:

`testImplementation`::
Used for declaring implementation dependencies for all variants of the test component.
This is where you should declare dependencies of any variants.
Note this configuration inherit all dependencies declared on the tested component (library or application).

`test__Variant__ExecutableImplementation` (e.g. `testExecutableImplementation`) extends `testImplementation`::
Used for declaring implementation dependencies for a specific variant of the test component.
This is where you should declare dependencies of the specific variant.


There is no configurations that can be used by consumers for this plugin.

The following configurations are used by the unit test itself:

`cppCompileTest__Variant__` (e.g. `cppCompileTest`) extends `test__Variant__ExecutableImplementation`::
Used for compiling the unit test.
This configuration contains the compile include roots of the unit test and is therefore used when invoking the {cpp} compiler to compile it.

`nativeLinkTest__Variant__` (e.g. `nativeLinkTest`) extends `test__Variant__ExecutableImplementation`::
Used for linking the unit test.
This configuration contains the libraries of the unit test and is therefore used when invoking the {cpp} linker to link it.

`nativeRuntimeTest__Variant__` (e.g. `nativeRuntimeTest`) extends `test__Variant__ExecutableImplementation`::
Used for executing the unit test.
This configuration contains the runtime libraries of the unit test.

[[sec:cpp_unit_test_conventions]]
== Conventions

The {cpp} Unit Test Plugin adds conventions for sources and tasks, shown below.

[[sec:cpp_unit_test_layout]]
=== Project layout

The {cpp} Unit Test Plugin assumes the project layout shown below.
None of these directories needs to exist or have anything in them.
The {cpp} Unit Test Plugin will compile whatever it finds, and handles anything which is missing.

`src/test/cpp`::
{cpp} source with extension of `.cpp`, `.c++` or `.cc`

`src/test/headers`::
Headers - headers needed to compile the unit test

You <<building_cpp_projects.adoc#sec:custom_cpp_source_set_paths,configure the project layout>> by configuring the `source` and `privateHeaders` respectively on the `unitTest` script block.

[[sec:cpp_unit_test_compile_task]]
=== `compileTest__Variant__Cpp` Task

The {cpp} Unit Test Plugin adds a link:{groovyDslPath}/org.gradle.language.cpp.tasks.CppCompile.html[CppCompile] instance for each variant of the test component to build (e.g. `compileTestCpp`).
Read the <<building_cpp_projects.adoc#sec:introducing_build_variants-cpp,introduction to build variants>> for more information.
Some of the most common configuration options are shown below.

[horizontal]
link:{groovyDslPath}/org.gradle.language.cpp.tasks.CppCompile.html#org.gradle.language.cpp.tasks.CppCompile:compilerArgs[compilerArgs]:: []

link:{groovyDslPath}/org.gradle.language.cpp.tasks.CppCompile.html#org.gradle.language.cpp.tasks.CppCompile:debuggable[debuggable]:: `true`

link:{groovyDslPath}/org.gradle.language.cpp.tasks.CppCompile.html#org.gradle.language.cpp.tasks.CppCompile:includes[includes]:: `configurations.cppCompileTest__Variant__` + `unitTest.privateHeaders`

link:{groovyDslPath}/org.gradle.language.cpp.tasks.CppCompile.html#org.gradle.language.cpp.tasks.CppCompile:macros[macros]:: [:]

link:{groovyDslPath}/org.gradle.language.cpp.tasks.CppCompile.html#org.gradle.language.cpp.tasks.CppCompile:objectFileDir[objectFileDir]:: `$buildDir/obj/test/__variant__`

link:{groovyDslPath}/org.gradle.language.cpp.tasks.CppCompile.html#org.gradle.language.cpp.tasks.CppCompile:optimized[optimized]:: `false`

link:{groovyDslPath}/org.gradle.language.cpp.tasks.CppCompile.html#org.gradle.language.cpp.tasks.CppCompile:positionIndependentCode[positionIndependentCode]:: `false`

link:{groovyDslPath}/org.gradle.language.cpp.tasks.CppCompile.html#org.gradle.language.cpp.tasks.CppCompile:source[source]:: `unitTest.cppSource`

link:{groovyDslPath}/org.gradle.language.cpp.tasks.CppCompile.html#org.gradle.language.cpp.tasks.CppCompile:systemIncludes[systemIncludes]:: derived from the tool chain

link:{groovyDslPath}/org.gradle.language.cpp.tasks.CppCompile.html#org.gradle.language.cpp.tasks.CppCompile:targetPlatform[targetPlatform]:: derived from the `TargetMachine` of the binary

link:{groovyDslPath}/org.gradle.language.cpp.tasks.CppCompile.html#org.gradle.language.cpp.tasks.CppCompile:toolChain[toolChain]:: <<building_cpp_projects.adoc#sec:cpp_supported_tool_chain,automatically selected based on target machine>>

[[sec:cpp_unit_test_link_task]]
=== `linkTest__Variant__` Task

The {cpp} Unit Test Plugin adds a link:{groovyDslPath}/org.gradle.nativeplatform.tasks.LinkExecutable.html[LinkExecutable] instance for each variant of the test component - e.g. `linkTest`.
Read the <<building_cpp_projects.adoc#sec:introducing_build_variants-cpp,introduction to build variants>> for more information.
Some of the most common configuration options are shown below.

[horizontal]
link:{groovyDslPath}/org.gradle.nativeplatform.tasks.LinkExecutable.html#org.gradle.nativeplatform.tasks.LinkExecutable:debuggable[debuggable]:: `true`

link:{groovyDslPath}/org.gradle.nativeplatform.tasks.LinkExecutable.html#org.gradle.nativeplatform.tasks.LinkExecutable:libs[libs]:: `configurations.nativeLinkTest__Variant__`

link:{groovyDslPath}/org.gradle.nativeplatform.tasks.LinkExecutable.html#org.gradle.nativeplatform.tasks.LinkExecutable:linkedFile[linkedFile]:: `$buildDir/exe/test/__variant__/baseName` (*nix) or `$buildDir\exe\test{backslash}__variant__\baseName.exe` (Windows)

link:{groovyDslPath}/org.gradle.nativeplatform.tasks.LinkExecutable.html#org.gradle.nativeplatform.tasks.LinkExecutable:linkerArgs[linkerArgs]:: []

link:{groovyDslPath}/org.gradle.nativeplatform.tasks.LinkExecutable.html#org.gradle.nativeplatform.tasks.LinkExecutable:source[source]:: `compile__Variant__Cpp.objects` (if present) + `compileTest__Variant__Cpp.objects`

link:{groovyDslPath}/org.gradle.nativeplatform.tasks.LinkExecutable.html#org.gradle.nativeplatform.tasks.LinkExecutable:targetPlatform[targetPlatform]:: derived from the `TargetMachine` of the binary

link:{groovyDslPath}/org.gradle.nativeplatform.tasks.LinkExecutable.html#org.gradle.nativeplatform.tasks.LinkExecutable:toolChain[toolChain]:: <<building_cpp_projects.adoc#sec:cpp_supported_tool_chain,automatically selected based on target machine>>

[[sec:cpp_unit_test_install_task]]
=== `installTest__Variant__` Task

The {cpp} Unit Test Plugin adds a link:{groovyDslPath}/org.gradle.nativeplatform.tasks.InstallExecutable.html[InstallExecutable] instance for each variant of the test component - e.g. `installTest`.
Read the <<building_cpp_projects.adoc#sec:introducing_build_variants-cpp,introduction to build variants>> for more information.
There is no need to configure any properties on the task.

[[sec:cpp_unit_test_run_task]]
=== `runTest__Variant__` Task

The {cpp} Unit Test Plugin adds a link:{groovyDslPath}/org.gradle.nativeplatform.test.tasks.RunTestExecutable.html[RunTestExecutable] instance for each variant of the test component - e.g. `runTest`.
Read the <<building_cpp_projects.adoc#sec:introducing_build_variants-cpp,introduction to build variants>> for more information.
Some of the most common configuration options are shown below.

[horizontal]
link:{groovyDslPath}/org.gradle.nativeplatform.test.tasks.RunTestExecutable.html#org.gradle.nativeplatform.test.tasks.RunTestExecutable:args[args]:: `[]`

link:{groovyDslPath}/org.gradle.nativeplatform.test.tasks.RunTestExecutable.html#org.gradle.nativeplatform.test.tasks.RunTestExecutable:ignoreFailures[ignoreFailures]:: `false`
