-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompatcommand.cpp
162 lines (135 loc) · 3.09 KB
/
compatcommand.cpp
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
// Template for a test suite.
#include <config.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/Asserter.h>
#include "Asserts.h"
#include "TCLInterpreter.h"
#include "TCLProcessor.h"
#include "TCLResult.h"
#ifdef HAVE_STD_NAMESPACE
using namespace std;
#endif
/* The CTCLProcessor is an abstract base class. It is therefore necessary
to create a concrete class and instantiate it to do any testing.
This command will reverse its arguments (command name included).
*/
class Reverse : public CTCLProcessor
{
bool deleted;
bool preCalled;
bool postCalled;
public:
Reverse(CTCLInterpreter* pInterp, string command = string("reverse"));
~Reverse();
virtual int operator()(CTCLInterpreter& interp,
CTCLResult& result,
int argc, char** argv);
virtual void OnDelete();
bool isDeleted();
bool wasPreCalled();
bool wasPostCalled();
virtual void preCommand();
virtual void postCommand();
};
// Implementing the test class;
Reverse::Reverse(CTCLInterpreter* pInterp,
string command) :
CTCLProcessor(command, pInterp),
deleted(false),
preCalled(false),
postCalled(false)
{
Register();
}
Reverse::~Reverse()
{
Unregister();
}
int
Reverse::operator()(CTCLInterpreter& interp,
CTCLResult& result,
int argc, char** argv)
{
for(int i = (argc-1); i >= 0; i--) {
result.AppendElement(argv[i]);
}
return TCL_OK;
}
void
Reverse::OnDelete()
{
deleted = true;
}
bool
Reverse::isDeleted()
{
return deleted;
}
void
Reverse::preCommand()
{
preCalled = true;
}
void
Reverse::postCommand()
{
postCalled = true;
}
bool
Reverse::wasPreCalled()
{
return preCalled;
}
bool
Reverse::wasPostCalled()
{
return postCalled;
}
///// The tests.
class argvprocessor : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(argvprocessor);
CPPUNIT_TEST(construction);
CPPUNIT_TEST(invoke);
CPPUNIT_TEST(unregister);
CPPUNIT_TEST_SUITE_END();
private:
Tcl_Interp* m_pRawInterp;
CTCLInterpreter* m_pInterpreter;
Reverse* m_pCommand;
public:
void setUp() {
m_pRawInterp = Tcl_CreateInterp();
m_pInterpreter = new CTCLInterpreter(m_pRawInterp);
m_pCommand = new Reverse(m_pInterpreter);
m_pCommand->Register();
}
void tearDown() {
delete m_pCommand;
delete m_pInterpreter;
}
protected:
void construction();
void invoke();
void unregister();
};
CPPUNIT_TEST_SUITE_REGISTRATION(argvprocessor);
// Ensure that we can get this thing constructed.
void argvprocessor::construction() {
EQMSG("command name", string("reverse"), m_pCommand->getCommandName());
Tcl_CmdInfo info;
int status = Tcl_GetCommandInfo(m_pRawInterp, "reverse", &info);
ASSERT(status == 1); // It's been registered.
}
// See if we can invoke the command.../
void argvprocessor::invoke() {
string result = m_pInterpreter->Eval("reverse a b cd e");
EQ(string("e cd b a reverse"), result);
ASSERT(m_pCommand->wasPreCalled());
ASSERT(m_pCommand->wasPostCalled());
}
// Now see that deletion is relayed correctly.
void argvprocessor::unregister()
{
m_pCommand->Unregister();
ASSERT(m_pCommand->isDeleted());
}