Eelmises artiklis oli juttu, kuidas CLion töötab üsna hästi koostöös TDM GCC-ga. Nüüd siis lisame meie projektile google testi. See käib suhteliselt lihtsalt paari järgmise sammuga.

Laadime alla praegusel hetkel viimase stabiilse google test zip faili: https://github.com/google/googletest/archive/release-1.7.0.zip

Pakin selle lahti asukohas C:\Src\cpp\googletest-release-1.7.0

Muudan faili CMakeLists.txt.

Rea

option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)

asemel panen: 

if(WIN32)
	option(gtest_disable_pthreads "Disable uses of pthreads in gtest." ON)
else()
	option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)
endif()

Nüüd annan käsureal järgmised käsklused:

mkdir tdm-mingw-build
cd tdm-mingw-build
cmake .. -G "MinGW Makefiles"
mingw32-make

CLion projekti CMakeLists.txt failis teen muudatused.

Faili algus on mul nüüd selline:

set(PROJECT_NAME_STR myGreatProject)
project(${PROJECT_NAME_STR} C CXX)
cmake_minimum_required(VERSION 3.3)
aux_source_directory(. SRC_LIST)

#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ")

Faili keskele lisan read:

set(PROJECT_TEST_NAME ${PROJECT_NAME_STR}_test )
set(GTEST_ROOT "C:/Src/cpp/googletest-release-1.7.0")
set(GTEST_LIBRARY_DIRS "${GTEST_ROOT}/tdm-mingw-build")
set(GTEST_INCLUDE_DIRS "${GTEST_ROOT}/include")
include_directories(${GTEST_INCLUDE_DIRS})

add_executable(${PROJECT_TEST_NAME} modern.cpp test/test-modern.cpp modern.h)
target_link_libraries(${PROJECT_TEST_NAME} "${GTEST_LIBRARY_DIRS}/libgtest.a" "${GTEST_LIBRARY_DIRS}/libgtest_main.a")

..ja faili lõppu:

set_property(TARGET ${PROJECT_TEST_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET ${PROJECT_TEST_NAME} PROPERTY CXX_STANDARD 14)

set_property(TARGET ${PROJECT_NAME_STR} PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET ${PROJECT_NAME_STR} PROPERTY CXX_STANDARD 14)

test kaustas on mul prooviks fail test-modern.cpp:

#include "../../modern.h"
#include "gtest/gtest.h"

TEST(modernTest, SimpleModern)
{
int actual = modern(1, 2);
EXPECT_EQ(120, actual);
}

TEST(modernTest, NextModern)
{
    int actual = modern(2, 3);
    EXPECT_EQ(168, actual);
}

Funktsiooni modern kirjeldan kahe faili abil:

modern.cpp

#include "modern.h"

auto sq(int n);

auto sq(int n)
{
    return n * n;
}


int modern(int x, int y) {
    int aval = 7;
    int bval = 8;
    auto c = aval + bval;
    int arr[] = {6, 1, 2, 3, 4};
    for (auto i : arr)
    {
        c += sq(i + x);
    }
    return c + y;
}

modern.h

#ifndef MYGREATPROJECT_ADD_H_H
#define MYGREATPROJECT_ADD_H_H
int modern(int x, int y);
#endif //MYGREATPROJECT_ADD_H_H

CLion testi jooksutamise pilt:

 

Nüüd tahame olla kindlad, et asi töötab ka Linuxiga. Kõigepealt kompileerime gtest-i:

Nüüd muudan faili CMakeLists.txt:

set(PROJECT_TEST_NAME ${PROJECT_NAME_STR}_test )

add_executable(${PROJECT_TEST_NAME} modern.cpp test/test-modern.cpp modern.h)
if(WIN32)
    set(GTEST_ROOT "C:/Src/cpp/googletest-release-1.7.0")
    set(GTEST_LIBRARY_DIRS "${GTEST_ROOT}/tdm-mingw-build")
    set(GTEST_INCLUDE_DIRS "${GTEST_ROOT}/include")
else()
    set(GTEST_ROOT "/home/tiit/Src/cpp/googletest-release-1.7.0")
    set(GTEST_LIBRARY_DIRS "${GTEST_ROOT}/linux-build")
    set(GTEST_INCLUDE_DIRS "${GTEST_ROOT}/include")
endif()

target_link_libraries(${PROJECT_TEST_NAME} "${GTEST_LIBRARY_DIRS}/libgtest.a" "${GTEST_LIBRARY_DIRS}/libgtest_main.a")
include_directories(${GTEST_INCLUDE_DIRS})

Tulemus:

 

Linke:

https://github.com/google/googletest