]> git.localhorst.tv Git - blank.git/blob - tst/integration/TestInstance.cpp
test for invoking with unknown argument
[blank.git] / tst / integration / TestInstance.cpp
1 #include "TestInstance.hpp"
2
3 #include <cppunit/extensions/HelperMacros.h>
4
5 using namespace std;
6
7
8 namespace blank {
9 namespace test {
10
11 namespace {
12
13 Process::Arguments combine_args(const TempDir &dir, const Process::Arguments &in, bool cmd) {
14         Process::Arguments out;
15         out.reserve(in.size() + (cmd ? 5 : 3));
16         out.emplace_back("blank");
17         out.insert(out.end(), in.begin(), in.end());
18         out.emplace_back("--save-path");
19         out.emplace_back(dir.Path());
20         if (cmd) {
21                 out.emplace_back("--cmd-port");
22                 out.emplace_back("12354");
23         }
24         return out;
25 }
26
27 }
28
29 TestInstance::TestInstance(const Process::Arguments &args, bool cmd)
30 : dir()
31 , proc(
32         "./blank" BLANK_SUFFIX,
33         combine_args(dir, args, cmd),
34         { })
35 , conn()
36 , out_buf()
37 , err_buf()
38 , cmd_buf() {
39         if (cmd) {
40                 // wait for command service startup
41                 // TODO: timeouts for reading from process
42                 WaitOutputLine("listening on TCP port 12354");
43                 // connect to command service
44                 conn = tcp::Socket("localhost", 12354);
45         }
46 }
47
48 TestInstance::~TestInstance() {
49         proc.Terminate();
50 }
51
52
53 void TestInstance::WriteInput(const string &data) {
54         const char *i = data.c_str();
55         const char *end = i + data.length();
56         while (i != end) {
57                 size_t len = proc.WriteIn(i, end - i);
58                 i += len;
59         }
60 }
61
62 void TestInstance::ReadOutputLine(string &line) {
63         while (!out_buf.Extract(line)) {
64                 // buffer exhausted, fetch more data
65                 out_buf.Update(proc.ReadOut(out_buf.WriteHead(), out_buf.Remain()));
66         }
67 }
68
69 void TestInstance::AssertOutputLine(const string &expected) {
70         string line;
71         ReadOutputLine(line);
72         CPPUNIT_ASSERT_EQUAL_MESSAGE(
73                 "unexpected line in stdout",
74                 expected, line);
75 }
76
77 void TestInstance::WaitOutputLine(const string &expected) {
78         string line;
79         while (true) {
80                 ReadOutputLine(line);
81                 if (line == expected) {
82                         return;
83                 }
84         }
85 }
86
87 void TestInstance::ReadErrorLine(string &line) {
88         while (!err_buf.Extract(line)) {
89                 // buffer exhausted, fetch more data
90                 err_buf.Update(proc.ReadErr(err_buf.WriteHead(), err_buf.Remain()));
91         }
92 }
93
94 void TestInstance::AssertErrorLine(const string &expected) {
95         string line;
96         ReadErrorLine(line);
97         CPPUNIT_ASSERT_EQUAL_MESSAGE(
98                 "unexpected line in stderr",
99                 expected, line);
100 }
101
102 void TestInstance::WaitErrorLine(const string &expected) {
103         string line;
104         while (true) {
105                 ReadErrorLine(line);
106                 if (line == expected) {
107                         return;
108                 }
109         }
110 }
111
112
113 void TestInstance::AssertExitStatus(int expected) {
114         CPPUNIT_ASSERT_EQUAL_MESSAGE(
115                 "unexpected line in stderr",
116                 expected, proc.Join());
117 }
118
119
120 void TestInstance::WaitCommandMessage(const string &line) {
121         WaitCommandLine(" > " + line);
122 }
123
124 void TestInstance::WaitCommandError(const string &line) {
125         WaitCommandLine(" ! " + line);
126 }
127
128 void TestInstance::WaitCommandBroadcast(const string &line) {
129         WaitCommandLine(" @ " + line);
130 }
131
132 void TestInstance::WaitCommandLine(const string &expected) {
133         string line;
134         while (true) {
135                 if (!cmd_buf.Extract(line)) {
136                         // buffer exhausted, fetch more data
137                         cmd_buf.Update(conn.Recv(cmd_buf.WriteHead(), cmd_buf.Remain()));
138                         continue;
139                 }
140                 if (line == expected) {
141                         return;
142                 }
143         }
144 }
145
146 }
147 }