|
| 1 | +#include "gmock/gmock.h" |
| 2 | + |
| 3 | +#include <functional> |
| 4 | + |
| 5 | +#include "CameraFactory.h" |
| 6 | +#include "IException.h" |
| 7 | +#include "Preference.h" |
| 8 | +#include "Pvl.h" |
| 9 | +#include "PvlGroup.h" |
| 10 | +#include "PvlKeyword.h" |
| 11 | + |
| 12 | +using namespace Isis; |
| 13 | + |
| 14 | +// Stable substrings taken from CameraFactory.cpp |
| 15 | +// - CameraVersion(Pvl&) outer catch |
| 16 | +static const QString CAMERA_VERSION_ERROR = |
| 17 | + "Unable to locate latest camera model version number from group [Instrument]"; |
| 18 | + |
| 19 | +class CameraFactoryTests : public ::testing::Test { |
| 20 | +protected: |
| 21 | + static void SetUpTestSuite() { |
| 22 | + // Standard ISIS test setup |
| 23 | + Preference::Preferences(true); |
| 24 | + } |
| 25 | + |
| 26 | + // Helper: ensure an IException is thrown and its message contains substring |
| 27 | + void expectIExceptionContaining(std::function<void()> fn, |
| 28 | + const QString &expectedSubstring) { |
| 29 | + try { |
| 30 | + fn(); |
| 31 | + FAIL() << "Expected an IException to be thrown"; |
| 32 | + } |
| 33 | + catch (IException &e) { |
| 34 | + QString msg = e.toString(); |
| 35 | + EXPECT_TRUE(msg.contains(expectedSubstring)) |
| 36 | + << "Expected exception message to contain:\n [" |
| 37 | + << expectedSubstring.toStdString() |
| 38 | + << "]\nBut actual message was:\n [" |
| 39 | + << msg.toStdString() << "]"; |
| 40 | + } |
| 41 | + catch (...) { |
| 42 | + FAIL() << "Expected an IException, but a different exception type was thrown"; |
| 43 | + } |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | + |
| 48 | +// 1) No Instrument group at all |
| 49 | +TEST_F(CameraFactoryTests, MissingInstrumentGroup) { |
| 50 | + Pvl lab; // empty: no Instrument |
| 51 | + |
| 52 | + expectIExceptionContaining( |
| 53 | + [&lab]() { |
| 54 | + (void) CameraFactory::CameraVersion(lab); |
| 55 | + }, |
| 56 | + CAMERA_VERSION_ERROR); |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +// 2) Instrument group present, but missing SpacecraftName keyword |
| 61 | +TEST_F(CameraFactoryTests, MissingSpacecraftName) { |
| 62 | + Pvl lab; |
| 63 | + PvlGroup inst("Instrument"); |
| 64 | + // No SpacecraftName, no InstrumentId yet |
| 65 | + lab.addGroup(inst); |
| 66 | + |
| 67 | + expectIExceptionContaining( |
| 68 | + [&lab]() { |
| 69 | + (void) CameraFactory::CameraVersion(lab); |
| 70 | + }, |
| 71 | + CAMERA_VERSION_ERROR); |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +// 3) Instrument group has bogus spacecraft/instrument; plugin cannot find a match |
| 76 | +TEST_F(CameraFactoryTests, UnsupportedCameraModel) { |
| 77 | + Pvl lab; |
| 78 | + PvlGroup inst("Instrument"); |
| 79 | + inst += PvlKeyword("SpacecraftName", "Bogus Spacecraft"); |
| 80 | + inst += PvlKeyword("InstrumentId", "Bogus Instrument"); |
| 81 | + lab.addGroup(inst); |
| 82 | + |
| 83 | + // CameraVersion will fail when trying to find a matching plugin group |
| 84 | + expectIExceptionContaining( |
| 85 | + [&lab]() { |
| 86 | + (void) CameraFactory::CameraVersion(lab); |
| 87 | + }, |
| 88 | + CAMERA_VERSION_ERROR); |
| 89 | +} |
0 commit comments