1#  Copyright (c) 1997-2021
2#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
3#  Technische Universität Berlin, Germany
4#  https://polymake.org
5#
6#  This program is free software; you can redistribute it and/or modify it
7#  under the terms of the GNU General Public License as published by the
8#  Free Software Foundation; either version 2, or (at your option) any
9#  later version: http://www.gnu.org/licenses/gpl.txt.
10#
11#  This program is distributed in the hope that it will be useful,
12#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#  GNU General Public License for more details.
15#-------------------------------------------------------------------------------
16
17#
18#  Mock class for testing
19#
20
21package Polymake::InteractiveViewer::Mock;
22
23sub simulate {
24   my $logfile=shift;
25   open my $log, "<", $logfile or die "can't read input log file $logfile: $!\n";
26   my $port;
27   while (@_ && shift(@_) !~ /SelectorThread$/) { }
28   $port=shift or die "could not find port number among arguments\n";
29
30   my @sockets;
31   push @sockets, new ClientSocket("localhost", $port);
32
33   local $_;
34   local $/="\n";
35   while (<$log>) {
36      if (s/^(\d+)([<=>]) //) {
37         my $socket=$sockets[$1];
38         defined($socket) or die "logfile $logfile, line $.: non-existing channel number $1\n";
39
40         if ($2 eq ">") {
41            # print something
42            print $socket $_ or die "socket $1 closed prematurely\n";
43
44         } elsif ($2 eq "<") {
45            # read one line and match against the expression
46            chomp;
47            my $match=qr/$_/;
48            $_=readline $socket;
49            if ($_ =~ $match) {
50               if (defined ($port=$+{port})) {
51                  # open an additonal channel
52                  push @sockets, new ClientSocket("localhost", $port);
53               }
54            } else {
55               chomp;
56               die "consumed input '$_' did not match the expectation\n";
57            }
58
59         } else {
60            # read and discard the input until the matching line occurs
61            chomp;
62            my $match=qr/$_/;
63            while (defined($_ = readline $socket) && $_ !~ $match) {
64            }
65            defined($_) or die "expected string $match did not occur\n";
66         }
67      } else {
68         die "logfile $logfile, line $.: unrecognized command $_\n";
69      }
70   }
71}
72
731
74
75# Local Variables:
76# cperl-indent-level:3
77# indent-tabs-mode:nil
78# End:
79