Skip to content

Commit

Permalink
Feature/split string action (#71)
Browse files Browse the repository at this point in the history
* add split string invocable class

* add split string invocable test
  • Loading branch information
justin-lyon authored Feb 22, 2024
1 parent a791d17 commit 067e541
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
24 changes: 24 additions & 0 deletions force-app/main/default/classes/SplitStringInvocable.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public with sharing class SplitStringInvocable {
@InvocableMethod(
label='Split String'
description='Given a string and a delimiter, return a collection of strings'
)
public static List<List<String>> execute(List<Request> requests) {
List<List<String>> output = new List<List<String>>();
for (Request req : requests) {
output.add(req.inputText.split(req.delimiter));
}
return output;
}

public class Request {
@InvocableVariable(required=true label='Input Text')
public String inputText;
@InvocableVariable(
required=true
label='Separator'
description='The delimiter that separates blocks of text inside your Input Text.'
)
public String delimiter = ';';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>
88 changes: 88 additions & 0 deletions force-app/main/default/classes/SplitStringInvocableTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
@IsTest
private class SplitStringInvocableTest {
private static final String INPUT_TEXT_SEMICOLONS = 'monday;tuesday;wednesday;thursday;friday';
private static final String INPUT_TEXT_COMMAS = 'monday,tuesday,wednesday,thursday,friday';
private static final List<String> WEEK_DAYS;

static {
WEEK_DAYS = INPUT_TEXT_SEMICOLONS.split(';');
}

@IsTest
static void it_should_return_list_of_string_given_a_semi_colon_delimited_string() {
List<SplitStringInvocable.Request> requests = new List<SplitStringInvocable.Request>();
SplitStringInvocable.Request request = new SplitStringInvocable.Request();
request.inputText = INPUT_TEXT_SEMICOLONS;
request.delimiter = ';';
requests.add(request);

Test.startTest();

List<List<String>> result = SplitStringInvocable.execute(requests);

Test.stopTest();

System.Assert.isFalse(result.isEmpty());
System.Assert.areEqual(1, result.size());

List<String> stringList = result.get(0);
System.Assert.isFalse(stringList.isEmpty());
System.Assert.areEqual(5, stringList.size());
System.Assert.areEqual(WEEK_DAYS, stringList);
}

@IsTest
static void it_should_return_list_of_string_given_a_comma_delimited_string() {
List<SplitStringInvocable.Request> requests = new List<SplitStringInvocable.Request>();
SplitStringInvocable.Request request = new SplitStringInvocable.Request();
request.inputText = INPUT_TEXT_COMMAS;
request.delimiter = ',';
requests.add(request);

Test.startTest();

List<List<String>> result = SplitStringInvocable.execute(requests);

Test.stopTest();

System.Assert.isFalse(result.isEmpty());
System.Assert.areEqual(1, result.size());

List<String> stringList = result.get(0);
System.Assert.isFalse(stringList.isEmpty());
System.Assert.areEqual(5, stringList.size());
System.Assert.areEqual(WEEK_DAYS, stringList);
}

@IsTest
static void it_should_handle_multiple_requests() {
List<SplitStringInvocable.Request> requests = new List<SplitStringInvocable.Request>();
SplitStringInvocable.Request r1 = new SplitStringInvocable.Request();
r1.inputText = INPUT_TEXT_SEMICOLONS;
r1.delimiter = ';';
requests.add(r1);
SplitStringInvocable.Request r2 = new SplitStringInvocable.Request();
r2.inputText = INPUT_TEXT_COMMAS;
r2.delimiter = ',';
requests.add(r2);

Test.startTest();

List<List<String>> result = SplitStringInvocable.execute(requests);

Test.stopTest();

System.Assert.isFalse(result.isEmpty());
System.Assert.areEqual(2, result.size());

List<String> listOne = result.get(0);
System.Assert.isFalse(listOne.isEmpty());
System.Assert.areEqual(5, listOne.size());
System.Assert.areEqual(WEEK_DAYS, listOne);

List<String> listTwo = result.get(1);
System.Assert.isFalse(listTwo.isEmpty());
System.Assert.areEqual(5, listTwo.size());
System.Assert.areEqual(WEEK_DAYS, listTwo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit 067e541

Please sign in to comment.