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/fortune_rewriter.h"
31 
32 #include <cstddef>
33 #include <string>
34 
35 #include "base/logging.h"
36 #include "base/system_util.h"
37 #include "converter/segments.h"
38 #include "request/conversion_request.h"
39 #include "testing/base/public/gunit.h"
40 
41 DECLARE_string(test_tmpdir);
42 
43 namespace mozc {
44 namespace {
45 
AddSegment(const string & key,const string & value,Segments * segments)46 void AddSegment(const string &key, const string &value,
47                 Segments *segments) {
48   segments->Clear();
49   Segment *seg = segments->push_back_segment();
50   seg->set_key(key);
51   Segment::Candidate *candidate = seg->add_candidate();
52   candidate->Init();
53   candidate->value = key;
54   candidate->content_key = key;
55   candidate->content_value = value;
56 }
57 
HasFortune(const Segments & segments)58 bool HasFortune(const Segments &segments) {
59   CHECK_EQ(segments.segments_size(), 1);
60   for (size_t i = 0; i < segments.segment(0).candidates_size(); ++i) {
61     const Segment::Candidate &candidate = segments.segment(0).candidate(i);
62     if ("今日の運勢" == candidate.description) {
63       // has valid value?
64       if ("大吉" == candidate.value ||
65           "吉" == candidate.value ||
66           "中吉" == candidate.value ||
67           "小吉" == candidate.value ||
68           "末吉" == candidate.value ||
69           "凶" == candidate.value) {
70         return true;
71       }
72     }
73   }
74   return false;
75 }
76 
77 class FortuneRewriterTest : public ::testing::Test {
78  protected:
SetUp()79   void SetUp() override {
80     SystemUtil::SetUserProfileDirectory(FLAGS_test_tmpdir);
81   }
82 };
83 
TEST_F(FortuneRewriterTest,BasicTest)84 TEST_F(FortuneRewriterTest, BasicTest) {
85   FortuneRewriter fortune_rewriter;
86   const ConversionRequest request;
87 
88   Segments segments;
89   AddSegment("test", "test", &segments);
90   fortune_rewriter.Rewrite(request, &segments);
91   EXPECT_FALSE(HasFortune(segments));
92 
93   AddSegment("おみくじ", "test", &segments);
94   fortune_rewriter.Rewrite(request, &segments);
95   EXPECT_TRUE(HasFortune(segments));
96 }
97 
98 }  // namespace
99 }  // namespace mozc
100