Skip to content

Commit 69b5984

Browse files
Copilotzeusongit
andauthored
DYN-10430: Add public ToString built-in method to DesignScript engine
Agent-Logs-Url: https://github.com/DynamoDS/Dynamo/sessions/287aacd6-0918-482c-bbc1-f554d6ee2efa Co-authored-by: zeusongit <32665108+zeusongit@users.noreply.github.com>
1 parent 4b362b0 commit 69b5984

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

src/Engine/ProtoCore/Lang/BuiltInFunctionEndPoint.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ public override StackValue Execute(ProtoCore.Runtime.Context c, List<StackValue>
372372
return StackValue.BuildInt(typeUID);
373373
case BuiltInMethods.MethodID.ToStringFromObject:
374374
case BuiltInMethods.MethodID.ToStringFromArray:
375+
case BuiltInMethods.MethodID.ToString:
375376
{
376377
ret = StringUtils.ConvertToString(formalParameters[0], runtimeCore, rmem);
377378
break;

src/Engine/ProtoCore/Lang/BuiltInMethods.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public enum MethodID
7070
ConditionalIf,
7171
ToStringFromObjectAndFormat,
7272
ToStringFromArrayAndFormat,
73+
ToString,
7374
}
7475

7576
//this array gets accessed using the MethodID enum
@@ -135,6 +136,7 @@ public enum MethodID
135136
Constants.kIfConditionalMethodName,
136137
"__ToStringFromObjectAndFormat", // kToStringFromObjectAndFormat
137138
"__ToStringFromArrayAndFormat", // kToStringFromArrayAndFormat
139+
"ToString", // kToString
138140
};
139141

140142
public static string GetMethodName(MethodID id)
@@ -833,6 +835,16 @@ public BuiltInMethods(Core core)
833835
ID = BuiltInMethods.MethodID.ToStringFromArrayAndFormat
834836
},
835837

838+
new BuiltInMethod
839+
{
840+
ReturnType = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.String, 0),
841+
Parameters = new List<KeyValuePair<string, ProtoCore.Type>>
842+
{
843+
new KeyValuePair<string, ProtoCore.Type>("object", TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.Var, 0)),
844+
},
845+
ID = BuiltInMethods.MethodID.ToString,
846+
},
847+
836848
new BuiltInMethod
837849
{
838850
ReturnType = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.Void, 0),

test/Engine/ProtoTest/TD/MultiLangTests/StringTest.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,5 +632,57 @@ public void TestStringFromObjectFormat_Prefs2()
632632
ProtoCore.Mirror.MirrorData.PrecisionFormat = oldpref;
633633

634634
}
635+
636+
[Test]
637+
[Category("SmokeTest")]
638+
public void WhenToStringCalledOnIntThenReturnsString()
639+
{
640+
String code =
641+
@"
642+
a = ToString(42);
643+
";
644+
thisTest.RunScriptSource(code);
645+
thisTest.Verify("a", "42");
646+
}
647+
648+
[Test]
649+
[Category("SmokeTest")]
650+
public void WhenToStringCalledOnDoubleThenReturnsString()
651+
{
652+
String code =
653+
@"
654+
a = ToString(1.5);
655+
";
656+
thisTest.RunScriptSource(code);
657+
thisTest.Verify("a", "1.500000");
658+
}
659+
660+
[Test]
661+
[Category("SmokeTest")]
662+
public void WhenToStringCalledOnBoolThenReturnsString()
663+
{
664+
String code =
665+
@"
666+
a = ToString(true);
667+
";
668+
thisTest.RunScriptSource(code);
669+
thisTest.Verify("a", "true");
670+
}
671+
672+
[Test]
673+
[Category("SmokeTest")]
674+
public void WhenToStringUsedInFunctionBodyThenNoWarning()
675+
{
676+
String code =
677+
@"
678+
def formatValues(x : double, y : double)
679+
{
680+
return = ToString(x) + "","" + ToString(y);
681+
}
682+
result = formatValues(1.0, 2.0);
683+
";
684+
thisTest.RunScriptSource(code);
685+
thisTest.Verify("result", "1.000000,2.000000");
686+
}
635687
}
636688
}

0 commit comments

Comments
 (0)