diff --git a/src/Engine/ProtoCore/Lang/BuiltInFunctionEndPoint.cs b/src/Engine/ProtoCore/Lang/BuiltInFunctionEndPoint.cs index c9ffd15929f..203be6002b6 100644 --- a/src/Engine/ProtoCore/Lang/BuiltInFunctionEndPoint.cs +++ b/src/Engine/ProtoCore/Lang/BuiltInFunctionEndPoint.cs @@ -372,6 +372,7 @@ public override StackValue Execute(ProtoCore.Runtime.Context c, List return StackValue.BuildInt(typeUID); case BuiltInMethods.MethodID.ToStringFromObject: case BuiltInMethods.MethodID.ToStringFromArray: + case BuiltInMethods.MethodID.ToString: { ret = StringUtils.ConvertToString(formalParameters[0], runtimeCore, rmem); break; diff --git a/src/Engine/ProtoCore/Lang/BuiltInMethods.cs b/src/Engine/ProtoCore/Lang/BuiltInMethods.cs index 8c308b49092..89b2d10ce57 100644 --- a/src/Engine/ProtoCore/Lang/BuiltInMethods.cs +++ b/src/Engine/ProtoCore/Lang/BuiltInMethods.cs @@ -70,6 +70,7 @@ public enum MethodID ConditionalIf, ToStringFromObjectAndFormat, ToStringFromArrayAndFormat, + ToString, } //this array gets accessed using the MethodID enum @@ -135,6 +136,7 @@ public enum MethodID Constants.kIfConditionalMethodName, "__ToStringFromObjectAndFormat", // kToStringFromObjectAndFormat "__ToStringFromArrayAndFormat", // kToStringFromArrayAndFormat + "ToString", // kToString }; public static string GetMethodName(MethodID id) @@ -833,6 +835,16 @@ public BuiltInMethods(Core core) ID = BuiltInMethods.MethodID.ToStringFromArrayAndFormat }, + new BuiltInMethod + { + ReturnType = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.String, 0), + Parameters = new List> + { + new KeyValuePair("object", TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.Var, 0)), + }, + ID = BuiltInMethods.MethodID.ToString, + }, + new BuiltInMethod { ReturnType = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.Void, 0), diff --git a/test/Engine/ProtoTest/TD/MultiLangTests/StringTest.cs b/test/Engine/ProtoTest/TD/MultiLangTests/StringTest.cs index ae2bc905365..8bfeabaefa6 100644 --- a/test/Engine/ProtoTest/TD/MultiLangTests/StringTest.cs +++ b/test/Engine/ProtoTest/TD/MultiLangTests/StringTest.cs @@ -632,5 +632,57 @@ public void TestStringFromObjectFormat_Prefs2() ProtoCore.Mirror.MirrorData.PrecisionFormat = oldpref; } + + [Test] + [Category("SmokeTest")] + public void WhenToStringCalledOnIntThenReturnsString() + { + String code = + @" + a = ToString(42); + "; + thisTest.RunScriptSource(code); + thisTest.Verify("a", "42"); + } + + [Test] + [Category("SmokeTest")] + public void WhenToStringCalledOnDoubleThenReturnsString() + { + String code = + @" + a = ToString(1.5); + "; + thisTest.RunScriptSource(code); + thisTest.Verify("a", "1.500000"); + } + + [Test] + [Category("SmokeTest")] + public void WhenToStringCalledOnBoolThenReturnsString() + { + String code = + @" + a = ToString(true); + "; + thisTest.RunScriptSource(code); + thisTest.Verify("a", "true"); + } + + [Test] + [Category("SmokeTest")] + public void WhenToStringUsedInFunctionBodyThenNoWarning() + { + String code = + @" + def formatValues(x : double, y : double) + { + return = ToString(x) + "","" + ToString(y); + } + result = formatValues(1.0, 2.0); + "; + thisTest.RunScriptSource(code); + thisTest.Verify("result", "1.000000,2.000000"); + } } }