config.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/config/config.cpp
  3. // Purpose: wxConfig unit test
  4. // Author: Marcin Wojdyr
  5. // Created: 2007-07-07
  6. // Copyright: (c) 2007 Marcin Wojdyr
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // NOTE: this test is compiled in test_gui because it uses wxColour for
  9. // its testing purpose.
  10. // See also tests/config/fileconf.cpp for wxFileConfig specific tests and
  11. // tests/config/regconf.cpp for wxRegConfig specific tests.
  12. // ----------------------------------------------------------------------------
  13. // headers
  14. // ----------------------------------------------------------------------------
  15. #include "testprec.h"
  16. #if wxUSE_CONFIG
  17. #ifdef __BORLANDC__
  18. #pragma hdrstop
  19. #endif
  20. #ifndef WX_PRECOMP
  21. #include "wx/wx.h"
  22. #endif // WX_PRECOMP
  23. #include "wx/config.h"
  24. #include "wx/colour.h"
  25. // --------------------------------------------------------------------------
  26. // test class
  27. // --------------------------------------------------------------------------
  28. class ConfigTestCase : public CppUnit::TestCase
  29. {
  30. public:
  31. ConfigTestCase() {}
  32. private:
  33. CPPUNIT_TEST_SUITE( ConfigTestCase );
  34. CPPUNIT_TEST( ReadWriteLocalTest );
  35. CPPUNIT_TEST( RecordingDefaultsTest );
  36. CPPUNIT_TEST_SUITE_END();
  37. void ReadWriteLocalTest();
  38. void RecordingDefaultsTest();
  39. // return the number of values we (attempted to) read
  40. int ReadValues(wxConfig *config, bool has_values);
  41. DECLARE_NO_COPY_CLASS(ConfigTestCase)
  42. };
  43. // register in the unnamed registry so that these tests are run by default
  44. CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase );
  45. // also include in its own registry so that these tests can be run alone
  46. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase, "ConfigTestCase" );
  47. void ConfigTestCase::ReadWriteLocalTest()
  48. {
  49. wxString app = wxT("wxConfigTestCase");
  50. wxString vendor = wxT("wxWidgets");
  51. wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""),
  52. wxCONFIG_USE_LOCAL_FILE);
  53. config->DeleteAll();
  54. config->Write(wxT("string1"), wxT("abc"));
  55. config->Write(wxT("string2"), wxString(wxT("def")));
  56. config->Write(wxT("int1"), 123);
  57. config->Write(wxString(wxT("long1")), 234L);
  58. config->Write(wxT("double1"), 345.67);
  59. config->Write(wxT("bool1"), true);
  60. #ifdef wxHAS_CONFIG_TEMPLATE_RW
  61. config->Write(wxT("color1"), wxColour(11,22,33,44));
  62. #endif // wxHAS_CONFIG_TEMPLATE_RW
  63. config->Flush();
  64. delete config;
  65. config = new wxConfig(app, vendor, wxT(""), wxT(""),
  66. wxCONFIG_USE_LOCAL_FILE);
  67. wxString string1 = config->Read(wxT("string1"));
  68. CPPUNIT_ASSERT_EQUAL( "abc", string1 );
  69. string1 = config->Read(wxT("string1"), wxT("defaultvalue"));
  70. CPPUNIT_ASSERT_EQUAL( "abc", string1 );
  71. wxString string2;
  72. bool r = config->Read(wxT("string2"), &string2);
  73. CPPUNIT_ASSERT( r );
  74. CPPUNIT_ASSERT_EQUAL( "def", string2 );
  75. r = config->Read(wxT("string2"), &string2, wxT("defaultvalue"));
  76. CPPUNIT_ASSERT( r );
  77. CPPUNIT_ASSERT_EQUAL( "def", string2 );
  78. int int1 = config->Read(wxT("int1"), 5);
  79. CPPUNIT_ASSERT_EQUAL( 123, int1 );
  80. long long1;
  81. r = config->Read(wxT("long1"), &long1);
  82. CPPUNIT_ASSERT( r );
  83. CPPUNIT_ASSERT_EQUAL( 234L, long1 );
  84. CPPUNIT_ASSERT( config->ReadLong(wxT("long1"), 0) == 234 );
  85. double double1;
  86. r = config->Read(wxT("double1"), &double1);
  87. CPPUNIT_ASSERT( r );
  88. CPPUNIT_ASSERT_EQUAL( 345.67, double1 );
  89. CPPUNIT_ASSERT( config->ReadDouble(wxT("double1"), 0) == double1 );
  90. bool bool1;
  91. r = config->Read(wxT("foo"), &bool1); // there is no "foo" key
  92. CPPUNIT_ASSERT( !r );
  93. r = config->Read(wxT("bool1"), &bool1);
  94. CPPUNIT_ASSERT( r );
  95. CPPUNIT_ASSERT_EQUAL( true, bool1 );
  96. CPPUNIT_ASSERT( config->ReadBool(wxT("bool1"), false) == bool1 );
  97. #ifdef wxHAS_CONFIG_TEMPLATE_RW
  98. wxColour color1;
  99. r = config->Read(wxT("color1"), &color1);
  100. CPPUNIT_ASSERT( r );
  101. CPPUNIT_ASSERT( color1 == wxColour(11,22,33,44) );
  102. CPPUNIT_ASSERT( config->ReadObject(wxT("color1"), wxNullColour) == color1 );
  103. #endif // wxHAS_CONFIG_TEMPLATE_RW
  104. config->DeleteAll();
  105. delete config;
  106. }
  107. int ConfigTestCase::ReadValues(wxConfig *config, bool has_values)
  108. {
  109. int read = 0;
  110. bool r;
  111. wxString string1 = config->Read(wxT("string1"), wxT("abc"));
  112. read++;
  113. wxString string2 = config->Read(wxT("string2"), wxString(wxT("def")));
  114. read++;
  115. wxString string3;
  116. r = config->Read(wxT("string3"), &string3, wxT("abc"));
  117. CPPUNIT_ASSERT_EQUAL( has_values, r );
  118. read++;
  119. wxString string4;
  120. r = config->Read(wxT("string4"), &string4, wxString(wxT("def")));
  121. CPPUNIT_ASSERT_EQUAL( has_values, r );
  122. read++;
  123. int int1;
  124. r = config->Read(wxT("int1"), &int1, 123);
  125. CPPUNIT_ASSERT_EQUAL( has_values, r );
  126. read++;
  127. int int2 = config->Read(wxT("int2"), 1234);
  128. CPPUNIT_ASSERT_EQUAL( int2, 1234 );
  129. read++;
  130. long long1;
  131. r = config->Read(wxString(wxT("long1")), &long1, 234L);
  132. CPPUNIT_ASSERT_EQUAL( has_values, r );
  133. read++;
  134. double double1;
  135. r = config->Read(wxT("double1"), &double1, 345.67);
  136. CPPUNIT_ASSERT_EQUAL( has_values, r );
  137. read++;
  138. bool bool1;
  139. r = config->Read(wxT("bool1"), &bool1, true);
  140. CPPUNIT_ASSERT_EQUAL( has_values, r );
  141. read++;
  142. #ifdef wxHAS_CONFIG_TEMPLATE_RW
  143. wxColour color1;
  144. r = config->Read(wxT("color1"), &color1, wxColour(11,22,33,44));
  145. CPPUNIT_ASSERT_EQUAL( has_values, r );
  146. read++;
  147. #endif // wxHAS_CONFIG_TEMPLATE_RW
  148. return read;
  149. }
  150. void ConfigTestCase::RecordingDefaultsTest()
  151. {
  152. wxString app = wxT("wxConfigTestCaseRD");
  153. wxString vendor = wxT("wxWidgets");
  154. wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""),
  155. wxCONFIG_USE_LOCAL_FILE);
  156. config->DeleteAll();
  157. config->SetRecordDefaults(false); // by default it is false
  158. ReadValues(config, false);
  159. CPPUNIT_ASSERT_EQUAL( 0, config->GetNumberOfEntries() );
  160. config->SetRecordDefaults(true);
  161. int read = ReadValues(config, false);
  162. CPPUNIT_ASSERT_EQUAL( read, config->GetNumberOfEntries() );
  163. ReadValues(config, true);
  164. config->DeleteAll();
  165. delete config;
  166. }
  167. #endif //wxUSE_CONFIG