|
| 1 | +/*********************************************************************************************************************** |
| 2 | +* OpenStudio(R), Copyright (c) 2008-2023, Alliance for Sustainable Energy, LLC, and other contributors. All rights reserved. |
| 3 | +* |
| 4 | +* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the |
| 5 | +* following conditions are met: |
| 6 | +* |
| 7 | +* (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following |
| 8 | +* disclaimer. |
| 9 | +* |
| 10 | +* (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following |
| 11 | +* disclaimer in the documentation and/or other materials provided with the distribution. |
| 12 | +* |
| 13 | +* (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote products |
| 14 | +* derived from this software without specific prior written permission from the respective party. |
| 15 | +* |
| 16 | +* (4) Other than as required in clauses (1) and (2), distributions in any form of modifications or other derivative works |
| 17 | +* may not use the "OpenStudio" trademark, "OS", "os", or any other confusingly similar designation without specific prior |
| 18 | +* written permission from Alliance for Sustainable Energy, LLC. |
| 19 | +* |
| 20 | +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND ANY CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 21 | +* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | +* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S), ANY CONTRIBUTORS, THE UNITED STATES GOVERNMENT, OR THE UNITED |
| 23 | +* STATES DEPARTMENT OF ENERGY, NOR ANY OF THEIR EMPLOYEES, BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | +* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 25 | +* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | +* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 27 | +* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +***********************************************************************************************************************/ |
| 29 | + |
| 30 | +#include <gtest/gtest.h> |
| 31 | +#include "EnergyPlusFixture.hpp" |
| 32 | + |
| 33 | +#include "../ForwardTranslator.hpp" |
| 34 | + |
| 35 | +#include "../../model/Model.hpp" |
| 36 | +#include "../../model/PythonPluginSearchPaths.hpp" |
| 37 | +#include "../../model/PythonPluginSearchPaths_Impl.hpp" |
| 38 | +#include "../../model/ExternalFile.hpp" |
| 39 | +#include "../../model/ExternalFile_Impl.hpp" |
| 40 | +#include "../../model/PythonPluginInstance.hpp" |
| 41 | +#include "../../model/PythonPluginInstance_Impl.hpp" |
| 42 | + |
| 43 | +#include "../../utilities/idf/Workspace.hpp" |
| 44 | +#include "../../utilities/idf/IdfObject.hpp" |
| 45 | +#include "../../utilities/idf/WorkspaceObject.hpp" |
| 46 | +#include "../../utilities/idf/IdfExtensibleGroup.hpp" |
| 47 | +#include "../../utilities/idf/WorkspaceExtensibleGroup.hpp" |
| 48 | +#include "../../utilities/core/PathHelpers.hpp" |
| 49 | +// E+ FieldEnums |
| 50 | +#include <utilities/idd/IddEnums.hxx> |
| 51 | +#include <utilities/idd/IddFactory.hxx> |
| 52 | +#include <utilities/idd/PythonPlugin_SearchPaths_FieldEnums.hxx> |
| 53 | + |
| 54 | +#include <resources.hxx> |
| 55 | + |
| 56 | +using namespace openstudio::energyplus; |
| 57 | +using namespace openstudio::model; |
| 58 | +using namespace openstudio; |
| 59 | + |
| 60 | +TEST_F(EnergyPlusFixture, ForwardTranslator_PythonPluginSearchPaths) { |
| 61 | + |
| 62 | + ForwardTranslator ft; |
| 63 | + |
| 64 | + Model m; |
| 65 | + PythonPluginSearchPaths pythonPluginSearchPaths = m.getUniqueModelObject<PythonPluginSearchPaths>(); |
| 66 | + |
| 67 | + pythonPluginSearchPaths.setName("My PythonPluginSearchPaths"); |
| 68 | + |
| 69 | + EXPECT_TRUE(pythonPluginSearchPaths.searchPaths().empty()); |
| 70 | + // No search paths; not translated |
| 71 | + { |
| 72 | + EXPECT_TRUE(pythonPluginSearchPaths.setAddCurrentWorkingDirectorytoSearchPath(false)); // Opposite from IDD default |
| 73 | + EXPECT_TRUE(pythonPluginSearchPaths.setAddInputFileDirectorytoSearchPath(false)); // Opposite from IDD default |
| 74 | + EXPECT_TRUE(pythonPluginSearchPaths.setAddepinEnvironmentVariabletoSearchPath(false)); // Opposite from IDD default |
| 75 | + |
| 76 | + const Workspace w = ft.translateModel(m); |
| 77 | + |
| 78 | + const auto idfObjs = w.getObjectsByType(IddObjectType::PythonPlugin_SearchPaths); |
| 79 | + ASSERT_EQ(0u, idfObjs.size()); |
| 80 | + } |
| 81 | + |
| 82 | + { |
| 83 | + EXPECT_TRUE(pythonPluginSearchPaths.setAddCurrentWorkingDirectorytoSearchPath(true)); |
| 84 | + EXPECT_TRUE(pythonPluginSearchPaths.setAddInputFileDirectorytoSearchPath(true)); |
| 85 | + EXPECT_TRUE(pythonPluginSearchPaths.setAddepinEnvironmentVariabletoSearchPath(true)); |
| 86 | + |
| 87 | + std::vector<std::string> searchPaths({"/path/to/lib1", "/path/to/lib2"}); |
| 88 | + EXPECT_TRUE(pythonPluginSearchPaths.setSearchPaths(searchPaths)); |
| 89 | + EXPECT_EQ(2u, pythonPluginSearchPaths.searchPaths().size()); |
| 90 | + |
| 91 | + const Workspace w = ft.translateModel(m); |
| 92 | + |
| 93 | + const auto idfObjs = w.getObjectsByType(IddObjectType::PythonPlugin_SearchPaths); |
| 94 | + ASSERT_EQ(1u, idfObjs.size()); |
| 95 | + |
| 96 | + const auto& idfObject = idfObjs.front(); |
| 97 | + EXPECT_EQ("Yes", idfObject.getString(PythonPlugin_SearchPathsFields::AddCurrentWorkingDirectorytoSearchPath).get()); |
| 98 | + EXPECT_EQ("Yes", idfObject.getString(PythonPlugin_SearchPathsFields::AddInputFileDirectorytoSearchPath).get()); |
| 99 | + EXPECT_EQ("Yes", idfObject.getString(PythonPlugin_SearchPathsFields::AddepinEnvironmentVariabletoSearchPath).get()); |
| 100 | + |
| 101 | + ASSERT_EQ(2u, idfObject.extensibleGroups().size()); |
| 102 | + for (int i = 0; i < 2; ++i) { |
| 103 | + EXPECT_EQ(searchPaths[i], idfObject.extensibleGroups()[i].getString(PythonPlugin_SearchPathsExtensibleFields::SearchPath).get()); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + { |
| 108 | + EXPECT_TRUE(pythonPluginSearchPaths.setAddCurrentWorkingDirectorytoSearchPath(false)); // Opposite from IDD default |
| 109 | + EXPECT_TRUE(pythonPluginSearchPaths.setAddInputFileDirectorytoSearchPath(false)); // Opposite from IDD default |
| 110 | + EXPECT_TRUE(pythonPluginSearchPaths.setAddepinEnvironmentVariabletoSearchPath(false)); // Opposite from IDD default |
| 111 | + |
| 112 | + std::vector<std::string> searchPaths({"/path/to/lib1", "/path/to/lib2"}); |
| 113 | + EXPECT_TRUE(pythonPluginSearchPaths.setSearchPaths(searchPaths)); |
| 114 | + EXPECT_EQ(2u, pythonPluginSearchPaths.searchPaths().size()); |
| 115 | + |
| 116 | + const Workspace w = ft.translateModel(m); |
| 117 | + |
| 118 | + const auto idfObjs = w.getObjectsByType(IddObjectType::PythonPlugin_SearchPaths); |
| 119 | + ASSERT_EQ(1u, idfObjs.size()); |
| 120 | + |
| 121 | + const auto& idfObject = idfObjs.front(); |
| 122 | + EXPECT_EQ("No", idfObject.getString(PythonPlugin_SearchPathsFields::AddCurrentWorkingDirectorytoSearchPath).get()); |
| 123 | + EXPECT_EQ("No", idfObject.getString(PythonPlugin_SearchPathsFields::AddInputFileDirectorytoSearchPath).get()); |
| 124 | + EXPECT_EQ("No", idfObject.getString(PythonPlugin_SearchPathsFields::AddepinEnvironmentVariabletoSearchPath).get()); |
| 125 | + |
| 126 | + ASSERT_EQ(2u, idfObject.extensibleGroups().size()); |
| 127 | + for (int i = 0; i < 2; ++i) { |
| 128 | + EXPECT_EQ(searchPaths[i], idfObject.extensibleGroups()[i].getString(PythonPlugin_SearchPathsExtensibleFields::SearchPath).get()); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + { |
| 133 | + EXPECT_TRUE(pythonPluginSearchPaths.setAddCurrentWorkingDirectorytoSearchPath(false)); // Opposite from IDD default |
| 134 | + EXPECT_TRUE(pythonPluginSearchPaths.setAddInputFileDirectorytoSearchPath(false)); // Opposite from IDD default |
| 135 | + EXPECT_TRUE(pythonPluginSearchPaths.setAddepinEnvironmentVariabletoSearchPath(false)); // Opposite from IDD default |
| 136 | + |
| 137 | + openstudio::path p = resourcesPath() / toPath("model/PythonPluginThermochromicWindow.py"); |
| 138 | + ASSERT_TRUE(exists(p)); |
| 139 | + |
| 140 | + boost::optional<ExternalFile> externalfile = ExternalFile::getExternalFile(m, openstudio::toString(p)); |
| 141 | + ASSERT_TRUE(externalfile) << "Path doesn't exist: '" << p << "'"; |
| 142 | + |
| 143 | + PythonPluginInstance pythonPluginInstance(*externalfile, "ZN_1_wall_south_Window_1_Control"); |
| 144 | + |
| 145 | + std::vector<std::string> searchPaths({"/path/to/lib1", "/path/to/lib2"}); |
| 146 | + EXPECT_TRUE(pythonPluginSearchPaths.setSearchPaths(searchPaths)); |
| 147 | + EXPECT_EQ(2u, pythonPluginSearchPaths.searchPaths().size()); |
| 148 | + |
| 149 | + const Workspace w = ft.translateModel(m); |
| 150 | + |
| 151 | + const auto idfObjs = w.getObjectsByType(IddObjectType::PythonPlugin_SearchPaths); |
| 152 | + ASSERT_EQ(1u, idfObjs.size()); |
| 153 | + |
| 154 | + const auto& idfObject = idfObjs.front(); |
| 155 | + EXPECT_EQ("No", idfObject.getString(PythonPlugin_SearchPathsFields::AddCurrentWorkingDirectorytoSearchPath).get()); |
| 156 | + EXPECT_EQ("No", idfObject.getString(PythonPlugin_SearchPathsFields::AddInputFileDirectorytoSearchPath).get()); |
| 157 | + EXPECT_EQ("No", idfObject.getString(PythonPlugin_SearchPathsFields::AddepinEnvironmentVariabletoSearchPath).get()); |
| 158 | + |
| 159 | + ASSERT_EQ(3u, idfObject.extensibleGroups().size()); |
| 160 | + for (int i = 0; i < 2; ++i) { |
| 161 | + EXPECT_EQ(searchPaths[i], idfObject.extensibleGroups()[i].getString(PythonPlugin_SearchPathsExtensibleFields::SearchPath).get()); |
| 162 | + } |
| 163 | + // since PythonPlugin_Instance is translated after PythonPlugin_SearchPaths, this search path is appended to the existing search paths |
| 164 | + EXPECT_EQ(openstudio::toString(externalfile->filePath().parent_path()), |
| 165 | + idfObject.extensibleGroups()[2].getString(PythonPlugin_SearchPathsExtensibleFields::SearchPath).get()); |
| 166 | + } |
| 167 | +} |
0 commit comments