Minimized the test code

Also, modified the Completer interface to facilitate the minimization of the
test code.
This commit is contained in:
Strahinja Val Markovic 2012-05-07 22:10:28 -07:00
parent 6430677f4f
commit 0110611996
4 changed files with 117 additions and 141 deletions

View File

@ -53,14 +53,14 @@ void ThreadMain( TaskStack &task_stack )
} // unnamed namespace
Completer::Completer( const Pylist &candidates )
Completer::Completer( const std::vector< std::string > &candidates )
: threading_enabled_( false )
{
AddCandidatesToDatabase( candidates, "", "" );
}
Completer::Completer( const Pylist &candidates,
Completer::Completer( const std::vector< std::string > &candidates,
const std::string &filetype,
const std::string &filepath )
: threading_enabled_( false )
@ -91,18 +91,35 @@ void Completer::EnableThreading()
void Completer::AddCandidatesToDatabase( const Pylist &new_candidates,
const std::string &filetype,
const std::string &filepath )
{
int num_candidates = len( new_candidates );
std::vector< std::string > candidates;
candidates.reserve( num_candidates );
for (int i = 0; i < num_candidates; ++i)
{
candidates.push_back( extract< std::string >( new_candidates[ i ] ) );
}
AddCandidatesToDatabase( candidates, filetype, filepath );
}
void Completer::AddCandidatesToDatabase(
const std::vector< std::string > &new_candidates,
const std::string &filetype,
const std::string &filepath )
{
std::vector< Candidate *> &candidates =
GetCandidateVector( filetype, filepath );
int num_candidates = len( new_candidates );
int num_candidates = new_candidates.size();
candidates.clear();
candidates.reserve( num_candidates );
std::string candidate_text;
for (int i = 0; i < num_candidates; ++i)
{
candidate_text = extract< std::string >( new_candidates[ i ] );
const std::string &candidate_text = new_candidates[ i ];
Candidate *&candidate = GetValueElseInsert( candidate_repository_,
candidate_text, NULL );
if ( !candidate )
@ -113,24 +130,26 @@ void Completer::AddCandidatesToDatabase( const Pylist &new_candidates,
}
void Completer::CandidatesForQuery( const std::string &query,
Pylist &candidates ) const
std::vector< std::string > Completer::CandidatesForQuery(
const std::string &query ) const
{
CandidatesForQueryAndType( query, "", candidates );
return CandidatesForQueryAndType( query, "" );
}
void Completer::CandidatesForQueryAndType( const std::string &query,
const std::string &filetype,
Pylist &candidates ) const
std::vector< std::string > Completer::CandidatesForQueryAndType(
const std::string &query,
const std::string &filetype ) const
{
std::vector< Result > results;
ResultsForQueryAndType( query, filetype, results );
std::vector< std::string > candidates;
foreach ( const Result& result, results )
{
candidates.append( *result.Text() );
candidates.push_back( *result.Text() );
}
return candidates;
}
@ -156,6 +175,7 @@ Future Completer::CandidatesForQueryAndTypeAsync(
return Future( move( future ) );
}
AsyncResults Completer::ResultsForQueryAndType(
const std::string &query,
const std::string &filetype ) const
@ -165,6 +185,7 @@ AsyncResults Completer::ResultsForQueryAndType(
return results;
}
void Completer::ResultsForQueryAndType( const std::string &query,
const std::string &filetype,
std::vector< Result > &results ) const

View File

@ -56,25 +56,30 @@ class Completer : boost::noncopyable
{
public:
Completer() {}
Completer( const Pylist &candidates );
Completer( const Pylist &candidates,
Completer( const std::vector< std::string > &candidates );
Completer( const std::vector< std::string > &candidates,
const std::string &filetype,
const std::string &filepath );
~Completer();
void EnableThreading();
void AddCandidatesToDatabase(
const std::vector< std::string > &new_candidates,
const std::string &filetype,
const std::string &filepath );
void AddCandidatesToDatabase( const Pylist &new_candidates,
const std::string &filetype,
const std::string &filepath );
// Only provided for tests!
void CandidatesForQuery( const std::string &query,
Pylist &candidates ) const;
std::vector< std::string > CandidatesForQuery(
const std::string &query ) const;
void CandidatesForQueryAndType( const std::string &query,
const std::string &filetype,
Pylist &candidates ) const;
std::vector< std::string > CandidatesForQueryAndType(
const std::string &query,
const std::string &filetype ) const;
Future CandidatesForQueryAndTypeAsync( const std::string &query,
const std::string &filetype ) const;

View File

@ -30,9 +30,14 @@ BOOST_PYTHON_MODULE(indexer)
.def( "ResultsReady", &Future::ResultsReady )
.def( "GetResults", &Future::GetResults );
void (Completer::*actd) (const Pylist&,
const std::string&,
const std::string&) =
&Completer::AddCandidatesToDatabase;
class_< Completer, boost::noncopyable >( "Completer" )
.def( "EnableThreading", &Completer::EnableThreading )
.def( "AddCandidatesToDatabase", &Completer::AddCandidatesToDatabase )
.def( "AddCandidatesToDatabase", actd )
.def( "CandidatesForQueryAndTypeAsync",
&Completer::CandidatesForQueryAndTypeAsync );
}

View File

@ -30,19 +30,7 @@ namespace YouCompleteMe
namespace
{
std::vector<std::string> ToStringVector( const boost::python::list &pylist )
{
std::vector<std::string> values;
for (int i = 0; i < boost::python::len( pylist ); ++i)
{
values.push_back(
boost::python::extract< std::string >( pylist[ i ] ) );
}
return values;
}
Pylist Candidates( const std::string &a,
std::vector< std::string > Candidates( const std::string &a,
const std::string &b = std::string(),
const std::string &c = std::string(),
const std::string &d = std::string(),
@ -52,167 +40,124 @@ Pylist Candidates( const std::string &a,
const std::string &h = std::string(),
const std::string &i = std::string() )
{
Pylist candidates;
candidates.append( a );
std::vector< std::string > candidates;
candidates.push_back( a );
if ( !b.empty() )
candidates.append( b );
candidates.push_back( b );
if ( !c.empty() )
candidates.append( c );
candidates.push_back( c );
if ( !d.empty() )
candidates.append( d );
candidates.push_back( d );
if ( !e.empty() )
candidates.append( e );
candidates.push_back( e );
if ( !f.empty() )
candidates.append( f );
candidates.push_back( f );
if ( !g.empty() )
candidates.append( g );
candidates.push_back( g );
if ( !h.empty() )
candidates.append( h );
candidates.push_back( h );
if ( !i.empty() )
candidates.append( i );
candidates.push_back( i );
return candidates;
}
} // unnamed namespace
class CompleterTest : public ::testing::Test
{
protected:
virtual void SetUp()
{
Py_Initialize();
}
};
TEST_F( CompleterTest, OneCandidate )
TEST( CompleterTest, OneCandidate )
{
Pylist results;
Completer( Candidates( "foobar" ) ).CandidatesForQuery( "fbr", results );
EXPECT_THAT( ToStringVector( results ), ElementsAre( "foobar" ) );
EXPECT_THAT( Completer( Candidates(
"foobar" ) ).CandidatesForQuery( "fbr" ),
ElementsAre( "foobar" ) );
}
TEST_F( CompleterTest, ManyCandidateSimple )
TEST( CompleterTest, ManyCandidateSimple )
{
Pylist results;
Completer( Candidates(
EXPECT_THAT( Completer( Candidates(
"foobar",
"foobartest",
"Foobartest" ) ).CandidatesForQuery( "fbr", results );
EXPECT_THAT( ToStringVector( results ),
"Foobartest" ) ).CandidatesForQuery( "fbr" ),
WhenSorted( ElementsAre( "Foobartest",
"foobar",
"foobartest" ) ) );
}
TEST_F( CompleterTest, FirstCharSameAsQueryWins )
TEST( CompleterTest, FirstCharSameAsQueryWins )
{
Pylist results;
Completer( Candidates(
EXPECT_THAT( Completer( Candidates(
"foobar",
"afoobar" ) ).CandidatesForQuery( "fbr", results );
EXPECT_THAT( ToStringVector( results ),
"afoobar" ) ).CandidatesForQuery( "fbr" ),
ElementsAre( "foobar",
"afoobar" ) );
}
TEST_F( CompleterTest, CompleteMatchForWordBoundaryCharsWins )
TEST( CompleterTest, CompleteMatchForWordBoundaryCharsWins )
{
Pylist results;
Completer( Candidates(
EXPECT_THAT( Completer( Candidates(
"FooBarQux",
"FBaqux" ) ).CandidatesForQuery( "fbq", results );
EXPECT_THAT( ToStringVector( results ),
"FBaqux" ) ).CandidatesForQuery( "fbq" ),
ElementsAre( "FooBarQux",
"FBaqux" ) );
Pylist results2;
Completer( Candidates(
EXPECT_THAT( Completer( Candidates(
"CompleterTest",
"CompleteMatchForWordBoundaryCharsWins"
) ).CandidatesForQuery( "ct", results2 );
EXPECT_THAT( ToStringVector( results2 ),
"CompleteMatchForWordBoundaryCharsWins" ) )
.CandidatesForQuery( "ct" ),
ElementsAre( "CompleterTest",
"CompleteMatchForWordBoundaryCharsWins" ) );
Pylist results3;
Completer( Candidates(
EXPECT_THAT( Completer( Candidates(
"FooBar",
"FooBarRux" ) ).CandidatesForQuery( "fbr", results3 );
EXPECT_THAT( ToStringVector( results3 ),
"FooBarRux" ) ).CandidatesForQuery( "fbr" ),
ElementsAre( "FooBarRux",
"FooBar" ) );
}
TEST_F( CompleterTest, RatioUtilizationTieBreak )
TEST( CompleterTest, RatioUtilizationTieBreak )
{
Pylist results;
Completer( Candidates(
EXPECT_THAT( Completer( Candidates(
"FooBarQux",
"FooBarQuxZaa" ) ).CandidatesForQuery( "fbq", results );
EXPECT_THAT( ToStringVector( results ),
"FooBarQuxZaa" ) ).CandidatesForQuery( "fbq" ),
ElementsAre( "FooBarQux",
"FooBarQuxZaa" ) );
Pylist results2;
Completer( Candidates(
EXPECT_THAT( Completer( Candidates(
"FooBar",
"FooBarRux" ) ).CandidatesForQuery( "fba", results2 );
EXPECT_THAT( ToStringVector( results2 ),
"FooBarRux" ) ).CandidatesForQuery( "fba" ),
ElementsAre( "FooBar",
"FooBarRux" ) );
}
TEST_F( CompleterTest, QueryPrefixOfCandidateWins )
TEST( CompleterTest, QueryPrefixOfCandidateWins )
{
Pylist results;
Completer( Candidates(
EXPECT_THAT( Completer( Candidates(
"foobar",
"fbaroo" ) ).CandidatesForQuery( "foo", results );
EXPECT_THAT( ToStringVector( results ),
"fbaroo" ) ).CandidatesForQuery( "foo" ),
ElementsAre( "foobar",
"fbaroo" ) );
}
TEST_F( CompleterTest, ShorterCandidateWins )
TEST( CompleterTest, ShorterCandidateWins )
{
Pylist results;
Completer( Candidates(
EXPECT_THAT( Completer( Candidates(
"FooBarQux",
"FaBarQux" ) ).CandidatesForQuery( "fbq", results );
EXPECT_THAT( ToStringVector( results ),
"FaBarQux" ) ).CandidatesForQuery( "fbq" ),
ElementsAre( "FaBarQux",
"FooBarQux" ) );
Pylist results2;
Completer( Candidates(
EXPECT_THAT( Completer( Candidates(
"CompleterT",
"CompleterTest" ) ).CandidatesForQuery( "co", results2 );
EXPECT_THAT( ToStringVector( results2 ),
"CompleterTest" ) ).CandidatesForQuery( "co" ),
ElementsAre( "CompleterT",
"CompleterTest" ) );
}
TEST_F( CompleterTest, SameLowercaseCandidateWins )
TEST( CompleterTest, SameLowercaseCandidateWins )
{
Pylist results;
Completer( Candidates(
EXPECT_THAT( Completer( Candidates(
"foobar",
"Foobar" ) ).CandidatesForQuery( "foo", results );
EXPECT_THAT( ToStringVector( results ),
"Foobar" ) ).CandidatesForQuery( "foo" ),
ElementsAre( "foobar",
"Foobar" ) );
}