33
33
// session_handler_main --input input.txt --profile /tmp/mozc
34
34
// --dictionary oss --engine desktop
35
35
//
36
+ // session_handler_main --test --input input.txt --profile /tmp/mozc
37
+ //
36
38
/* Example of input.txt (tsv format)
37
39
# Enable IME
38
40
SEND_KEY ON
@@ -60,25 +62,29 @@ SHOW_LOG_BY_VALUE ございました
60
62
#include < utility>
61
63
#include < vector>
62
64
63
- #include " absl/strings/str_cat.h"
64
- #include " base/file_stream.h"
65
- #include " base/init_mozc.h"
66
- #include " base/system_util.h"
65
+ #include " absl/flags/declare.h"
67
66
#include " absl/flags/flag.h"
68
67
#include " absl/status/status.h"
69
68
#include " absl/status/statusor.h"
70
69
#include " absl/strings/numbers.h"
70
+ #include " absl/strings/str_cat.h"
71
+ #include " base/file_stream.h"
72
+ #include " base/init_mozc.h"
73
+ #include " base/system_util.h"
71
74
#include " data_manager/oss/oss_data_manager.h"
72
75
#include " data_manager/testing/mock_data_manager.h"
73
76
#include " engine/engine.h"
74
77
#include " protocol/candidate_window.pb.h"
75
78
#include " protocol/commands.pb.h"
76
79
#include " session/session_handler_tool.h"
77
80
78
- ABSL_FLAG (std::string, input, " " , " Input file" );
81
+ ABSL_DECLARE_FLAG (bool , use_history_rewriter);
82
+
83
+ ABSL_FLAG (std::string, input, " " , " Input file. " );
79
84
ABSL_FLAG (std::string, profile, " " , " User profile directory" );
80
85
ABSL_FLAG (std::string, engine, " " , " Conversion engine: 'mobile' or 'desktop'" );
81
86
ABSL_FLAG (std::string, dictionary, " " , " Dictionary: 'oss' or 'test'" );
87
+ ABSL_FLAG (bool , test, false , " Run as a test and quit." );
82
88
83
89
namespace mozc {
84
90
void Show (const commands::Output &output) {
@@ -110,44 +116,44 @@ void ShowLog(const commands::Output &output, const int cand_id) {
110
116
}
111
117
}
112
118
113
- void ParseLine (session::SessionHandlerInterpreter &handler, std::string line) {
119
+ bool ParseLine (session::SessionHandlerInterpreter &handler, std::string line) {
114
120
std::vector<std::string> args = handler.Parse (line);
115
121
if (args.empty ()) {
116
- return ;
122
+ return true ;
117
123
}
118
124
const std::string &command = args[0 ];
119
125
120
126
if (command == " SHOW_ALL" ) {
121
127
std::cout << absl::StrCat (handler.LastOutput ()) << std::endl;
122
- return ;
128
+ return true ;
123
129
}
124
130
if (command == " SHOW_OUTPUT" ) {
125
131
commands::Output output = handler.LastOutput ();
126
132
output.mutable_removed_candidate_words_for_debug ()->Clear ();
127
133
std::cout << absl::StrCat (output.Utf8DebugString ()) << std::endl;
128
- return ;
134
+ return true ;
129
135
}
130
136
if (command == " SHOW_RESULT" ) {
131
137
const commands::Output &output = handler.LastOutput ();
132
138
std::cout << absl::StrCat (output.result ().Utf8DebugString ()) << std::endl;
133
- return ;
139
+ return true ;
134
140
}
135
141
if (command == " SHOW_CANDIDATES" ) {
136
142
std::cout << absl::StrCat (
137
143
handler.LastOutput ().candidate_window ().Utf8DebugString ())
138
144
<< std::endl;
139
- return ;
145
+ return true ;
140
146
}
141
147
if (command == " SHOW_REMOVED_CANDIDATES" ) {
142
148
std::cout << absl::StrCat (handler.LastOutput ()
143
149
.removed_candidate_words_for_debug ()
144
150
.Utf8DebugString ())
145
151
<< std::endl;
146
- return ;
152
+ return true ;
147
153
}
148
154
if (command == " SHOW" ) {
149
155
Show (handler.LastOutput ());
150
- return ;
156
+ return true ;
151
157
}
152
158
if (command == " SHOW_LOG" ) {
153
159
uint32_t id;
@@ -156,27 +162,29 @@ void ParseLine(session::SessionHandlerInterpreter &handler, std::string line) {
156
162
} else {
157
163
std::cout << " ERROR: " << line << std::endl;
158
164
}
159
- return ;
165
+ return false ;
160
166
}
161
167
if (command == " SHOW_LOG_BY_VALUE" ) {
162
168
if (args.size () != 2 ) {
163
169
std::cout << " ERROR: " << line << std::endl;
164
- return ;
170
+ return false ;
165
171
}
166
172
for (const uint32_t id : handler.GetCandidateIdsByValue (args[1 ])) {
167
173
ShowLog (handler.LastOutput (), id);
168
174
}
169
175
for (const uint32_t id : handler.GetRemovedCandidateIdsByValue (args[1 ])) {
170
176
ShowLog (handler.LastOutput (), id);
171
177
}
172
- return ;
178
+ return true ;
173
179
}
174
180
175
181
const absl::Status status = handler.Eval (args);
176
182
if (!status.ok ()) {
177
183
std::cout << " ERROR: " << line << std::endl;
178
184
std::cout << " ERROR: " << status.message () << std::endl;
185
+ return false ;
179
186
}
187
+ return true ;
180
188
}
181
189
182
190
std::unique_ptr<const DataManager> CreateDataManager (
@@ -215,19 +223,44 @@ int main(int argc, char **argv) {
215
223
if (!absl::GetFlag (FLAGS_profile).empty ()) {
216
224
mozc::SystemUtil::SetUserProfileDirectory (absl::GetFlag (FLAGS_profile));
217
225
}
218
- auto engine = mozc::CreateEngine (absl::GetFlag (FLAGS_engine),
219
- absl::GetFlag (FLAGS_dictionary));
226
+
227
+ std::string engine_name = absl::GetFlag (FLAGS_engine);
228
+ std::string dictionary_name = absl::GetFlag (FLAGS_dictionary);
229
+ const bool is_test = absl::GetFlag (FLAGS_test);
230
+ if (is_test) {
231
+ absl::SetFlag (&FLAGS_use_history_rewriter, true );
232
+ if (engine_name.empty ()) {
233
+ engine_name = " desktop" ;
234
+ }
235
+ if (dictionary_name.empty ()) {
236
+ dictionary_name = " mock" ;
237
+ }
238
+ }
239
+
240
+ auto engine = mozc::CreateEngine (engine_name, dictionary_name);
220
241
if (!engine.ok ()) {
221
242
std::cout << " engine init error" << std::endl;
222
243
return 1 ;
223
244
}
224
245
mozc::session::SessionHandlerInterpreter handler (*std::move (engine));
225
246
226
247
std::string line;
227
- if (!absl::GetFlag (FLAGS_input).empty ()) {
228
- mozc::InputFileStream input (absl::GetFlag (FLAGS_input));
248
+ if (const std::string filepath = absl::GetFlag (FLAGS_input);
249
+ !filepath.empty ()) {
250
+ mozc::InputFileStream input (filepath);
251
+ bool is_passed = true ;
229
252
while (std::getline (input, line)) {
230
- mozc::ParseLine (handler, line);
253
+ is_passed &= mozc::ParseLine (handler, line);
254
+ }
255
+
256
+ if (is_test) {
257
+ if (is_passed) {
258
+ std::cout << " [ PASSED ] " << filepath << std::endl;
259
+ return 0 ;
260
+ } else {
261
+ std::cout << " [ FAILED ] " << filepath << std::endl;
262
+ return 1 ;
263
+ }
231
264
}
232
265
}
233
266
0 commit comments