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 "content/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 // Creates a human-readable hyphenated word. This function inserts '-' |
| 36 // characters to all places where we can insert hyphens to improve the |
| 37 // readability of this unit test. |
| 38 void Hyphenate(const string16& word, string16* hyphenated_word) { |
| 39 hyphenated_word->assign(word); |
| 40 size_t position = word.length(); |
| 41 while (position > 0) { |
| 42 size_t new_position = hyphenator_->ComputeLastHyphenLocation(word, |
| 43 position); |
| 44 ASSERT_LT(new_position, position); |
| 45 if (new_position > 0) |
| 46 hyphenated_word->insert(new_position, 1, '-'); |
| 47 position = new_position; |
| 48 } |
| 49 } |
| 50 |
| 51 private: |
| 52 scoped_ptr<Hyphenator> hyphenator_; |
| 53 }; |
| 54 |
| 55 // Verifies that our hyphenator yields the same hyphenated words as the original |
| 56 // hyphen library does. |
| 57 TEST_F(HyphenatorTest, HyphenateWords) { |
| 58 static const struct { |
| 59 const char* input; |
| 60 const char* expected; |
| 61 } kTestCases[] = { |
| 62 { "and", "and" }, |
| 63 { "concupiscent,", "con-cu-pis-cent," }, |
| 64 { "evidence.", "ev-i-dence." }, |
| 65 { "first", "first" }, |
| 66 { "getting", "get-ting" }, |
| 67 { "hedgehog", "hedge-hog" }, |
| 68 { "remarkable", "re-mark-able" }, |
| 69 { "straightened", "straight-ened" }, |
| 70 { "undid", "un-did" }, |
| 71 { "were", "were" }, |
| 72 { "Simply", "Sim-ply" }, |
| 73 { "Undone.", "Un-done." }, |
| 74 { "comfortably", "com-fort-ably"}, |
| 75 { "declination", "dec-li-na-tion" }, |
| 76 { "flamingo:", "flamin-go:" }, |
| 77 { "lination", "lina-tion" }, |
| 78 { "reciprocity", "rec-i-proc-i-ty" }, |
| 79 { "throughout", "through-out" }, |
| 80 { "undid", "un-did" }, |
| 81 { "undone.", "un-done." }, |
| 82 { "unnecessary", "un-nec-es-sary" }, |
| 83 }; |
| 84 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 85 string16 input = ASCIIToUTF16(kTestCases[i].input); |
| 86 string16 expected = ASCIIToUTF16(kTestCases[i].expected); |
| 87 string16 actual; |
| 88 Hyphenate(input, &actual); |
| 89 EXPECT_EQ(expected, actual); |
| 90 } |
| 91 } |
OLD | NEW |