OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/renderer/hyphenator/hyphenator.h" |
| 6 |
| 7 #include "base/path_service.h" |
| 8 #include "base/platform_file.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "third_party/hyphen/hyphen.h" |
| 12 |
| 13 // A unit test for our hyphenator. This class loads a sample hyphenation |
| 14 // dictionary and hyphenates words. |
| 15 class HyphenatorTest : public testing::Test { |
| 16 public: |
| 17 HyphenatorTest() { |
| 18 Initialize(); |
| 19 } |
| 20 |
| 21 bool Initialize() { |
| 22 FilePath dictionary_path; |
| 23 if (!PathService::Get(base::DIR_SOURCE_ROOT, &dictionary_path)) |
| 24 return false; |
| 25 dictionary_path = dictionary_path.AppendASCII("third_party"); |
| 26 dictionary_path = dictionary_path.AppendASCII("hyphen"); |
| 27 dictionary_path = dictionary_path.AppendASCII("hyph_en_US.dic"); |
| 28 base::PlatformFile file = base::CreatePlatformFile( |
| 29 dictionary_path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
| 30 NULL, NULL); |
| 31 hyphenator_.reset(new Hyphenator(file)); |
| 32 return hyphenator_->Initialize(); |
| 33 } |
| 34 |
| 35 void Hyphenate(const string16& word, string16* hyphenated_word) { |
| 36 hyphenated_word->assign(word); |
| 37 size_t position = word.length(); |
| 38 while (position) { |
| 39 size_t new_position = hyphenator_->ComputeLastHyphenLocation(word, |
| 40 position); |
| 41 ASSERT_LT(new_position, position); |
| 42 if (new_position > 0) |
| 43 hyphenated_word->insert(new_position, 1, '-'); |
| 44 position = new_position; |
| 45 } |
| 46 } |
| 47 |
| 48 private: |
| 49 scoped_ptr<Hyphenator> hyphenator_; |
| 50 }; |
| 51 |
| 52 // Verifies that our hyphenator yields the same hyphenated words as the original |
| 53 // hyphen library does. |
| 54 TEST_F(HyphenatorTest, HyphenateWords) { |
| 55 static const struct { |
| 56 const char* input; |
| 57 const char* expected; |
| 58 } kTestCases[] = { |
| 59 { "and", "and" }, |
| 60 { "concupiscent,", "con-cu-pis-cent," }, |
| 61 { "evidence.", "ev-i-dence." }, |
| 62 { "first", "first" }, |
| 63 { "getting", "get-ting" }, |
| 64 { "hedgehog", "hedge-hog" }, |
| 65 { "remarkable", "re-mark-able" }, |
| 66 { "straightened", "straight-ened" }, |
| 67 { "undid", "un-did" }, |
| 68 { "were", "were" }, |
| 69 { "Simply", "Sim-ply" }, |
| 70 { "Undone.", "Un-done." }, |
| 71 { "comfortably", "com-fort-ably"}, |
| 72 { "declination", "dec-li-na-tion" }, |
| 73 { "flamingo:", "flamin-go:" }, |
| 74 { "lination", "lina-tion" }, |
| 75 { "reciprocity", "rec-i-proc-i-ty" }, |
| 76 { "throughout", "through-out" }, |
| 77 { "undid", "un-did" }, |
| 78 { "undone.", "un-done." }, |
| 79 { "unnecessary", "un-nec-es-sary" }, |
| 80 }; |
| 81 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 82 string16 input = ASCIIToUTF16(kTestCases[i].input); |
| 83 string16 expected = ASCIIToUTF16(kTestCases[i].expected); |
| 84 string16 actual; |
| 85 Hyphenate(input, &actual); |
| 86 EXPECT_EQ(expected, actual); |
| 87 } |
| 88 } |
OLD | NEW |