1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
#include <boost/test/unit_test.hpp>
#include "parse/ValueRefParser.h"
#include "universe/ValueRefs.h"
#include "CommonTest.h"
struct ValueRefStringFixture {
ValueRefStringFixture():
result(0),
value(0),
statistic(0),
variable(0)
{}
~ValueRefStringFixture() {
delete result;
}
bool parse(std::string phrase, ValueRef::ValueRef<std::string>*& result) {
const parse::lexer& lexer = lexer.instance();
boost::spirit::qi::in_state_type in_state;
boost::spirit::qi::eoi_type eoi;
boost::spirit::qi::_1_type _1;
std::string::const_iterator begin_phrase = phrase.begin();
std::string::const_iterator end_phrase = phrase.end();
auto begin = lexer.begin(begin_phrase, end_phrase);
auto end = lexer.end();
bool matched = boost::spirit::qi::phrase_parse(
begin, end,
string_grammar[boost::phoenix::ref(result) = _1] > eoi,
in_state("WS")[lexer.self]
);
return matched && begin == end;
}
typedef std::pair<ValueRef::ReferenceType, std::string> ReferenceType;
typedef std::pair<ValueRef::StatisticType, std::string> StatisticType;
static const std::array<ReferenceType, 4> referenceTypes;
static const std::array<StatisticType, 9> statisticTypes;
static const std::array<std::string, 3> containerTypes;
static const std::array<std::string, 13> attributes;
ValueRef::ValueRef<std::string>* result;
const ValueRef::Constant<std::string>* value;
const ValueRef::Statistic<std::string>* statistic;
const ValueRef::Variable<std::string>* variable;
};
const std::array<ValueRefStringFixture::ReferenceType, 4> ValueRefStringFixture::referenceTypes = {{
std::make_pair(ValueRef::SOURCE_REFERENCE, "Source"),
std::make_pair(ValueRef::EFFECT_TARGET_REFERENCE, "Target"),
std::make_pair(ValueRef::CONDITION_LOCAL_CANDIDATE_REFERENCE, "LocalCandidate"),
std::make_pair(ValueRef::CONDITION_ROOT_CANDIDATE_REFERENCE, "RootCandidate")
}};
const std::array<ValueRefStringFixture::StatisticType, 9> ValueRefStringFixture::statisticTypes = {{
std::make_pair(ValueRef::MAX, "Max"),
std::make_pair(ValueRef::MEAN, "Mean"),
std::make_pair(ValueRef::MIN, "Min"),
std::make_pair(ValueRef::MODE, "Mode"),
std::make_pair(ValueRef::PRODUCT, "Product"),
std::make_pair(ValueRef::RMS, "RMS"),
std::make_pair(ValueRef::SPREAD, "Spread"),
std::make_pair(ValueRef::STDEV, "StDev"),
std::make_pair(ValueRef::SUM, "Sum")
}};
const std::array<std::string, 3> ValueRefStringFixture::containerTypes = {{
"Fleet",
"Planet",
"System"
}};
const std::array<std::string, 13> ValueRefStringFixture::attributes = {{
"Age",
"CreationTurn",
"DesignID",
"FinalDestinationID",
"FleetID",
"ID",
"NextSystemID",
"NumShips",
"Owner",
"PlanetID",
"PreviousSystemID",
"ProducedByEmpireID",
"SystemID"
}};
BOOST_FIXTURE_TEST_SUITE(TestValueRefStringParser, ValueRefStringFixture)
BOOST_AUTO_TEST_CASE(StringLiteralParserString) {
BOOST_CHECK(parse("Tiny", result));
BOOST_REQUIRE_EQUAL(typeid(ValueRef::Constant<std::string>), typeid(*result));
value = dynamic_cast<const ValueRef::Constant<std::string>*>(result);
BOOST_CHECK_EQUAL(value->Value(), "Tiny");
}
BOOST_AUTO_TEST_CASE(StringLiteralParserSpaceString) {
BOOST_CHECK(parse("\"A little bit of text with spaces.\"", result));
BOOST_REQUIRE_EQUAL(typeid(ValueRef::Constant<std::string>), typeid(*result));
value = dynamic_cast<const ValueRef::Constant<std::string>*>(result);
BOOST_CHECK_EQUAL(value->Value(), "A little bit of text with spaces.");
}
BOOST_AUTO_TEST_CASE(StringLiteralParserInteger) {
BOOST_CHECK(parse("5", result));
BOOST_REQUIRE_EQUAL(typeid(ValueRef::Constant<std::string>), typeid(*result));
value = dynamic_cast<const ValueRef::Constant<std::string>*>(result);
BOOST_CHECK_EQUAL(value->Value(), "5");
}
BOOST_AUTO_TEST_CASE(StringLiteralParserReal) {
BOOST_CHECK(parse("7.21", result));
BOOST_REQUIRE_EQUAL(typeid(ValueRef::Constant<std::string>), typeid(*result));
value = dynamic_cast<const ValueRef::Constant<std::string>*>(result);
BOOST_CHECK_EQUAL(value->Value(), "7.21");
}
BOOST_AUTO_TEST_CASE(StringVariableParserCurrentTurn) {
BOOST_CHECK(parse("CurrentTurn", result));
std::string property[] = { "CurrentTurn" };
BOOST_REQUIRE_EQUAL(typeid(ValueRef::StringCast<int>), typeid(*result));
variable = dynamic_cast<const ValueRef::StringCast<int>*>(result);
BOOST_CHECK_EQUAL(variable->GetReferenceType(), ValueRef::NON_OBJECT_REFERENCE);
BOOST_CHECK_EQUAL_COLLECTIONS(
variable->PropertyName().begin(), variable->PropertyName().end(),
property, property + 1
);
}
BOOST_AUTO_TEST_CASE(StringLiteralParserMalformed) {
BOOST_CHECK_THROW(parse("\"A bit of text with missing quotes, whoops", result), std::runtime_error);
BOOST_CHECK(!result);
}
BOOST_AUTO_TEST_CASE(StringVariableParserValue) {
BOOST_CHECK(parse("Value", result));
std::string property[] = { "Value" };
BOOST_REQUIRE_EQUAL(typeid(ValueRef::Variable<std::string>), typeid(*result));
variable = dynamic_cast<const ValueRef::Variable<std::string>*>(result);
BOOST_CHECK_EQUAL(variable->GetReferenceType(), ValueRef::EFFECT_TARGET_REFERENCE);
BOOST_CHECK_EQUAL_COLLECTIONS(
variable->PropertyName().begin(), variable->PropertyName().end(),
property, property + 1
);
}
BOOST_AUTO_TEST_CASE(StringVariableParserTypeless) {
for (const ReferenceType& reference : referenceTypes) {
for (const std::string& attribute : attributes) {
std::string phrase = reference.second + "." + attribute;
BOOST_CHECK_MESSAGE(parse(phrase, result), "Failed to parse: \"" + phrase + "\"");
std::string property[] = {
reference.second,
attribute
};
BOOST_CHECK_EQUAL(typeid(ValueRef::StringCast<int>), typeid(*result));
if(variable = dynamic_cast<const ValueRef::StringCast<int>*>(result)) {
BOOST_CHECK_EQUAL(variable->GetReferenceType(), reference.first);
BOOST_CHECK_EQUAL_COLLECTIONS(
variable->PropertyName().begin(), variable->PropertyName().end(),
property, property + 2
);
}
delete result;
result = 0;
}
}
}
BOOST_AUTO_TEST_CASE(StringVariableParserTyped) {
for (const ReferenceType& reference : referenceTypes) {
for (const std::string& type : containerTypes) {
for (const std::string& attribute : attributes) {
std::string phrase = reference.second + "." + type + "." + attribute;
BOOST_CHECK_MESSAGE(parse(phrase, result), "Failed to parse: \"" + phrase + "\"");
std::string property[] = {
reference.second,
type,
attribute
};
BOOST_CHECK_EQUAL(typeid(ValueRef::StringCast<int>), typeid(*result));
if(variable = dynamic_cast<const ValueRef::StringCast<int>*>(result)) {
BOOST_CHECK_EQUAL(variable->GetReferenceType(), reference.first);
BOOST_CHECK_EQUAL_COLLECTIONS(
variable->PropertyName().begin(), variable->PropertyName().end(),
property, property + 3
);
}
delete result;
result = 0;
}
}
}
}
// XXX: Statistic COUNT, UNIQUE_COUNT and IF not tested.
BOOST_AUTO_TEST_CASE(StringStatisticParserTypeless) {
for (const StatisticType& statisticType : statisticTypes) {
for (const std::string& attribute : attributes) {
std::string property[] = { attribute };
std::string phrase = "Statistic " + statisticType.second + " Value = " + attribute + " Condition = All";
BOOST_CHECK_MESSAGE(parse(phrase, result), "Failed to parse \"" + phrase + "\"");
BOOST_CHECK_EQUAL(typeid(ValueRef::Statistic<std::string>), typeid(*result));
if(statistic = dynamic_cast<const ValueRef::Statistic<std::string>*>(result)) {
BOOST_CHECK_EQUAL(statistic->GetStatisticType(), statisticType.first);
BOOST_CHECK_EQUAL(statistic->GetReferenceType(), ValueRef::EFFECT_TARGET_REFERENCE);
BOOST_CHECK_EQUAL_COLLECTIONS(
statistic->PropertyName().begin(), statistic->PropertyName().end(),
property, property + 1
);
BOOST_CHECK_EQUAL(typeid(Condition::All), typeid(*(statistic->GetSamplingCondition())));
}
delete result;
result = 0;
}
}
}
BOOST_AUTO_TEST_CASE(StringStatisticParserTyped) {
for (const StatisticType& statisticType : statisticTypes) {
for (const std::string& containerType : containerTypes) {
for (const std::string& attribute : attributes) {
std::string property[] = {
containerType,
attribute
};
std::string phrase = "Statistic " + statisticType.second + " Value = " + containerType + "." + attribute + " Condition = All";
BOOST_CHECK_MESSAGE(parse(phrase, result), "Failed to parse \"" + phrase + "\"");
BOOST_CHECK_EQUAL(typeid(ValueRef::Statistic<std::string>), typeid(*result));
if(statistic = dynamic_cast<const ValueRef::Statistic<std::string>*>(result)) {
BOOST_CHECK_EQUAL(statistic->GetStatisticType(), statisticType.first);
BOOST_CHECK_EQUAL(statistic->GetReferenceType(), ValueRef::EFFECT_TARGET_REFERENCE);
BOOST_CHECK_EQUAL_COLLECTIONS(
statistic->PropertyName().begin(), statistic->PropertyName().end(),
property, property + 2
);
BOOST_CHECK_EQUAL(typeid(Condition::All), typeid(*(statistic->GetSamplingCondition())));
}
delete result;
result = 0;
}
}
}
}
BOOST_AUTO_TEST_SUITE_END()
|