From 07699c3a5290b3203a5d7251af3b19195505b4ad Mon Sep 17 00:00:00 2001
From: Roman Tolchenov <roman.tolchenov@stfc.ac.uk>
Date: Wed, 14 Oct 2015 15:02:50 +0100
Subject: [PATCH] Re #11035. Added the API tests.

---
 Framework/API/CMakeLists.txt                  |  1 -
 .../API/inc/MantidAPI/InstrumentValidator.h   |  1 +
 .../inc/MantidAPI/MatrixWorkspaceValidator.h  |  2 +-
 Framework/API/test/CommonBinsValidatorTest.h  | 41 ++++++++++++++++++-
 Framework/API/test/HistogramValidatorTest.h   | 16 +++++++-
 Framework/API/test/InstrumentValidatorTest.h  | 26 +++++++++++-
 .../API/test/MatrixWorkspaceValidatorTest.h   | 27 ------------
 Framework/API/test/NumericAxisValidatorTest.h | 18 +++++++-
 Framework/API/test/RawCountValidatorTest.h    | 15 ++++++-
 Framework/API/test/SampleValidatorTest.h      | 20 ++++++++-
 Framework/API/test/SpectraAxisValidatorTest.h | 18 ++++++--
 .../API/test/WorkspaceUnitValidatorTest.h     | 17 +++++++-
 .../inc/MantidTestHelpers/FakeObjects.h       |  2 +-
 .../InstrumentWidget/InstrumentActor.cpp      | 34 +++++++--------
 14 files changed, 175 insertions(+), 63 deletions(-)
 delete mode 100644 Framework/API/test/MatrixWorkspaceValidatorTest.h

diff --git a/Framework/API/CMakeLists.txt b/Framework/API/CMakeLists.txt
index cf55421199a..f847f9e46dd 100644
--- a/Framework/API/CMakeLists.txt
+++ b/Framework/API/CMakeLists.txt
@@ -377,7 +377,6 @@ set ( TEST_FILES
 	LogManagerTest.h
 	MDGeometryTest.h
 	MatrixWorkspaceMDIteratorTest.h
-	MatrixWorkspaceValidatorTest.h
 	MemoryManagerTest.h
 	ModeratorModelTest.h
 	MultiDomainFunctionTest.h
diff --git a/Framework/API/inc/MantidAPI/InstrumentValidator.h b/Framework/API/inc/MantidAPI/InstrumentValidator.h
index e6277e1a20a..f448510a245 100644
--- a/Framework/API/inc/MantidAPI/InstrumentValidator.h
+++ b/Framework/API/inc/MantidAPI/InstrumentValidator.h
@@ -2,6 +2,7 @@
 #define MANTID_API_INSTRUMENTVALIDATOR_H_
 
 #include "MantidAPI/DllConfig.h"
+#include "MantidAPI/ExperimentInfo.h"
 #include "MantidKernel/TypedValidator.h"
 
 namespace Mantid {
diff --git a/Framework/API/inc/MantidAPI/MatrixWorkspaceValidator.h b/Framework/API/inc/MantidAPI/MatrixWorkspaceValidator.h
index 9d8c052b5be..c77272bab0d 100644
--- a/Framework/API/inc/MantidAPI/MatrixWorkspaceValidator.h
+++ b/Framework/API/inc/MantidAPI/MatrixWorkspaceValidator.h
@@ -2,7 +2,7 @@
 #define MANTID_API_MATRIXWORKSPACEVALIDATOR_H_
 
 #include "MantidAPI/DllConfig.h"
-#include "MantidAPI/MatrixWorkspace_fwd.h"
+#include "MantidAPI/MatrixWorkspace.h"
 #include "MantidKernel/TypedValidator.h"
 
 namespace Mantid {
diff --git a/Framework/API/test/CommonBinsValidatorTest.h b/Framework/API/test/CommonBinsValidatorTest.h
index 6b6bec0f7f8..52c30750034 100644
--- a/Framework/API/test/CommonBinsValidatorTest.h
+++ b/Framework/API/test/CommonBinsValidatorTest.h
@@ -4,6 +4,7 @@
 #include <cxxtest/TestSuite.h>
 
 #include "MantidAPI/CommonBinsValidator.h"
+#include "MantidTestHelpers/FakeObjects.h"
 
 using Mantid::API::CommonBinsValidator;
 
@@ -14,12 +15,48 @@ public:
   static CommonBinsValidatorTest *createSuite() { return new CommonBinsValidatorTest(); }
   static void destroySuite( CommonBinsValidatorTest *suite ) { delete suite; }
 
+  void test_empty()
+  {
+    auto ws = boost::make_shared<WorkspaceTester>();
+    CommonBinsValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "");
+  }
 
-  void test_Something()
+  void xtest_zero_length_bins()
   {
-    TS_FAIL( "You forgot to write a test!");
+    auto ws = boost::make_shared<WorkspaceTester>();
+    ws->init(2, 11, 10);
+    CommonBinsValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "");
   }
 
+  void test_common_bins()
+  {
+    auto ws = boost::make_shared<WorkspaceTester>();
+    ws->init(3, 11, 10);
+    for(size_t k = 0; k < 3; ++k)
+    for(size_t i = 0; i < 11; ++i)
+    {
+      auto di = double(i);
+      ws->dataX(k)[i] = di * (1.0 + 0.001 * di);
+    }
+    CommonBinsValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "");
+  }
+
+  void test_diff_bins()
+  {
+    auto ws = boost::make_shared<WorkspaceTester>();
+    ws->init(3, 11, 10);
+    for(size_t k = 0; k < 3; ++k)
+    for(size_t i = 0; i < 11; ++i)
+    {
+      auto di = double(i + k);
+      ws->dataX(k)[i] = di * (1.0 + 0.001 * di);
+    }
+    CommonBinsValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "The workspace must have common bin boundaries for all histograms");
+  }
 
 };
 
