Skip to content

Commit

Permalink
Implemented Dependency Injection and GMock.
Browse files Browse the repository at this point in the history
  • Loading branch information
Venkata Suresh Kumar Vukkem committed Mar 18, 2011
1 parent 1952971 commit 212f26d
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 30 deletions.
11 changes: 10 additions & 1 deletion include/call_parser/FileBasedCallDataParser.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@
#include <string>
using namespace std;

#include "util/FileReader.hh"

#include "CallDataParser.hh"

namespace CallParser {

class FileBasedCallDataParser : public CallDataParser {

public:
/*
<<<<<<< Updated upstream
FileBasedCallDataParser(string fileName);
=======
*/
FileBasedCallDataParser(util::FileReader fileReader);
FileBasedCallDataParser(const FileBasedCallDataParser& fileBasedCallDataParser);
//>>>>>>> Stashed changes
~FileBasedCallDataParser();
virtual list<CallRecord*>* parseRecords();
private:
CallRecord* toCallRecord(string line);

string _fileName;
util::FileReader fileReaderM;
};
}

Expand Down
9 changes: 5 additions & 4 deletions include/util/FileReader.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ namespace util
{
public:
FileReader(const std::string fileName);
~FileReader();
bool open();
std::string getLine();
void close();
FileReader(const FileReader& fileReader);
virtual ~FileReader();
virtual bool open();
virtual std::string getLine();
virtual void close();

private:
std::string fileNameM;
Expand Down
76 changes: 60 additions & 16 deletions src/call_parser/FileBasedCallDataParser.cc
Original file line number Diff line number Diff line change
@@ -1,52 +1,44 @@
#include "call_parser/FileBasedCallDataParser.hh"

//#include <fstream>
#include <iostream>
#include <iomanip>
#include <boost/xpressive/xpressive.hpp>
#include <boost/lexical_cast.hpp>

#include "util/FileReader.hh"

using namespace util;
using namespace std;
using namespace boost::xpressive;

namespace CallParser {

/*
<<<<<<< Updated upstream
FileBasedCallDataParser::FileBasedCallDataParser(string fileName) :
_fileName(fileName) {
}
FileBasedCallDataParser::~FileBasedCallDataParser() {
}
list<CallRecord*>* FileBasedCallDataParser::parseRecords() {
list<CallRecord*>* callRecordList = new list<CallRecord*> ;
//ifstream recFile(_fileName.c_str());
util::FileReader fileReader(_fileName);
/*
if (!recFile) {
cerr << "Unable to open file: " << _fileName << endl;
return callRecordList;
}
*/
if (!fileReader.open()) {
//util::FileReader fileReader(_fileName);
if (!fileReaderM.open()) {
return callRecordList;
}
string line;
// while (std::getline(recFile, line)) {
while ((line = fileReader.getLine()) != "") {
while ((line = fileReaderM.getLine()) != "") {
CallRecord* callRec = toCallRecord(line);
if(callRec != NULL){
callRecordList->push_back(callRec);
}
}
//recFile.close();
fileReader.close();
fileReaderM.close();
return callRecordList;
}
Expand All @@ -69,4 +61,56 @@ namespace CallParser {
}
return NULL;
}
*/
FileBasedCallDataParser::FileBasedCallDataParser(FileReader fileReader) :
fileReaderM(fileReader) {
}

FileBasedCallDataParser::FileBasedCallDataParser(const FileBasedCallDataParser& fileBasedCallDataParser)
: fileReaderM(fileBasedCallDataParser.fileReaderM)
{

}

FileBasedCallDataParser::~FileBasedCallDataParser() {
}

list<CallRecord*>* FileBasedCallDataParser::parseRecords() {
list<CallRecord*>* callRecordList = new list<CallRecord*> ;

if (!fileReaderM.open()) {
return callRecordList;
}

string line;
while ((line = fileReaderM.getLine()) != "") {
CallRecord* callRec = toCallRecord(line);
if(callRec != NULL){
callRecordList->push_back(callRec);
}
}

fileReaderM.close();

return callRecordList;
}

CallRecord* FileBasedCallDataParser::toCallRecord(string line) {
sregex rex =
sregex::compile("([^ ]+) ([^ ]+) ([^ ]+) ([^ ]+) ([^ ]+)");
smatch what;

CallRecord* callRec=NULL;

if (regex_match(line, what, rex)) {
try{
callRec=new CallRecord(what[1],CallType::toCallType(what[2]),
boost::lexical_cast<int>(what[3]),what[4],what[5]);
return callRec;
}catch(...){
cerr<<"Invalid Record Found: "<<line<<endl;
}
//>>>>>>> Stashed changes
}
}
}
6 changes: 6 additions & 0 deletions src/util/FileReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ namespace util

}

