We have a TradingStrategy, which main goal is to price assets based on an externally provided factor. Upon start, the strategy registers itself with FactorStrategy object, from which it will receive factor updates.
#include <cmath>
#include <gtest/gtest.h>
class FactorStrategy {
public:
class FactorListener {
public:
virtual ~FactorListener() = default;
virtual void onFactorUpdate(double newFactor) = 0;
};
void registerFactorListener(FactorListener* /*listener*/) {}
};
class TradingStrategy : public FactorStrategy::FactorListener {
public:
void start(FactorStrategy& factorStrategy) {
factorStrategy.registerFactorListener(this);
started = true;
}
long calculateStrategyPrice(long assetPrice) {
assert(started);
return static_cast<long>(assetPrice * factor);
}
// received through the FactorStrategy
void onFactorUpdate(double newFactor) override {
factor = newFactor;
}
bool started { false };
double factor { 1.0 };
};
TEST(TradingStrategyTest, calculatePrice_beforeFirstFactorUpdate) {
TradingStrategy strategy;
strategy.started = true;
EXPECT_EQ(1234, strategy.calculateStrategyPrice(1234));
}
#include <cmath>
#include <gtest/gtest.h>
class FactorStrategy {
public:
class FactorListener {
public:
virtual ~FactorListener() = default;
virtual void onFactorUpdate(double newFactor) = 0;
};
void registerFactorListener(FactorListener* /*listener*/) {}
};
class TradingStrategy : public FactorStrategy::FactorListener {
public:
void start(FactorStrategy& factorStrategy) {
factor = NAN;
factorStrategy.registerFactorListener(this);
started = true;
}
long calculateStrategyPrice(long assetPrice) {
assert(started);
return static_cast<long>(assetPrice * factor);
}
// received through the FactorStrategy
void onFactorUpdate(double newFactor) override {
factor = newFactor;
}
bool started { false };
double factor { 1.0 };
};
TEST(TradingStrategyTest, calculatePrice_beforeFirstFactorUpdate) {
TradingStrategy strategy;
strategy.started = true;
// FAIL! In reality, calculateStrategyPrice would return NAN in this scenario
EXPECT_EQ(1234, strategy.calculateStrategyPrice(1234));
}
#include <cmath>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class FactorStrategy {
public:
class FactorListener {
public:
virtual ~FactorListener() = default;
virtual void onFactorUpdate(double newFactor) = 0;
};
void registerFactorListener(FactorListener* /*listener*/) {}
};
class TradingStrategy : public FactorStrategy::FactorListener {
public:
void start(FactorStrategy& factorStrategy) {
factorStrategy.registerFactorListener(this);
started = true;
}
long calculateStrategyPrice(long assetPrice) {
assert(started);
return static_cast<long>(assetPrice * factor);
}
// received through the FactorStrategy
void onFactorUpdate(double newFactor) override {
factor = newFactor;
}
private:
bool started { false };
double factor { 1.0 };
};
class MockFactorStrategy : public FactorStrategy {
public:
MOCK_METHOD1(registerFactorListener, void(FactorListener*));
};
TEST(TradingStrategyTest, calculatePrice_beforeFirstFactorUpdate) {
testing::NiceMock<MockFactorStrategy> mockFactorStrategy;
TradingStrategy strategy;
strategy.start(mockFactorStrategy);
EXPECT_EQ(1234, strategy.calculateStrategyPrice(1234));
}