Basic tests for writing item, monster, and terrain definitions

This commit is contained in:
2015-08-01 14:52:10 -04:00
parent b01457ee88
commit f4e7eea724
5 changed files with 214 additions and 6 deletions

View File

@@ -252,8 +252,11 @@
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 */; };
91EF27751B693D4800666469 /* ter_write.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91EF27741B693D4800666469 /* ter_write.cpp */; };
91EF27771B693D5500666469 /* item_read.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91EF27761B693D5500666469 /* item_read.cpp */; };
91EF27791B693D5F00666469 /* item_write.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91EF27781B693D5F00666469 /* item_write.cpp */; };
91EF277B1B693D6E00666469 /* monst_read.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91EF277A1B693D6E00666469 /* monst_read.cpp */; };
91EF277D1B693D7D00666469 /* monst_write.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91EF277C1B693D7D00666469 /* monst_write.cpp */; };
91F6F8E318F87F3700E3EA15 /* sfml-audio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 91F6F8DD18F87F3700E3EA15 /* sfml-audio.framework */; };
91F6F8E418F87F3700E3EA15 /* sfml-audio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 91F6F8DD18F87F3700E3EA15 /* sfml-audio.framework */; };
91F6F8E518F87F3700E3EA15 /* sfml-audio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 91F6F8DD18F87F3700E3EA15 /* sfml-audio.framework */; };
@@ -768,8 +771,11 @@
91EF05291904D082001BEF85 /* bold.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = bold.ttf; sourceTree = "<group>"; };
91EF052A1904D082001BEF85 /* plain.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = plain.ttf; sourceTree = "<group>"; };
91EF27721B693D3800666469 /* ter_read.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ter_read.cpp; sourceTree = "<group>"; };
91EF27741B693D4800666469 /* ter_write.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ter_write.cpp; sourceTree = "<group>"; };
91EF27761B693D5500666469 /* item_read.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = item_read.cpp; sourceTree = "<group>"; };
91EF27781B693D5F00666469 /* item_write.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = item_write.cpp; sourceTree = "<group>"; };
91EF277A1B693D6E00666469 /* monst_read.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = monst_read.cpp; sourceTree = "<group>"; };
91EF277C1B693D7D00666469 /* monst_write.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = monst_write.cpp; sourceTree = "<group>"; };
91F06E8F1A2EBEE70038E902 /* special_parse.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = special_parse.hpp; sourceTree = "<group>"; };
91F6F8DD18F87F3700E3EA15 /* sfml-audio.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = "sfml-audio.framework"; path = "/Library/Frameworks/sfml-audio.framework"; sourceTree = "<absolute>"; };
91F6F8DE18F87F3700E3EA15 /* sfml-graphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = "sfml-graphics.framework"; path = "/Library/Frameworks/sfml-graphics.framework"; sourceTree = "<absolute>"; };
@@ -1313,8 +1319,11 @@
91C763DA1B4EE6E00086D879 /* map_read.cpp */,
91C763DC1B4EE7950086D879 /* map_write.cpp */,
91EF27721B693D3800666469 /* ter_read.cpp */,
91EF27741B693D4800666469 /* ter_write.cpp */,
91EF27761B693D5500666469 /* item_read.cpp */,
91EF27781B693D5F00666469 /* item_write.cpp */,
91EF277A1B693D6E00666469 /* monst_read.cpp */,
91EF277C1B693D7D00666469 /* monst_write.cpp */,
);
name = src;
sourceTree = "<group>";
@@ -1809,8 +1818,11 @@
91C763DB1B4EE77F0086D879 /* map_read.cpp in Sources */,
91C763DD1B4EE7950086D879 /* map_write.cpp in Sources */,
91EF27731B693D3900666469 /* ter_read.cpp in Sources */,
91EF27751B693D4800666469 /* ter_write.cpp in Sources */,
91EF27771B693D5500666469 /* item_read.cpp in Sources */,
91EF27791B693D5F00666469 /* item_write.cpp in Sources */,
91EF277B1B693D6E00666469 /* monst_read.cpp in Sources */,
91EF277D1B693D7D00666469 /* monst_write.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@@ -43,6 +43,9 @@ void load_spec_graphics();
// These aren't static solely so that the test cases can access them.
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);
map_data buildOutMapData(location which, cScenario& scenario);
map_data buildTownMapData(size_t which, cScenario& scenario);
@@ -355,7 +358,7 @@ void writeScenarioToXml(ticpp::Printer&& data, cScenario& scenario) {
data.CloseElement("scenario");
}
static void writeTerrainToXml(ticpp::Printer&& data) {
void writeTerrainToXml(ticpp::Printer&& data, cScenario& scenario) {
data.OpenElement("terrains");
data.PushAttribute("boes", scenario.format_ed_version());
for(size_t i = 0; i < scenario.ter_types.size(); i++) {
@@ -401,7 +404,7 @@ static void writeTerrainToXml(ticpp::Printer&& data) {
data.CloseElement("terrains");
}
static void writeItemsToXml(ticpp::Printer&& data) {
void writeItemsToXml(ticpp::Printer&& data, cScenario& scenario) {
data.OpenElement("items");
data.PushAttribute("boes", scenario.format_ed_version());
for(size_t i = 0; i < scenario.scen_items.size(); i++) {
@@ -453,7 +456,7 @@ static void writeItemsToXml(ticpp::Printer&& data) {
data.CloseElement("items");
}
static void writeMonstersToXml(ticpp::Printer&& data) {
void writeMonstersToXml(ticpp::Printer&& data, cScenario& scenario) {
std::ostringstream str;
data.OpenElement("monsters");
data.PushAttribute("boes", scenario.format_ed_version());
@@ -948,15 +951,15 @@ void save_scenario(bool rename) {
// Then the terrains...
std::ostream& terrain = scen_file.newFile("scenario/terrain.xml");
writeTerrainToXml(ticpp::Printer("terrain.xml", terrain));
writeTerrainToXml(ticpp::Printer("terrain.xml", terrain), scenario);
// ...items...
std::ostream& items = scen_file.newFile("scenario/items.xml");
writeItemsToXml(ticpp::Printer("items.xml", items));
writeItemsToXml(ticpp::Printer("items.xml", items), scenario);
// ...and monsters
std::ostream& monsters = scen_file.newFile("scenario/monsters.xml");
writeMonstersToXml(ticpp::Printer("monsters.xml", monsters));
writeMonstersToXml(ticpp::Printer("monsters.xml", monsters), scenario);
// And the special nodes.
std::ostream& scen_spec = scen_file.newFile("scenario/scenario.spec");

60
test/item_write.cpp Normal file
View File

@@ -0,0 +1,60 @@
//
// item_write.cpp
// BoE
//
// Created by Celtic Minstrel on 15-07-29.
//
//
#include <fstream>
#include "catch.hpp"
#include "tinyprint.h"
#include "scenario.hpp"
using namespace std;
using namespace ticpp;
extern Document xmlDocFromStream(istream& stream, string name);
extern void readItemsFromXml(Document&& data, cScenario& scenario);
extern void writeItemsToXml(Printer&& data, cScenario& scenario);
static void in_and_out(string name, cScenario& scen) {
string fpath = "junk/item_";
fpath += name;
fpath += ".xml";
ofstream fout;
fout.exceptions(ios::badbit);
fout.open(fpath);
writeItemsToXml(Printer(name, fout), scen);
fout.close();
// Reconstruct the scenario, to ensure that it doesn't just pass due to old data still being around
scen.~cScenario();
new(&scen) cScenario();
ifstream fin;
fin.exceptions(ios::badbit);
fin.open(fpath);
readItemsFromXml(xmlDocFromStream(fin, name), scen);
}
TEST_CASE("Saving item types") {
cScenario scen;
scen.scen_items.resize(1);
scen.scen_items[0].variety = eItemType::POTION;
scen.scen_items[0].item_level = 0;
scen.scen_items[0].graphic_num = 60;
scen.scen_items[0].value = 100;
scen.scen_items[0].weight = 5;
scen.scen_items[0].name = "Potion";
scen.scen_items[0].full_name = "Test Potion";
SECTION("With basic information") {
in_and_out("basic", scen);
REQUIRE(scen.scen_items.size() == 1);
CHECK(scen.scen_items[0].variety == eItemType::POTION);
CHECK(scen.scen_items[0].item_level == 0);
CHECK(scen.scen_items[0].graphic_num == 60);
CHECK(scen.scen_items[0].value == 100);
CHECK(scen.scen_items[0].weight == 5);
CHECK(scen.scen_items[0].name == "Potion");
CHECK(scen.scen_items[0].full_name == "Test Potion");
}
}

71
test/monst_write.cpp Normal file
View File

@@ -0,0 +1,71 @@
//
// monst_write.cpp
// BoE
//
// Created by Celtic Minstrel on 15-07-29.
//
//
#include <fstream>
#include "catch.hpp"
#include "tinyprint.h"
#include "scenario.hpp"
using namespace std;
using namespace ticpp;
extern Document xmlDocFromStream(istream& stream, string name);
extern void readMonstersFromXml(Document&& data, cScenario& scenario);
extern void writeMonstersToXml(Printer&& data, cScenario& scenario);
static void in_and_out(string name, cScenario& scen) {
string fpath = "junk/monst_";
fpath += name;
fpath += ".xml";
ofstream fout;
fout.exceptions(ios::badbit);
fout.open(fpath);
writeMonstersToXml(Printer(name, fout), scen);
fout.close();
// Reconstruct the scenario, to ensure that it doesn't just pass due to old data still being around
scen.~cScenario();
new(&scen) cScenario();
ifstream fin;
fin.exceptions(ios::badbit);
fin.open(fpath);
readMonstersFromXml(xmlDocFromStream(fin, name), scen);
}
TEST_CASE("Saving monster types") {
cScenario scen;
scen.scen_monsters.resize(2);
scen.scen_monsters[1].m_name = "Test Monster";
scen.scen_monsters[1].level = 3;
scen.scen_monsters[1].armor = 5;
scen.scen_monsters[1].skill = 6;
scen.scen_monsters[1].m_health = 10;
scen.scen_monsters[1].m_type = eRace::MAGICAL;
scen.scen_monsters[1].x_width = 1;
scen.scen_monsters[1].y_width = 2;
scen.scen_monsters[1].picture_num = 17;
scen.scen_monsters[1].default_attitude = eAttitude::HOSTILE_B;
SECTION("With the minimal required information") {
in_and_out("basic", scen);
CHECK(scen.scen_monsters[1].m_name == "Test Monster");
CHECK(scen.scen_monsters[1].level == 3);
CHECK(scen.scen_monsters[1].armor == 5);
CHECK(scen.scen_monsters[1].skill == 6);
CHECK(scen.scen_monsters[1].m_health == 10);
CHECK(scen.scen_monsters[1].speed == 4);
CHECK(scen.scen_monsters[1].m_type == eRace::MAGICAL);
CHECK(scen.scen_monsters[1].x_width == 1);
CHECK(scen.scen_monsters[1].y_width == 2);
CHECK(scen.scen_monsters[1].picture_num == 17);
CHECK(scen.scen_monsters[1].default_attitude == eAttitude::HOSTILE_B);
for(int i = 0; i <= 8; i++) {
eDamageType dmg = eDamageType(i);
CAPTURE(dmg);
CHECK(scen.scen_monsters[1].resist[dmg] == 100);
}
}
}

62
test/ter_write.cpp Normal file
View File

@@ -0,0 +1,62 @@
//
// ter_write.cpp
// BoE
//
// Created by Celtic Minstrel on 15-07-29.
//
//
#include <fstream>
#include "catch.hpp"
#include "tinyprint.h"
#include "scenario.hpp"
using namespace std;
using namespace ticpp;
extern Document xmlDocFromStream(istream& stream, string name);
extern void readTerrainFromXml(Document&& data, cScenario& scenario);
extern void writeTerrainToXml(Printer&& data, cScenario& scenario);
static void in_and_out(string name, cScenario& scen) {
string fpath = "junk/ter_";
fpath += name;
fpath += ".xml";
ofstream fout;
fout.exceptions(ios::badbit);
fout.open(fpath);
writeTerrainToXml(Printer(name, fout), scen);
fout.close();
// Reconstruct the scenario, to ensure that it doesn't just pass due to old data still being around
scen.~cScenario();
new(&scen) cScenario();
ifstream fin;
fin.exceptions(ios::badbit);
fin.open(fpath);
readTerrainFromXml(xmlDocFromStream(fin, name), scen);
}
TEST_CASE("Saving terrain types") {
cScenario scen;
scen.ter_types.resize(1);
scen.ter_types[0].name = "Test Terrain";
scen.ter_types[0].picture = 0;
scen.ter_types[0].map_pic = 1;
scen.ter_types[0].blockage = eTerObstruct::BLOCK_SIGHT;
scen.ter_types[0].trim_type = eTrimType::CITY;
scen.ter_types[0].combat_arena = 2;
scen.ter_types[0].special = eTerSpec::WILDERNESS_SURFACE;
SECTION("With basic information") {
in_and_out("basic", scen);
REQUIRE(scen.ter_types.size() == 1);
CHECK(scen.ter_types[0].name == "Test Terrain");
CHECK(scen.ter_types[0].picture == 0);
CHECK(scen.ter_types[0].map_pic == 1);
CHECK(scen.ter_types[0].blockage == eTerObstruct::BLOCK_SIGHT);
CHECK(scen.ter_types[0].trim_type == eTrimType::CITY);
CHECK(scen.ter_types[0].combat_arena == 2);
CHECK(scen.ter_types[0].special == eTerSpec::WILDERNESS_SURFACE);
CHECK(scen.ter_types[0].flag1 == -1);
CHECK(scen.ter_types[0].step_sound == eStepSnd::STEP);
}
}