Skip to content

Commit e1c2bec

Browse files
Add --test mode to session_handler_main.
* When `--test` mode is enabled, it returns 0 when all the tests are passed, and 1 when there are any failures. * Changed `ParseLine` to return a status code to manage the result of tests. * Changed the output format to indicate if the test case is `PASSED` or `FAILED`. Example: ``` session_handler_main --input data/test/session/scenario/transliterations.txt --profile /tmp/mozc --dictionary test --engine desktop --test ``` #codehealth PiperOrigin-RevId: 758061701
1 parent e947a4c commit e1c2bec

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

src/session/session_handler_main.cc

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
// session_handler_main --input input.txt --profile /tmp/mozc
3434
// --dictionary oss --engine desktop
3535
//
36+
// session_handler_main --test --input input.txt --profile /tmp/mozc
37+
//
3638
/* Example of input.txt (tsv format)
3739
# Enable IME
3840
SEND_KEY ON
@@ -60,25 +62,29 @@ SHOW_LOG_BY_VALUE ございました
6062
#include <utility>
6163
#include <vector>
6264

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"
6766
#include "absl/flags/flag.h"
6867
#include "absl/status/status.h"
6968
#include "absl/status/statusor.h"
7069
#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"
7174
#include "data_manager/oss/oss_data_manager.h"
7275
#include "data_manager/testing/mock_data_manager.h"
7376
#include "engine/engine.h"
7477
#include "protocol/candidate_window.pb.h"
7578
#include "protocol/commands.pb.h"
7679
#include "session/session_handler_tool.h"
7780

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. ");
7984
ABSL_FLAG(std::string, profile, "", "User profile directory");
8085
ABSL_FLAG(std::string, engine, "", "Conversion engine: 'mobile' or 'desktop'");
8186
ABSL_FLAG(std::string, dictionary, "", "Dictionary: 'oss' or 'test'");
87+
ABSL_FLAG(bool, test, false, "Run as a test and quit.");
8288

8389
namespace mozc {
8490
void Show(const commands::Output &output) {
@@ -110,44 +116,44 @@ void ShowLog(const commands::Output &output, const int cand_id) {
110116
}
111117
}
112118

113-
void ParseLine(session::SessionHandlerInterpreter &handler, std::string line) {
119+
bool ParseLine(session::SessionHandlerInterpreter &handler, std::string line) {
114120
std::vector<std::string> args = handler.Parse(line);
115121
if (args.empty()) {
116-
return;
122+
return true;
117123
}
118124
const std::string &command = args[0];
119125

120126
if (command == "SHOW_ALL") {
121127
std::cout << absl::StrCat(handler.LastOutput()) << std::endl;
122-
return;
128+
return true;
123129
}
124130
if (command == "SHOW_OUTPUT") {
125131
commands::Output output = handler.LastOutput();
126132
output.mutable_removed_candidate_words_for_debug()->Clear();
127133
std::cout << absl::StrCat(output.Utf8DebugString()) << std::endl;
128-
return;
134+
return true;
129135
}
130136
if (command == "SHOW_RESULT") {
131137
const commands::Output &output = handler.LastOutput();
132138
std::cout << absl::StrCat(output.result().Utf8DebugString()) << std::endl;
133-
return;
139+
return true;
134140
}
135141
if (command == "SHOW_CANDIDATES") {
136142
std::cout << absl::StrCat(
137143
handler.LastOutput().candidate_window().Utf8DebugString())
138144
<< std::endl;
139-
return;
145+
return true;
140146
}
141147
if (command == "SHOW_REMOVED_CANDIDATES") {
142148
std::cout << absl::StrCat(handler.LastOutput()
143149
.removed_candidate_words_for_debug()
144150
.Utf8DebugString())
145151
<< std::endl;
146-
return;
152+
return true;
147153
}
148154
if (command == "SHOW") {
149155
Show(handler.LastOutput());
150-
return;
156+
return true;
151157
}
152158
if (command == "SHOW_LOG") {
153159
uint32_t id;
@@ -156,27 +162,29 @@ void ParseLine(session::SessionHandlerInterpreter &handler, std::string line) {
156162
} else {
157163
std::cout << "ERROR: " << line << std::endl;
158164
}
159-
return;
165+
return false;
160166
}
161167
if (command == "SHOW_LOG_BY_VALUE") {
162168
if (args.size() != 2) {
163169
std::cout << "ERROR: " << line << std::endl;
164-
return;
170+
return false;
165171
}
166172
for (const uint32_t id : handler.GetCandidateIdsByValue(args[1])) {
167173
ShowLog(handler.LastOutput(), id);
168174
}
169175
for (const uint32_t id : handler.GetRemovedCandidateIdsByValue(args[1])) {
170176
ShowLog(handler.LastOutput(), id);
171177
}
172-
return;
178+
return true;
173179
}
174180

175181
const absl::Status status = handler.Eval(args);
176182
if (!status.ok()) {
177183
std::cout << "ERROR: " << line << std::endl;
178184
std::cout << "ERROR: " << status.message() << std::endl;
185+
return false;
179186
}
187+
return true;
180188
}
181189

182190
std::unique_ptr<const DataManager> CreateDataManager(
@@ -215,19 +223,44 @@ int main(int argc, char **argv) {
215223
if (!absl::GetFlag(FLAGS_profile).empty()) {
216224
mozc::SystemUtil::SetUserProfileDirectory(absl::GetFlag(FLAGS_profile));
217225
}
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);
220241
if (!engine.ok()) {
221242
std::cout << "engine init error" << std::endl;
222243
return 1;
223244
}
224245
mozc::session::SessionHandlerInterpreter handler(*std::move(engine));
225246

226247
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;
229252
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+
}
231264
}
232265
}
233266

0 commit comments

Comments
 (0)