diff --git a/Framework/API/test/HistogramValidatorTest.h b/Framework/API/test/HistogramValidatorTest.h
index d8914567bc7..5e506df1cf5 100644
--- a/Framework/API/test/HistogramValidatorTest.h
+++ b/Framework/API/test/HistogramValidatorTest.h
@@ -4,6 +4,7 @@
 #include <cxxtest/TestSuite.h>
 
 #include "MantidAPI/HistogramValidator.h"
+#include "MantidTestHelpers/FakeObjects.h"
 
 using Mantid::API::HistogramValidator;
 
@@ -15,9 +16,20 @@ public:
   static void destroySuite( HistogramValidatorTest *suite ) { delete suite; }
 
 
-  void test_Something()
+  void test_success()
   {
-    TS_FAIL( "You forgot to write a test!");
+    auto ws = boost::make_shared<WorkspaceTester>();
+    ws->init(2, 11, 10);
+    HistogramValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "");
+  }
+
+  void test_fail()
+  {
+    auto ws = boost::make_shared<WorkspaceTester>();
+    ws->init(2, 10, 10);
+    HistogramValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "The workspace must contain histogram data");
   }
 
 
diff --git a/Framework/API/test/InstrumentValidatorTest.h b/Framework/API/test/InstrumentValidatorTest.h
index 7c9894a5e5a..72a78d16204 100644
--- a/Framework/API/test/InstrumentValidatorTest.h
+++ b/Framework/API/test/InstrumentValidatorTest.h
@@ -4,6 +4,9 @@
 #include <cxxtest/TestSuite.h>
 
 #include "MantidAPI/InstrumentValidator.h"
+#include "MantidGeometry/Instrument/ObjComponent.h"
+#include "MantidTestHelpers/FakeObjects.h"
+#include "MantidTestHelpers/ComponentCreationHelper.h"
 
 using Mantid::API::InstrumentValidator;
 
@@ -14,10 +17,29 @@ public:
   static InstrumentValidatorTest *createSuite() { return new InstrumentValidatorTest(); }
   static void destroySuite( InstrumentValidatorTest *suite ) { delete suite; }
 
+  void test_success()
+  {
+    auto ws = boost::make_shared<WorkspaceTester>();
+    auto instr = boost::make_shared<Mantid::Geometry::Instrument>("TestInstrument");
+    ws->setInstrument(instr);
+    // Define a sample as a simple sphere
+    auto sample = new Mantid::Geometry::ObjComponent(
+        "samplePos",
+        ComponentCreationHelper::createSphere(0.1, V3D(0, 0, 0), "1"),
+        instr.get());
+    instr->setPos(0.0, 0.0, 0.0);
+    instr->add(sample);
+    instr->markAsSamplePos(sample);
+
+    InstrumentValidator validator;
+    TS_ASSERT_EQUALS(validator.checkValidity(ws), "");
+  }
 
