From 2c20cf0157500b2310b220e771b583f3022d264b Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Sun, 14 May 2017 20:51:53 -0700 Subject: [PATCH] Add some partial update tests --- src/position.cc | 2 ++ src/position.h | 1 + src/query.cc | 75 ++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 68 insertions(+), 10 deletions(-) diff --git a/src/position.cc b/src/position.cc index 803e4f6b..ec3a0d3f 100644 --- a/src/position.cc +++ b/src/position.cc @@ -71,6 +71,8 @@ bool Position::operator<(const Position& that) const { Range::Range() {} +Range::Range(Position position) : Range(position, position) {} + Range::Range(Position start, Position end) : start(start), end(end) {} Range::Range(const char* encoded) { diff --git a/src/position.h b/src/position.h index fd3d576d..d933a84b 100644 --- a/src/position.h +++ b/src/position.h @@ -28,6 +28,7 @@ struct Range { Position end; Range(); + explicit Range(Position position); Range(Position start, Position end); explicit Range(const char* encoded); diff --git a/src/query.cc b/src/query.cc index 6cffb866..e409f648 100644 --- a/src/query.cc +++ b/src/query.cc @@ -3,6 +3,7 @@ #include "indexer.h" #include +#include #include #include @@ -755,17 +756,71 @@ void QueryDatabase::UpdateDetailedNames(size_t* qualified_name_index, SymbolKind } } -#if false -void DoTest() { - IndexFile previous("foo.cc"); - IndexFile current("foo.cc"); +TEST_SUITE("query"); + +IndexUpdate GetDelta(IndexFile previous, IndexFile current) { QueryDatabase db; - - IndexFunc* func = previous.Resolve(previous.ToFuncId("usr")); - - IdMap previous_map(&db, previous.id_cache); IdMap current_map(&db, current.id_cache); - IndexUpdate::CreateDelta(&previous_map, ¤t_map, &previous, ¤t); + return IndexUpdate::CreateDelta(&previous_map, ¤t_map, &previous, ¤t); } -#endif \ No newline at end of file + +TEST_CASE("remove defs") { + IndexFile previous("foo.cc"); + IndexFile current("foo.cc"); + + previous.ToTypeId("usr1"); + previous.ToFuncId("usr2"); + previous.ToVarId("usr3"); + + IndexUpdate update = GetDelta(previous, current); + + REQUIRE(update.types_removed == std::vector{ "usr1" }); + REQUIRE(update.funcs_removed == std::vector{ "usr2" }); + REQUIRE(update.vars_removed == std::vector{ "usr3" }); +} + +TEST_CASE("func callers") { + IndexFile previous("foo.cc"); + IndexFile current("foo.cc"); + + IndexFunc* pf = previous.Resolve(previous.ToFuncId("usr")); + IndexFunc* cf = current.Resolve(current.ToFuncId("usr")); + + pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)))); + cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)))); + + IndexUpdate update = GetDelta(previous, current); + + REQUIRE(update.funcs_removed == std::vector{}); + REQUIRE(update.funcs_callers.size() == 1); + REQUIRE(update.funcs_callers[0].id == QueryFuncId(0)); + REQUIRE(update.funcs_callers[0].to_remove.size() == 1); + REQUIRE(update.funcs_callers[0].to_remove[0].loc.range == Range(Position(1, 0))); + REQUIRE(update.funcs_callers[0].to_add.size() == 1); + REQUIRE(update.funcs_callers[0].to_add[0].loc.range == Range(Position(2, 0))); +} + +TEST_CASE("type usages") { + IndexFile previous("foo.cc"); + IndexFile current("foo.cc"); + + IndexType* pt = previous.Resolve(previous.ToTypeId("usr")); + IndexType* ct = current.Resolve(current.ToTypeId("usr")); + + pt->uses.push_back(Range(Position(1, 0))); + ct->uses.push_back(Range(Position(2, 0))); + + IndexUpdate update = GetDelta(previous, current); + + REQUIRE(update.types_removed == std::vector{}); + REQUIRE(update.types_def_update == std::vector{}); + REQUIRE(update.types_uses.size() == 1); + REQUIRE(update.types_uses[0].to_remove.size() == 1); + REQUIRE(update.types_uses[0].to_remove[0].range == Range(Position(1, 0))); + REQUIRE(update.types_uses[0].to_add.size() == 1); + REQUIRE(update.types_uses[0].to_add[0].range == Range(Position(2, 0))); +} + + +TEST_SUITE_END(); \ No newline at end of file