Test-Driven Development Exercise
Task 1 Solution
So how did we TDD task 1? Click on the tabs below to find out...
Here is the first in a series of videos showing how we applied TDD to the problem at hand. The video is about 14 minutes long.
#ifndef ACCOUNT_H_
#define ACCOUNT_H_class Account
{
private:
int numberOfLines_;
double basicRate_;
double extraLineRate_; double computeExtraLineCharges();public:
Account(int numberOfLines, double basicRate, double extraLineRate);
virtual ~Account(); double computeBill();
};#endif /* ACCOUNT_H_ */
#include "Account.h"Account::Account(int numberOfLines, double basicRate, double extraLineRate)
: numberOfLines_(numberOfLines),
basicRate_(basicRate),
extraLineRate_(extraLineRate)
{
}Account::~Account()
{
}double Account::computeExtraLineCharges()
{
return (numberOfLines_-1)*extraLineRate_;
}double Account::computeBill()
{
return basicRate_ + computeExtraLineCharges();
}
#include <gtest/gtest.h>
#include "Account.h"
using std::string;class AccountTest: public testing::Test
{
public:
static const double GOLD_BASIC_RATE;
static const double GOLD_EXTRA_LINE_RATE; static const double SILVER_BASIC_RATE;
static const double SILVER_EXTRA_LINE_RATE; AccountTest()
{
}
virtual ~AccountTest()
{
} Account makeGoldAccountWith(int numberOfLines)
{
return Account(numberOfLines, GOLD_BASIC_RATE, GOLD_EXTRA_LINE_RATE);
} Account makeSilverAccountWith(int numberOfLines)
{
return Account(numberOfLines, SILVER_BASIC_RATE, SILVER_EXTRA_LINE_RATE);
}
};const double AccountTest::GOLD_BASIC_RATE = 49.95;
const double AccountTest::GOLD_EXTRA_LINE_RATE = 14.50;const double AccountTest::SILVER_BASIC_RATE = 29.95;
const double AccountTest::SILVER_EXTRA_LINE_RATE = 21.50;TEST_F(AccountTest, testGoldAccount)
{
ASSERT_EQ(GOLD_BASIC_RATE, makeGoldAccountWith(1).computeBill());
ASSERT_EQ(GOLD_BASIC_RATE+GOLD_EXTRA_LINE_RATE, makeGoldAccountWith(2).computeBill());
}TEST_F(AccountTest, testSilverAccount)
{
ASSERT_EQ(SILVER_BASIC_RATE, makeSilverAccountWith(1).computeBill());
ASSERT_EQ(SILVER_BASIC_RATE+(2*SILVER_EXTRA_LINE_RATE), makeSilverAccountWith(3).computeBill());
}
Click on the link below to download the source code for this task.
(Reading...)
Formatting Hints:
To format... | use this markup: |
---|---|
source code | {source:lang=java|lineNumbers=false}...{source} (Supported languages: java, cpp, csharp, c, python.) |
bold text | __some text__ |
italicized text | ~~some text~~ |
underlined text | @@some text@@ |
a method or class name | ``myMethod(...)`` |