File indexing completed on 2024-05-19 05:42:15

0001 // ct_lvtprj_project_file.t.cpp                                         -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvtprj_projectfile.h>
0021 
0022 #include <filesystem>
0023 #include <iostream>
0024 
0025 #include <QString> // for .endsWith
0026 
0027 #include <catch2-local-includes.h>
0028 
0029 namespace fs = std::filesystem;
0030 
0031 TEST_CASE("Project Management")
0032 {
0033     fs::path project_path = fs::temp_directory_path() / "ct_lvt_testprojects" / "testproject.lks";
0034     fs::path project_backup = fs::temp_directory_path() / "ct_lvt_testprojects" / "testproject.lks";
0035 
0036     Codethink::lvtprj::ProjectFile file;
0037 
0038     REQUIRE(file.isOpen() == false);
0039 
0040     auto err = file.createEmpty();
0041     REQUIRE(!err.has_error());
0042     REQUIRE(file.isOpen() == true);
0043 
0044     file.setProjectName("CodeVis");
0045     file.setProjectInformation("Sligthly longer line of information");
0046     file.setSourceCodePath("/some/path/src/");
0047 
0048     REQUIRE(file.location() == "");
0049 
0050     // Open location uses a random directory name, I can't check for equality.
0051     REQUIRE(file.openLocation().empty() == false);
0052 
0053     REQUIRE(file.projectName() == "CodeVis");
0054     REQUIRE(file.projectInformation() == "Sligthly longer line of information");
0055     REQUIRE(file.sourceCodePath() == "/some/path/src/");
0056 
0057     err = file.saveAs(project_path, Codethink::lvtprj::ProjectFile::BackupFileBehavior::Discard);
0058     if (err.has_error()) {
0059         FAIL("Unexpected error: " << err.error().errorMessage);
0060     }
0061     REQUIRE(err.has_value());
0062     REQUIRE(!err.has_error());
0063     REQUIRE(file.location() == project_path);
0064 
0065     Codethink::lvtprj::ProjectFile project2;
0066     err = project2.open(project_path);
0067     if (err.has_error()) {
0068         FAIL("Unexpected error: " << err.error().errorMessage);
0069     }
0070 
0071     // check that the information of both projects is the same.
0072     REQUIRE(project2.projectName() == file.projectName());
0073     REQUIRE(project2.projectInformation() == file.projectInformation());
0074     REQUIRE(project2.location() == file.location());
0075 
0076     // those needs to be different since there are two instances of the same project open.
0077     REQUIRE(project2.openLocation() != file.openLocation());
0078 
0079     fs::path project_path_without_suffix = fs::temp_directory_path() / "ct_lvt_testprojects" / "testproject";
0080     err = file.saveAs(project_path_without_suffix, Codethink::lvtprj::ProjectFile::BackupFileBehavior::Discard);
0081     if (err.has_error()) {
0082         FAIL("Unexpected error: " << err.error().errorMessage);
0083     }
0084     REQUIRE(!err.has_error());
0085 
0086     QString locationWithoutSuffix = QString::fromStdString(file.location().string());
0087     REQUIRE(locationWithoutSuffix.endsWith(".lks"));
0088 
0089     // cleanup
0090     try {
0091         std::filesystem::remove(project_path);
0092         std::filesystem::remove(project_backup);
0093     } catch (...) {
0094     };
0095 }