Skip to content

Commit ece276f

Browse files
authored
Supporting iterate string by split function (#3328)
* fix string split function * add a new test case * add a test case
1 parent a30eaa4 commit ece276f

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

libraries/Microsoft.Bot.Expressions/BuiltInFunctions.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2706,12 +2706,27 @@ private static Dictionary<string, ExpressionEvaluator> BuildFunctionLookup()
27062706
Apply(
27072707
args =>
27082708
{
2709-
string inputStr = ParseStringOrNull(args[0]);
2710-
string segStr = ParseStringOrNull(args[1]);
2711-
return inputStr.Split(segStr.ToCharArray());
2709+
var inputStr = string.Empty;
2710+
var seperator = string.Empty;
2711+
if (args.Count == 1)
2712+
{
2713+
inputStr = ParseStringOrNull(args[0]);
2714+
}
2715+
else
2716+
{
2717+
inputStr = ParseStringOrNull(args[0]);
2718+
seperator = ParseStringOrNull(args[1]);
2719+
}
2720+
2721+
if (seperator == string.Empty)
2722+
{
2723+
return inputStr.Select(c => c.ToString()).ToArray();
2724+
}
2725+
2726+
return inputStr.Split(seperator.ToCharArray());
27122727
}, VerifyStringOrNull),
27132728
ReturnType.Object,
2714-
(expression) => ValidateArityAndAnyType(expression, 2, 2, ReturnType.String)),
2729+
(expression) => ValidateArityAndAnyType(expression, 1, 2, ReturnType.String)),
27152730
new ExpressionEvaluator(
27162731
ExpressionType.Substring,
27172732
Substring,

tests/Microsoft.Bot.Expressions.Tests/BadExpressionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class BadExpressionTests
6060
Test("replaceIgnoreCase(one, 'l', 'k')"), // replaceIgnoreCase only accept string parameter
6161
Test("replaceIgnoreCase('hi', 1, 'k')"), // replaceIgnoreCase only accept string parameter
6262
Test("replaceIgnoreCase('hi', 'l', 1)"), // replaceIgnoreCase only accept string parameter
63-
Test("split(hello)"), // split need two parameters
63+
Test("split(hello, 'a', 'b')"), // split need one or two parameters
6464
Test("split(one, 'l')"), // split only accept string parameter
6565
Test("split(hello, 1)"), // split only accept string parameter
6666
Test("substring(hello, 0.5)"), // the second parameter of substring must be integer

tests/Microsoft.Bot.Expressions.Tests/ExpressionEngineTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,11 @@ public class ExpressionEngineTests
365365
Test("replaceIgnoreCase('hello', 'L', 'k')", "hekko"),
366366
Test("replaceIgnoreCase(nullObj, 'L', 'k')", string.Empty),
367367
Test("split('hello','e')", new string[] { "h", "llo" }),
368+
Test("split('hello','')", new string[] { "h", "e", "l", "l", "o" }),
369+
Test("split('','')", new string[] { }),
368370
Test("split(nullObj,'e')", new string[] { string.Empty }),
369-
370-
//Test("split('hello',nullObj)", new string[] { "h", "e", "l", "l", "o" }),
371-
Test("split('hello',nullObj)", new string[] { "hello" }),
371+
Test("split('hello')", new string[] { "h", "e", "l", "l", "o" }),
372+
Test("split('hello',nullObj)", new string[] { "h", "e", "l", "l", "o" }),
372373
Test("substring('hello', 0, 5)", "hello"),
373374
Test("substring('hello', 0, 3)", "hel"),
374375
Test("substring('hello', 3)", "lo"),

0 commit comments

Comments
 (0)