1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
// Copyright (C) 2018 ycmd contributors
//
// This file is part of ycmd.
//
// ycmd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ycmd is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ycmd. If not, see <http://www.gnu.org/licenses/>.
#include "Character.h"
#include "Repository.h"
#include "CodePoint.h"
#include "TestUtils.h"
#include <array>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using ::testing::TestWithParam;
using ::testing::ValuesIn;
namespace YouCompleteMe {
// Check that characters equalities and inequalities are symmetric (a == b if
// and only if b == a).
MATCHER( CharactersAreEqual, "" ) {
for ( size_t i = 0; i < arg.size() - 1; ++i ) {
for ( size_t j = i + 1; j < arg.size(); ++j ) {
if ( !( *arg[ i ] == *arg[ j ] ) || !( *arg[ j ] == *arg[ i ] ) ) {
return false;
}
}
}
return true;
}
MATCHER( CharactersAreNotEqual, "" ) {
for ( size_t i = 0; i < arg.size() - 1; ++i ) {
for ( size_t j = i + 1; j < arg.size(); ++j ) {
if ( *arg[ i ] == *arg[ j ] || *arg[ j ] == *arg[ i ] ) {
return false;
}
}
}
return true;
}
MATCHER( CharactersAreEqualWhenCaseIsIgnored, "" ) {
for ( size_t i = 0; i < arg.size() - 1; ++i ) {
for ( size_t j = i + 1; j < arg.size(); ++j ) {
if ( !( arg[ i ]->EqualsIgnoreCase( *arg[ j ] ) ) ||
!( arg[ j ]->EqualsIgnoreCase( *arg[ i ] ) ) ) {
return false;
}
}
}
return true;
}
MATCHER( CharactersAreNotEqualWhenCaseIsIgnored, "" ) {
for ( size_t i = 0; i < arg.size() - 1; ++i ) {
for ( size_t j = i + 1; j < arg.size(); ++j ) {
if ( arg[ i ]->EqualsIgnoreCase( *arg[ j ] ) ||
arg[ j ]->EqualsIgnoreCase( *arg[ i ] ) ) {
return false;
}
}
}
return true;
}
MATCHER( BaseCharactersAreEqual, "" ) {
for ( size_t i = 0; i < arg.size() - 1; ++i ) {
for ( size_t j = i + 1; j < arg.size(); ++j ) {
if ( !( arg[ i ]->EqualsBase( *arg[ j ] ) ) ||
!( arg[ j ]->EqualsBase( *arg[ i ] ) ) ) {
return false;
}
}
}
return true;
}
MATCHER( BaseCharactersAreNotEqual, "" ) {
for ( size_t i = 0; i < arg.size() - 1; ++i ) {
for ( size_t j = i + 1; j < arg.size(); ++j ) {
if ( arg[ i ]->EqualsBase( *arg[ j ] ) ||
arg[ j ]->EqualsBase( *arg[ i ] ) ) {
return false;
}
}
}
return true;
}
struct TextCharacterPair {
const char* text;
CharacterTuple character_tuple;
};
std::ostream& operator<<( std::ostream& os,
const TextCharacterPair &pair ) {
os << "{ " << PrintToString( pair.text ) << ", "
<< PrintToString( pair.character_tuple ) << " }";
return os;
}
class CharacterTest : public TestWithParam< TextCharacterPair > {
protected:
CharacterTest()
: repo_( Repository< Character >::Instance() ) {
}
virtual void SetUp() {
repo_.ClearElements();
pair_ = GetParam();
}
Repository< Character > &repo_;
const char* text_;
TextCharacterPair pair_;
};
TEST( CharacterTest, ExceptionThrownWhenLeadingByteInCodePointIsInvalid ) {
try {
// Leading byte cannot start with bits '10'.
Character( NormalizeInput( "\xaf" ) );
FAIL() << "Expected UnicodeDecodeError exception.";
} catch ( const UnicodeDecodeError &error ) {
EXPECT_STREQ( error.what(), "Invalid leading byte in code point." );
} catch ( ... ) {
FAIL() << "Expected UnicodeDecodeError exception.";
}
}
TEST( CharacterTest, ExceptionThrownWhenCodePointIsOutOfBound ) {
try {
// Leading byte indicates a sequence of three bytes but only two are given.
Character( NormalizeInput( "\xe4\xbf" ) );
FAIL() << "Expected UnicodeDecodeError exception.";
} catch ( const UnicodeDecodeError &error ) {
EXPECT_STREQ( error.what(), "Invalid code point length." );
} catch ( ... ) {
FAIL() << "Expected UnicodeDecodeError exception.";
}
}
TEST_P( CharacterTest, PropertiesAreCorrect ) {
EXPECT_THAT( Character( NormalizeInput( pair_.text ) ),
IsCharacterWithProperties( pair_.character_tuple ) );
}
const std::array< TextCharacterPair, 13 > tests = { {
// Musical symbol eighth note (three code points)
{ "𝅘𝅥𝅮", { "𝅘𝅥𝅮", "𝅘", "𝅘𝅥𝅮", "𝅘𝅥𝅮", false, false, false, false } },
// Punctuations
// Fullwidth low line
{ "_", { "_", "_", "_", "_", true, false, true, false } },
// Wavy dash
{ "〰", { "〰", "〰", "〰", "〰", true, false, true, false } },
// Left floor
{ "⌊", { "⌊", "⌊", "⌊", "⌊", true, false, true, false } },
// Fullwidth right square bracket
{ "]", { "]", "]", "]", "]", true, false, true, false } },
{ "«", { "«", "«", "«", "«", true, false, true, false } },
// Right substitution bracket
{ "⸃", { "⸃", "⸃", "⸃", "⸃", true, false, true, false } },
// Large one dot over two dots punctuation
{ "𐬽", { "𐬽", "𐬽", "𐬽", "𐬽", true, false, true, false } },
// Letters
// Latin capital letter S with dot below and dot above (three code points)
{ "Ṩ", { "Ṩ", "s", "ṩ", "ṩ", false, true, false, true } },
// Greek small letter alpha with psili and varia and ypogegrammeni (four code
// points)
{ "ᾂ", { "ᾂ", "α", "ἂι", "ἊΙ", false, true, false, false } },
// Greek capital letter eta with dasia and perispomeni and prosgegrammeni
// (four code points)
{ "ᾟ", { "ᾟ", "η", "ἧι", "ἧΙ", false, true, false, true } },
// Hiragana voiced iteration mark (two code points)
{ "ゞ", { "ゞ", "ゝ", "ゞ", "ゞ", false, true, false, false } },
// Hebrew letter shin with Dagesh and Shin dot (three code points)
{ "שּׁ", { "שּׁ", "ש", "שּׁ", "שּׁ", false, true, false, false } }
} };
INSTANTIATE_TEST_SUITE_P( UnicodeTest, CharacterTest, ValuesIn( tests ) );
TEST( CharacterTest, Equality ) {
Repository< Character >& repo( Repository< Character >::Instance() );
// The lowercase of the Latin capital letter e with acute "É" (which can be
// represented as the Latin capital letter "E" plus the combining acute
// character) is the Latin small letter e with acute "é".
EXPECT_THAT( repo.GetElements( { NormalizeInput( "e" ),
NormalizeInput( "é" ),
NormalizeInput( "E" ),
NormalizeInput( "É" ) } ),
CharactersAreNotEqual() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "é" ),
NormalizeInput( "é" ) } ),
CharactersAreEqual() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "É" ),
NormalizeInput( "É" ) } ),
CharactersAreEqual() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "e" ),
NormalizeInput( "E" ) } ),
CharactersAreEqualWhenCaseIsIgnored() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "é" ),
NormalizeInput( "É" ),
NormalizeInput( "É" ) } ),
CharactersAreEqualWhenCaseIsIgnored() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "e" ),
NormalizeInput( "é" ),
NormalizeInput( "é" ),
NormalizeInput( "E" ),
NormalizeInput( "É" ),
NormalizeInput( "É" ) } ),
BaseCharactersAreEqual() );
// The Greek capital letter omega "Ω" is the same character as the ohm sign
// "Ω". The lowercase of both characters is the Greek small letter omega "ω".
EXPECT_THAT( repo.GetElements( { NormalizeInput( "Ω" ),
NormalizeInput( "Ω" ) } ),
CharactersAreEqual() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "ω" ),
NormalizeInput( "Ω" ),
NormalizeInput( "Ω" ) } ),
CharactersAreEqualWhenCaseIsIgnored() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "ω" ),
NormalizeInput( "Ω" ),
NormalizeInput( "Ω" ) } ),
BaseCharactersAreEqual() );
// The Latin capital letter a with ring above "Å" (which can be represented as
// the Latin capital letter "A" plus the combining ring above character) is
// the same character as the angstrom sign "Å". The lowercase of these
// characters is the Latin small letter a with ring above "å" (which can also
// be represented as the Latin small letter "a" plus the combining ring above
// character).
EXPECT_THAT( repo.GetElements( { NormalizeInput( "a" ),
NormalizeInput( "å" ),
NormalizeInput( "A" ),
NormalizeInput( "Å" ) } ),
CharactersAreNotEqual() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "å" ),
NormalizeInput( "å" ) } ),
CharactersAreEqual() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "Å" ),
NormalizeInput( "Å" ),
NormalizeInput( "Å" ) } ),
CharactersAreEqual() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "å" ),
NormalizeInput( "å" ),
NormalizeInput( "Å" ),
NormalizeInput( "Å" ),
NormalizeInput( "Å" ) } ),
CharactersAreEqualWhenCaseIsIgnored() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "a" ),
NormalizeInput( "å" ),
NormalizeInput( "å" ),
NormalizeInput( "A" ),
NormalizeInput( "Å" ),
NormalizeInput( "Å" ),
NormalizeInput( "Å" ) } ),
BaseCharactersAreEqual() );
// The uppercase of the Greek small letter sigma "σ" and Greek small letter
// final sigma "ς" is the Greek capital letter sigma "Σ".
EXPECT_THAT( repo.GetElements( { NormalizeInput( "σ" ),
NormalizeInput( "ς" ),
NormalizeInput( "Σ" ) } ),
CharactersAreNotEqual() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "σ" ),
NormalizeInput( "ς" ),
NormalizeInput( "Σ" ) } ),
CharactersAreEqualWhenCaseIsIgnored() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "σ" ),
NormalizeInput( "ς" ),
NormalizeInput( "Σ" ) } ),
BaseCharactersAreEqual() );
// The lowercase of the Greek capital theta symbol "ϴ" and capital letter
// theta "Θ" is the Greek small letter theta "θ". There is also the Greek
// theta symbol "ϑ" whose uppercase is "Θ".
EXPECT_THAT( repo.GetElements( { NormalizeInput( "θ" ),
NormalizeInput( "ϑ" ),
NormalizeInput( "ϴ" ),
NormalizeInput( "Θ" ) } ),
CharactersAreNotEqual() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "θ" ),
NormalizeInput( "ϑ" ),
NormalizeInput( "ϴ" ),
NormalizeInput( "Θ" ) } ),
CharactersAreEqualWhenCaseIsIgnored() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "θ" ),
NormalizeInput( "ϑ" ),
NormalizeInput( "ϴ" ),
NormalizeInput( "Θ" ) } ),
BaseCharactersAreEqual() );
// In the Latin alphabet, the uppercase of "i" (with a dot) is "I" (without a
// dot). However, in the Turkish alphabet (a variant of the Latin alphabet),
// there are two distinct versions of the letter "i":
// - "ı" (without a dot) whose uppercase is "I" (without a dot);
// - "i" (with a dot) whose uppercase is "İ" (with a dot), which can also be
// represented as the letter "I" plus the combining dot above character.
// Since our matching is language-independent, the Turkish form is ignored and
// the letter "ı" (without a dot) does not match "I" (without a dot) when the
// case is ignored. Similarly, "ı" plus the combining dot above character does
// not match "İ" (with a dot) or "I" plus the combining dot above character
// but "i" (with a dot) plus the combining dot above does.
EXPECT_THAT( repo.GetElements( { NormalizeInput( "i" ),
NormalizeInput( "I" ),
NormalizeInput( "ı" ),
NormalizeInput( "ı̇" ),
NormalizeInput( "i̇" ),
NormalizeInput( "İ" ) } ),
CharactersAreNotEqual() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "İ" ),
NormalizeInput( "İ" ) } ),
CharactersAreEqual() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "i" ),
NormalizeInput( "I" ) } ),
CharactersAreEqualWhenCaseIsIgnored() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "i̇" ),
NormalizeInput( "İ" ),
NormalizeInput( "İ" ) } ),
CharactersAreEqualWhenCaseIsIgnored() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "ı" ),
NormalizeInput( "ı̇" ),
NormalizeInput( "I" ),
NormalizeInput( "İ" ) } ),
CharactersAreNotEqualWhenCaseIsIgnored() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "i" ),
NormalizeInput( "ı" ) } ),
BaseCharactersAreNotEqual() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "i" ),
NormalizeInput( "i̇" ),
NormalizeInput( "I" ),
NormalizeInput( "İ" ),
NormalizeInput( "İ" ) } ),
BaseCharactersAreEqual() );
EXPECT_THAT( repo.GetElements( { NormalizeInput( "ı" ),
NormalizeInput( "ı̇" ) } ),
BaseCharactersAreEqual() );
}
TEST( CharacterTest, SmartMatching ) {
// The letter "é" and "É" appear twice in the tests as they can be represented
// on one code point or two ("e"/"E" plus the combining acute character).
EXPECT_TRUE ( Character( NormalizeInput( "e" ) )
.MatchesSmart( Character( NormalizeInput( "e" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "e" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "e" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "e" ) )
.MatchesSmart( Character( NormalizeInput( "E" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "e" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "e" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "é" ) )
.MatchesSmart( Character( NormalizeInput( "e" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "é" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "é" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "é" ) )
.MatchesSmart( Character( NormalizeInput( "E" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "é" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "é" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "é" ) )
.MatchesSmart( Character( NormalizeInput( "e" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "é" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "é" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "é" ) )
.MatchesSmart( Character( NormalizeInput( "E" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "é" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "é" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "E" ) )
.MatchesSmart( Character( NormalizeInput( "e" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "E" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "E" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "E" ) )
.MatchesSmart( Character( NormalizeInput( "E" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "E" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "E" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "É" ) )
.MatchesSmart( Character( NormalizeInput( "e" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "É" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "É" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "É" ) )
.MatchesSmart( Character( NormalizeInput( "E" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "É" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "É" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "É" ) )
.MatchesSmart( Character( NormalizeInput( "e" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "É" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "É" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "É" ) )
.MatchesSmart( Character( NormalizeInput( "E" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "É" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
EXPECT_TRUE ( Character( NormalizeInput( "É" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "è" ) )
.MatchesSmart( Character( NormalizeInput( "e" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "è" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "è" ) )
.MatchesSmart( Character( NormalizeInput( "é" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "è" ) )
.MatchesSmart( Character( NormalizeInput( "E" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "è" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
EXPECT_FALSE( Character( NormalizeInput( "è" ) )
.MatchesSmart( Character( NormalizeInput( "É" ) ) ) );
}
} // namespace YouCompleteMe
|