-  void test_Something()
+  void test_fail()
   {
-    TS_FAIL( "You forgot to write a test!");
+    auto ws = boost::make_shared<WorkspaceTester>();
+    InstrumentValidator validator;
+    TS_ASSERT_EQUALS(validator.checkValidity(ws), "The instrument is missing the following components: sample holder");
   }
 
 
diff --git a/Framework/API/test/MatrixWorkspaceValidatorTest.h b/Framework/API/test/MatrixWorkspaceValidatorTest.h
deleted file mode 100644
index 1f16a48247f..00000000000
--- a/Framework/API/test/MatrixWorkspaceValidatorTest.h
+++ /dev/null
@@ -1,27 +0,0 @@
-#ifndef MANTID_API_MATRIXWORKSPACEVALIDATORTEST_H_
-#define MANTID_API_MATRIXWORKSPACEVALIDATORTEST_H_
-
-#include <cxxtest/TestSuite.h>
-
-#include "MantidAPI/MatrixWorkspaceValidator.h"
-
-using Mantid::API::MatrixWorkspaceValidator;
-
-class MatrixWorkspaceValidatorTest : public CxxTest::TestSuite {
-public:
-  // This pair of boilerplate methods prevent the suite being created statically
-  // This means the constructor isn't called when running other tests
-  static MatrixWorkspaceValidatorTest *createSuite() { return new MatrixWorkspaceValidatorTest(); }
-  static void destroySuite( MatrixWorkspaceValidatorTest *suite ) { delete suite; }
-
-
-  void test_Something()
-  {
-    TS_FAIL( "You forgot to write a test!");
-  }
-
-
-};
-
-
-#endif /* MANTID_API_MATRIXWORKSPACEVALIDATORTEST_H_ */
\ No newline at end of file
diff --git a/Framework/API/test/NumericAxisValidatorTest.h b/Framework/API/test/NumericAxisValidatorTest.h
index 3634e8ece0a..70df673a9ff 100644
--- a/Framework/API/test/NumericAxisValidatorTest.h
+++ b/Framework/API/test/NumericAxisValidatorTest.h
@@ -4,6 +4,7 @@
 #include <cxxtest/TestSuite.h>
 
 #include "MantidAPI/NumericAxisValidator.h"
+#include "MantidTestHelpers/FakeObjects.h"
 
 using Mantid::API::NumericAxisValidator;
 
@@ -15,9 +16,22 @@ public:
   static void destroySuite( NumericAxisValidatorTest *suite ) { delete suite; }
 
 
-  void test_Something()
+  void test_success()
   {
-    TS_FAIL( "You forgot to write a test!");
+    auto ws = boost::make_shared<WorkspaceTester>();
+    ws->init(2, 11, 10);
+    auto newAxis = new NumericAxis(2);
+    ws->replaceAxis(1, newAxis);
+    NumericAxisValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "");
+  }
+
+  void test_fail()
+  {
+    auto ws = boost::make_shared<WorkspaceTester>();
+    ws->init(2, 11, 10);
+    NumericAxisValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "A workspace with axis being a Numeric Axis is required here.");
   }
 
 
diff --git a/Framework/API/test/RawCountValidatorTest.h b/Framework/API/test/RawCountValidatorTest.h
index e4221089bb8..0dccddd22f0 100644
--- a/Framework/API/test/RawCountValidatorTest.h
+++ b/Framework/API/test/RawCountValidatorTest.h
@@ -4,6 +4,7 @@
 #include <cxxtest/TestSuite.h>
 
 #include "MantidAPI/RawCountValidator.h"
+#include "MantidTestHelpers/FakeObjects.h"
 
 using Mantid::API::RawCountValidator;
 
@@ -15,9 +16,19 @@ public:
   static void destroySuite( RawCountValidatorTest *suite ) { delete suite; }
 
 
