Skip to content
Snippets Groups Projects
TableWorkspacePropertyTest.h 3.07 KiB
Newer Older
#ifndef TESTTABLEWORKSPACEPROPERTY_
#define TESTTABLEWORKSPACEPROPERTY_

#include <vector>
#include <algorithm>
#include <boost/shared_ptr.hpp>
#include <cxxtest/TestSuite.h>

#include "MantidDataObjects/TableWorkspace.h"
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidKernel/Property.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/ColumnFactory.h"
using namespace Mantid::DataObjects;
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace std;

class TableWorkspaceAlgorithm : public Algorithm {
  /// no arg constructor
  TableWorkspaceAlgorithm() : Algorithm() {}
  /// virtual destructor
  ~TableWorkspaceAlgorithm() override {}
  /// Algorithm's name for identification overriding a virtual method
  const std::string name() const override { return "TableWorkspaceAlgorithm"; }
  /// Algorithm's version for identification overriding a virtual method
  int version() const override { return (1); }
  /// Algorithm's category for identification overriding a virtual method
  const std::string category() const override { return "Examples"; }
  const std::string summary() const override { return "Test summary"; }
  /// Initialisation code
  void init() override;
  /// Execution code
  void exec() override;

  /// Static reference to the logger class
  static Mantid::Kernel::Logger &g_log;
void TableWorkspaceAlgorithm::init() {
  declareProperty(Mantid::Kernel::make_unique<WorkspaceProperty<Workspace>>(
      "Table", "", Direction::Input));
void TableWorkspaceAlgorithm::exec() {
  Workspace_sptr b = getProperty("Table");
  TableWorkspace_sptr t = boost::dynamic_pointer_cast<TableWorkspace>(b);
  TableRow r = t->getFirstRow();
  r << "FIRST" << 11;
  r.next();
  r << "SECOND" << 22;
class TableWorkspacePropertyTest : public CxxTest::TestSuite {
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
  static TableWorkspacePropertyTest *createSuite() {
    return new TableWorkspacePropertyTest();
  }
  static void destroySuite(TableWorkspacePropertyTest *suite) { delete suite; }
  TableWorkspacePropertyTest() {
    t.reset(new TableWorkspace(10));
    t->addColumn("str", "Name");
    t->addColumn("int", "Nunber");
    AnalysisDataService::Instance().add(
        "tst", boost::dynamic_pointer_cast<Workspace>(t));
  }
  void testProperty() {
    TableWorkspaceAlgorithm alg;
    alg.initialize();
    alg.setPropertyValue("Table", "tst");
    alg.execute();
    TableWorkspace_sptr table;
    TS_ASSERT_THROWS_NOTHING(
        table =
            AnalysisDataService::Instance().retrieveWS<TableWorkspace>("tst"));
    TS_ASSERT(table);
    TS_ASSERT_EQUALS(table->rowCount(), 10);
    TableRow r = table->getFirstRow();
    std::string s;
    int n;
    r >> s >> n;
    TS_ASSERT_EQUALS(s, "FIRST");
    TS_ASSERT_EQUALS(n, 11);
    r.next();
    r >> s >> n;
    TS_ASSERT_EQUALS(s, "SECOND");
    TS_ASSERT_EQUALS(n, 22);
  }
private:
  boost::shared_ptr<TableWorkspace> t;
};
#endif /*TESTTABLEWORKSPACE_*/