Skip to content

Commit 1e215b4

Browse files
committed
Finish fragment validation unit tests
1 parent 390e613 commit 1e215b4

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

src/Validation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,7 @@ void ValidateExecutableVisitor::visitFragmentSpread(const peg::ast_node& fragmen
14571457
if (!matchesScopedType(innerType))
14581458
{
14591459
// http://spec.graphql.org/June2018/#sec-Fragment-spread-is-possible
1460-
auto position = typeCondition->begin();
1460+
auto position = fragmentSpread.begin();
14611461
std::ostringstream message;
14621462

14631463
message << "Incompatible fragment spread target type: " << innerType

test/ValidationTests.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,3 +1091,96 @@ TEST_F(ValidationExamplesCase, Example140)
10911091

10921092
ASSERT_TRUE(errors.empty());
10931093
}
1094+
1095+
TEST_F(ValidationExamplesCase, Example141)
1096+
{
1097+
// http://spec.graphql.org/June2018/#example-85110
1098+
auto query = R"(fragment petFragment on Pet {
1099+
name
1100+
... on Dog {
1101+
barkVolume
1102+
}
1103+
}
1104+
1105+
fragment catOrDogFragment on CatOrDog {
1106+
... on Cat {
1107+
meowVolume
1108+
}
1109+
}
1110+
1111+
query {
1112+
dog {
1113+
...petFragment
1114+
...catOrDogFragment
1115+
}
1116+
})"_graphql;
1117+
1118+
auto errors = service::buildErrorValues(_service->validate(query)).release<response::ListType>();
1119+
1120+
ASSERT_TRUE(errors.empty());
1121+
}
1122+
1123+
TEST_F(ValidationExamplesCase, CounterExample142)
1124+
{
1125+
// http://spec.graphql.org/June2018/#example-a8dcc
1126+
auto query = R"(fragment sentientFragment on Sentient {
1127+
... on Dog {
1128+
barkVolume
1129+
}
1130+
}
1131+
1132+
fragment humanOrAlienFragment on HumanOrAlien {
1133+
... on Cat {
1134+
meowVolume
1135+
}
1136+
})"_graphql;
1137+
1138+
auto errors = service::buildErrorValues(_service->validate(query)).release<response::ListType>();
1139+
1140+
EXPECT_EQ(errors.size(), 4) << "2 incompatible type + 2 unused fragments";
1141+
ASSERT_GE(errors.size(), size_t { 2 });
1142+
EXPECT_EQ(R"js({"message":"Incompatible target type on inline fragment name: Dog","locations":[{"line":2,"column":8}]})js", response::toJSON(std::move(errors[0]))) << "error should match";
1143+
EXPECT_EQ(R"js({"message":"Incompatible target type on inline fragment name: Cat","locations":[{"line":8,"column":8}]})js", response::toJSON(std::move(errors[1]))) << "error should match";
1144+
}
1145+
1146+
TEST_F(ValidationExamplesCase, Example143)
1147+
{
1148+
// http://spec.graphql.org/June2018/#example-dc875
1149+
auto query = R"(fragment unionWithInterface on Pet {
1150+
...dogOrHumanFragment
1151+
}
1152+
1153+
fragment dogOrHumanFragment on DogOrHuman {
1154+
... on Dog {
1155+
barkVolume
1156+
}
1157+
}
1158+
1159+
query {
1160+
dog {
1161+
...unionWithInterface
1162+
}
1163+
})"_graphql;
1164+
1165+
auto errors = service::buildErrorValues(_service->validate(query)).release<response::ListType>();
1166+
1167+
ASSERT_TRUE(errors.empty());
1168+
}
1169+
1170+
TEST_F(ValidationExamplesCase, CounterExample144)
1171+
{
1172+
// http://spec.graphql.org/June2018/#example-c9c63
1173+
auto query = R"(fragment nonIntersectingInterfaces on Pet {
1174+
...sentientFragment
1175+
}
1176+
1177+
fragment sentientFragment on Sentient {
1178+
name
1179+
})"_graphql;
1180+
1181+
auto errors = service::buildErrorValues(_service->validate(query)).release<response::ListType>();
1182+
1183+
EXPECT_EQ(errors.size(), 3) << "1 incompatible type + 2 unused fragments";
1184+
ASSERT_GE(errors.size(), size_t { 1 });
1185+
EXPECT_EQ(R"js({"message":"Incompatible fragment spread target type: Sentient name: sentientFragment","locations":[{"line":2,"column":7}]})js", response::toJSON(std::move(errors[0]))) << "error should match";
1186+
}

0 commit comments

Comments
 (0)