summaryrefslogtreecommitdiffstats
path: root/tests/testcategories.cc
blob: e2d4b259e5034656f4d60a953a08226ef3877f96 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* testcategories			KPilot
**
** Copyright (C) 2005 by Adriaan de Groot <[email protected])
**
** Test the functions related to category handling.
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

/*
** Bug reports and questions can be sent to [email protected]
*/

#include "options.h"

#include <tdeaboutdata.h>
#include <tdeapplication.h>
#include <kdebug.h>
#include <klocale.h>
#include <tdecmdlineargs.h>

#include "pilotLocalDatabase.h"
#include "pilotRecord.h"
#include "pilotAppInfo.h"

// Name of a bogus broken DB
#define BOGUS_NAME "bogus"

// Name of an actual DB
#define MEMO_NAME "MemoDB"

TQStringList categories( const PilotAppInfoBase *appinfo )
{
	TQStringList cats;

	for (unsigned int i=0; i<Pilot::CATEGORY_COUNT; i++)
	{
		TQString cat = appinfo->categoryName(i);
		if (!cat.isEmpty())
		{
			TQString s = CSL1("(%1:%2)").arg(i).arg(cat);
			cats.append(s);
		}
	}

	return cats;
}

TQStringList listCategories( const TQString &dir, const char *dbname )
{
	TQStringList cats;
	PilotLocalDatabase *database = new PilotLocalDatabase( dir, dbname );
	if (!database->isOpen()) return cats;

	PilotAppInfoBase *appinfo = new PilotAppInfoBase( database );
	appinfo->dump();

	cats = categories( appinfo );

	delete appinfo;
	delete database;
	return cats;
}

void badAppInfoCreation()
{
	FUNCTIONSETUP;
	PilotAppInfoBase *appinfo = new PilotAppInfoBase( 0L );
	appinfo->dump();
	KPILOT_DELETE( appinfo ) ;

	PilotLocalDatabase *database = new PilotLocalDatabase( BOGUS_NAME );
	appinfo = new PilotAppInfoBase( database );
	appinfo->dump();
	KPILOT_DELETE( appinfo );
}

void categoryNames( const TQString &dir )
{
	PilotLocalDatabase *database = new PilotLocalDatabase( dir, MEMO_NAME );
	if (!database->isOpen())
	{
		WARNINGKPILOT << "Can not open database '" << MEMO_NAME << "'" << endl;
		return;
	}

	PilotAppInfoBase *appinfo = new PilotAppInfoBase( database );
	appinfo->dump();

	DEBUGKPILOT << "# Done dumping" << endl;

	if (!appinfo->categoryInfo())
	{
		WARNINGKPILOT << "Could not read required database" << endl;
		return;
	}

	const char *funnyname = "OneTwoThreeFourFiveSixSevenEight";
	const int funnyname_length = strlen(funnyname);

	if (funnyname_length < 20)
	{
		WARNINGKPILOT << "String of example category names is too short." << endl;
		return;
	}

	DEBUGKPILOT << "# Updating category names with various lengths." << endl;
	DEBUGKPILOT << "# Expect three truncation errors and two bad category numbers." << endl;
	for (unsigned int i=0; i<Pilot::CATEGORY_COUNT+2; i++)
	{
		TQString name = TQString::fromLatin1(funnyname+funnyname_length-i-3);
		if (!appinfo->setCategoryName(i,name))
		{
			WARNINGKPILOT << "Failed to set category " << i << " name to <" << name << ">" << endl;
		}
		else
		{
			TQString categoryname = appinfo->categoryName(i);
			if (categoryname != name)
			{
				WARNINGKPILOT << "Category name " << i
					<< " set to <" << name
					<< "> and returns <"
					<< categoryname << ">" << endl;
			}
		}
	}

	DEBUGKPILOT << "# Final categories\n#   " << categories( appinfo ).join("\n#   ") << endl;
}

static const TDECmdLineOptions options[] =
{
	{"verbose", "Verbose output", 0},
	{"data-dir <path>","Set data directory", "."},
	TDECmdLineLastOption
};


int main(int argc, char **argv)
{
	TDEApplication::disableAutoDcopRegistration();

	TDEAboutData aboutData("testcategories","Test Categories","0.1");
	TDECmdLineArgs::init(argc,argv,&aboutData);
	TDECmdLineArgs::addCmdLineOptions( options );

	TDEApplication app( false, false );

	TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

#ifdef DEBUG
	debug_level= (args->isSet("verbose")) ? 4 : 0;
#endif

	Q_UNUSED(argc);
	Q_UNUSED(argv);

	static const char *files[] = {
		MEMO_NAME,
		"AddressDB",
		"MailDB",
		"ToDoDB",
		0L
	};

	TQString datadir = args->getOption("data-dir");

	DEBUGKPILOT << "### testcategories\n#\n#" << endl;
	DEBUGKPILOT << "# Listing categories from database files.\n#" << endl;

	Pilot::setupPilotCodec( CSL1("Latin1") );

	// Include arbitrary break-off point, in case
	for (unsigned int i = 0; i<sizeof(files)/sizeof(const char *) ; i++)
	{
		if (!files[i])
		{
			break;
		}
		DEBUGKPILOT << "# Categories (" << files[i] << "): " << endl;
		DEBUGKPILOT << "#   " << listCategories( datadir, files[i] ).join("\n#   ") << "\n#\n";
	}
	// Should bail, not crash
	DEBUGKPILOT << "# Categories (nonexistent): " << endl;
	(void) listCategories( datadir, "nonexistent" );

	DEBUGKPILOT << "# Categories (bogus): " << endl;
	(void) listCategories( datadir, BOGUS_NAME );

	DEBUGKPILOT << "#\n# Trying to pass broken pointers to category functions.\n# Four errors are expected.\n#" << endl;
	badAppInfoCreation();

	DEBUGKPILOT << "#\n# Checking category names." << endl;
	categoryNames( datadir );

	DEBUGKPILOT << "# OK.\n" << endl;
	return 0;
}