diff --git a/.gitignore b/.gitignore index 259148fa..32673502 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ *.exe *.out *.app +build +.DS_Store +*.pro.user diff --git a/tdd_intro/3rd_party/gmock/include/gmock/internal/gmock-internal-utils.h b/tdd_intro/3rd_party/gmock/include/gmock/internal/gmock-internal-utils.h index e12b7d7d..5507611a 100644 --- a/tdd_intro/3rd_party/gmock/include/gmock/internal/gmock-internal-utils.h +++ b/tdd_intro/3rd_party/gmock/include/gmock/internal/gmock-internal-utils.h @@ -46,6 +46,13 @@ #include "gmock/internal/gmock-port.h" #include "gtest/gtest.h" +namespace { + +const testing::internal::RelationToSourceCopy kCopy; +const testing::internal::RelationToSourceReference kReference; + +} + namespace testing { namespace internal { diff --git a/tdd_intro/3rd_party/gtest/googletest/include/gtest/internal/gtest-internal.h b/tdd_intro/3rd_party/gtest/googletest/include/gtest/internal/gtest-internal.h index 2a6e4dad..ba899d0a 100644 --- a/tdd_intro/3rd_party/gtest/googletest/include/gtest/internal/gtest-internal.h +++ b/tdd_intro/3rd_party/gtest/googletest/include/gtest/internal/gtest-internal.h @@ -1045,8 +1045,12 @@ void CopyArray(const T* from, size_t size, U* to) { // native array it represents. // We use 2 different structs to allow non-copyable types to be used, as long // as RelationToSourceReference() is passed. -struct RelationToSourceReference {}; -struct RelationToSourceCopy {}; +struct RelationToSourceReference { + RelationToSourceReference() {} +}; +struct RelationToSourceCopy { + RelationToSourceCopy() {} +}; // Adapts a native array to a read-only STL-style container. Instead // of the complete STL container concept, this adaptor only implements diff --git a/tdd_intro/cleanroom/cleanroom.pro b/tdd_intro/cleanroom/cleanroom.pro index b92a4278..31ff6a0b 100644 --- a/tdd_intro/cleanroom/cleanroom.pro +++ b/tdd_intro/cleanroom/cleanroom.pro @@ -1,4 +1,4 @@ TEMPLATE = subdirs -SUBDIRS += \ - chatclient +#SUBDIRS += \ +# chatclient diff --git a/tdd_intro/gmock.pri b/tdd_intro/gmock.pri index 73de1d85..0e854fac 100644 --- a/tdd_intro/gmock.pri +++ b/tdd_intro/gmock.pri @@ -1,6 +1,6 @@ win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../3rd_party/gmock/release/ -lgmock else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../3rd_party/gmock/debug/ -lgmock -else:unix:!macx: LIBS += -L$$OUT_PWD/../../3rd_party/gmock/ -lgmock +else:unix: LIBS += -L$$OUT_PWD/../../3rd_party/gmock/ -lgmock INCLUDEPATH += $$PWD/3rd_party/gmock/include INCLUDEPATH += $$PWD/3rd_party/gtest/googletest/include @@ -9,4 +9,4 @@ win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_ else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gmock/debug/libgmock.a else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gmock/release/gmock.lib else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gmock/debug/gmock.lib -else:unix:!macx: PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gmock/libgmock.a +else:unix: PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gmock/libgmock.a diff --git a/tdd_intro/gtest.pri b/tdd_intro/gtest.pri index 586007bb..bc93ca62 100644 --- a/tdd_intro/gtest.pri +++ b/tdd_intro/gtest.pri @@ -1,6 +1,6 @@ win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../3rd_party/gtest/release/ -lgtest else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../3rd_party/gtest/debug/ -lgtest -else:unix:!macx: LIBS += -L$$OUT_PWD/../../3rd_party/gtest/ -lgtest +else:unix: LIBS += -L$$OUT_PWD/../../3rd_party/gtest/ -lgtest INCLUDEPATH += $$PWD/3rd_party/gtest/googletest/include DEPENDPATH += $$PWD/3rd_party/gtest/googletest/include @@ -10,4 +10,4 @@ win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_ else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/debug/libgtest.a else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/release/gtest.lib else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/debug/gtest.lib -else:unix:!macx: PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/libgtest.a +else:unix: PRE_TARGETDEPS += $$OUT_PWD/../../3rd_party/gtest/libgtest.a diff --git a/tdd_intro/homework/04_weather_client/04_weather_client.pro b/tdd_intro/homework/04_weather_client/04_weather_client.pro index dec9b6a8..e8f86261 100644 --- a/tdd_intro/homework/04_weather_client/04_weather_client.pro +++ b/tdd_intro/homework/04_weather_client/04_weather_client.pro @@ -1,4 +1,5 @@ include(../../gmock.pri) +include(../../gtest.pri) TEMPLATE = app CONFIG += console c++11 diff --git a/tdd_intro/homework/04_weather_client/test.cpp b/tdd_intro/homework/04_weather_client/test.cpp index 346ea809..476405e2 100644 --- a/tdd_intro/homework/04_weather_client/test.cpp +++ b/tdd_intro/homework/04_weather_client/test.cpp @@ -47,6 +47,8 @@ Each line means "" : "": #include #include +#include + struct Weather { short temperature = 0; diff --git a/tdd_intro/homework/06_coffee/06_coffee.pro b/tdd_intro/homework/06_coffee/06_coffee.pro index dec9b6a8..05f06c68 100644 --- a/tdd_intro/homework/06_coffee/06_coffee.pro +++ b/tdd_intro/homework/06_coffee/06_coffee.pro @@ -1,3 +1,4 @@ +include(../../gtest.pri) include(../../gmock.pri) TEMPLATE = app diff --git a/tdd_intro/homework/06_coffee/test.cpp b/tdd_intro/homework/06_coffee/test.cpp index 33b9093e..f4cdaa13 100644 --- a/tdd_intro/homework/06_coffee/test.cpp +++ b/tdd_intro/homework/06_coffee/test.cpp @@ -31,18 +31,6 @@ class ISourceOfIngredients virtual void AddCream(int gram) = 0; }; - -enum Cup -{ - Normal, - Big -}; - -enum Coffee -{ - Americano -}; - class MockSourceOfIngredients : public ISourceOfIngredients { public: @@ -54,66 +42,147 @@ class MockSourceOfIngredients : public ISourceOfIngredients MOCK_METHOD1(AddMilkFoam, void(int)); MOCK_METHOD1(AddChocolate, void(int)); MOCK_METHOD1(AddCream, void(int)); + + void SetupCappuchinoExpectations(int grams) + { + const int cappuccinoTemperature = 80; + + EXPECT_CALL(*this, AddWater(grams, cappuccinoTemperature)).Times(1); + EXPECT_CALL(*this, AddMilk(grams / 3)).Times(1); + EXPECT_CALL(*this, AddCoffee(grams / 3)).Times(1); + EXPECT_CALL(*this, AddMilkFoam(grams / 3)).Times(1); + } + + void SetupLatteExpectations(int grams) + { + const int temperature = 90; + + EXPECT_CALL(*this, AddWater(grams, temperature)).Times(1); + EXPECT_CALL(*this, AddMilk(grams / 4)).Times(1); + EXPECT_CALL(*this, AddCoffee(grams / 2)).Times(1); + EXPECT_CALL(*this, AddMilkFoam(grams / 4)).Times(1); + } + + void SetupMarochinoExpectations(int grams, const int temperature) + { + EXPECT_CALL(*this, AddWater(grams, temperature)).Times(1); + EXPECT_CALL(*this, AddChocolate(grams / 4)).Times(1); + EXPECT_CALL(*this, AddCoffee(grams / 4)).Times(1); + EXPECT_CALL(*this, AddMilkFoam(grams / 4)).Times(1); + } +}; + +enum CupSize +{ + Little, + Big }; class CoffeeMachine { public: - CoffeeMachine(ISourceOfIngredients& source) : m_source(source) + CoffeeMachine(ISourceOfIngredients& source) : m_ingridientsSource(source) + {} + + void MakeCappuccino(const CupSize cupSize) + { + const int grams = GramsFromCupSize(cupSize); + const int temperature = 80; + + m_ingridientsSource.AddWater(grams, temperature); + m_ingridientsSource.AddMilk(grams / 3); + m_ingridientsSource.AddCoffee(grams / 3); + m_ingridientsSource.AddMilkFoam(grams / 3); + } + + void MakeLatte(const CupSize cupSize) { + const int grams = GramsFromCupSize(cupSize); + const int temperature = 90; + m_ingridientsSource.AddWater(grams, temperature); + m_ingridientsSource.AddMilk(grams / 4); + m_ingridientsSource.AddCoffee(grams / 2); + m_ingridientsSource.AddMilkFoam(grams / 4); } - void CreateCoffee(const Cup cup, const Coffee coffee) + + void MakeMarochino(const CupSize cupSize, const int temperature) + { + const int grams = GramsFromCupSize(cupSize); + + m_ingridientsSource.AddWater(grams, temperature); + m_ingridientsSource.AddChocolate(grams / 4); + m_ingridientsSource.AddCoffee(grams / 4); + m_ingridientsSource.AddMilkFoam(grams / 4); + } + +private: + int GramsFromCupSize(const CupSize cupSize) { - m_source.AddCoffee(0); - m_source.SetCupSize(0); - m_source.AddWater(0, 0); + if (cupSize == CupSize::Little) + { + return 100; + } + + return 140; //for CupSize::Big } + private: - ISourceOfIngredients& m_source; + ISourceOfIngredients& m_ingridientsSource; }; +TEST(CoffeeMachine, MakeBigCappuccino) +{ + MockSourceOfIngredients ingridientsSource; -// Architecture -// Class CoffeMachine -// Class-Mock SourceOfIngredients + const int grams = 140; + ingridientsSource.SetupCappuchinoExpectations(grams); -// - americano: water & coffee 1:2 or 1:3. Water temp 60C + CoffeeMachine coffeeMachine(ingridientsSource); + coffeeMachine.MakeCappuccino(CupSize::Big); +} -// Tests list: -// 1. americano + 100 gram = 1 coffe -// 2. americano + 140 gram = 1 large coffee -// 3. AddCoffee, SetCupSize and AddWater calls once -// 4. Check parameters -// 5. Same for each recipe +TEST(CoffeeMachine, MakeLittleCappuccino) +{ + MockSourceOfIngredients ingridientsSource; -TEST(CoffeeMachine, CoffemachineIsHere) + const int grams = 100; + ingridientsSource.SetupCappuchinoExpectations(grams); + + CoffeeMachine coffeeMachine(ingridientsSource); + coffeeMachine.MakeCappuccino(CupSize::Little); +} + +TEST(CoffeeMachine, MakeBigLatte) { - MockSourceOfIngredients si; - CoffeeMachine cm(si); + MockSourceOfIngredients ingridientsSource; + + const int grams = 140; + ingridientsSource.SetupLatteExpectations(grams); + + CoffeeMachine coffeeMachine(ingridientsSource); + coffeeMachine.MakeLatte(CupSize::Big); } -TEST(CoffeeMachine, CallsImportantThings) +TEST(CoffeeMachine, MakeLittleLatte) { - MockSourceOfIngredients si; - CoffeeMachine cm(si); + MockSourceOfIngredients ingridientsSource; - EXPECT_CALL(si, AddCoffee(::testing::_)).Times(1); - EXPECT_CALL(si, SetCupSize(::testing::_)).Times(1); - EXPECT_CALL(si, AddWater(::testing::_, ::testing::_)).Times(1); + const int grams = 100; + ingridientsSource.SetupLatteExpectations(grams); - cm.CreateCoffee(Cup::Normal, Coffee::Americano); + CoffeeMachine coffeeMachine(ingridientsSource); + coffeeMachine.MakeLatte(CupSize::Little); } -//- americano: water & coffee 1:3 Water temp 60C -TEST(CoffeeMachine, Americano) +TEST(CoffeeMachine, MakeBigMarochino) { - MockSourceOfIngredients si; - CoffeeMachine cm(si); + MockSourceOfIngredients ingridientsSource; - EXPECT_CALL(si, AddCoffee(75)).Times(1); - EXPECT_CALL(si, SetCupSize(100)).Times(1); - EXPECT_CALL(si, AddWater(25, 60)).Times(1); + const int grams = 140; + const int temperature = 50; + ingridientsSource.SetupMarochinoExpectations(grams, temperature); - cm.CreateCoffee(Cup::Normal, Coffee::Americano); + CoffeeMachine coffeeMachine(ingridientsSource); + coffeeMachine.MakeMarochino(CupSize::Big, temperature); }