diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/IFunction.h b/Code/Mantid/Framework/API/inc/MantidAPI/IFunction.h index 6499e23dd679..6027de4d2636 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/IFunction.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/IFunction.h @@ -422,7 +422,8 @@ class MANTID_API_DLL IFunction /// Set an attribute value template void setAttributeValue(const std::string& attName,const T& value){setAttribute(attName,Attribute(value));} - void setAttributeValue(const std::string& attName,const char* value){setAttribute(attName,Attribute(std::string(value)));} + void setAttributeValue(const std::string& attName,const char* value); + void setAttributeValue(const std::string& attName,const std::string& value); //@} /// Set up the function for a fit. diff --git a/Code/Mantid/Framework/API/src/IFunction.cpp b/Code/Mantid/Framework/API/src/IFunction.cpp index aaf8426f0bcb..571f6008c167 100644 --- a/Code/Mantid/Framework/API/src/IFunction.cpp +++ b/Code/Mantid/Framework/API/src/IFunction.cpp @@ -167,7 +167,7 @@ std::string IFunction::asString()const { std::string attName = attr[i]; std::string attValue = this->getAttribute(attr[i]).value(); - if (!attValue.empty()) + if (!attValue.empty() && attValue != "\"\"" ) { ostr<<','< IFunction::getAttributeNames() const { diff --git a/Code/Mantid/Framework/API/test/FunctionAttributeTest.h b/Code/Mantid/Framework/API/test/FunctionAttributeTest.h index 366abc7a2525..8cb273a50a9a 100644 --- a/Code/Mantid/Framework/API/test/FunctionAttributeTest.h +++ b/Code/Mantid/Framework/API/test/FunctionAttributeTest.h @@ -22,6 +22,7 @@ class IFT_Funct: public ParamFunction, public IFunction1D declareAttribute("IAttr", Attribute(0)); declareAttribute("BAttr", Attribute(false)); declareAttribute("SAttr", Attribute("")); + declareAttribute("SQAttr", Attribute("",true)); declareAttribute("VAttr", Attribute(std::vector())); std::vector v(3); v[0] = 1; @@ -152,6 +153,30 @@ class FunctionAttributeTest : public CxxTest::TestSuite } + void test_quoted_string_attribute() + { + IFT_Funct f; + IFunction::Attribute att = f.getAttribute("SQAttr"); + + TS_ASSERT_EQUALS( att.asString(), "\"\"" ); + TS_ASSERT_EQUALS( att.type(), "std::string" ); + + att.setString( "text" ); + TS_ASSERT_EQUALS( att.asString(), "\"text\"" ); + + att.fromString("25"); + TS_ASSERT_EQUALS( att.asString(), "\"25\"" ); + + f.setAttribute("SQAttr", IFunction::Attribute("Hello",true)); + TS_ASSERT_EQUALS( f.getAttribute("SQAttr").asString(), "\"Hello\"" ); + + f.setAttributeValue("SQAttr", "World"); + TS_ASSERT_EQUALS( f.getAttribute("SQAttr").asString(), "\"World\"" ); + + TS_ASSERT( f.getAttribute("SQAttr").value() == "\"World\"" ); + + } + void test_vector_attribute() { IFT_Funct f; @@ -231,19 +256,26 @@ class FunctionAttributeTest : public CxxTest::TestSuite void test_factory_creation() { auto f = Mantid::API::FunctionFactory::Instance().createInitialized( - "name=IFT_Funct,DAttr=12.0,IAttr=777,BAttr=true, SAttr= \"Hello world!\",VAttr=(4,5,6)"); + "name=IFT_Funct,DAttr=12.0,IAttr=777,BAttr=true, SAttr= \"Hello world!\", SQAttr= \"Hello world!\",VAttr=(4,5,6)"); TS_ASSERT( f ); TS_ASSERT_EQUALS( f->getAttribute("DAttr").asDouble(), 12.0 ); TS_ASSERT_EQUALS( f->getAttribute("IAttr").asInt(), 777 ); TS_ASSERT_EQUALS( f->getAttribute("BAttr").asBool(), true ); TS_ASSERT_EQUALS( f->getAttribute("SAttr").asString(), "Hello world!" ); + TS_ASSERT_EQUALS( f->getAttribute("SQAttr").asString(), "\"Hello world!\"" ); std::vector v = f->getAttribute("VAttr").asVector(); TS_ASSERT_EQUALS( v.size(), 3 ); TS_ASSERT_EQUALS( v[0], 4 ); TS_ASSERT_EQUALS( v[1], 5 ); TS_ASSERT_EQUALS( v[2], 6 ); - TS_ASSERT_EQUALS( f->asString(), "name=IFT_Funct,BAttr=true,DAttr=12,IAttr=777,SAttr=Hello world!,VAttr=(4,5,6),VAttr1=(1,2,3)" ); + TS_ASSERT_EQUALS( f->asString(), "name=IFT_Funct,BAttr=true,DAttr=12,IAttr=777,SAttr=Hello world!,SQAttr=\"Hello world!\",VAttr=(4,5,6),VAttr1=(1,2,3)" ); + } + + void test_empty_string_attributes_do_not_show_by_asString() + { + IFT_Funct f; + TS_ASSERT_EQUALS( f.asString(), "name=IFT_Funct,BAttr=false,DAttr=0,IAttr=0,VAttr=(),VAttr1=(1,2,3)" ); } }; diff --git a/Code/Mantid/Framework/CurveFitting/test/TabulatedFunctionTest.h b/Code/Mantid/Framework/CurveFitting/test/TabulatedFunctionTest.h index e53faba3138e..ccc3d05fbd7e 100644 --- a/Code/Mantid/Framework/CurveFitting/test/TabulatedFunctionTest.h +++ b/Code/Mantid/Framework/CurveFitting/test/TabulatedFunctionTest.h @@ -10,6 +10,7 @@ #include "MantidAPI/AlgorithmFactory.h" #include "MantidAPI/AnalysisDataService.h" #include "MantidAPI/FileFinder.h" +#include "MantidAPI/FunctionFactory.h" #include "MantidTestHelpers/WorkspaceCreationHelper.h" @@ -233,6 +234,31 @@ class TabulatedFunctionTest : public CxxTest::TestSuite TS_ASSERT( fun.hasAttribute("WorkspaceIndex") ); } + void test_factory_create_from_file() + { + std::string inif = "name=TabulatedFunction,FileName=\"" + m_nexusFileName + "\",WorkspaceIndex=17,Scaling=2"; + auto funf = Mantid::API::FunctionFactory::Instance().createInitialized(inif); + TS_ASSERT( funf ); + TS_ASSERT_EQUALS( funf->getAttribute("Workspace").asString(), ""); + TS_ASSERT_EQUALS( funf->getAttribute("WorkspaceIndex").asInt(), 17); + TS_ASSERT_EQUALS( funf->getAttribute("FileName").asUnquotedString(), m_nexusFileName); + TS_ASSERT_EQUALS( funf->getParameter("Scaling"), 2.0); + } + + void test_factory_create_from_workspace() + { + auto ws = WorkspaceCreationHelper::Create2DWorkspaceFromFunction(Fun(),1,-5.0,5.0,0.1,false); + AnalysisDataService::Instance().add( "TABULATEDFUNCTIONTEST_WS", ws ); + std::string inif = "name=TabulatedFunction,Workspace=TABULATEDFUNCTIONTEST_WS,WorkspaceIndex=71,Scaling=3.14"; + auto funf = Mantid::API::FunctionFactory::Instance().createInitialized(inif); + TS_ASSERT( funf ); + TS_ASSERT_EQUALS( funf->getAttribute("Workspace").asString(), "TABULATEDFUNCTIONTEST_WS"); + TS_ASSERT_EQUALS( funf->getAttribute("WorkspaceIndex").asInt(), 71); + TS_ASSERT_EQUALS( funf->getAttribute("FileName").asUnquotedString(), ""); + TS_ASSERT_EQUALS( funf->getParameter("Scaling"), 3.14); + AnalysisDataService::Instance().clear(); + } + private: const std::string m_asciiFileName; const std::string m_nexusFileName;