summaryrefslogtreecommitdiff
path: root/src/cmdline/mocks/terminal.cc
blob: e51fcd5cbbc6d27b4716f1a6411d210c0544ab9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/** \file terminal.cc */

// Copyright (C) 2010 Daniel Burrows
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING.  If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.


// Local includes:
#include "terminal.h"

using testing::AnyNumber;
using testing::Invoke;
using testing::NiceMock;
using testing::Return;
using testing::StrictMock;
using testing::_;

namespace aptitude
{
  namespace cmdline
  {
    namespace mocks
    {
      terminal_input::terminal_input()
      {
        // Make sure the program doesn't abort if there's an
        // unexpected invocation of prompt_for_input().
        ON_CALL(*this, prompt_for_input(_))
          .WillByDefault(Return(std::wstring()));
      }

      terminal_locale::terminal_locale()
      {
        EXPECT_CALL(*this, wcwidth(_))
          .WillRepeatedly(Return(1));
      }

      terminal_metrics::terminal_metrics()
      {
      }

      terminal_output::terminal_output()
      {
      }

      class combining_terminal_output::impl : public combining_terminal_output
      {
	friend std::shared_ptr<impl> std::make_shared<impl>();
        impl();

        std::wstring pending_writes;

        void do_write_text(const std::wstring &s);

        friend class testing::NiceMock<impl>;
        friend class testing::StrictMock<impl>;

      public:
        void write_text(const std::wstring &s);
        void move_to_beginning_of_line();
        void flush();

        static std::shared_ptr<impl> create_default();
        static std::shared_ptr<impl> create_nice();
        static std::shared_ptr<impl> create_strict();
      };

      combining_terminal_output::impl::impl()
      {
      }

      void combining_terminal_output::impl::do_write_text(const std::wstring &s)
      {
        std::wstring::size_type start = 0;
        for(std::wstring::size_type nl = s.find('\n', start);
            nl != s.npos; nl = s.find('\n', start))
          {
            pending_writes.append(s, start, (nl - start) + 1);
            start = nl + 1;

            output(pending_writes);
            pending_writes.clear();
          }

        pending_writes.append(s, start, s.npos);
      }

      void combining_terminal_output::impl::write_text(const std::wstring &s)
      {
        do_write_text(s);
      }

      void combining_terminal_output::impl::move_to_beginning_of_line()
      {
        do_write_text(L"\r");
      }

      void combining_terminal_output::impl::flush()
      {
        if(!pending_writes.empty())
          {
            output(pending_writes);
            pending_writes.clear();
          }
      }

      terminal_with_combined_output::terminal_with_combined_output()
      {
      }

      terminal_with_combined_output::~terminal_with_combined_output()
      {
      }

      combining_terminal_output::combining_terminal_output()
      {
      }

      std::shared_ptr<combining_terminal_output::impl> combining_terminal_output::impl::create_default()
      {
        return std::shared_ptr<combining_terminal_output::impl>();
      }

      std::shared_ptr<combining_terminal_output::impl> combining_terminal_output::impl::create_nice()
      {
        return std::make_shared<NiceMock<combining_terminal_output::impl> >();
      }

      std::shared_ptr<combining_terminal_output::impl> combining_terminal_output::impl::create_strict()
      {
        return std::make_shared<StrictMock<combining_terminal_output::impl> >();
      }

      std::shared_ptr<combining_terminal_output> combining_terminal_output::create_default()
      {
        return impl::create_default();
      }

      std::shared_ptr<combining_terminal_output> combining_terminal_output::create_nice()
      {
        return impl::create_nice();
      }

      std::shared_ptr<combining_terminal_output> combining_terminal_output::create_strict()
      {
        return impl::create_strict();
      }
    }
  }
}