Skip to content

Commit

Permalink
Cover set comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
ipavlic committed Dec 30, 2024
1 parent d270642 commit ce8b5a5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
3 changes: 0 additions & 3 deletions force-app/main/default/classes/function/MatchFields.cls
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ public class MatchFields implements SObjectPredicate {
} else if (valueSet instanceof Set<Integer>) {
Set<Integer> validValues = (Set<Integer>) valueSet;
return validValues.contains((Integer) value);
} else if (valueSet instanceof Set<Long>) {
Set<Long> validValues = (Set<Long>) valueSet;
return validValues.contains((Long) value);
} else if (valueSet instanceof Set<String>) {
Set<String> validValues = (Set<String>) valueSet;
return validValues.contains((String) value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@IsTest
private class MatchFieldsTest {
@IsTest
static void containsDecimal() {
System.Assert.isTrue(new MatchFields().field(Opportunity.Amount).isIn(new Set<Decimal>{10.0}).call(new Opportunity(Amount = 10.0)));
}

@IsTest
static void containsDouble() {
System.Assert.isTrue(new MatchFields().field(Opportunity.Amount).isIn(new Set<Double>{10.0}).call(new Opportunity(Amount = 10.0)));
}

@IsTest
static void containsInteger() {
System.Assert.isTrue(new MatchFields().field(Account.NumberOfEmployees).isIn(new Set<Integer>{10}).call(new Account(NumberOfEmployees = 10)));
}

@IsTest
static void containsBoolean() {
System.Assert.isTrue(new MatchFields().field(Contact.DoNotCall).isIn(new Set<Boolean>{false}).call(new Contact(DoNotCall = false)));
System.Assert.isFalse(new MatchFields().field(Contact.DoNotCall).isIn(new Set<Boolean>{true}).call(new Contact(DoNotCall = false)));
}

@IsTest
static void containsDate() {
Date today = Date.today();
System.Assert.isTrue(new MatchFields().field(Event.ActivityDate).isIn(new Set<Date>{today}).call(new Event(ActivityDate = today)));
}

@IsTest
static void containsDatetime() {
Datetime now = Datetime.now();
System.Assert.isTrue(new MatchFields().field(Event.ActivityDateTime).isIn(new Set<Datetime>{now}).call(new Event(ActivityDateTime = now)));
}

@IsTest
static void containsId() {
Id oppId = TestUtility.getFakeId(Opportunity.SObjectType);
System.Assert.isTrue(new MatchFields().field(Opportunity.Id).isIn(new Set<Id>{oppId}).call(new Opportunity(Id = oppId)));
System.Assert.isFalse(new MatchFields().field(Opportunity.Id).isIn(new Set<Id>{oppId}).call(new Opportunity(Id = TestUtility.getFakeId(Opportunity.SObjectType))));
}

@IsTest
static void containsString() {
System.Assert.isTrue(new MatchFields().field(Opportunity.Name).isIn(new Set<String>{'foo'}).call(new Opportunity(Name = 'foo')));
System.Assert.isFalse(new MatchFields().field(Opportunity.Name).isIn(new Set<String>{'foo'}).call(new Opportunity(Name = 'bar')));
}
}
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>61.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ private class ModifySObjectTest {
new ModifySObject().setFields(new Opportunity(Amount = 2000)).call(opp);
System.Assert.areEqual(2000, opp.Amount);
}

@IsTest
private static void exceptionIsThrownForNull() {
try {
new ModifySObject().setField(Opportunity.Amount, 100).call(null);
System.Assert.fail();
} catch (IllegalArgumentException e) {
System.Assert.areEqual('Record is null', e.getMessage());
}
}
}

0 comments on commit ce8b5a5

Please sign in to comment.