Skip to content

Commit

Permalink
Added base tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sl0mek committed Aug 22, 2023
1 parent a7673f8 commit 7ece468
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
26 changes: 24 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,39 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(Cars)

Include(FetchContent)

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

set(SRC_LIST
ElectricCar.cpp
ElectricEngine.cpp
HybridCar.cpp
main.cpp
PetrolCar.cpp
PetrolEngine.cpp
Car.cpp
InvalidGear.cpp
)

add_executable(${PROJECT_NAME} ${SRC_LIST})
add_library(${PROJECT_NAME}-lib STATIC ${SRC_LIST})

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}-lib )
target_compile_options(${PROJECT_NAME} PUBLIC -Wall -Wpedantic -Wextra)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR})

enable_testing()

add_executable(${PROJECT_NAME}-ut tests.cpp)
target_link_libraries(${PROJECT_NAME}-ut ${PROJECT_NAME}-lib GTest::gtest_main)

include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME}-ut)
add_test(NAME runTests COMMAND ${PROJECT_NAME}-ut)
11 changes: 9 additions & 2 deletions PetrolEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ void PetrolEngine::changeGear(int gear)
// -1 is for REAR
// 0 is for NEUTRAL

if (gear <= -1 || gear >= gears_) {
if (gear < -1 || gear >= gears_) {
std::cout << gear << std::endl;
throw InvalidGear("Incorrect gear change");
}
if(std::abs(gear - gears_) != 1){
if(std::abs(gear - currentGear_) != 1 && std::abs(abs(gear) - abs(currentGear_)) != 0){
std::cout << std::abs(gear - currentGear_) << std::endl;
throw InvalidGear("Incorrect gear change");
}

currentGear_ = gear;
std::cout << __FUNCTION__ << std::endl;
}

int PetrolEngine::getCurrentGear()
{
return currentGear_;
}
1 change: 1 addition & 0 deletions PetrolEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class PetrolEngine
public:
PetrolEngine(int power, float capacity, int gears);
void changeGear(int gear);
int getCurrentGear();

private:
int power_; // in HP
Expand Down
30 changes: 30 additions & 0 deletions tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <gtest/gtest.h>
#include <stdexcept>
#include "PetrolEngine.hpp"

TEST(PetrolEngineTest, ValidGearChange) {
PetrolEngine engine(500, 2, 7);

EXPECT_EQ(engine.getCurrentGear(), 0);
engine.changeGear(1);
EXPECT_EQ(engine.getCurrentGear(), 1);
engine.changeGear(2);
EXPECT_EQ(engine.getCurrentGear(), 2);
engine.changeGear(1);
EXPECT_EQ(engine.getCurrentGear(), 1);
engine.changeGear(-1);
EXPECT_EQ(engine.getCurrentGear(), -1);
}

TEST(PetrolEngineTest, InvalidGearChange) {
PetrolEngine engine(500, 2, 7);

EXPECT_THROW(engine.changeGear(-2), InvalidGear);
EXPECT_THROW(engine.changeGear(8), InvalidGear);
EXPECT_THROW(engine.changeGear(9), InvalidGear);
engine.changeGear(1);
engine.changeGear(2);
EXPECT_THROW(engine.changeGear(-1), InvalidGear);
EXPECT_THROW(engine.changeGear(4), InvalidGear);

}

0 comments on commit 7ece468

Please sign in to comment.