35#include <wx/filename.h>
36#include <wx/stdpaths.h>
67 bool aResetParamsIfMissing =
true ) :
126 nlohmann::json nestedJson = {
127 {
"meta", { {
"version", 0 } } },
131 ( *m_parent.Internals() )[
"/test_nested"_json_pointer] = nestedJson;
133 BOOST_CHECK( !m_parent.IsModified() );
142 m_parent.ClearModified();
145 m_parent.ReleaseNestedSettings( nested );
150 "Parent should not be marked modified after releasing nested settings "
151 "when no intentional changes were made" );
163 nlohmann::json nestedJson = {
164 {
"meta", { {
"version", 0 } } },
166 {
"extra_value", 42 }
169 ( *m_parent.Internals() )[
"/test_nested"_json_pointer] = nestedJson;
176 nested->m_testValue = 99;
179 bool modified = nested->SaveToFile();
180 BOOST_CHECK( modified );
183 auto parentJson = m_parent.GetJson(
"test_nested.test_value" );
185 BOOST_CHECK( parentJson.has_value() );
188 m_parent.ReleaseNestedSettings( nested );
199 nlohmann::json nestedJson = {
200 {
"meta", { {
"version", 0 } } },
204 ( *m_parent.Internals() )[
"/test_nested"_json_pointer] = nestedJson;
209 nested->m_testValue = 77;
211 m_parent.ClearModified();
212 m_parent.ReleaseNestedSettings( nested );
215 auto parentJson = m_parent.GetJson(
"test_nested.test_value" );
217 BOOST_CHECK( parentJson.has_value() );
231 nlohmann::json nestedJson = {
232 {
"meta", { {
"version", 0 } } },
236 ( *m_parent.Internals() )[
"/test_nested"_json_pointer] = nestedJson;
244 BOOST_CHECK( nested->SaveToFile() );
246 auto parentJson = m_parent.GetJson(
"test_nested.extra_value" );
248 BOOST_CHECK( parentJson.has_value() );
251 m_parent.ReleaseNestedSettings( nested );
291 nlohmann::json nestedJson = {
292 {
"meta", { {
"version", 0 } } }
295 ( *m_parent.Internals() )[
"/test_nested"_json_pointer] = nestedJson;
300 BOOST_CHECK( nested->SaveToFile() );
302 auto version = m_parent.GetJson(
"test_nested.meta.version" );
303 auto migrated = m_parent.GetJson(
"test_nested.migrated_field" );
305 BOOST_CHECK( version.has_value() );
307 BOOST_CHECK( migrated.has_value() );
310 m_parent.ReleaseNestedSettings( nested );
342 wxFileName tempDir( wxStandardPaths::Get().GetTempDir(), wxEmptyString );
343 wxString tempName = wxString::Format( wxT(
"kicad_qa_24402_%ld" ), wxGetProcessId() );
344 tempDir.AppendDir( tempName );
346 BOOST_REQUIRE( wxFileName::Mkdir( tempDir.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
348 wxFileName tempFile( tempDir.GetPath(), wxT(
"test.json" ) );
359 wxDateTime originalModTime = tempFile.GetModificationTime();
362 wxMilliSleep( 1500 );
375 wxFileName refreshed( tempFile.GetFullPath() );
376 wxDateTime newModTime = refreshed.GetModificationTime();
379 "File mtime should not change when serialized contents match on-disk "
388 wxFileName refreshedAfterChange( tempFile.GetFullPath() );
389 BOOST_CHECK( refreshedAfterChange.GetModificationTime() != originalModTime );
392 wxRemoveFile( tempFile.GetFullPath() );
393 wxFileName::Rmdir( tempDir.GetPath() );
424 wxFileName tempDir( wxStandardPaths::Get().GetTempDir(), wxEmptyString );
425 wxString tempName = wxString::Format( wxT(
"kicad_qa_24402_nested_%ld" ), wxGetProcessId() );
426 tempDir.AppendDir( tempName );
428 BOOST_REQUIRE( wxFileName::Mkdir( tempDir.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
430 wxFileName tempFile( tempDir.GetPath(), wxT(
"parent.json" ) );
436 nested->m_testValue = 5;
445 nested->m_testValue = 123;
452 std::ifstream in( tempFile.GetFullPath().fn_str(), std::ios::in | std::ios::binary );
453 std::string contents( ( std::istreambuf_iterator<char>( in ) ),
454 std::istreambuf_iterator<char>() );
455 nlohmann::json onDisk = nlohmann::json::parse( contents );
459 wxRemoveFile( tempFile.GetFullPath() );
460 wxFileName::Rmdir( tempDir.GetPath() );
471 wxFileName tempDir( wxStandardPaths::Get().GetTempDir(), wxEmptyString );
472 wxString tempName = wxString::Format( wxT(
"kicad_qa_24402_churn_%ld" ), wxGetProcessId() );
473 tempDir.AppendDir( tempName );
475 BOOST_REQUIRE( wxFileName::Mkdir( tempDir.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
477 wxFileName tempFile( tempDir.GetPath(), wxT(
"parent.json" ) );
483 nlohmann::json nestedJson = { {
"meta", { {
"version", 0 } } }, {
"test_value", 5 } };
484 ( *parent.
Internals() )[
"/test_nested"_json_pointer] = nestedJson;
487 std::ifstream in0( tempFile.GetFullPath().fn_str(), std::ios::binary );
488 std::string before( ( std::istreambuf_iterator<char>( in0 ) ),
489 std::istreambuf_iterator<char>() );
491 BOOST_REQUIRE( before.find(
"extra_value" ) == std::string::npos );
501 std::ifstream in1( tempFile.GetFullPath().fn_str(), std::ios::binary );
502 std::string after( ( std::istreambuf_iterator<char>( in1 ) ),
503 std::istreambuf_iterator<char>() );
506 "Releasing an unchanged nested setting must not add default keys or "
507 "rewrite the existing file" );
509 wxRemoveFile( tempFile.GetFullPath() );
510 wxFileName::Rmdir( tempDir.GetPath() );
A disk-backed parent that carries a nested setting, mirroring the project file / nested board or ERC ...
DISK_PARENT_SETTINGS(const wxString &aFilename)
Verify that SaveToFile does not rewrite the on-disk file when its serialized contents match what alre...
DISK_SETTINGS(const wxString &aFilename)
void Set(const std::string &aPath, ValueType aVal)
Stores a value into the JSON document Will throw an exception if ValueType isn't something that the l...
bool m_modified
True if the JSON data store has been written to since the last file write.
void SetManager(SETTINGS_MANAGER *aManager)
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
void registerMigration(int aOldSchemaVersion, int aNewSchemaVersion, std::function< bool(void)> aMigrator)
Registers a migration from one schema version to another.
JSON_SETTINGS_INTERNALS * Internals()
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
Calls Store() and then writes the contents of the JSON document to a file.
bool m_resetParamsIfMissing
Whether or not to set parameters to their default value if missing from JSON on Load()
A nested settings subclass that migrates a v0 block to v1 by writing a key directly into the JSON,...
MIGRATING_NESTED_SETTINGS(JSON_SETTINGS *aParent, const std::string &aPath)
~MIGRATING_NESTED_SETTINGS() override
NESTED_SETTINGS_TEST_FIXTURE()
TEST_JSON_SETTINGS m_parent
JSON_SETTINGS * m_parent
A pointer to the parent object to load and store from.
NESTED_SETTINGS(const std::string &aName, int aSchemaVersion, JSON_SETTINGS *aParent, const std::string &aPath, bool aLoadFromFile=true)
bool LoadFromFile(const wxString &aDirectory="") override
Loads the JSON document from the parent and then calls Load()
A test-accessible JSON_SETTINGS subclass that exposes the protected m_modified flag.
A minimal nested settings subclass for testing.
~TEST_NESTED_SETTINGS() override
TEST_NESTED_SETTINGS(JSON_SETTINGS *aParent, const std::string &aPath, bool aResetParamsIfMissing=true)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(ReleaseDoesNotMarkParentModified)
Verify that releasing nested settings does not mark the parent as modified.
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")