From f3a9e5edac72ec01a4222b42252c5da8947ef21b Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Sun, 3 Jul 2016 15:28:32 +0100 Subject: [PATCH] Added code and unit tests for the SaveFile function in ContactDataObject --- .../cdo/ContactDataObject-Save.cpp | 29 +++++++++++++++++++ source/contacteditor/cdo/ContactDataObject.h | 4 ++- source/tests/xestiaab_contactsave.h | 18 ++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/source/contacteditor/cdo/ContactDataObject-Save.cpp b/source/contacteditor/cdo/ContactDataObject-Save.cpp index d14240b..daf6159 100644 --- a/source/contacteditor/cdo/ContactDataObject-Save.cpp +++ b/source/contacteditor/cdo/ContactDataObject-Save.cpp @@ -19,6 +19,35 @@ #include "ContactDataObject.h" #include "../../version.h" +ContactSaveStatus ContactDataObject::SaveFile(wxString SaveFilename){ + + // Get the data using SaveString. + + wxString SaveData = ""; + SaveString(&SaveData); + + // Open a file and try to write into it. + + wxFile ContactFile; + + if (!ContactFile.Open(SaveFilename, wxFile::write, wxS_DEFAULT)){ + + return CONTACTSAVE_FILEERROR; + + } + + if (ContactFile.Write(SaveData, wxConvUTF8) == false){ + + return CONTACTSAVE_WRITEERROR; + + } + + ContactFile.Close(); + + return CONTACTSAVE_OK; + +} + ContactSaveStatus ContactDataObject::SaveString(wxString *SaveData){ ContactSaveStatus SaveDataStatus = CONTACTSAVE_UNITTESTFAIL; diff --git a/source/contacteditor/cdo/ContactDataObject.h b/source/contacteditor/cdo/ContactDataObject.h index 5942425..c8c42b0 100644 --- a/source/contacteditor/cdo/ContactDataObject.h +++ b/source/contacteditor/cdo/ContactDataObject.h @@ -40,7 +40,9 @@ enum ContactLoadStatus{ enum ContactSaveStatus{ CONTACTSAVE_UNITTESTFAIL = -1, - CONTACTSAVE_OK, + CONTACTSAVE_OK, + CONTACTSAVE_FILEERROR, + CONTACTSAVE_WRITEERROR }; enum ContactKindType{ diff --git a/source/tests/xestiaab_contactsave.h b/source/tests/xestiaab_contactsave.h index 26b38e6..d0c2cfe 100644 --- a/source/tests/xestiaab_contactsave.h +++ b/source/tests/xestiaab_contactsave.h @@ -713,4 +713,22 @@ TEST(ContactSave, ContactSaveTests){ ASSERT_EQ(SaveDataCompare, SaveData); +} + +TEST(ContactSave, ContactSaveFileTests){ + + ContactDataObject TestFile1; + ContactDataObject TestFile2; + ContactDataObject TestFile3; + + ASSERT_EQ(CONTACTLOAD_OK, TestFile1.LoadFile("LoadCheck-Load1.vcf")); + ASSERT_EQ(CONTACTSAVE_OK, TestFile1.SaveFile("SaveCheck-Save1.vcf")); + ASSERT_EQ(CONTACTSAVE_FILEERROR, TestFile1.SaveFile("/invalidfile")); + + ASSERT_EQ(CONTACTLOAD_OK, TestFile2.LoadFile("LoadCheck-Load2.vcf")); + ASSERT_EQ(CONTACTSAVE_OK, TestFile2.SaveFile("SaveCheck-Save2.vcf")); + + ASSERT_EQ(CONTACTLOAD_OK, TestFile3.LoadFile("LoadCheck-Load4.vcf")); + ASSERT_EQ(CONTACTSAVE_OK, TestFile3.SaveFile("SaveCheck-Save4.vcf")); + } \ No newline at end of file -- 2.39.5