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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
|
///////////////////////////////////////////////////////////////////////////////
// Name: tests/fileconf/fileconf.cpp
// Purpose: wxFileConf unit test
// Author: Vadim Zeitlin
// Created: 2004-09-19
// Copyright: (c) 2004 Vadim Zeitlin
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#if wxUSE_FILECONFIG
#ifndef WX_PRECOMP
#endif // WX_PRECOMP
#include "wx/fileconf.h"
#include "wx/sstream.h"
#include "wx/log.h"
static const wxChar *testconfig =
wxT("[root]\n")
wxT("entry=value\n")
wxT("[root/group1]\n")
wxT("[root/group1/subgroup]\n")
wxT("subentry=subvalue\n")
wxT("subentry2=subvalue2\n")
wxT("[root/group2]\n")
;
// ----------------------------------------------------------------------------
// local functions
// ----------------------------------------------------------------------------
static wxString Dump(wxFileConfig& fc)
{
wxStringOutputStream sos;
fc.Save(sos);
return wxTextFile::Translate(sos.GetString(), wxTextFileType_Unix);
}
// helper macro to test wxFileConfig contents
#define wxVERIFY_FILECONFIG(t, fc) CPPUNIT_ASSERT_EQUAL(wxString(t), Dump(fc))
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
class FileConfigTestCase : public CppUnit::TestCase
{
public:
FileConfigTestCase() { }
private:
CPPUNIT_TEST_SUITE( FileConfigTestCase );
CPPUNIT_TEST( Path );
CPPUNIT_TEST( AddEntries );
CPPUNIT_TEST( GetEntries );
CPPUNIT_TEST( GetGroups );
CPPUNIT_TEST( HasEntry );
CPPUNIT_TEST( HasGroup );
CPPUNIT_TEST( Binary );
CPPUNIT_TEST( Save );
CPPUNIT_TEST( DeleteEntry );
CPPUNIT_TEST( DeleteAndWriteEntry );
CPPUNIT_TEST( DeleteLastRootEntry );
CPPUNIT_TEST( DeleteGroup );
CPPUNIT_TEST( DeleteAll );
CPPUNIT_TEST( RenameEntry );
CPPUNIT_TEST( RenameGroup );
CPPUNIT_TEST( CreateEntriesAndSubgroup );
CPPUNIT_TEST( CreateSubgroupAndEntries );
CPPUNIT_TEST( DeleteLastGroup );
CPPUNIT_TEST( DeleteAndRecreateGroup );
CPPUNIT_TEST( AddToExistingRoot );
CPPUNIT_TEST( ReadNonExistent );
CPPUNIT_TEST( ReadEmpty );
CPPUNIT_TEST( ReadFloat );
CPPUNIT_TEST_SUITE_END();
void Path();
void AddEntries();
void GetEntries();
void GetGroups();
void HasEntry();
void HasGroup();
void Binary();
void Save();
void DeleteEntry();
void DeleteAndWriteEntry();
void DeleteLastRootEntry();
void DeleteGroup();
void DeleteAll();
void RenameEntry();
void RenameGroup();
void CreateEntriesAndSubgroup();
void CreateSubgroupAndEntries();
void DeleteLastGroup();
void DeleteAndRecreateGroup();
void AddToExistingRoot();
void ReadNonExistent();
void ReadEmpty();
void ReadFloat();
static wxString ChangePath(wxFileConfig& fc, const wxChar *path)
{
fc.SetPath(path);
return fc.GetPath();
}
void CheckGroupEntries(const wxFileConfig& fc,
const wxChar *path,
size_t nEntries,
...);
void CheckGroupSubgroups(const wxFileConfig& fc,
const wxChar *path,
size_t nGroups,
...);
DECLARE_NO_COPY_CLASS(FileConfigTestCase)
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase, "FileConfigTestCase" );
void FileConfigTestCase::Path()
{
wxStringInputStream sis(testconfig);
wxFileConfig fc(sis);
CPPUNIT_ASSERT( ChangePath(fc, wxT("")) == wxT("") );
CPPUNIT_ASSERT( ChangePath(fc, wxT("/")) == wxT("") );
CPPUNIT_ASSERT( ChangePath(fc, wxT("root")) == wxT("/root") );
CPPUNIT_ASSERT( ChangePath(fc, wxT("/root")) == wxT("/root") );
CPPUNIT_ASSERT( ChangePath(fc, wxT("/root/group1/subgroup")) == wxT("/root/group1/subgroup") );
CPPUNIT_ASSERT( ChangePath(fc, wxT("/root/group2")) == wxT("/root/group2") );
}
void FileConfigTestCase::AddEntries()
{
wxFileConfig fc;
wxVERIFY_FILECONFIG( wxT(""), fc );
fc.Write(wxT("/Foo"), wxT("foo"));
wxVERIFY_FILECONFIG( wxT("Foo=foo\n"), fc );
fc.Write(wxT("/Bar/Baz"), wxT("baz"));
wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc );
fc.DeleteAll();
wxVERIFY_FILECONFIG( wxT(""), fc );
fc.Write(wxT("/Bar/Baz"), wxT("baz"));
wxVERIFY_FILECONFIG( wxT("[Bar]\nBaz=baz\n"), fc );
fc.Write(wxT("/Foo"), wxT("foo"));
wxVERIFY_FILECONFIG( wxT("Foo=foo\n[Bar]\nBaz=baz\n"), fc );
}
void
FileConfigTestCase::CheckGroupEntries(const wxFileConfig& fc,
const wxChar *path,
size_t nEntries,
...)
{
wxConfigPathChanger change(&fc, wxString(path) + wxT("/"));
CPPUNIT_ASSERT( fc.GetNumberOfEntries() == nEntries );
va_list ap;
va_start(ap, nEntries);
long cookie;
wxString name;
for ( bool cont = fc.GetFirstEntry(name, cookie);
cont;
cont = fc.GetNextEntry(name, cookie), nEntries-- )
{
CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
}
CPPUNIT_ASSERT( nEntries == 0 );
va_end(ap);
}
void
FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig& fc,
const wxChar *path,
size_t nGroups,
...)
{
wxConfigPathChanger change(&fc, wxString(path) + wxT("/"));
CPPUNIT_ASSERT( fc.GetNumberOfGroups() == nGroups );
va_list ap;
va_start(ap, nGroups);
long cookie;
wxString name;
for ( bool cont = fc.GetFirstGroup(name, cookie);
cont;
cont = fc.GetNextGroup(name, cookie), nGroups-- )
{
CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
}
CPPUNIT_ASSERT( nGroups == 0 );
va_end(ap);
}
void FileConfigTestCase::GetEntries()
{
wxStringInputStream sis(testconfig);
wxFileConfig fc(sis);
CheckGroupEntries(fc, wxT(""), 0);
CheckGroupEntries(fc, wxT("/root"), 1, wxT("entry"));
CheckGroupEntries(fc, wxT("/root/group1"), 0);
CheckGroupEntries(fc, wxT("/root/group1/subgroup"),
2, wxT("subentry"), wxT("subentry2"));
}
void FileConfigTestCase::GetGroups()
{
wxStringInputStream sis(testconfig);
wxFileConfig fc(sis);
CheckGroupSubgroups(fc, wxT(""), 1, wxT("root"));
CheckGroupSubgroups(fc, wxT("/root"), 2, wxT("group1"), wxT("group2"));
CheckGroupSubgroups(fc, wxT("/root/group1"), 1, wxT("subgroup"));
CheckGroupSubgroups(fc, wxT("/root/group2"), 0);
}
void FileConfigTestCase::HasEntry()
{
wxStringInputStream sis(testconfig);
wxFileConfig fc(sis);
CPPUNIT_ASSERT( !fc.HasEntry(wxT("root")) );
CPPUNIT_ASSERT( fc.HasEntry(wxT("root/entry")) );
CPPUNIT_ASSERT( fc.HasEntry(wxT("/root/entry")) );
CPPUNIT_ASSERT( fc.HasEntry(wxT("root/group1/subgroup/subentry")) );
CPPUNIT_ASSERT( !fc.HasEntry(wxT("")) );
CPPUNIT_ASSERT( !fc.HasEntry(wxT("root/group1")) );
CPPUNIT_ASSERT( !fc.HasEntry(wxT("subgroup/subentry")) );
CPPUNIT_ASSERT( !fc.HasEntry(wxT("/root/no_such_group/entry")) );
CPPUNIT_ASSERT( !fc.HasGroup(wxT("/root/no_such_group")) );
}
void FileConfigTestCase::HasGroup()
{
wxStringInputStream sis(testconfig);
wxFileConfig fc(sis);
CPPUNIT_ASSERT( fc.HasGroup(wxT("root")) );
CPPUNIT_ASSERT( fc.HasGroup(wxT("root/group1")) );
CPPUNIT_ASSERT( fc.HasGroup(wxT("root/group1/subgroup")) );
CPPUNIT_ASSERT( fc.HasGroup(wxT("root/group2")) );
CPPUNIT_ASSERT( !fc.HasGroup(wxT("")) );
CPPUNIT_ASSERT( !fc.HasGroup(wxT("root/group")) );
CPPUNIT_ASSERT( !fc.HasGroup(wxT("root//subgroup")) );
CPPUNIT_ASSERT( !fc.HasGroup(wxT("foot/subgroup")) );
CPPUNIT_ASSERT( !fc.HasGroup(wxT("foot")) );
}
void FileConfigTestCase::Binary()
{
wxStringInputStream sis(
"[root]\n"
"binary=Zm9vCg==\n"
);
wxFileConfig fc(sis);
wxMemoryBuffer buf;
fc.Read("/root/binary", &buf);
CPPUNIT_ASSERT( memcmp("foo\n", buf.GetData(), buf.GetDataLen()) == 0 );
buf.SetDataLen(0);
buf.AppendData("\0\1\2", 3);
fc.Write("/root/012", buf);
wxVERIFY_FILECONFIG(
"[root]\n"
"binary=Zm9vCg==\n"
"012=AAEC\n",
fc
);
}
void FileConfigTestCase::Save()
{
wxStringInputStream sis(testconfig);
wxFileConfig fc(sis);
wxVERIFY_FILECONFIG( testconfig, fc );
}
void FileConfigTestCase::DeleteEntry()
{
wxStringInputStream sis(testconfig);
wxFileConfig fc(sis);
CPPUNIT_ASSERT( !fc.DeleteEntry(wxT("foo")) );
CPPUNIT_ASSERT( fc.DeleteEntry(wxT("root/group1/subgroup/subentry")) );
wxVERIFY_FILECONFIG( wxT("[root]\n")
wxT("entry=value\n")
wxT("[root/group1]\n")
wxT("[root/group1/subgroup]\n")
wxT("subentry2=subvalue2\n")
wxT("[root/group2]\n"),
fc );
// group should be deleted now as well as it became empty
wxConfigPathChanger change(&fc, wxT("root/group1/subgroup/subentry2"));
CPPUNIT_ASSERT( fc.DeleteEntry(wxT("subentry2")) );
wxVERIFY_FILECONFIG( wxT("[root]\n")
wxT("entry=value\n")
wxT("[root/group1]\n")
wxT("[root/group2]\n"),
fc );
}
void FileConfigTestCase::DeleteAndWriteEntry()
{
wxStringInputStream sis(
"[root/group1]\n"
"subentry=subvalue\n"
"subentry2=subvalue2\n"
"subentry3=subvalue3\n"
);
wxFileConfig fc(sis);
fc.DeleteEntry("/root/group1/subentry2");
fc.Write("/root/group1/subentry2", "testvalue");
fc.DeleteEntry("/root/group2/subentry2");
fc.Write("/root/group2/subentry2", "testvalue2");
fc.DeleteEntry("/root/group1/subentry2");
fc.Write("/root/group1/subentry2", "testvalue");
fc.DeleteEntry("/root/group2/subentry2");
fc.Write("/root/group2/subentry2", "testvalue2");
wxVERIFY_FILECONFIG( "[root/group1]\n"
"subentry=subvalue\n"
"subentry3=subvalue3\n"
"subentry2=testvalue\n"
"[root/group2]\n"
"subentry2=testvalue2\n",
fc );
fc.DeleteEntry("/root/group2/subentry2");
wxVERIFY_FILECONFIG( "[root/group1]\n"
"subentry=subvalue\n"
"subentry3=subvalue3\n"
"subentry2=testvalue\n",
fc );
fc.DeleteEntry("/root/group1/subentry2");
fc.DeleteEntry("/root/group1/subentry");
fc.DeleteEntry("/root/group1/subentry3");
wxVERIFY_FILECONFIG( "", fc );
}
void FileConfigTestCase::DeleteLastRootEntry()
{
// This tests for the bug which occurred when the last entry of the root
// group was deleted: this corrupted internal state and resulted in a crash
// after trying to write the just deleted entry again.
wxStringInputStream sis("");
wxFileConfig fc(sis);
fc.Write("key", "value");
wxVERIFY_FILECONFIG( "key=value\n", fc );
fc.DeleteEntry("key");
wxVERIFY_FILECONFIG( "", fc );
fc.Write("key", "value");
wxVERIFY_FILECONFIG( "key=value\n", fc );
}
void FileConfigTestCase::DeleteGroup()
{
wxStringInputStream sis(testconfig);
wxFileConfig fc(sis);
CPPUNIT_ASSERT( !fc.DeleteGroup(wxT("foo")) );
CPPUNIT_ASSERT( fc.DeleteGroup(wxT("root/group1")) );
wxVERIFY_FILECONFIG( wxT("[root]\n")
wxT("entry=value\n")
wxT("[root/group2]\n"),
fc );
// notice trailing slash: it should be ignored
CPPUNIT_ASSERT( fc.DeleteGroup(wxT("root/group2/")) );
wxVERIFY_FILECONFIG( wxT("[root]\n")
wxT("entry=value\n"),
fc );
CPPUNIT_ASSERT( fc.DeleteGroup(wxT("root")) );
CPPUNIT_ASSERT( Dump(fc).empty() );
}
void FileConfigTestCase::DeleteAll()
{
wxStringInputStream sis(testconfig);
wxFileConfig fc(sis);
CPPUNIT_ASSERT( fc.DeleteAll() );
CPPUNIT_ASSERT( Dump(fc).empty() );
}
void FileConfigTestCase::RenameEntry()
{
wxStringInputStream sis(testconfig);
wxFileConfig fc(sis);
fc.SetPath(wxT("root"));
CPPUNIT_ASSERT( fc.RenameEntry(wxT("entry"), wxT("newname")) );
wxVERIFY_FILECONFIG( wxT("[root]\n")
wxT("newname=value\n")
wxT("[root/group1]\n")
wxT("[root/group1/subgroup]\n")
wxT("subentry=subvalue\n")
wxT("subentry2=subvalue2\n")
wxT("[root/group2]\n"),
fc );
fc.SetPath(wxT("group1/subgroup"));
CPPUNIT_ASSERT( !fc.RenameEntry(wxT("entry"), wxT("newname")) );
CPPUNIT_ASSERT( !fc.RenameEntry(wxT("subentry"), wxT("subentry2")) );
CPPUNIT_ASSERT( fc.RenameEntry(wxT("subentry"), wxT("subentry1")) );
wxVERIFY_FILECONFIG( wxT("[root]\n")
wxT("newname=value\n")
wxT("[root/group1]\n")
wxT("[root/group1/subgroup]\n")
wxT("subentry2=subvalue2\n")
wxT("subentry1=subvalue\n")
wxT("[root/group2]\n"),
fc );
}
void FileConfigTestCase::RenameGroup()
{
wxStringInputStream sis(testconfig);
wxFileConfig fc(sis);
CPPUNIT_ASSERT( fc.RenameGroup(wxT("root"), wxT("foot")) );
wxVERIFY_FILECONFIG( wxT("[foot]\n")
wxT("entry=value\n")
wxT("[foot/group1]\n")
wxT("[foot/group1/subgroup]\n")
wxT("subentry=subvalue\n")
wxT("subentry2=subvalue2\n")
wxT("[foot/group2]\n"),
fc );
// renaming a path doesn't work, it must be the immediate group
CPPUNIT_ASSERT( !fc.RenameGroup(wxT("foot/group1"), wxT("group2")) );
fc.SetPath(wxT("foot"));
// renaming to a name of existing group doesn't work
CPPUNIT_ASSERT( !fc.RenameGroup(wxT("group1"), wxT("group2")) );
// try exchanging the groups names and then restore them back
CPPUNIT_ASSERT( fc.RenameGroup(wxT("group1"), wxT("groupTmp")) );
wxVERIFY_FILECONFIG( wxT("[foot]\n")
wxT("entry=value\n")
wxT("[foot/groupTmp]\n")
wxT("[foot/groupTmp/subgroup]\n")
wxT("subentry=subvalue\n")
wxT("subentry2=subvalue2\n")
wxT("[foot/group2]\n"),
fc );
CPPUNIT_ASSERT( fc.RenameGroup(wxT("group2"), wxT("group1")) );
wxVERIFY_FILECONFIG( wxT("[foot]\n")
wxT("entry=value\n")
wxT("[foot/groupTmp]\n")
wxT("[foot/groupTmp/subgroup]\n")
wxT("subentry=subvalue\n")
wxT("subentry2=subvalue2\n")
wxT("[foot/group1]\n"),
fc );
CPPUNIT_ASSERT( fc.RenameGroup(wxT("groupTmp"), wxT("group2")) );
wxVERIFY_FILECONFIG( wxT("[foot]\n")
wxT("entry=value\n")
wxT("[foot/group2]\n")
wxT("[foot/group2/subgroup]\n")
wxT("subentry=subvalue\n")
wxT("subentry2=subvalue2\n")
wxT("[foot/group1]\n"),
fc );
CPPUNIT_ASSERT( fc.RenameGroup(wxT("group1"), wxT("groupTmp")) );
wxVERIFY_FILECONFIG( wxT("[foot]\n")
wxT("entry=value\n")
wxT("[foot/group2]\n")
wxT("[foot/group2/subgroup]\n")
wxT("subentry=subvalue\n")
wxT("subentry2=subvalue2\n")
wxT("[foot/groupTmp]\n"),
fc );
CPPUNIT_ASSERT( fc.RenameGroup(wxT("group2"), wxT("group1")) );
wxVERIFY_FILECONFIG( wxT("[foot]\n")
wxT("entry=value\n")
wxT("[foot/group1]\n")
wxT("[foot/group1/subgroup]\n")
wxT("subentry=subvalue\n")
wxT("subentry2=subvalue2\n")
wxT("[foot/groupTmp]\n"),
fc );
CPPUNIT_ASSERT( fc.RenameGroup(wxT("groupTmp"), wxT("group2")) );
wxVERIFY_FILECONFIG( wxT("[foot]\n")
wxT("entry=value\n")
wxT("[foot/group1]\n")
wxT("[foot/group1/subgroup]\n")
wxT("subentry=subvalue\n")
wxT("subentry2=subvalue2\n")
wxT("[foot/group2]\n"),
fc );
}
void FileConfigTestCase::CreateSubgroupAndEntries()
{
wxFileConfig fc;
fc.Write(wxT("sub/sub_first"), wxT("sub_one"));
fc.Write(wxT("first"), wxT("one"));
wxVERIFY_FILECONFIG( wxT("first=one\n")
wxT("[sub]\n")
wxT("sub_first=sub_one\n"),
fc );
}
void FileConfigTestCase::CreateEntriesAndSubgroup()
{
wxFileConfig fc;
fc.Write(wxT("first"), wxT("one"));
fc.Write(wxT("second"), wxT("two"));
fc.Write(wxT("sub/sub_first"), wxT("sub_one"));
wxVERIFY_FILECONFIG( wxT("first=one\n")
wxT("second=two\n")
wxT("[sub]\n")
wxT("sub_first=sub_one\n"),
fc );
}
static void EmptyConfigAndWriteKey()
{
wxFileConfig fc(wxT("deleteconftest"));
const wxString groupPath = wxT("/root");
if ( fc.Exists(groupPath) )
{
// using DeleteGroup exposes the problem, using DeleteAll doesn't
CPPUNIT_ASSERT( fc.DeleteGroup(groupPath) );
}
// the config must be empty for the problem to arise
CPPUNIT_ASSERT( !fc.GetNumberOfEntries(true) );
CPPUNIT_ASSERT( !fc.GetNumberOfGroups(true) );
// this crashes on second call of this function
CPPUNIT_ASSERT( fc.Write(groupPath + wxT("/entry"), wxT("value")) );
}
void FileConfigTestCase::DeleteLastGroup()
{
/*
We make 2 of the same calls, first to create a file config with a single
group and key...
*/
::EmptyConfigAndWriteKey();
/*
... then the same but this time the key's group is deleted before the
key is written again. This causes a crash.
*/
::EmptyConfigAndWriteKey();
// clean up
wxLogNull noLogging;
(void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(wxT("deleteconftest")));
}
void FileConfigTestCase::DeleteAndRecreateGroup()
{
static const wxChar *confInitial =
wxT("[First]\n")
wxT("Value1=Foo\n")
wxT("[Second]\n")
wxT("Value2=Bar\n");
wxStringInputStream sis(confInitial);
wxFileConfig fc(sis);
fc.DeleteGroup(wxT("Second"));
wxVERIFY_FILECONFIG( wxT("[First]\n")
wxT("Value1=Foo\n"),
fc );
fc.Write(wxT("Second/Value2"), wxT("New"));
wxVERIFY_FILECONFIG( wxT("[First]\n")
wxT("Value1=Foo\n")
wxT("[Second]\n")
wxT("Value2=New\n"),
fc );
}
void FileConfigTestCase::AddToExistingRoot()
{
static const wxChar *confInitial =
wxT("[Group]\n")
wxT("value1=foo\n");
wxStringInputStream sis(confInitial);
wxFileConfig fc(sis);
fc.Write(wxT("/value1"), wxT("bar"));
wxVERIFY_FILECONFIG(
wxT("value1=bar\n")
wxT("[Group]\n")
wxT("value1=foo\n"),
fc
);
}
void FileConfigTestCase::ReadNonExistent()
{
static const char *confTest =
"community=censored\n"
"[City1]\n"
"URL=www.fake1.na\n"
"[City1/A1]\n"
"[City1/A1/1]\n"
"IP=192.168.1.66\n"
"URL=www.fake2.na\n"
;
wxStringInputStream sis(confTest);
wxFileConfig fc(sis);
wxString url;
CPPUNIT_ASSERT( !fc.Read("URL", &url) );
}
void FileConfigTestCase::ReadEmpty()
{
static const char *confTest = "";
wxStringInputStream sis(confTest);
wxFileConfig fc(sis);
}
void FileConfigTestCase::ReadFloat()
{
static const char *confTest =
"x=1.234\n"
"y=-9876.5432\n"
"z=2e+308\n"
;
wxStringInputStream sis(confTest);
wxFileConfig fc(sis);
float f;
CPPUNIT_ASSERT( fc.Read("x", &f) );
CPPUNIT_ASSERT_EQUAL( 1.234f, f );
CPPUNIT_ASSERT( fc.Read("y", &f) );
CPPUNIT_ASSERT_EQUAL( -9876.5432f, f );
}
#endif // wxUSE_FILECONFIG
|