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 // A mock class for testing.
31 //
32 // Note: Entries added by a method cannot be retrieved by other methods.
33 //       For example, even if you add an entry with a key "きょうと" and value
34 //       "京都" by AddLookupPrefix, you will get no results by calling
35 //       LookupReverse() with the key "京都" or by calling LookupPredictive()
36 //       with the key "きょう".
37 //       If you want results from a lookup method, you have to call the
38 //       corresponding Add...() method beforehand.
39 //       LookupPredictive() doesn't return results by any other strings other
40 //       than the one used in AddLookupPredictive(); If you have called
41 //       AddLookupPredictive("きょう", "きょうと", "京都"), you will get no
42 //       results by calling, for example, LookupPredictive("き", ...).
43 //       On the other hand, LookupPrefix() and LookupReverse() actually performs
44 //       prefix searches. If you have called
45 //       AddLookupPrefix("きょうと", "きょうと", "京都"), you will get a result
46 //       calling LookupPrefix("きょうとだいがく", ...).
47 //       Tokens looked up by these methods all have the "lid" and "rid" with the
48 //       value 1.
49 // TODO(manabe): An interface to change "lid"/"rid".
50 
51 #ifndef MOZC_DICTIONARY_DICTIONARY_MOCK_H_
52 #define MOZC_DICTIONARY_DICTIONARY_MOCK_H_
53 
54 #include <list>
55 #include <map>
56 #include <string>
57 #include <vector>
58 
59 #include "base/port.h"
60 #include "base/string_piece.h"
61 #include "dictionary/dictionary_interface.h"
62 #include "dictionary/dictionary_token.h"
63 #include "request/conversion_request.h"
64 
65 namespace mozc {
66 namespace dictionary {
67 
68 class DictionaryMock : public DictionaryInterface {
69  public:
70   DictionaryMock();
71   virtual ~DictionaryMock();
72 
73   virtual bool HasKey(StringPiece key) const;
74 
75   virtual bool HasValue(StringPiece value) const;
76 
77   // DictionaryMock doesn't support a limitation.  Note also that only the
78   // tokens whose keys exactly match the registered key are looked up; see the
79   // comment of AddLookupPredictive.
80   virtual void LookupPredictive(StringPiece key,
81                                 const ConversionRequest &conversion_request,
82                                 Callback *callback) const;
83 
84   virtual void LookupPrefix(StringPiece key,
85                             const ConversionRequest &conversion_request,
86                             Callback *callback) const;
87 
88   virtual void LookupExact(StringPiece key,
89                            const ConversionRequest &conversion_request,
90                            Callback *callback) const;
91 
92   // For reverse lookup, the reading is stored in Token::value and the word
93   // is stored in Token::key.
94   virtual void LookupReverse(StringPiece str,
95                              const ConversionRequest &conversion_request,
96                              Callback *callback) const;
97 
98   // Adds a string-result pair to the predictive search result.
99   // LookupPrefix will return the result only when the search key exactly
100   // matches the string registered by this function.
101   // Note that str and key are not necessarily the same, as in the case of the
102   // spelling correction. (This applies to other types of searches.)
103   void AddLookupPredictive(const string &str,
104                            const string &key, const string &value,
105                            Token::AttributesBitfield token_attributes);
106 
107   // Adds a string-token pair to the prefix search result.
108   // LookupPrefix will return the result when the left part of the search key
109   // partially matches the string registered by this function.
110   void AddLookupPrefix(const string &str,
111                        const string &key, const string &value,
112                        Token::AttributesBitfield token_attributes);
113 
114   // Adds a string-token pair to the reverse search result.
115   // Same as AddLookupPrefix, but they have different dictionaries
116   // internally.
117   void AddLookupReverse(const string &str,
118                         const string &key, const string &value,
119                         Token::AttributesBitfield token_attributes);
120 
121   // Adds a string-token pair to the exact search result.
122   // Same as AddLookupPrefix, but they have different dictionaries
123   // internally.
124   void AddLookupExact(const string &str,
125                       const string &key, const string &value,
126                       Token::AttributesBitfield token_attributes);
127 
128  private:
129   std::map<string, std::vector<Token *>> reverse_dictionary_;
130   std::map<string, std::vector<Token *>> prefix_dictionary_;
131   std::map<string, std::vector<Token *>> exact_dictionary_;
132   std::map<string, std::vector<Token *>> predictive_dictionary_;
133 
134   DISALLOW_COPY_AND_ASSIGN(DictionaryMock);
135 };
136 
137 }  // namespace dictionary
138 }  // namespace mozc
139 
140 #endif  // MOZC_DICTIONARY_DICTIONARY_MOCK_H_
141