aboutsummaryrefslogtreecommitdiff
path: root/tests/pager/driver.cxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2016-05-15 17:11:27 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2016-05-31 18:42:55 +0300
commit61ef82ec2b2ca396667f92a4e5c6ceb729c42086 (patch)
tree57ca5868483f361a9da28bbfc32f0cc838787b3e /tests/pager/driver.cxx
parent79bb0331cb93a736193e733b5ae26d040931a1aa (diff)
Port to MinGW
Diffstat (limited to 'tests/pager/driver.cxx')
-rw-r--r--tests/pager/driver.cxx111
1 files changed, 111 insertions, 0 deletions
diff --git a/tests/pager/driver.cxx b/tests/pager/driver.cxx
new file mode 100644
index 0000000..cab3078
--- /dev/null
+++ b/tests/pager/driver.cxx
@@ -0,0 +1,111 @@
+// file : tests/pager/driver.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <vector>
+#include <string>
+#include <utility> // move()
+#include <cassert>
+#include <sstream>
+#include <iostream>
+
+#include <butl/pager>
+
+using namespace std;
+using namespace butl;
+
+int
+main (int argc, const char* argv[])
+{
+ bool child (false);
+ bool interactive (false);
+ string pgprog;
+ vector<string> pgopts;
+
+ assert (argc > 0);
+
+ int i (1);
+ for (; i != argc; ++i)
+ {
+ string v (argv[i]);
+ if (pgprog.empty ())
+ {
+ if (v == "-c")
+ child = true;
+ else if (v == "-i")
+ interactive = true;
+ else
+ {
+ pgprog = move (v);
+ interactive = true;
+ }
+ }
+ else
+ pgopts.emplace_back (move (v));
+ }
+
+ if (i != argc)
+ {
+ if (!child)
+ cerr << "usage: " << argv[0] << " [-c] [-i] [<pager> [<options>]]"
+ << endl;
+
+ return 1;
+ }
+
+ const char* s (R"delim(
+class fdstream_base
+{
+protected:
+ fdstream_base () = default;
+ fdstream_base (int fd): buf_ (fd) {}
+
+protected:
+ fdbuf buf_;
+};
+
+class ifdstream: fdstream_base, public std::istream
+{
+public:
+ ifdstream (): std::istream (&buf_) {}
+ ifdstream (int fd): fdstream_base (fd), std::istream (&buf_) {}
+
+ void close () {buf_.close ();}
+ void open (int fd) {buf_.open (fd);}
+ bool is_open () const {return buf_.is_open ();}
+};
+)delim");
+
+ if (child)
+ {
+ string il;
+ string ol;
+ istringstream is (s);
+ do
+ {
+ getline (cin, il);
+ getline (is, ol);
+ }
+ while (cin.good () && is.good () && il == ol);
+ return cin.eof () && !cin.bad () && is.eof () ? 0 : 1;
+ }
+
+ try
+ {
+ string prog (argv[0]);
+ vector<string> opts ({"-c"});
+
+ pager p ("pager test",
+ false,
+ interactive ? (pgprog.empty () ? nullptr : &pgprog) : &prog,
+ interactive ? (pgopts.empty () ? nullptr : &pgopts) : &opts);
+
+ p.stream () << s;
+
+ assert (p.wait ());
+ }
+ catch (const system_error&)
+ {
+ assert (false);
+ }
+}