FileReader::FileReader(const FileReader& fileReader)
: fileNameM(fileReader.fileNameM)
{

}

FileReader::~FileReader()
{

Expand Down
5 changes: 4 additions & 1 deletion test/call_cost/SimpleCallCostCalculatorTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ using namespace std;
#include "call_parser/CallDataParser.hh"
#include "call_parser/FileBasedCallDataParser.hh"
#include "call_data/CallCostRecord.hh"
#include "util/FileReader.hh"

using CallCost::CallCostCalculator;
using CallCost::SimpleCallCostCalculator;
using CallParser::CallDataParser;
using CallParser::FileBasedCallDataParser;
using TestCommon::release;
using namespace util;

#define TEST_CASE SimpleCallCostCalculatorTest

Expand All @@ -39,7 +41,8 @@ GTEST(shouldGenerateCallRecordForValidInputFile)
const char *if_cur_dir_is_root = "test/call_parser/call_data_small.txt";
const char *if_cur_dir_is_test = "call_parser/call_data_small.txt";
const char *data_file_name = if_cur_dir_is_root;
CallDataParser *dataParser = new FileBasedCallDataParser(data_file_name);
FileReader fileReader(data_file_name);
CallDataParser *dataParser = new FileBasedCallDataParser(fileReader);
list<CallRecord*>* dataRecords = dataParser->parseRecords();

EXPECT_TRUE(dataRecords != NULL);
Expand Down
113 changes: 107 additions & 6 deletions test/call_parser/FileBasedCallDataParserTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <gmock/gmock.h>
#include <vector>
#include <fstream>
#include <string>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/range/iterator_range.hpp>
using namespace std;
Expand All @@ -15,15 +16,113 @@ using namespace CallParser;
#include "../TestCommon.hh"
using namespace TestCommon;

#include "util/FileReader.hh"

using ::testing::Return;

class MockFileReader : public ::util::FileReader {

public:
MockFileReader(const string &filename) : FileReader(filename) {}
public:
MOCK_METHOD0(open, bool());
MOCK_METHOD0(getLine, string());
MOCK_METHOD0(close, void());
};


#define TEST_CASE FileBasedCallDataParserTest

//
//
//GTEST( shouldGenerateCallRecordForValidInputFile)
//{
// const char *if_cur_dir_is_root = "test/call_parser/call_data_small.txt";
// const char *if_cur_dir_is_test = "call_parser/call_data_small.txt";
// const char *data_file_name = if_cur_dir_is_root;
// CallDataParser *dataParser = new FileBasedCallDataParser(data_file_name);
//=======
GTEST(countOftheCallRecordsMustBeEqualToNumberOfValidEntries)
{
const string data_file_name("dummyfile");
MockFileReader fileReader(data_file_name);
EXPECT_CALL(fileReader, open()).Times(1);
EXPECT_CALL(fileReader, getLine())
.Times(7)
.WillOnce(Return("21/02/2011 LOC 5 Nick 974503244"))
.WillOnce(Return("04/01/2011 LOC MNO InvalidRecord 164503234"))
.WillOnce(Return("23/02/2011 STD 1 Dale 864503214"))
.WillOnce(Return("04/01/2011 XXX 30 InvalidRecord 164503234"))
.WillOnce(Return("04/01/2011 ISD 30 Nick 164503234"))
.WillOnce(Return("04/01/2011ISD 30 InvalidRecord 164503234"))
.WillOnce(Return(""));
EXPECT_CALL(fileReader, close()).Times(1);
CallDataParser *dataParser = new FileBasedCallDataParser(fileReader);
list<CallRecord*>* dataRecords = dataParser->parseRecords();

EXPECT_TRUE(dataRecords != NULL);
EXPECT_EQ(3,dataRecords->size());

release(dataRecords);
}

GTEST(shouldGenerateOneCallRecord)
{
const string data_file_name("dummyfile");
MockFileReader fileReader(data_file_name);
EXPECT_CALL(fileReader, open()).Times(1);
EXPECT_CALL(fileReader, getLine())
.Times(2)
.WillOnce(Return("21/02/2011 LOC 5 Nick 974503244"))
.WillOnce(Return(""));
EXPECT_CALL(fileReader, close()).Times(1);
CallDataParser *dataParser = new FileBasedCallDataParser(fileReader);
list<CallRecord*>* dataRecords = dataParser->parseRecords();

EXPECT_TRUE(dataRecords != NULL);
EXPECT_EQ(1,dataRecords->size());

BOOST_FOREACH(CallRecord* rec,*dataRecords) {
EXPECT_EQ("21/02/2011 LOC 5 Nick 974503244",rec->operator string());
}

release(dataRecords);
}

GTEST(shouldNotGenerateCallRecordForEntryWithInvalidCallDuration)
{
const string data_file_name("dummyfile");
MockFileReader fileReader(data_file_name);
EXPECT_CALL(fileReader, open()).Times(1);
EXPECT_CALL(fileReader, getLine())
.Times(2)
.WillOnce(Return("04/01/2011 LOC MNO InvalidRecord 164503234"))
.WillOnce(Return(""));
EXPECT_CALL(fileReader, close()).Times(1);
CallDataParser *dataParser = new FileBasedCallDataParser(fileReader);
list<CallRecord*>* dataRecords = dataParser->parseRecords();

EXPECT_TRUE(dataRecords == NULL);
}


GTEST( shouldGenerateCallRecordForValidInputFile)
/*
GTEST(shouldGenerateCallRecordForValidInputFile)
{
const char *if_cur_dir_is_root = "test/call_parser/call_data_small.txt";
const char *if_cur_dir_is_test = "call_parser/call_data_small.txt";
const char *data_file_name = if_cur_dir_is_root;
CallDataParser *dataParser = new FileBasedCallDataParser(data_file_name);
const char *if_cur_dir_is_root = "test/call_parser/call_data_small.txt";
const char *if_cur_dir_is_test = "call_parser/call_data_small.txt";
const char *data_file_name = if_cur_dir_is_root;
MockFileReader fileReader(data_file_name);
EXPECT_CALL(fileReader, getLine())
.Times(7)
.WillOnce(Return("21/02/2011 LOC 5 Nick 974503244"))
.WillOnce(Return("04/01/2011 LOC MNO InvalidRecord 164503234"))
.WillOnce(Return("23/02/2011 STD 1 Dale 864503214"))
.WillOnce(Return("04/01/2011 XXX 30 InvalidRecord 164503234"))
.WillOnce(Return("04/01/2011 ISD 30 Nick 164503234"))
.WillOnce(Return("04/01/2011ISD 30 InvalidRecord 164503234"));
CallDataParser *dataParser = new FileBasedCallDataParser(fileReader);
>>>>>>> Stashed changes
list<CallRecord*>* dataRecords = dataParser->parseRecords();
EXPECT_TRUE(dataRecords != NULL);
Expand All @@ -47,10 +146,12 @@ GTEST( shouldGenerateCallRecordForValidInputFile)
release(dataRecords);
}
*/

GTEST(shouldNotGenerateRecordForInvalidFile)
{
CallDataParser *dataParser = new FileBasedCallDataParser("xx");
util::FileReader fileReader("xx");
CallDataParser *dataParser = new FileBasedCallDataParser(fileReader);
list<CallRecord*>* dataRecords = dataParser->parseRecords();

EXPECT_TRUE(dataRecords != NULL);
Expand Down
2 changes: 0 additions & 2 deletions test/call_parser/call_data_small_v4.txt

This file was deleted.

0 comments on commit 212f26d

Please sign in to comment.