Skip to content

Commit e676a21

Browse files
committed
Inflection-77 Adding optional <initial> tag in InflectionTest#testInflections
1 parent c41ec8c commit e676a21

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

inflection/src/inflection/dialog/InflectableStringConcept.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <inflection/dialog/SpeakableString.h>
77
#include <inflection/dialog/SemanticFeatureConcept.h>
88
#include <inflection/dialog/SemanticFeatureModel.h>
9-
#include <inflection/dialog/SemanticUtils.hpp>
9+
#include <inflection/dialog/DisplayValue.h>
1010
/**
1111
* An object that provides a way to format a word with additional grammatical category values or semantic features of a word for a given language.
1212
*/

inflection/src/inflection/dialog/PronounConcept.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ PronounConcept::PronounConcept(const SemanticFeatureModel& model, const ::std::v
211211
if (pronounData->numValues() == 0) {
212212
throw ::inflection::exception::IllegalArgumentException(u"Display data can not be empty.");
213213
}
214-
215214
for (const auto &[semanticFeature, grammeme]: defaultConstraints) {
216215
this->defaultConstraints.emplace(semanticFeature.getName(), grammeme);
217216
}

inflection/test/src/inflection/dialog/InflectionTest.cpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <inflection/dialog/InflectableStringConcept.hpp>
77
#include <inflection/dialog/SemanticFeatureModel.hpp>
8+
#include <inflection/dialog/SemanticFeature.hpp>
89
#include <inflection/dialog/SpeakableString.hpp>
910
#include <inflection/dialog/LocalizedCommonConceptFactoryProvider.hpp>
1011
#include <inflection/exception/XMLParseException.hpp>
@@ -35,9 +36,26 @@ static std::string getInfoStringForTestInput(const ::inflection::dialog::Semanti
3536
return "locale=" + model.getLocale().getName() + (source.isEmpty() ? "" : std::string(" source=") + inflection::util::StringUtils::to_string(source.toString())) + " {" + constraintsStr + "}";
3637
}
3738

38-
static void compareInflection(const ::inflection::dialog::SemanticFeatureModel& model, const ::inflection::dialog::SpeakableString& expected, const ::inflection::dialog::SpeakableString& source, const std::map<std::u16string, std::u16string>& constraints, const std::map<std::u16string, std::u16string>& resultAttributes)
39+
static void compareInflection(const ::inflection::dialog::SemanticFeatureModel& model,
40+
const ::inflection::dialog::SpeakableString& expected,
41+
const ::inflection::dialog::SpeakableString& source,
42+
const std::map<std::u16string, std::u16string>& constraints,
43+
const std::map<std::u16string, std::u16string>& resultAttributes,
44+
const std::map<std::u16string, std::u16string>& initialAttributes)
3945
{
40-
inflection::dialog::InflectableStringConcept inflectableConcept(&model, source);
46+
std::map<inflection::dialog::SemanticFeature, ::std::u16string> intitialConstraints;
47+
for(const auto& [attrKey, attrVal] : initialAttributes) {
48+
auto featureName = model.getFeature(attrKey);
49+
if (featureName == nullptr) {
50+
FAIL_CHECK(model.getLocale().getName() + std::string(": feature name is not recognized: ") + inflection::util::StringUtils::to_string(attrKey));
51+
}
52+
const auto& boundedValues(npc(featureName)->getBoundedValues());
53+
if (!boundedValues.empty() && boundedValues.find(attrVal) == boundedValues.end()) {
54+
FAIL_CHECK(model.getLocale().getName() + std::string(": feature value \"") + inflection::util::StringUtils::to_string(attrVal) + "\" is not valid for feature \"" + inflection::util::StringUtils::to_string(attrKey) + "\"");
55+
}
56+
intitialConstraints[*featureName] = attrVal;
57+
}
58+
inflection::dialog::InflectableStringConcept inflectableConcept(&model, source, intitialConstraints);
4159
for (const auto& constraint : constraints) {
4260
auto featureName = model.getFeature(constraint.first);
4361
if (featureName == nullptr) {
@@ -171,16 +189,26 @@ TEST_CASE("InflectionTest#testInflections")
171189
if (xmlStrEqual(sourceNode->name, (const xmlChar *) "source") == 0) {
172190
throw ::inflection::exception::XMLParseException(u"Expecting element <source>, got <" + ::inflection::util::StringUtils::to_u16string(std::string(reinterpret_cast<const char*>(sourceNode->name))) + u">");
173191
}
174-
xmlNodePtr resultNode = xmlNextElementSibling(sourceNode);
175-
if (xmlStrEqual(resultNode->name, (const xmlChar *) "result") == 0) {
176-
throw ::inflection::exception::XMLParseException(u"Expecting element <result>, got <" + ::inflection::util::StringUtils::to_u16string(std::string(reinterpret_cast<const char*>(resultNode->name))) + u">");
192+
193+
// optional initial child tag
194+
curTestChild = xmlNextElementSibling(sourceNode);
195+
xmlNodePtr initialNode = nullptr;
196+
if(xmlStrEqual(curTestChild->name, (const xmlChar*) "initial") != 0) {
197+
initialNode = curTestChild;
198+
curTestChild = xmlNextElementSibling(curTestChild);
199+
}
200+
201+
if (xmlStrEqual(curTestChild->name, (const xmlChar *) "result") == 0) {
202+
throw ::inflection::exception::XMLParseException(u"Expecting element <result>, got <" + ::inflection::util::StringUtils::to_u16string(std::string(reinterpret_cast<const char*>(curTestChild->name))) + u">");
177203
}
204+
xmlNodePtr resultNode = curTestChild;
178205
auto sourceConstraints(XMLUtils::getAttributes(sourceNode));
179206
auto resultAttributes(XMLUtils::getAttributes(resultNode));
207+
auto initialAttributes(XMLUtils::getAttributes(initialNode));
180208
auto sourceString(getSpeakableString(sourceNode));
181209
auto resultString(getSpeakableString(resultNode));
182210

183-
compareInflection(*npc(model), resultString, sourceString, sourceConstraints, resultAttributes);
211+
compareInflection(*npc(model), resultString, sourceString, sourceConstraints, resultAttributes, initialAttributes);
184212
numTests++;
185213
}
186214
xmlFreeDoc(xmlDoc);

inflection/test/src/util/XMLUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <inflection/npc.hpp>
1212

1313
std::map<std::u16string, std::u16string> XMLUtils::getAttributes(xmlNodePtr node) {
14+
if(node == nullptr) return { };
1415
xmlAttr* attribute = node->properties;
1516
std::map<std::u16string, std::u16string> result;
1617
while (attribute) {

0 commit comments

Comments
 (0)