Basic write tests for towns, dialogue, and outdoors sectors

Also:
- Additional read test for dialogue
- Don't save empty outdoor encounters
- Don't save town timers without an assigned node
This commit is contained in:
2015-09-02 15:58:07 -04:00
parent 9377e604d8
commit 8f4d5453cd
7 changed files with 200 additions and 8 deletions

View File

@@ -253,6 +253,9 @@
91CC173E1B421CA0003D9A69 /* scen_write.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91CC173B1B421CA0003D9A69 /* scen_write.cpp */; };
91CC17491B422D5C003D9A69 /* scen_read.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91CC173A1B421CA0003D9A69 /* scen_read.cpp */; };
91D634560F8FD77800674AB3 /* BoE.icns in Resources */ = {isa = PBXBuildFile; fileRef = 2B8F435C0C0973680012E4A8 /* BoE.icns */; };
91E381461B97673700F69B81 /* town_write.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91E381451B97671E00F69B81 /* town_write.cpp */; };
91E381481B97677900F69B81 /* talk_write.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91E381471B97675900F69B81 /* talk_write.cpp */; };
91E3814A1B97679800F69B81 /* out_write.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91E381491B97678D00F69B81 /* out_write.cpp */; };
91EF052C1904D099001BEF85 /* bold.ttf in Copy Fonts */ = {isa = PBXBuildFile; fileRef = 91EF05291904D082001BEF85 /* bold.ttf */; };
91EF052D1904D099001BEF85 /* plain.ttf in Copy Fonts */ = {isa = PBXBuildFile; fileRef = 91EF052A1904D082001BEF85 /* plain.ttf */; };
91EF27731B693D3900666469 /* ter_read.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91EF27721B693D3800666469 /* ter_read.cpp */; };
@@ -766,6 +769,9 @@
91E1862B1B2B2AC0006A99EA /* estreams.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = estreams.cpp; sourceTree = "<group>"; };
91E30F2A1A74819B0057C54A /* fileio_party.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fileio_party.cpp; sourceTree = "<group>"; };
91E30F2D1A7481C20057C54A /* fileio.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fileio.cpp; sourceTree = "<group>"; };
91E381451B97671E00F69B81 /* town_write.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = town_write.cpp; sourceTree = "<group>"; };
91E381471B97675900F69B81 /* talk_write.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = talk_write.cpp; sourceTree = "<group>"; };
91E381491B97678D00F69B81 /* out_write.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = out_write.cpp; sourceTree = "<group>"; };
91E5C5A10F9EACE200C21460 /* oldstructs.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = oldstructs.hpp; path = ../oldstructs.hpp; sourceTree = "<group>"; };
91E5C7970F9F60EC00C21460 /* town.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = town.hpp; sourceTree = "<group>"; };
91E5C7980F9F60EC00C21460 /* town.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = town.cpp; sourceTree = "<group>"; };
@@ -1336,8 +1342,11 @@
91EF277A1B693D6E00666469 /* monst_read.cpp */,
91EF277C1B693D7D00666469 /* monst_write.cpp */,
91C2A6EC1B8FA91400346948 /* town_read.cpp */,
91E381451B97671E00F69B81 /* town_write.cpp */,
91C2A6EE1B8FAA8E00346948 /* talk_read.cpp */,
91E381471B97675900F69B81 /* talk_write.cpp */,
91C2A6ED1B8FA9FB00346948 /* out_read.cpp */,
91E381491B97678D00F69B81 /* out_write.cpp */,
);
name = src;
sourceTree = "<group>";
@@ -1855,6 +1864,9 @@
911A14031B8FAFC600900FD9 /* town_read.cpp in Sources */,
911A14041B8FB00300900FD9 /* talk_read.cpp in Sources */,
911A14051B8FB00600900FD9 /* out_read.cpp in Sources */,
91E381461B97673700F69B81 /* town_write.cpp in Sources */,
91E381481B97677900F69B81 /* talk_write.cpp in Sources */,
91E3814A1B97679800F69B81 /* out_write.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@@ -46,6 +46,9 @@ void writeScenarioToXml(ticpp::Printer&& data, cScenario& scenario);
void writeTerrainToXml(ticpp::Printer&& data, cScenario& scenario);
void writeItemsToXml(ticpp::Printer&& data, cScenario& scenario);
void writeMonstersToXml(ticpp::Printer&& data, cScenario& scenario);
void writeOutdoorsToXml(ticpp::Printer&& data, cOutdoors& sector);
void writeTownToXml(ticpp::Printer&& data, cTown& town);
void writeDialogueToXml(ticpp::Printer&& data, cSpeech& talk, int town_num);
map_data buildOutMapData(location which, cScenario& scenario);
map_data buildTownMapData(size_t which, cScenario& scenario);
@@ -597,7 +600,7 @@ void writeMonstersToXml(ticpp::Printer&& data, cScenario& scenario) {
data.CloseElement("monsters");
}
static void writeOutdoorsToXml(ticpp::Printer&& data, cOutdoors& sector) {
void writeOutdoorsToXml(ticpp::Printer&& data, cOutdoors& sector) {
data.OpenElement("sector");
data.PushAttribute("boes", scenario.format_ed_version());
data.PushElement("name", sector.out_name);
@@ -609,10 +612,14 @@ static void writeOutdoorsToXml(ticpp::Printer&& data, cOutdoors& sector) {
case AMBIENT_DRIP: data.PushElement("sound", "drip"); break;
case AMBIENT_CUSTOM: data.PushElement("sound", sector.out_sound); break;
}
for(auto& enc : sector.special_enc)
data.PushElement("encounter", enc);
for(auto& enc : sector.wandering)
data.PushElement("wandering", enc);
for(auto& enc : sector.special_enc) {
if(!enc.isNull())
data.PushElement("encounter", enc);
}
for(auto& enc : sector.wandering) {
if(!enc.isNull())
data.PushElement("wandering", enc);
}
for(size_t i = 0; i < sector.sign_locs.size(); i++) {
if(sector.sign_locs[i].text.empty()) continue;
data.OpenElement("sign");
@@ -634,7 +641,7 @@ static void writeOutdoorsToXml(ticpp::Printer&& data, cOutdoors& sector) {
data.CloseElement("sector");
}
static void writeTownToXml(ticpp::Printer&& data, cTown& town) {
void writeTownToXml(ticpp::Printer&& data, cTown& town) {
static const char directions[] = {'n', 'w', 's', 'e'};
data.OpenElement("town");
data.PushAttribute("boes", scenario.format_ed_version());
@@ -679,7 +686,7 @@ static void writeTownToXml(ticpp::Printer&& data, cTown& town) {
if(town.spec_on_hostile >= 0)
data.PushElement("onoffend", town.spec_on_hostile);
for(size_t i = 0; i < town.timers.size(); i++) {
if(town.timers[i].time < 0) continue;
if(town.timers[i].time < 0 || town.timers[i].node < 0) continue;
data.OpenElement("timer");
data.PushAttribute("freq", town.timers[i].time);
data.PushText(town.timers[i].node);
@@ -776,7 +783,7 @@ static void writeTownToXml(ticpp::Printer&& data, cTown& town) {
data.CloseElement("town");
}
static void writeDialogueToXml(ticpp::Printer&& data, cSpeech& talk, int town_num) {
void writeDialogueToXml(ticpp::Printer&& data, cSpeech& talk, int town_num) {
data.OpenElement("dialogue");
data.PushAttribute("boes", scenario.format_ed_version());
for(size_t i = 0; i < 10; i++) {

View File

@@ -0,0 +1,8 @@
<dialogue boes="2.0.0">
<personality id='0'>
<title>Billy Kumquat</title>
<look>You see a small boy juggling fruit.</look>
<name>"I'm Billy Kumquat!"</name>
<job>"I juggle!"</job>
</personality>
</dialogue>

48
test/out_write.cpp Normal file
View File

@@ -0,0 +1,48 @@
//
// out_write.cpp
// BoE
//
// Created by Celtic Minstrel on 15-09-02.
//
//
#include <fstream>
#include "catch.hpp"
#include "tinyprint.h"
#include "scenario.hpp"
#include "outdoors.hpp"
using namespace std;
using namespace ticpp;
extern Document xmlDocFromStream(istream& stream, string name);
extern void readOutdoorsFromXml(Document&& data, cOutdoors& out);
extern void writeOutdoorsToXml(ticpp::Printer&& data, cOutdoors& sector);
static void in_and_out(string name, cOutdoors& out, cScenario& scen) {
string fpath = "junk/out_";
fpath += name;
fpath += ".xml";
ofstream fout;
fout.exceptions(ios::badbit);
fout.open(fpath);
writeOutdoorsToXml(Printer(name, fout), out);
fout.close();
// Reconstruct the sector, to ensure that it doesn't just pass due to old data still being around
out.~cOutdoors();
new(&out) cOutdoors(scen);
ifstream fin;
fin.exceptions(ios::badbit);
fin.open(fpath);
readOutdoorsFromXml(xmlDocFromStream(fin, name), out);
}
TEST_CASE("Saving an outdoors sector") {
cScenario scen;
cOutdoors out(scen);
out.out_name = "The Outdoors Test";
SECTION("With the minimal required information") {
in_and_out("basic", out, scen);
CHECK(out.out_name == "The Outdoors Test");
}
}

View File

@@ -111,4 +111,14 @@ TEST_CASE("Loading a town dialogue definition") {
doc = xmlDocFromStream(fin, "node_too_many_strings.xml");
REQUIRE_THROWS_AS(readDialogueFromXml(move(doc), talk, 0), xBadNode);
}
SECTION("With the minimal required information for a personality") {
fin.open("files/talk/minimal.xml");
doc = xmlDocFromStream(fin, "minimal.xml");
REQUIRE_NOTHROW(readDialogueFromXml(move(doc), talk, 0));
CHECK(talk.people[0].title == "Billy Kumquat");
CHECK(talk.people[0].look == "You see a small boy juggling fruit.");
CHECK(talk.people[0].name == "\"I'm Billy Kumquat!\"");
CHECK(talk.people[0].job == "\"I juggle!\"");
}
}

53
test/talk_write.cpp Normal file
View File

@@ -0,0 +1,53 @@
//
// talk_write.cpp
// BoE
//
// Created by Celtic Minstrel on 15-09-02.
//
//
#include <fstream>
#include "catch.hpp"
#include "tinyprint.h"
#include "talking.hpp"
using namespace std;
using namespace ticpp;
extern Document xmlDocFromStream(istream& stream, string name);
extern void readDialogueFromXml(Document&& data, cSpeech& talk, int town_num);
extern void writeDialogueToXml(ticpp::Printer&& data, cSpeech& talk, int town_num);
static void in_and_out(string name, cSpeech& talk, int town_num = 0) {
string fpath = "junk/talk_";
fpath += name;
fpath += ".xml";
ofstream fout;
fout.exceptions(ios::badbit);
fout.open(fpath);
writeDialogueToXml(Printer(name, fout), talk, town_num);
fout.close();
// Reconstruct the dialogue, to ensure that it doesn't just pass due to old data still being around
talk.~cSpeech();
new(&talk) cSpeech();
ifstream fin;
fin.exceptions(ios::badbit);
fin.open(fpath);
readDialogueFromXml(xmlDocFromStream(fin, name), talk, town_num);
}
TEST_CASE("Saving dialogue") {
cSpeech talk;
talk.people[0].title = "Test Person";
talk.people[0].look = "You see a person. What an amazing sight!";
talk.people[0].name = "\"My name is John Smith. How do you do?\"";
talk.people[0].job = "\"Isn't it obvious? I test things!\"";
SECTION("With the minimal required information for a personality") {
in_and_out("basic", talk);
CHECK(talk.people[0].title == "Test Person");
CHECK(talk.people[0].look == "You see a person. What an amazing sight!");
CHECK(talk.people[0].name == "\"My name is John Smith. How do you do?\"");
CHECK(talk.people[0].job == "\"Isn't it obvious? I test things!\"");
}
}

54
test/town_write.cpp Normal file
View File

@@ -0,0 +1,54 @@
//
// town_write.cpp
// BoE
//
// Created by Celtic Minstrel on 15-09-02.
//
//
#include <fstream>
#include "catch.hpp"
#include "tinyprint.h"
#include "scenario.hpp"
#include "regtown.hpp"
using namespace std;
using namespace ticpp;
extern Document xmlDocFromStream(istream& stream, string name);
extern void readTownFromXml(Document&& data, cTown*& town, cScenario& scen);
extern void writeTownToXml(ticpp::Printer&& data, cTown& town);
static void in_and_out(string name, cTown*& town, cScenario& scen) {
string fpath = "junk/town_";
fpath += name;
fpath += ".xml";
ofstream fout;
fout.exceptions(ios::badbit);
fout.open(fpath);
writeTownToXml(Printer(name, fout), *town);
fout.close();
// Reconstruct the town, to ensure that it doesn't just pass due to old data still being around
delete town;
ifstream fin;
fin.exceptions(ios::badbit);
fin.open(fpath);
readTownFromXml(xmlDocFromStream(fin, name), town, scen);
}
TEST_CASE("Saving a town") {
cScenario scen;
cTown* town = new cTinyTown(scen);
town->town_name = "Test Town";
town->in_town_rect = {2,3,30,29};
town->difficulty = 1;
town->lighting_type = LIGHT_NONE;
SECTION("With the minimal required information") {
in_and_out("basic", town, scen);
CHECK(town->max_dim() == 32);
CHECK(town->town_name == "Test Town");
CHECK(town->in_town_rect == rect(2,3,30,29));
CHECK(town->difficulty == 1);
CHECK(town->lighting_type == LIGHT_NONE);
}
}