@@ -68,6 +68,7 @@ using ::testing::Eq;
6868using ::testing::IsEmpty;
6969using ::testing::Pair;
7070using ::testing::Property;
71+ using ::testing::SizeIs;
7172
7273using AstType = ast_internal::Type;
7374using Severity = TypeCheckIssue::Severity;
@@ -1281,6 +1282,63 @@ TEST(TypeCheckerImplTest, ExpectedTypeDoesntMatch) {
12811282 " expected type 'map<string, string>' but found 'map<string, int>'" )));
12821283}
12831284
1285+ TEST (TypeCheckerImplTest, BadSourcePosition) {
1286+ google::protobuf::Arena arena;
1287+ TypeCheckEnv env (GetSharedTestingDescriptorPool ());
1288+
1289+ TypeCheckerImpl impl (std::move (env));
1290+ ASSERT_OK_AND_ASSIGN (auto ast, MakeTestParsedAst (" foo" ));
1291+ auto & ast_impl = AstImpl::CastFromPublicAst (*ast);
1292+ ast_impl.source_info ().mutable_positions ()[1 ] = -42 ;
1293+ ASSERT_OK_AND_ASSIGN (ValidationResult result, impl.Check (std::move (ast)));
1294+ ASSERT_OK_AND_ASSIGN (auto source, NewSource (" foo" ));
1295+
1296+ EXPECT_FALSE (result.IsValid ());
1297+ ASSERT_THAT (result.GetIssues (), SizeIs (1 ));
1298+
1299+ EXPECT_EQ (
1300+ result.GetIssues ()[0 ].ToDisplayString (*source),
1301+ " ERROR: <input>:-1:-1: undeclared reference to 'foo' (in container '')" );
1302+ }
1303+
1304+ TEST (TypeCheckerImplTest, BadLineOffsets) {
1305+ google::protobuf::Arena arena;
1306+ TypeCheckEnv env (GetSharedTestingDescriptorPool ());
1307+
1308+ TypeCheckerImpl impl (std::move (env));
1309+ ASSERT_OK_AND_ASSIGN (auto source, NewSource (" \n foo" ));
1310+
1311+ {
1312+ ASSERT_OK_AND_ASSIGN (auto ast, MakeTestParsedAst (" \n foo" ));
1313+ auto & ast_impl = AstImpl::CastFromPublicAst (*ast);
1314+ ast_impl.source_info ().mutable_line_offsets ()[1 ] = 1 ;
1315+ ASSERT_OK_AND_ASSIGN (ValidationResult result, impl.Check (std::move (ast)));
1316+
1317+ EXPECT_FALSE (result.IsValid ());
1318+ ASSERT_THAT (result.GetIssues (), SizeIs (1 ));
1319+
1320+ EXPECT_EQ (result.GetIssues ()[0 ].ToDisplayString (*source),
1321+ " ERROR: <input>:-1:-1: undeclared reference to 'foo' (in "
1322+ " container '')" );
1323+ }
1324+ {
1325+ ASSERT_OK_AND_ASSIGN (auto ast, MakeTestParsedAst (" \n foo" ));
1326+ auto & ast_impl = AstImpl::CastFromPublicAst (*ast);
1327+ ast_impl.source_info ().mutable_line_offsets ().clear ();
1328+ ast_impl.source_info ().mutable_line_offsets ().push_back (-1 );
1329+ ast_impl.source_info ().mutable_line_offsets ().push_back (2 );
1330+
1331+ ASSERT_OK_AND_ASSIGN (ValidationResult result, impl.Check (std::move (ast)));
1332+
1333+ EXPECT_FALSE (result.IsValid ());
1334+ ASSERT_THAT (result.GetIssues (), SizeIs (1 ));
1335+
1336+ EXPECT_EQ (result.GetIssues ()[0 ].ToDisplayString (*source),
1337+ " ERROR: <input>:-1:-1: undeclared reference to 'foo' (in "
1338+ " container '')" );
1339+ }
1340+ }
1341+
12841342TEST (TypeCheckerImplTest, ContainerLookupForMessageCreation) {
12851343 TypeCheckEnv env (GetSharedTestingDescriptorPool ());
12861344 env.set_container (" google.protobuf" );
0 commit comments