1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "rewriter/normalization_rewriter.h"
31
32 #include <string>
33
34 #include "base/system_util.h"
35 #include "converter/segments.h"
36 #include "request/conversion_request.h"
37 #include "testing/base/public/gunit.h"
38
39 DECLARE_string(test_tmpdir);
40
41 namespace mozc {
42 namespace {
43
AddSegment(const string & key,const string & value,Segments * segments)44 void AddSegment(const string &key, const string &value,
45 Segments *segments) {
46 segments->Clear();
47 Segment *seg = segments->push_back_segment();
48 seg->set_key(key);
49 Segment::Candidate *candidate = seg->add_candidate();
50 candidate->Init();
51 candidate->value = value;
52 candidate->content_value = value;
53 }
54
55 class NormalizationRewriterTest : public ::testing::Test {
56 protected:
57 NormalizationRewriterTest() = default;
58 ~NormalizationRewriterTest() override = default;
59
SetUp()60 void SetUp() override {
61 SystemUtil::SetUserProfileDirectory(FLAGS_test_tmpdir);
62 }
63 };
64
TEST_F(NormalizationRewriterTest,NormalizationTest)65 TEST_F(NormalizationRewriterTest, NormalizationTest) {
66 NormalizationRewriter normalization_rewriter;
67 Segments segments;
68 const ConversionRequest request;
69
70 segments.Clear();
71 AddSegment("test", "test", &segments);
72 EXPECT_FALSE(normalization_rewriter.Rewrite(request, &segments));
73 EXPECT_EQ("test", segments.segment(0).candidate(0).value);
74
75 segments.Clear();
76 AddSegment("きょうと", "京都", &segments);
77 EXPECT_FALSE(normalization_rewriter.Rewrite(request, &segments));
78 EXPECT_EQ("京都", segments.segment(0).candidate(0).value);
79
80 // Wave dash (U+301C)
81 segments.Clear();
82 AddSegment("なみ", "〜", &segments);
83 #ifdef OS_WIN
84 EXPECT_TRUE(normalization_rewriter.Rewrite(request, &segments));
85 // U+FF5E
86 EXPECT_EQ("~", segments.segment(0).candidate(0).value);
87 #else
88 EXPECT_FALSE(normalization_rewriter.Rewrite(request, &segments));
89 // U+301C
90 EXPECT_EQ("〜", segments.segment(0).candidate(0).value);
91 #endif
92
93 // not normalized.
94 segments.Clear();
95 // U+301C
96 AddSegment("なみ", "〜", &segments);
97 segments.mutable_segment(0)->mutable_candidate(0)->attributes |=
98 Segment::Candidate::USER_DICTIONARY;
99 EXPECT_FALSE(normalization_rewriter.Rewrite(request, &segments));
100 // U+301C
101 EXPECT_EQ("〜", segments.segment(0).candidate(0).value);
102 }
103
104 } // namespace
105 } // namespace mozc
106