-  void test_Something()
+  void test_success()
   {
-    TS_FAIL( "You forgot to write a test!");
+    auto ws = boost::make_shared<WorkspaceTester>();
+    RawCountValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "");
+  }
+
+  void test_fail()
+  {
+    auto ws = boost::make_shared<WorkspaceTester>();
+    ws->isDistribution(true);
+    RawCountValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "A workspace containing numbers of counts is required here");
   }
 
 
diff --git a/Framework/API/test/SampleValidatorTest.h b/Framework/API/test/SampleValidatorTest.h
index 0ae6e9fe1b1..1a0f38be56c 100644
--- a/Framework/API/test/SampleValidatorTest.h
+++ b/Framework/API/test/SampleValidatorTest.h
@@ -4,6 +4,8 @@
 #include <cxxtest/TestSuite.h>
 
 #include "MantidAPI/SampleValidator.h"
+#include "MantidTestHelpers/FakeObjects.h"
+#include "MantidTestHelpers/ComponentCreationHelper.h"
 
 using Mantid::API::SampleValidator;
 
@@ -15,11 +17,25 @@ public:
   static void destroySuite( SampleValidatorTest *suite ) { delete suite; }
 
 
-  void test_Something()
+  void test_fail()
   {
-    TS_FAIL( "You forgot to write a test!");
+    auto ws = boost::make_shared<WorkspaceTester>();
+    ws->init(2, 11, 10);
+    SampleValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "The sample is missing the following properties: shape,material");
   }
 
+  void test_success()
+  {
+    auto ws = boost::make_shared<WorkspaceTester>();
+    auto sphere = ComponentCreationHelper::createSphere(1.0, V3D(), "sphere");
+    Mantid::Kernel::Material material("stuff", Mantid::PhysicalConstants::NeutronAtom(), 10);
+    sphere->setMaterial(material);
+    ws->mutableSample().setShape(*sphere);
+
+    SampleValidator validator;
+    TS_ASSERT_EQUALS(validator.checkValidity(ws), "");
+  }
 
 };
 
diff --git a/Framework/API/test/SpectraAxisValidatorTest.h b/Framework/API/test/SpectraAxisValidatorTest.h
index 70f35471f72..e2ffd1391ff 100644
--- a/Framework/API/test/SpectraAxisValidatorTest.h
+++ b/Framework/API/test/SpectraAxisValidatorTest.h
@@ -4,6 +4,7 @@
 #include <cxxtest/TestSuite.h>
 
 #include "MantidAPI/SpectraAxisValidator.h"
+#include "MantidTestHelpers/FakeObjects.h"
 
 using Mantid::API::SpectraAxisValidator;
 
@@ -14,12 +15,23 @@ public:
   static SpectraAxisValidatorTest *createSuite() { return new SpectraAxisValidatorTest(); }
   static void destroySuite( SpectraAxisValidatorTest *suite ) { delete suite; }
 
-
-  void test_Something()
+  void test_fail()
   {
-    TS_FAIL( "You forgot to write a test!");
+    auto ws = boost::make_shared<WorkspaceTester>();
+    ws->init(2, 11, 10);
+    auto newAxis = new NumericAxis(2);
+    ws->replaceAxis(1, newAxis);
+    SpectraAxisValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "A workspace with axis being Spectra Number is required here.");
   }
 
+  void test_success()
+  {
+    auto ws = boost::make_shared<WorkspaceTester>();
+    ws->init(2, 11, 10);
+    SpectraAxisValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "");
+  }
 
 };
 
diff --git a/Framework/API/test/WorkspaceUnitValidatorTest.h b/Framework/API/test/WorkspaceUnitValidatorTest.h
index 3e3d13b23be..d77fd7482d3 100644
--- a/Framework/API/test/WorkspaceUnitValidatorTest.h
+++ b/Framework/API/test/WorkspaceUnitValidatorTest.h
@@ -4,6 +4,7 @@
 #include <cxxtest/TestSuite.h>
 
 #include "MantidAPI/WorkspaceUnitValidator.h"
+#include "MantidTestHelpers/FakeObjects.h"
 
 using Mantid::API::WorkspaceUnitValidator;
 
@@ -15,9 +16,21 @@ public:
   static void destroySuite( WorkspaceUnitValidatorTest *suite ) { delete suite; }
 
 
