aboutsummaryrefslogtreecommitdiff
path: root/tests/pager
diff options
context:
space:
mode:
Diffstat (limited to 'tests/pager')
-rw-r--r--tests/pager/buildfile7
-rw-r--r--tests/pager/driver.cxx111
2 files changed, 118 insertions, 0 deletions
diff --git a/tests/pager/buildfile b/tests/pager/buildfile
new file mode 100644
index 0000000..e42f3b0
--- /dev/null
+++ b/tests/pager/buildfile
@@ -0,0 +1,7 @@
+# file : tests/pager/buildfile
+# copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
+# license : MIT; see accompanying LICENSE file
+
+exe{driver}: cxx{driver} ../../butl/lib{butl}
+
+include ../../butl/
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);
+ }
+}