Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@
*.exe
*.out
*.app
build
.DS_Store
*.pro.user
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tdd_intro/cleanroom/cleanroom.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TEMPLATE = subdirs

SUBDIRS += \
chatclient
#SUBDIRS += \
# chatclient
4 changes: 2 additions & 2 deletions tdd_intro/gmock.pri
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions tdd_intro/gtest.pri
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions tdd_intro/homework/04_weather_client/04_weather_client.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include(../../gmock.pri)
include(../../gtest.pri)

TEMPLATE = app
CONFIG += console c++11
Expand Down
2 changes: 2 additions & 0 deletions tdd_intro/homework/04_weather_client/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Each line means "<request>" : "<response>":
#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include <cmath>

struct Weather
{
short temperature = 0;
Expand Down
1 change: 1 addition & 0 deletions tdd_intro/homework/06_coffee/06_coffee.pro
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include(../../gtest.pri)
include(../../gmock.pri)

TEMPLATE = app
Expand Down
161 changes: 115 additions & 46 deletions tdd_intro/homework/06_coffee/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ class ISourceOfIngredients
virtual void AddCream(int gram) = 0;
};


enum Cup
{
Normal,
Big
};

enum Coffee
{
Americano
};

class MockSourceOfIngredients : public ISourceOfIngredients
{
public:
Expand All @@ -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);
}