-  void test_Something()
+  void test_fail()
   {
-    TS_FAIL( "You forgot to write a test!");
+    auto ws = boost::make_shared<WorkspaceTester>();
+    ws->init(2, 11, 10);
+    WorkspaceUnitValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "The workspace must have units");
+  }
+
+  void test_success()
+  {
+    auto ws = boost::make_shared<WorkspaceTester>();
+    ws->init(2, 11, 10);
+    ws->getAxis(0)->setUnit("TOF");
+    WorkspaceUnitValidator validator;
+    TS_ASSERT_EQUALS(validator.isValid(ws), "");
   }
 
 
diff --git a/Framework/TestHelpers/inc/MantidTestHelpers/FakeObjects.h b/Framework/TestHelpers/inc/MantidTestHelpers/FakeObjects.h
index b9c906a2804..59a51ac705f 100644
--- a/Framework/TestHelpers/inc/MantidTestHelpers/FakeObjects.h
+++ b/Framework/TestHelpers/inc/MantidTestHelpers/FakeObjects.h
@@ -118,7 +118,7 @@ public:
     m_axes[1] = new Mantid::API::SpectraAxis(this);
   }
   size_t size() const { return vec.size() * blocksize(); }
-  size_t blocksize() const { return vec[0].dataY().size(); }
+  size_t blocksize() const { return vec.empty() ? 0 : vec[0].dataY().size(); }
   ISpectrum *getSpectrum(const size_t index) { return &vec[index]; }
   const ISpectrum *getSpectrum(const size_t index) const {
     return &vec[index];
diff --git a/MantidPlot/src/Mantid/InstrumentWidget/InstrumentActor.cpp b/MantidPlot/src/Mantid/InstrumentWidget/InstrumentActor.cpp
index 5e35f163b35..5c0e4b60da7 100644
--- a/MantidPlot/src/Mantid/InstrumentWidget/InstrumentActor.cpp
+++ b/MantidPlot/src/Mantid/InstrumentWidget/InstrumentActor.cpp
@@ -1,35 +1,37 @@
 #include "InstrumentActor.h"
 #include "CompAssemblyActor.h"
-#include "ObjComponentActor.h"
-#include "SampleActor.h"
 #include "ComponentActor.h"
+#include "GLActorVisitor.h"
 #include "ObjCompAssemblyActor.h"
+#include "ObjComponentActor.h"
 #include "RectangularDetectorActor.h"
-#include "GLActorVisitor.h"
+#include "SampleActor.h"
 
-#include "MantidKernel/Exception.h"
-#include "MantidKernel/V3D.h"
-#include "MantidKernel/ConfigService.h"
-#include "MantidGeometry/Objects/Object.h"
-#include "MantidGeometry/ICompAssembly.h"
-#include "MantidGeometry/IObjComponent.h"
-#include "MantidAPI/MatrixWorkspace.h"
-#include "MantidAPI/IMaskWorkspace.h"
 #include "MantidAPI/AnalysisDataService.h"
+#include "MantidAPI/CommonBinsValidator.h"
 #include "MantidAPI/FrameworkManager.h"
 #include "MantidAPI/IAlgorithm.h"
+#include "MantidAPI/IMaskWorkspace.h"
+#include "MantidAPI/MatrixWorkspace.h"
 #include "MantidAPI/WorkspaceFactory.h"
-#include "MantidAPI/WorkspaceValidators.h"
 
-#include <boost/math/special_functions/fpclassify.hpp>
+#include "MantidGeometry/Objects/Object.h"
+#include "MantidGeometry/ICompAssembly.h"
+#include "MantidGeometry/IDTypes.h"
+#include "MantidGeometry/IObjComponent.h"
+
+#include "MantidKernel/ConfigService.h"
+#include "MantidKernel/Exception.h"
+#include "MantidKernel/ReadLock.h"
+#include "MantidKernel/V3D.h"
+
 #include <boost/algorithm/string.hpp>
+#include <boost/math/special_functions/fpclassify.hpp>
 
-#include <QSettings>
 #include <QMessageBox>
+#include <QSettings>
 
 #include <numeric>
-#include "MantidGeometry/IDTypes.h"
-#include "MantidKernel/ReadLock.h"
 
 using namespace Mantid::Kernel::Exception;
 using namespace Mantid::Geometry;
-- 
GitLab