int main() { Toy toto; Toy ET(Toy::ALIEN, "green", "./alien.txt"); toto.setName("TOTO !"); Toy moi = ET; if (toto.getType() == Toy::BASIC_TOY) std::cout << "basic toy: " << toto.getName() << std::endl; std::cout << toto.getAscii() << std::endl; if (moi.getType() == Toy::ALIEN) std::cout << "this alien is: " << moi.getName() << std::endl << moi.getAscii() << std::endl; return 1337; }
bool ToyStory::tellMeAStory(std::string const & filename, Toy & character1, toy_speak speak1, Toy & character2, toy_speak speak2) { std::ifstream file(filename.data()); std::cout << character1.getAscii() << std::endl; std::cout << character2.getAscii() << std::endl; if (file.is_open()) { char buffer[1024]; std::string sentence; size_t index; int count = 0; while (!file.eof()) { memset(buffer, 0, 1024); file.getline(buffer, 1024); sentence = buffer; if ((index = sentence.find("picture:")) == 0) { std::string picture = sentence.substr(8); if (count % 2) { if (!character1.setAscii(picture)) { return false; } std::cout << character1.getAscii() << std::endl; } else { if (!character2.setAscii(picture)) { return false; } std::cout << character2.getAscii() << std::endl; } } else { if (count % 2) { if (!(character1.*speak1)(sentence)) { return false; } } else { if (!(character2.*speak2)(sentence)) { return false; } } } count++; } } return true; }
int main() { // PICTURE Tester *tester = new Tester("Picture"); Picture p; tester->test(p.data, std::string(""), "constructor without filename make an empty string"); p.getPictureFromFile(std::string("file.test")); tester->test(p.data, std::string("this is a test\ncool"), "when a correct filename is provided, it correctly reads the file"); p.getPictureFromFile(std::string("notExistingFile.test")); tester->test(p.data, std::string("ERROR"), "when there is an error, the string is set to ERROR"); delete tester; // PICTURE // TOY tester = new Tester("Toy"); Toy t; tester->test(t.getType(), Toy::BASIC_TOY, "default constructor set the type to BASIC_TOY"); tester->test(t.getName(), std::string("toy"), "default constructor set the name to toy"); tester->test(t.getAscii(), std::string(""), "default constructor set the ascii to ''"); Toy t2(Toy::ALIEN, std::string("E.T"), std::string("file.test")); tester->test(t2.getType(), Toy::ALIEN, "constructor set the type correctly"); tester->test(t2.getName(), std::string("E.T"), "constructor set the name correctly"); tester->test(t2.getAscii(), std::string("this is a test\ncool"), "constructor set the ascii correctly"); t = t2; tester->test(t.getType(), t2.getType() , "operator= set the type correctly"); tester->test(t.getName(), t2.getName(), "operator= set the name correctly"); tester->test(t.getAscii(), t2.getAscii(), "operator= set the ascii correctly"); delete tester; // TOY // BUZZ + WOODY tester = new Tester("Buzz and Woody"); Buzz buzz(std::string("foo")); Woody woody(std::string("bar"), std::string("file.test")); tester->test(buzz.getType(), Toy::BUZZ, "Buzz constructor (no file) set the type correctly"); tester->test(buzz.getName(), std::string("foo"), "Buzz constructor (no file) set the name correctly"); tester->test(buzz.getAscii(), std::string("Buzz !!!"), "Buzz constructor (no file) set the ascii correctly"); tester->test(woody.getType(), Toy::WOODY, "Woody constructor set the type correctly"); tester->test(woody.getName(), std::string("bar"), "Woody constructor set the name correctly"); tester->test(woody.getAscii(), std::string("this is a test\ncool"), "Woody constructor set the ascii correctly"); tester->warn("The following tests cannot be unit tested, please check by hand"); t.speak("I cannot unit test this... Does it work ? (toy)"); buzz.speak("I cannot unit test this... Does it work ? (buzz)"); woody.speak("I cannot unit test this... Does it work ? (woody)"); delete tester; // BUZZ + WOODY // OPERATOR<< tester = new Tester("operator<<"); t << "woohoo"; tester->test(t.getAscii(), std::string("woohoo"), "operator<< with TOY and STRING"); tester->warn("The following test cannot be unit tested, please check by hand"); std::cout << t; delete tester; // OPERATOR<< return 0; }
Toy::Toy(const Toy &ty) { _name = ty.getName(); _pic.data = ty.getAscii(); _type = ty.getType(); }