From 3ee479a731951ee832cbc611b25dd9acfba8922c Mon Sep 17 00:00:00 2001 From: emily Date: Fri, 5 Jun 2020 15:24:00 +0200 Subject: [PATCH] new cpp version with just catch2 and approvals. Builds faster than the version in the cpp folder --- cpp-catch2/.gitignore | 4 ++ cpp-catch2/CMakeLists.txt | 47 ++++++++++++++++ cpp-catch2/README.md | 20 +++++++ cpp-catch2/src/GildedRose.cc | 80 ++++++++++++++++++++++++++++ cpp-catch2/src/GildedRose.h | 24 +++++++++ cpp-catch2/test/ApprovalTests.hpp | 1 + cpp-catch2/test/gildedrose_catch.cpp | 14 +++++ cpp-catch2/test/main.cpp | 5 ++ 8 files changed, 195 insertions(+) create mode 100644 cpp-catch2/.gitignore create mode 100644 cpp-catch2/CMakeLists.txt create mode 100644 cpp-catch2/README.md create mode 100644 cpp-catch2/src/GildedRose.cc create mode 100644 cpp-catch2/src/GildedRose.h create mode 100644 cpp-catch2/test/ApprovalTests.hpp create mode 100644 cpp-catch2/test/gildedrose_catch.cpp create mode 100644 cpp-catch2/test/main.cpp diff --git a/cpp-catch2/.gitignore b/cpp-catch2/.gitignore new file mode 100644 index 00000000..401f2947 --- /dev/null +++ b/cpp-catch2/.gitignore @@ -0,0 +1,4 @@ +/build_meson/ +/subprojects/Catch2-*/ +/subprojects/packagecache/ +/cmake-build-*/ diff --git a/cpp-catch2/CMakeLists.txt b/cpp-catch2/CMakeLists.txt new file mode 100644 index 00000000..4d205699 --- /dev/null +++ b/cpp-catch2/CMakeLists.txt @@ -0,0 +1,47 @@ +cmake_minimum_required(VERSION 3.14..3.16) +set(CMAKE_VERBOSE_MAKEFILE ON) +project(GildedRoseKata VERSION 1.0 + DESCRIPTION "The GildedRose Refactoring kata for an approval testing approach" + LANGUAGES CXX) +include(FetchContent) + +FetchContent_Declare( + catch2 + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG v2.12.2 +) +FetchContent_MakeAvailable(catch2) +LIST(APPEND CMAKE_MODULE_PATH + ${catch2_SOURCE_DIR}/contrib + ) + +FetchContent_Declare( + approvaltests_ho + URL https://github.com/approvals/ApprovalTests.cpp/releases/download/v.10.0.0/ApprovalTests.v.10.0.0.hpp + DOWNLOAD_NO_EXTRACT TRUE + DOWNLOAD_DIR ${CMAKE_CURRENT_BINARY_DIR}/approvaltests + DOWNLOAD_NAME ApprovalTests.v.10.0.0.hpp +) +FetchContent_GetProperties(approvaltests_ho) +if (NOT approvaltests_ho_POPULATED) + FetchContent_Populate(approvaltests_ho) +endif () +add_library(approvaltests INTERFACE) +target_include_directories(approvaltests + INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/approvaltests + INTERFACE ${catch2_SOURCE_DIR}/single_include/catch2 + ) + +add_executable(gildedrose_catch2 + src/GildedRose.h + src/GildedRose.cc + test/gildedrose_catch.cpp + test/main.cpp) +set_target_properties(gildedrose_catch2 PROPERTIES CXX_STANDARD 11) +target_include_directories(gildedrose_catch2 + PUBLIC src) +target_link_libraries(gildedrose_catch2 Catch2::Catch2 approvaltests) + +include(CTest) +include(ParseAndAddCatchTests) +ParseAndAddCatchTests(gildedrose_catch2) \ No newline at end of file diff --git a/cpp-catch2/README.md b/cpp-catch2/README.md new file mode 100644 index 00000000..66b941cb --- /dev/null +++ b/cpp-catch2/README.md @@ -0,0 +1,20 @@ +C++ version of Gilded Rose with Catch 2 and Approvals +====================================================== + +This is a C++ start of the ApprovalTest version of the Gilded Rose Refactoring Kata. See +the [top level readme](https://github.com/emilybache/GildedRose-Refactoring-Kata) + for a general description of the exercise. + + +CMake +----- + +CMake is included in CLion from JetBrains. Without CMake files +CLion has a hard time to handle c-projects. + +To install CMake (if you don't use CLion) on macOS using brew + + brew install cmake + +Tested on CMake 3.15.3 (included with CLion 2019.3) on macOS + diff --git a/cpp-catch2/src/GildedRose.cc b/cpp-catch2/src/GildedRose.cc new file mode 100644 index 00000000..8df23e47 --- /dev/null +++ b/cpp-catch2/src/GildedRose.cc @@ -0,0 +1,80 @@ +#include "GildedRose.h" + +GildedRose::GildedRose(vector & items) : items(items) +{} + +void GildedRose::updateQuality() +{ + for (int i = 0; i < items.size(); i++) + { + if (items[i].name != "Aged Brie" && items[i].name != "Backstage passes to a TAFKAL80ETC concert") + { + if (items[i].quality > 0) + { + if (items[i].name != "Sulfuras, Hand of Ragnaros") + { + items[i].quality = items[i].quality - 1; + } + } + } + else + { + if (items[i].quality < 50) + { + items[i].quality = items[i].quality + 1; + + if (items[i].name == "Backstage passes to a TAFKAL80ETC concert") + { + if (items[i].sellIn < 11) + { + if (items[i].quality < 50) + { + items[i].quality = items[i].quality + 1; + } + } + + if (items[i].sellIn < 6) + { + if (items[i].quality < 50) + { + items[i].quality = items[i].quality + 1; + } + } + } + } + } + + if (items[i].name != "Sulfuras, Hand of Ragnaros") + { + items[i].sellIn = items[i].sellIn - 1; + } + + if (items[i].sellIn < 0) + { + if (items[i].name != "Aged Brie") + { + if (items[i].name != "Backstage passes to a TAFKAL80ETC concert") + { + if (items[i].quality > 0) + { + if (items[i].name != "Sulfuras, Hand of Ragnaros") + { + items[i].quality = items[i].quality - 1; + } + } + } + else + { + items[i].quality = items[i].quality - items[i].quality; + } + } + else + { + if (items[i].quality < 50) + { + items[i].quality = items[i].quality + 1; + } + } + } + } +} diff --git a/cpp-catch2/src/GildedRose.h b/cpp-catch2/src/GildedRose.h new file mode 100644 index 00000000..8464f87b --- /dev/null +++ b/cpp-catch2/src/GildedRose.h @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; + +class Item +{ +public: + string name; + int sellIn; + int quality; + Item(string name, int sellIn, int quality) : name(name), sellIn(sellIn), quality(quality) + {} +}; + +class GildedRose +{ +public: + vector & items; + GildedRose(vector & items); + + void updateQuality(); +}; + diff --git a/cpp-catch2/test/ApprovalTests.hpp b/cpp-catch2/test/ApprovalTests.hpp new file mode 100644 index 00000000..6da10acd --- /dev/null +++ b/cpp-catch2/test/ApprovalTests.hpp @@ -0,0 +1 @@ +#include "ApprovalTests.v.10.0.0.hpp" \ No newline at end of file diff --git a/cpp-catch2/test/gildedrose_catch.cpp b/cpp-catch2/test/gildedrose_catch.cpp new file mode 100644 index 00000000..5fe4c870 --- /dev/null +++ b/cpp-catch2/test/gildedrose_catch.cpp @@ -0,0 +1,14 @@ +#include "catch2/catch.hpp" +#include "ApprovalTests.hpp" + +#include "GildedRose.h" + +TEST_CASE("Foo") { + + vector items; + items.push_back(Item("foo", 0, 0)); + GildedRose app(items); + app.updateQuality(); + REQUIRE("fixme" == app.items[0].name); + +} diff --git a/cpp-catch2/test/main.cpp b/cpp-catch2/test/main.cpp new file mode 100644 index 00000000..94bae37b --- /dev/null +++ b/cpp-catch2/test/main.cpp @@ -0,0 +1,5 @@ +#define CATCH_CONFIG_MAIN +#define APPROVALS_CATCH + +#include "catch2/catch.hpp" +#include "ApprovalTests.hpp" \ No newline at end of file