This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
#include <string>
#include <vector>
#include "base/format_macros.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/renderer/spellchecker/spellcheck_worditerator.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
struct TestCase {
const char* language;
bool allow_contraction;
const wchar_t* expected_words;
};
}
TEST(SpellcheckWordIteratorTest, SplitWord) {
const wchar_t kTestText[] =
L"!@#$%^&*()"
L"hello:hello a\xFB03x"
L"\x03B3\x03B5\x03B9\x03AC\x0020\x03C3\x03BF\x03C5"
L"\x0437\x0434\x0440\x0430\x0432\x0441\x0442\x0432"
L"\x0443\x0439\x0442\x0435"
L"\x05e9\x05c1\x05b8\x05dc\x05d5\x05b9\x05dd "
L"\x05e6\x0027\x05d9\x05e4\x05e1 \x05e6\x05F3\x05d9\x05e4\x05e1 "
L"\x05e6\x05d4\x0022\x05dc \x05e6\x05d4\x05f4\x05dc "
L"\"\x05e6\x05d4\x0022\x05dc\" '\x05e9\x05c1\x05b8\x05dc\x05d5'"
L"\x0627\x064e\x0644\x0633\x064e\x0651\x0644\x0627"
L"\x0645\x064f\x0020\x0639\x064e\x0644\x064e\x064a"
L"\x0652\x0643\x064f\x0645\x0652"
L"\x0930\x093E\x091C\x0927\x093E\x0928"
L"\x0e2a\x0e27\x0e31\x0e2a\x0e14\x0e35\x0020\x0e04"
L"\x0e23\x0e31\x0e1a"
L"\x3053\x3093\x306B\x3061\x306F"
L"\x4F60\x597D"
L"\xC548\xB155\xD558\xC138\xC694"
L"\xFF28\xFF45\xFF4C\xFF4C\xFF4F "
L"e.g.,";
static const TestCase kTestCases[] = {
{
"en-US", true, L"hello:hello affix Hello e.g"
}, {
"en-US", false, L"hello hello affix Hello e g"
}, {
"el-GR", true,
L"\x03B3\x03B5\x03B9\x03AC\x0020\x03C3\x03BF\x03C5"
}, {
"ru-RU", true,
L"\x0437\x0434\x0440\x0430\x0432\x0441\x0442\x0432"
L"\x0443\x0439\x0442\x0435"
}, {
"he-IL", true,
L"\x05e9\x05dc\x05d5\x05dd "
L"\x05e6\x0027\x05d9\x05e4\x05e1 \x05e6\x05F3\x05d9\x05e4\x05e1 "
L"\x05e6\x05d4\x0022\x05dc \x05e6\x05d4\x05f4\x05dc "
L"\x05e6\x05d4\x0022\x05dc \x05e9\x05dc\x05d5"
}, {
"ar", true,
L"\x0627\x0644\x0633\x0644\x0627\x0645\x0020\x0639"
L"\x0644\x064a\x0643\x0645"
}, {
"hi-IN", true,
L"\x0930\x093E\x091C\x0927\x093E\x0928"
}, {
"th-TH", true,
L"\x0e2a\x0e27\x0e31\x0e2a\x0e14\x0e35\x0020\x0e04"
L"\x0e23\x0e31\x0e1a"
}, {
"ko-KR", true,
L"\x110b\x1161\x11ab\x1102\x1167\x11bc\x1112\x1161"
L"\x1109\x1166\x110b\x116d"
},
};
for (size_t i = 0; i < arraysize(kTestCases); ++i) {
SCOPED_TRACE(base::StringPrintf("kTestCases[%" PRIuS "]: language=%s", i,
kTestCases[i].language));
SpellcheckCharAttribute attributes;
attributes.SetDefaultLanguage(kTestCases[i].language);
base::string16 input(base::WideToUTF16(kTestText));
SpellcheckWordIterator iterator;
EXPECT_TRUE(iterator.Initialize(&attributes,
kTestCases[i].allow_contraction));
EXPECT_TRUE(iterator.SetText(input.c_str(), input.length()));
std::vector<base::string16> expected_words;
base::SplitString(
base::WideToUTF16(kTestCases[i].expected_words), ' ', &expected_words);
base::string16 actual_word;
int actual_start, actual_end;
size_t index = 0;
while (iterator.GetNextWord(&actual_word, &actual_start, &actual_end)) {
EXPECT_TRUE(index < expected_words.size());
if (index < expected_words.size())
EXPECT_EQ(expected_words[index], actual_word);
++index;
}
}
}
TEST(SpellcheckWordIteratorTest, RuleSetConsistency) {
SpellcheckCharAttribute attributes;
attributes.SetDefaultLanguage("en-US");
const wchar_t kTestText[] = L"\x1791\x17c1\x002e";
base::string16 input(base::WideToUTF16(kTestText));
SpellcheckWordIterator iterator;
EXPECT_TRUE(iterator.Initialize(&attributes, true));
EXPECT_TRUE(iterator.SetText(input.c_str(), input.length()));
base::string16 actual_word;
int actual_start, actual_end;
EXPECT_FALSE(iterator.GetNextWord(&actual_word, &actual_start, &actual_end));
EXPECT_EQ(0, actual_start);
EXPECT_EQ(0, actual_end);
}
TEST(SpellcheckWordIteratorTest, TreatNumbersAsWordCharacters) {
static const struct {
const char* language;
const wchar_t* text;
bool left_to_right;
} kTestCases[] = {
{
"en-US", L"0123456789" L"a", true,
}, {
"el-GR", L"0123456789" L"\x03B1", true,
}, {
"ru-RU", L"0123456789" L"\x0430", true,
}, {
"he-IL", L"0123456789" L"\x05D0", false,
}, {
"ar", L"0123456789" L"\x0627", false,
}, {
"hi-IN", L"0123456789" L"\x0905", true,
}, {
"th-TH", L"0123456789" L"\x0e01", true,
}, {
"ko-KR", L"0123456789" L"\x1100\x1161", true,
},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
SCOPED_TRACE(base::StringPrintf("kTestCases[%" PRIuS "]: language=%s", i,
kTestCases[i].language));
SpellcheckCharAttribute attributes;
attributes.SetDefaultLanguage(kTestCases[i].language);
base::string16 input_word(base::WideToUTF16(kTestCases[i].text));
SpellcheckWordIterator iterator;
EXPECT_TRUE(iterator.Initialize(&attributes, true));
EXPECT_TRUE(iterator.SetText(input_word.c_str(), input_word.length()));
base::string16 actual_word;
int actual_start, actual_end;
EXPECT_TRUE(iterator.GetNextWord(&actual_word, &actual_start, &actual_end));
if (kTestCases[i].left_to_right)
EXPECT_EQ(input_word, actual_word);
else
EXPECT_NE(input_word, actual_word);
}
}
TEST(SpellcheckWordIteratorTest, Initialization) {
{
SpellcheckCharAttribute attributes;
attributes.SetDefaultLanguage("en-US");
SpellcheckWordIterator iterator;
EXPECT_TRUE(iterator.Initialize(&attributes, true));
}
{
SpellcheckCharAttribute attributes;
SpellcheckWordIterator iterator;
EXPECT_FALSE(iterator.Initialize(&attributes, true));
}
}