Skip to content

Commit

Permalink
refs #10878. Mock out behaviour.
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Jan 13, 2015
1 parent 75043a3 commit c9264e5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
Expand Up @@ -43,10 +43,10 @@ class PeakShape;
virtual ~PeakShapeSphericalFactory();
/// Make product
PeakShape* create(const std::string &source) const;

private:
/// Set a successor should this factory be unsuitable
void setSuccessor(PeakShapeFactory_const_sptr successorFactory);

private:
/// Successor factory
PeakShapeFactory_const_sptr m_successor;

Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/DataObjects/test/CMakeLists.txt
Expand Up @@ -10,7 +10,7 @@ if ( CXXTEST_FOUND )
../../TestHelpers/src/NexusTestHelper.cpp
)
cxxtest_add_test ( DataObjectsTest ${TEST_FILES} )
target_link_libraries( DataObjectsTest DataObjects )
target_link_libraries( DataObjectsTest DataObjects ${GMOCK_LIBRARIES} ${GTEST_LIBRARIES})
# Specify implicit dependency, but don't link to it
add_dependencies ( DataObjectsTest UserAlgorithms )
add_dependencies ( FrameworkTests DataObjectsTest )
Expand Down
23 changes: 23 additions & 0 deletions Code/Mantid/Framework/DataObjects/test/MockObjects.h
@@ -0,0 +1,23 @@
#ifndef MOCKOBJECTS_H_
#define MOCKOBJECTS_H_

#include <gmock/gmock.h>
#include "MantidDataObjects/PeakShapeFactory.h"
#include "MantidDataObjects/PeakShape.h"

namespace Mantid {
namespace DataObjects {

class MockPeakShapeFactory : public PeakShapeFactory {
public:
MOCK_CONST_METHOD1(create,
PeakShape*(const std::string& source));
MOCK_METHOD1(setSuccessor,
void(boost::shared_ptr<const PeakShapeFactory> successorFactory));
virtual ~MockPeakShapeFactory() {}
};

}
}

#endif /* MOCKOBJECTS_H_ */
Expand Up @@ -2,11 +2,15 @@
#define MANTID_DATAOBJECTS_PEAKSHAPESPHERICALFACTORYTEST_H_

#include <cxxtest/TestSuite.h>
#include <gmock/gmock.h>
#include <jsoncpp/json/json.h>

#include "MantidDataObjects/PeakShapeSphericalFactory.h"
#include "MantidDataObjects/PeakShapeSpherical.h"
#include "MantidKernel/VMD.h"
#include "MantidAPI/SpecialCoordinateSystem.h"
#include "MockObjects.h"


using namespace Mantid::DataObjects;
using namespace Mantid::Kernel;
Expand All @@ -21,7 +25,49 @@ class PeakShapeSphericalFactoryTest : public CxxTest::TestSuite
static void destroySuite( PeakShapeSphericalFactoryTest *suite ) { delete suite; }


// More tests needed TODO
void test_invalid_json_with_no_successor()
{
PeakShapeSphericalFactory factory;
TS_ASSERT_THROWS(factory.create(""), std::invalid_argument&);
}

void test_invalid_json_with_successor()
{
using namespace testing;

// We expect it to try to use the deletate factory. If it cannot process the json.
MockPeakShapeFactory* delegate = new MockPeakShapeFactory;
EXPECT_CALL(*delegate, create(_)).Times(1);

PeakShapeSphericalFactory factory;
factory.setSuccessor( PeakShapeFactory_const_sptr(delegate) );
// Run create with empty JSON.
factory.create("");

TS_ASSERT(Mock::VerifyAndClearExpectations(delegate));
}

void test_use_successor_when_different_shape_found()
{
using namespace testing;

// We expect it to try to use the deletate factory. If it cannot process the json.
MockPeakShapeFactory* delegate = new MockPeakShapeFactory;
EXPECT_CALL(*delegate, create(_)).Times(1);

PeakShapeSphericalFactory factory;
factory.setSuccessor( PeakShapeFactory_const_sptr(delegate) );

// Minimal valid JSON for describing the shape.
Json::Value root;
root["shape"] = "square";
Json::StyledWriter writer;
const std::string str_json = writer.write(root);

factory.create(str_json);

TS_ASSERT(Mock::VerifyAndClearExpectations(delegate));
}

void test_create()
{
Expand Down

0 comments on commit c9264e5

Please sign in to comment.