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 ] ) ) ( 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 namespace YouCompleteMe
{ {
Completer::Completer( const Pylist &candidates )
{
AddCandidatesToDatabase( candidates );
}
Completer::~Completer() Completer::~Completer()
{ {
foreach ( Candidate* candidate, candidates_ ) foreach ( Candidate* candidate, candidates_ )

View File

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

View File

@ -27,14 +27,14 @@ namespace YouCompleteMe
namespace namespace
{ {
int NumWordBoundaryCharMatches( const std::string &text, int NumWordBoundaryCharMatches( const std::string &query,
const std::string &word_boundary_chars ) const std::string &word_boundary_chars )
{ {
int i = 0; int i = 0;
int j = 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; ++i;
++j; ++j;
} }
@ -69,7 +69,7 @@ Result::Result( bool is_subsequence,
text_( text ) text_( text )
{ {
if ( is_subsequence ) 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() ) if ( query.empty() || text_->empty() )
return; return;
first_char_same_in_query_and_text_ = query[ 0 ] == (*text_)[ 0 ]; first_char_same_in_query_and_text_ =
int num_wb_matches = NumWordBoundaryCharMatches( *text_, toupper( query[ 0 ] ) == toupper( (*text_)[ 0 ] );
int num_wb_matches = NumWordBoundaryCharMatches( query,
word_boundary_chars ); word_boundary_chars );
ratio_of_word_boundary_chars_in_query_ = ratio_of_word_boundary_chars_in_query_ =
num_wb_matches / static_cast< double >( query.length() ); 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; 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 } // unnamed namespace
class CompleterTest : public ::testing::Test class CompleterTest : public ::testing::Test
@ -56,30 +88,19 @@ class CompleterTest : public ::testing::Test
TEST_F( CompleterTest, OneCandidate ) TEST_F( CompleterTest, OneCandidate )
{ {
Pylist candidates;
candidates.append( "foobar" );
Completer completer;
completer.AddCandidatesToDatabase( candidates );
Pylist results; Pylist results;
completer.GetCandidatesForQuery( "fbr", results ); Completer( Candidates( "foobar" ) ).GetCandidatesForQuery( "fbr", results );
EXPECT_THAT( ToStringVector( results ), ElementsAre( "foobar" ) ); EXPECT_THAT( ToStringVector( results ), ElementsAre( "foobar" ) );
} }
TEST_F( CompleterTest, ManyCandidateSimple ) TEST_F( CompleterTest, ManyCandidateSimple )
{ {
Pylist candidates;
candidates.append( "foobar" );
candidates.append( "foobartest" );
candidates.append( "Foobartest" );
Completer completer;
completer.AddCandidatesToDatabase( candidates );
Pylist results; Pylist results;
completer.GetCandidatesForQuery( "fbr", results ); Completer( Candidates(
"foobar",
"foobartest",
"Foobartest" ) ).GetCandidatesForQuery( "fbr", results );
EXPECT_THAT( ToStringVector( results ), EXPECT_THAT( ToStringVector( results ),
WhenSorted( ElementsAre( "Foobartest", WhenSorted( ElementsAre( "Foobartest",
@ -89,15 +110,10 @@ TEST_F( CompleterTest, ManyCandidateSimple )
TEST_F( CompleterTest, FirstCharSameAsQueryWins ) TEST_F( CompleterTest, FirstCharSameAsQueryWins )
{ {
Pylist candidates;
candidates.append( "foobar" );
candidates.append( "afoobar" );
Completer completer;
completer.AddCandidatesToDatabase( candidates );
Pylist results; Pylist results;
completer.GetCandidatesForQuery( "fbr", results ); Completer( Candidates(
"foobar",
"afoobar" ) ).GetCandidatesForQuery( "fbr", results );
EXPECT_THAT( ToStringVector( results ), EXPECT_THAT( ToStringVector( results ),
ElementsAre( "foobar", ElementsAre( "foobar",
@ -106,19 +122,67 @@ TEST_F( CompleterTest, FirstCharSameAsQueryWins )
TEST_F( CompleterTest, CompleteMatchForWordBoundaryCharsWins ) TEST_F( CompleterTest, CompleteMatchForWordBoundaryCharsWins )
{ {
Pylist candidates;
candidates.append( "FooBarQux" );
candidates.append( "FBaqux" );
Completer completer;
completer.AddCandidatesToDatabase( candidates );
Pylist results; Pylist results;
completer.GetCandidatesForQuery( "fbq", results ); Completer( Candidates(
"FooBarQux",
"FBaqux" ) ).GetCandidatesForQuery( "fbq", results );
EXPECT_THAT( ToStringVector( results ), EXPECT_THAT( ToStringVector( results ),
ElementsAre( "FooBarQux", ElementsAre( "FooBarQux",
"FBaqux" ) ); "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 } // namespace YouCompleteMe