diff --git a/Code/Mantid/Framework/API/test/WorkspaceGroupTest.h b/Code/Mantid/Framework/API/test/WorkspaceGroupTest.h index 70ccc259c6a0..6884bf0973f2 100644 --- a/Code/Mantid/Framework/API/test/WorkspaceGroupTest.h +++ b/Code/Mantid/Framework/API/test/WorkspaceGroupTest.h @@ -65,19 +65,19 @@ class WorkspaceGroupTest : public CxxTest::TestSuite public: /// Make a simple group - WorkspaceGroup_sptr makeGroup() + WorkspaceGroup_sptr makeGroup(const std::string& groupName = "group",const std::string& prefix = "ws") { + WorkspaceGroup_sptr group(new WorkspaceGroup()); + AnalysisDataService::Instance().addOrReplace(groupName, group); + for (size_t i=0; i<3; i++) { boost::shared_ptr ws(new WorkspaceTester()); ws->initialize(2,3,4); - AnalysisDataService::Instance().addOrReplace("ws" + Strings::toString(i), ws); + AnalysisDataService::Instance().addOrReplace(prefix + Strings::toString(i), ws); + group->add(prefix + Strings::toString(i)); } - WorkspaceGroup_sptr group(new WorkspaceGroup()); - AnalysisDataService::Instance().addOrReplace("group", group); - group->add("ws0"); - group->add("ws1"); - group->add("ws2"); + return group; } @@ -334,6 +334,78 @@ class WorkspaceGroupTest : public CxxTest::TestSuite TS_ASSERT_THROWS( group->isInGroup( *b ), std::runtime_error ); } + void test_renameWorkspaceToWorkspaceGroupChild() + { + WorkspaceGroup_sptr group = makeGroup(); + + //create new top level workspace + boost::shared_ptr ws(new WorkspaceTester()); + ws->initialize(5,6,7); + AnalysisDataService::Instance().addOrReplace("NewWorkspace", ws); + + //rename workspace should replace child in group + TS_ASSERT_THROWS_NOTHING( AnalysisDataService::Instance().rename("NewWorkspace", "ws1") ); + + TS_ASSERT( group->getNames().size() == 3 ); + TS_ASSERT_THROWS_NOTHING( group->getItem("ws1") ); + auto ws1 = boost::dynamic_pointer_cast(AnalysisDataService::Instance().retrieve("ws1")); + TS_ASSERT( ws->blocksize() == 7 ); + } + + void test_renameWorkspaceChildToTopLevelWorkspace() + { + WorkspaceGroup_sptr group = makeGroup(); + + //create new top level workspace + boost::shared_ptr ws(new WorkspaceTester()); + ws->initialize(5,6,7); + AnalysisDataService::Instance().addOrReplace("NewWorkspace", ws); + + //rename child should replace top level workspace + TS_ASSERT_THROWS_NOTHING( AnalysisDataService::Instance().rename("ws1", "NewWorkspace") ); + + TS_ASSERT_EQUALS( group->getNames().size(), 3 ); + TS_ASSERT_THROWS( group->getItem("ws1"), std::out_of_range ); + TS_ASSERT_THROWS_NOTHING( group->getItem("NewWorkspace") ); + auto ws1 = boost::dynamic_pointer_cast(group->getItem("NewWorkspace")); + TS_ASSERT( ws1->blocksize() == 4 ); + } + + void test_renameChildFromOneGroupToAnother() + { + WorkspaceGroup_sptr groupA = makeGroup("wsA", "wsA"); + WorkspaceGroup_sptr groupB = makeGroup("wsB", "wsB"); + + TS_ASSERT_THROWS_NOTHING( AnalysisDataService::Instance().rename("wsA1", "wsB1") ); + + TS_ASSERT_EQUALS( groupA->getNames().size(), 3 ); + TS_ASSERT_EQUALS( groupB->getNames().size(), 3 ); + + TS_ASSERT_THROWS( groupA->getItem("wsA1"), std::out_of_range ); + TS_ASSERT_THROWS_NOTHING( groupB->getItem("wsB1") ); + + auto ws = boost::dynamic_pointer_cast(groupB->getItem("wsB1")); + TS_ASSERT( ws->blocksize() == 4 ); + } + + void test_renameOneGroupToAnother() + { + WorkspaceGroup_sptr groupA = makeGroup("wsA", "wsA"); + WorkspaceGroup_sptr groupB = makeGroup("wsB", "wsB"); + + TS_ASSERT_THROWS_NOTHING( AnalysisDataService::Instance().rename("wsB", "wsA") ); + + TS_ASSERT_THROWS_NOTHING( groupA = boost::dynamic_pointer_cast(AnalysisDataService::Instance().retrieve("wsA")) ); + TS_ASSERT_THROWS( AnalysisDataService::Instance().retrieve("wsB"), Mantid::Kernel::Exception::NotFoundError ); + + TS_ASSERT_THROWS_NOTHING( groupA->getItem("wsB1") ); + TS_ASSERT_THROWS( groupA->getItem("wsA1"), std::out_of_range ); + + //A's workspaces should still exist outside the renamed group + TS_ASSERT_THROWS_NOTHING( AnalysisDataService::Instance().retrieve("wsA1") ); + TS_ASSERT_THROWS_NOTHING( AnalysisDataService::Instance().retrieve("wsA2") ); + } + }; diff --git a/Code/Mantid/Framework/Algorithms/src/RenameWorkspace.cpp b/Code/Mantid/Framework/Algorithms/src/RenameWorkspace.cpp index a8a47796e3ab..3a563b880775 100644 --- a/Code/Mantid/Framework/Algorithms/src/RenameWorkspace.cpp +++ b/Code/Mantid/Framework/Algorithms/src/RenameWorkspace.cpp @@ -1,10 +1,11 @@ /*WIKI* -Renames a workspace to a different name in the data service. If the same name is provided for input and output then the algorithm will fail with an error. The Renaming is implemented as a removal of the original workspace from the data service and re-addition under the new name. +Renames a workspace to a different name in the data service. If the same name is provided for input and output then the algorithm will fail with an error. If run on a group workspace, the members of the group will be renamed if their names follow the pattern groupName_1, groupName_2, etc. (they will be renamed to newName_1, newname_2, etc.). Otherwise, only the group itself will be renamed - the members will keep their previous names. +There is a known issue that if a member of a workspace group is renamed to be the same as the member of a different workspace group, two pointers to the same object workspace are created. *WIKI*/ //---------------------------------------------------------------------- // Includes