Skip to content

Commit

Permalink
Re #2389. Correction to handling empty string attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Nov 12, 2013
1 parent eeaa228 commit 5c5c7e3
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/IFunction.h
Expand Up @@ -422,7 +422,8 @@ class MANTID_API_DLL IFunction
/// Set an attribute value
template<typename T>
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.
Expand Down
28 changes: 27 additions & 1 deletion Code/Mantid/Framework/API/src/IFunction.cpp
Expand Up @@ -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<<','<<attName<<'='<<attValue;
}
Expand Down Expand Up @@ -415,6 +415,9 @@ std::string IFunction::Attribute::asQuotedString()const
throw std::runtime_error("Trying to access a "+type()+" attribute "
"as string");
}

if ( attr.empty() ) return "\"\"";

std::string quoted(attr);
if( *(attr.begin()) != '\"' ) quoted = "\"" + attr;
if( *(quoted.end() - 1) != '\"' ) quoted += "\"";
Expand Down Expand Up @@ -1092,6 +1095,29 @@ bool IFunction::hasAttribute(const std::string& name)const
return m_attrs.find(name) != m_attrs.end();
}

/**
* Overload for const char* values.
* @param attName :: Attribute name
* @param value :: New attribute value to set
*/
void IFunction::setAttributeValue(const std::string &attName, const char *value)
{
std::string str(value);
setAttributeValue( attName, str );
}

/**
* Set string attribute by value. Make sure that quoted style doesn't change.
* @param attName :: Attribute name
* @param value :: New attribute value to set
*/
void IFunction::setAttributeValue(const std::string &attName, const std::string &value)
{
Attribute att = getAttribute(attName);
att.setString(value);
setAttribute( attName, att );
}

/// Returns a list of attribute names
std::vector<std::string> IFunction::getAttributeNames() const
{
Expand Down
36 changes: 34 additions & 2 deletions Code/Mantid/Framework/API/test/FunctionAttributeTest.h
Expand Up @@ -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<double>()));
std::vector<double> v(3);
v[0] = 1;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<double> 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)" );
}

};
Expand Down
26 changes: 26 additions & 0 deletions Code/Mantid/Framework/CurveFitting/test/TabulatedFunctionTest.h
Expand Up @@ -10,6 +10,7 @@
#include "MantidAPI/AlgorithmFactory.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/FileFinder.h"
#include "MantidAPI/FunctionFactory.h"

#include "MantidTestHelpers/WorkspaceCreationHelper.h"

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5c5c7e3

Please sign in to comment.