Bugfixes and more tests

This commit is contained in:
Strahinja Markovic 2012-04-15 20:10:39 -07:00
parent 86a22c5328
commit 820e2543b6
5 changed files with 110 additions and 39 deletions

View File

@ -36,7 +36,7 @@ std::string GetWordBoundaryChars( const std::string &text )
( i > 0 && text[ i - 1 ] == '_' && isalpha( text[ i ] ) )
)
{
result.push_back( tolower( text[ 0 ] ) );
result.push_back( tolower( text[ i ] ) );
}
}

View File

@ -21,6 +21,11 @@
namespace YouCompleteMe
{
Completer::Completer( const Pylist &candidates )
{
AddCandidatesToDatabase( candidates );
}
Completer::~Completer()
{
foreach ( Candidate* candidate, candidates_ )

View File

@ -37,6 +37,7 @@ class Completer
{
public:
Completer() {}
Completer( const Pylist &candidates );
~Completer();
void AddCandidatesToDatabase( const Pylist &candidates );

View File

@ -27,14 +27,14 @@ namespace YouCompleteMe
namespace
{
int NumWordBoundaryCharMatches( const std::string &text,
int NumWordBoundaryCharMatches( const std::string &query,
const std::string &word_boundary_chars )
{
int i = 0;
int j = 0;
while ( j < text.size() && i < word_boundary_chars.size() )
while ( j < query.size() && i < word_boundary_chars.size() )
{
if ( toupper( text[ j ] ) == toupper( word_boundary_chars[ i ] ) )
if ( toupper( query[ j ] ) == toupper( word_boundary_chars[ i ] ) )
++i;
++j;
}
@ -69,7 +69,7 @@ Result::Result( bool is_subsequence,
text_( text )
{
if ( is_subsequence )
SetResultFeaturesFromQuery( word_boundary_chars, query );
SetResultFeaturesFromQuery( query, word_boundary_chars );
}
@ -157,8 +157,9 @@ void Result::SetResultFeaturesFromQuery(
if ( query.empty() || text_->empty() )
return;
first_char_same_in_query_and_text_ = query[ 0 ] == (*text_)[ 0 ];
int num_wb_matches = NumWordBoundaryCharMatches( *text_,
first_char_same_in_query_and_text_ =
toupper( query[ 0 ] ) == toupper( (*text_)[ 0 ] );
int num_wb_matches = NumWordBoundaryCharMatches( query,
word_boundary_chars );
ratio_of_word_boundary_chars_in_query_ =
num_wb_matches / static_cast< double >( query.length() );

View File

@ -42,6 +42,38 @@ std::vector<std::string> ToStringVector( const boost::python::list &pylist )
return values;
}
Pylist Candidates( const std::string &a,
const std::string &b = std::string(),
const std::string &c = std::string(),
const std::string &d = std::string(),
const std::string &e = std::string(),
const std::string &f = std::string(),
const std::string &g = std::string(),
const std::string &h = std::string(),
const std::string &i = std::string() )
{
Pylist candidates;
candidates.append( a );
if ( !b.empty() )
candidates.append( b );
if ( !c.empty() )
candidates.append( c );
if ( !d.empty() )
candidates.append( d );
if ( !e.empty() )
candidates.append( e );
if ( !f.empty() )
candidates.append( f );
if ( !g.empty() )
candidates.append( g );
if ( !h.empty() )
candidates.append( h );
if ( !i.empty() )
candidates.append( i );
return candidates;
}
} // unnamed namespace
class CompleterTest : public ::testing::Test
@ -56,30 +88,19 @@ class CompleterTest : public ::testing::Test
TEST_F( CompleterTest, OneCandidate )
{
Pylist candidates;
candidates.append( "foobar" );
Completer completer;
completer.AddCandidatesToDatabase( candidates );
Pylist results;
completer.GetCandidatesForQuery( "fbr", results );
Completer( Candidates( "foobar" ) ).GetCandidatesForQuery( "fbr", results );
EXPECT_THAT( ToStringVector( results ), ElementsAre( "foobar" ) );
}
TEST_F( CompleterTest, ManyCandidateSimple )
{
Pylist candidates;
candidates.append( "foobar" );
candidates.append( "foobartest" );
candidates.append( "Foobartest" );
Completer completer;
completer.AddCandidatesToDatabase( candidates );
Pylist results;
completer.GetCandidatesForQuery( "fbr", results );
Completer( Candidates(
"foobar",
"foobartest",
"Foobartest" ) ).GetCandidatesForQuery( "fbr", results );
EXPECT_THAT( ToStringVector( results ),
WhenSorted( ElementsAre( "Foobartest",
@ -89,15 +110,10 @@ TEST_F( CompleterTest, ManyCandidateSimple )
TEST_F( CompleterTest, FirstCharSameAsQueryWins )
{
Pylist candidates;
candidates.append( "foobar" );
candidates.append( "afoobar" );
Completer completer;
completer.AddCandidatesToDatabase( candidates );
Pylist results;
completer.GetCandidatesForQuery( "fbr", results );
Completer( Candidates(
"foobar",
"afoobar" ) ).GetCandidatesForQuery( "fbr", results );
EXPECT_THAT( ToStringVector( results ),
ElementsAre( "foobar",
@ -106,19 +122,67 @@ TEST_F( CompleterTest, FirstCharSameAsQueryWins )
TEST_F( CompleterTest, CompleteMatchForWordBoundaryCharsWins )
{
Pylist candidates;
candidates.append( "FooBarQux" );
candidates.append( "FBaqux" );
Completer completer;
completer.AddCandidatesToDatabase( candidates );
Pylist results;
completer.GetCandidatesForQuery( "fbq", results );
Completer( Candidates(
"FooBarQux",
"FBaqux" ) ).GetCandidatesForQuery( "fbq", results );
EXPECT_THAT( ToStringVector( results ),
ElementsAre( "FooBarQux",
"FBaqux" ) );
Pylist results2;
Completer( Candidates(
"CompleterTest",
"CompleteMatchForWordBoundaryCharsWins"
) ).GetCandidatesForQuery( "ct", results2 );
EXPECT_THAT( ToStringVector( results2 ),
ElementsAre( "CompleterTest",
"CompleteMatchForWordBoundaryCharsWins" ) );
Pylist results3;
Completer( Candidates(
"FooBar",
"FooBarRux"
) ).GetCandidatesForQuery( "fbr", results3 );
EXPECT_THAT( ToStringVector( results3 ),
ElementsAre( "FooBarRux",
"FooBar" ) );
}
TEST_F( CompleterTest, RatioUtilizationTieBreak )
{
Pylist results;
Completer( Candidates(
"FooBarQux",
"FooBarQuxZaa" ) ).GetCandidatesForQuery( "fbq", results );
EXPECT_THAT( ToStringVector( results ),
ElementsAre( "FooBarQux",
"FooBarQuxZaa" ) );
}
TEST_F( CompleterTest, ShorterCandidateWins )
{
Pylist results;
Completer( Candidates(
"FooBarQux",
"FaBarQux" ) ).GetCandidatesForQuery( "fbq", results );
EXPECT_THAT( ToStringVector( results ),
ElementsAre( "FaBarQux",
"FooBarQux" ) );
Pylist results2;
Completer( Candidates(
"CompleterT",
"CompleterTest" ) ).GetCandidatesForQuery( "co", results2 );
EXPECT_THAT( ToStringVector( results2 ),
ElementsAre( "CompleterT",
"CompleterTest" ) );
}
} // namespace YouCompleteMe