Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Commit

Permalink
Merge pull request #324 from apiaryio/pksunkara/mixin-warn
Browse files Browse the repository at this point in the history
Warn when mixin base type is incompatible
  • Loading branch information
klokane committed May 13, 2015
2 parents b15d35b + 8a99e32 commit f1aef0e
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/MSONValueMemberParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,20 @@ namespace snowcrash {
IntermediateParseResult<mson::Mixin> mixin(sections.report);
cur = MSONMixinParser::parse(node, siblings, pd, mixin);

element.build(mixin.node);
if (baseType != mixin.node.baseType) {

if (pd.exportSourceMap()) {
elementSM.mixin = mixin.sourceMap;
// WARN: Mixin base type should be compatible with the parent base type
mdp::CharactersRangeSet sourceMap = mdp::BytesRangeSetToCharactersRangeSet(node->sourceMap, pd.sourceData);
sections.report.warnings.push_back(Warning("mixin base type should be the same as parent base type. objects should contain object mixins. arrays should contain array mixins",
LogicalErrorWarning,
sourceMap));
}
else {
element.build(mixin.node);

if (pd.exportSourceMap()) {
elementSM.mixin = mixin.sourceMap;
}
}
}
else if (pd.sectionContext() == MSONOneOfSectionType) {
Expand Down
92 changes: 92 additions & 0 deletions test/test-BlueprintParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1001,3 +1001,95 @@ TEST_CASE("Report error when not finding a mixin type", "[blueprint]")
REQUIRE(blueprint.report.error.code == MSONError);
REQUIRE(blueprint.report.error.message == "base type 'B' is not defined in the document");
}

TEST_CASE("When an object contains a mixin of array type", "[blueprint]")
{
mdp::ByteBuffer source = \
"# Data Structures\n"\
"## C (object)\n"\
"+ Include A\n"\
"\n"\
"## D (object)\n"\
"+ Include B";

NamedTypes namedTypes;
NamedTypeHelper::build("A", mson::ObjectBaseType, namedTypes);
NamedTypeHelper::build("B", mson::ValueBaseType, namedTypes);

ParseResult<Blueprint> blueprint;
SectionParserHelper<Blueprint, BlueprintParser>::parse(source, BlueprintSectionType, blueprint, ExportSourcemapOption, Models(), &blueprint, namedTypes);

REQUIRE(blueprint.report.error.code == Error::OK);
REQUIRE(blueprint.report.warnings.size() == 1);
REQUIRE(blueprint.report.warnings[0].code == LogicalErrorWarning);
SourceMapHelper::check(blueprint.report.warnings[0].location, 59, 11);
}

TEST_CASE("When an array contains a mixin of object type", "[blueprint]")
{
mdp::ByteBuffer source = \
"# Data Structures\n"\
"## C (array)\n"\
"+ Include A\n"\
"\n"\
"## D (array)\n"\
"+ Include B";

NamedTypes namedTypes;
NamedTypeHelper::build("A", mson::ObjectBaseType, namedTypes);
NamedTypeHelper::build("B", mson::ValueBaseType, namedTypes);

ParseResult<Blueprint> blueprint;
SectionParserHelper<Blueprint, BlueprintParser>::parse(source, BlueprintSectionType, blueprint, ExportSourcemapOption, Models(), &blueprint, namedTypes);

REQUIRE(blueprint.report.error.code == Error::OK);
REQUIRE(blueprint.report.warnings.size() == 1);
REQUIRE(blueprint.report.warnings[0].code == LogicalErrorWarning);
SourceMapHelper::check(blueprint.report.warnings[0].location, 31, 13);
}

TEST_CASE("When an object member contains a mixin of array type", "[blueprint]")
{
mdp::ByteBuffer source = \
"# Data Structures\n"\
"## C\n"\
"+ d (object)\n"\
" + Include A\n"\
"+ e (object)\n"\
" + Include B";

NamedTypes namedTypes;
NamedTypeHelper::build("A", mson::ObjectBaseType, namedTypes);
NamedTypeHelper::build("B", mson::ValueBaseType, namedTypes);

ParseResult<Blueprint> blueprint;
SectionParserHelper<Blueprint, BlueprintParser>::parse(source, BlueprintSectionType, blueprint, ExportSourcemapOption, Models(), &blueprint, namedTypes);

REQUIRE(blueprint.report.error.code == Error::OK);
REQUIRE(blueprint.report.warnings.size() == 1);
REQUIRE(blueprint.report.warnings[0].code == LogicalErrorWarning);
SourceMapHelper::check(blueprint.report.warnings[0].location, 69, 11);
}

TEST_CASE("When an array member contains a mixin of object type", "[blueprint]")
{
mdp::ByteBuffer source = \
"# Data Structures\n"\
"## C\n"\
"+ d (array)\n"\
" + Include A\n"\
"+ e (array)\n"\
" + Include B";

NamedTypes namedTypes;
NamedTypeHelper::build("A", mson::ObjectBaseType, namedTypes);
NamedTypeHelper::build("B", mson::ValueBaseType, namedTypes);

ParseResult<Blueprint> blueprint;
SectionParserHelper<Blueprint, BlueprintParser>::parse(source, BlueprintSectionType, blueprint, ExportSourcemapOption, Models(), &blueprint, namedTypes);

REQUIRE(blueprint.report.error.code == Error::OK);
REQUIRE(blueprint.report.warnings.size() == 1);
REQUIRE(blueprint.report.warnings[0].code == LogicalErrorWarning);
SourceMapHelper::check(blueprint.report.warnings[0].location, 39, 12);
}

0 comments on commit f1aef0e

Please sign in to comment.