Skip to content

Commit

Permalink
Feat(Assertions): Adds Shadow Assert Class
Browse files Browse the repository at this point in the history
introduces an Org level Assert class that passes through the standard Assert methods by calling `System.Assert.MethodName`. This includes a number of example custom assertions that can all be used by calling Assert.MethodName

Signed-off-by: kpoorman <kjp@codefriar.com>
  • Loading branch information
codefriar committed Oct 4, 2023
1 parent 74ae41a commit 9321f4a
Show file tree
Hide file tree
Showing 3 changed files with 330 additions and 57 deletions.
57 changes: 0 additions & 57 deletions force-app/main/default/classes/experiments/AutoCallable.cls

This file was deleted.

330 changes: 330 additions & 0 deletions force-app/main/default/classes/test utilities/Assert.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,330 @@
/**
* @description Assert class 'shadows' the System.Assert class allowing us to add custom assertion methods accessed
* via the static Assert.* methods. This allows us to add custom assertion methods that are more readable and provide
* more flexibility and functionality than the standard System.Assert methods. Ultimately the goal is to provide
* end-developers with a more readable and flexible way to write assertions.
*/
@SuppressWarnings('PMD.ExcessivePublicCount, PMD.ExcessiveParameterList')
public with sharing class Assert {
/**
* @description An assertion that validates a list is at least a certain size
*
* @param collection List<Object> - the list to validate
* @param minSize Integer - the minimum size the list must be
* @param msg String - the message to display if the assertion fails
*/
public static void listMeetsMinimumSize(

Check warning on line 16 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L16

Added line #L16 was not covered by tests
List<Object> collection,
Integer minSize,
String msg
) {
System.Assert.isTrue(collection.size() >= minSize, msg);

Check warning on line 21 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L21

Added line #L21 was not covered by tests
}

/**
* @description An assertion that validates a string is deserializable to untyped JSON
*
* @param jsonString String - the string to validate
* @param msg String - the message to display if the assertion fails
*/
public static void stringIsDeserializableAsUntypedJson(

Check warning on line 30 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L30

Added line #L30 was not covered by tests
String jsonString,
String msg
) {
System.Assert.isNotNull(JSON.deserializeUntyped(jsonString), msg);

Check warning on line 34 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L34

Added line #L34 was not covered by tests
}

/**
* @description An assertion to validate that the object is an SObject
*
* @param obj Object - the object to validate
* @param msg String - the message to display if the assertion fails
*/
public static void isSObject(Object obj, String msg) {
System.Assert.isInstanceOfType(obj, SObject.class, msg);

Check warning on line 44 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L43-L44

Added lines #L43 - L44 were not covered by tests
}

/**
* @description An assertion to validate that the exception is of the expected type
*
* @param incomingException Exception - the exception to validate
* @param expectedExceptionType Type - the expected type of the exception
* @param msg String - the message to display if the assertion fails
*/
public static void caughtExpectedException(

Check warning on line 54 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L54

Added line #L54 was not covered by tests
Exception incomingException,
Type expectedExceptionType,
String msg
) {
System.Assert.isInstanceOfType(
incomingException,
expectedExceptionType,
msg

Check warning on line 62 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L59-L62

Added lines #L59 - L62 were not covered by tests
);
}

/**
* @description An assertion to validate that the exception is of the expected type and contains the expected
* message
*
* @param incomingException Exception - the exception to validate
* @param expectedExceptionType Type - the expected type of the exception
* @param expectedExceptionMessage String - A string that must exist in the resulting exception message
* @param msg String - the message to display if the assertion fails
*/
public static void caughtExpectedException(

Check warning on line 75 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L75

Added line #L75 was not covered by tests
Exception incomingException,
Type expectedExceptionType,
String expectedExceptionMessage,
String msg
) {
System.Assert.isInstanceOfType(
incomingException,
expectedExceptionType,
msg

Check warning on line 84 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L81-L84

Added lines #L81 - L84 were not covered by tests
);
System.Assert.isTrue(
incomingException.getMessage()
.containsIgnoreCase(expectedExceptionMessage),
msg

Check warning on line 89 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L86-L89

Added lines #L86 - L89 were not covered by tests
);
}

/**
* @description An assertion that validates the given value is within a given range
*
* @param value Integer - the value to validate
* @param min Integer - the minimum value the value must be
* @param max Integer - the maximum value the value must be
* @param msg String - the message to display if the assertion fails
*/
public static void isInRange(

Check warning on line 101 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L101

Added line #L101 was not covered by tests
Integer value,
Integer min,
Integer max,
String msg
) {
System.Assert.isTrue(value >= min && value <= max, msg);

Check warning on line 107 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L107

Added line #L107 was not covered by tests
}

/**
* @description An Assertion that validates that LogEvent__c records were generated.
* Note: this relies on the Log class that's part of ApexKit
*
* @param msg String - the message to display if the assertion fails
*/
public static void logsWereGenerated(String msg) {
Integer logEvents = [

Check warning on line 117 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L116-L117

Added lines #L116 - L117 were not covered by tests
SELECT COUNT()
FROM LogEvent__c
WITH SYSTEM_MODE
];
System.Assert.isTrue(logEvents > 0, msg);

Check warning on line 122 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L122

Added line #L122 was not covered by tests
}

/**
* @description An assertion that validates that LogEvent__c records were generated.
* Note this relies on the Log class that's part of ApexKit. This method override accepts no parameters
* but delegates to the variant above by specifying a default message.
*/
public static void logsWereGenerated() {
logsWereGenerated(
'Though expected, no log events were generated - if youre not using test.StopTest the events will never publish'

Check warning on line 132 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L130-L132

Added lines #L130 - L132 were not covered by tests
);
}

////////////////////// PASSTHROUGH TO SYSTEM.ASSERT //////////////////////
/**
* @description Passthrough method to the default system assert class
*
* @param expected Object expected value
* @param actual Object actual value
* @param msg String message to display if the assertion fails
*/
public static void areEqual(Object expected, Object actual, String msg) {
System.Assert.areEqual(expected, actual, msg);
}

/**
* @description Passthrough method to the default system assert class
*
* @param expected Object expected value
* @param actual Object actual value
*/
public static void areEqual(Object expected, Object actual) {
System.Assert.areEqual(expected, actual);
}

/**
* @description Passthrough method to the default system assert class
*
* @param notExpected Object not expected value
* @param actual Object actual value
* @param msg String message to display if the assertion fails
*/
public static void areNotEqual(
Object notExpected,
Object actual,
String msg
) {
System.Assert.areNotEqual(notExpected, actual, msg);
}

/**
* @description Passthrough method to the default system assert class
*
* @param notExpected Object not expected value
* @param actual Object actual value
*/
public static void areNotEqual(Object notExpected, Object actual) {
System.Assert.areNotEqual(notExpected, actual);
}

/**
* @description Passthrough method to the default system assert class
*
* @param msg String message to display
*/
public static void fail(String msg) {
System.Assert.fail(msg);

Check warning on line 189 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L188-L189

Added lines #L188 - L189 were not covered by tests
}

/**
* @description Passthrough method to the default system assert class
*/
public static void fail() {
System.Assert.fail();

Check warning on line 196 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L195-L196

Added lines #L195 - L196 were not covered by tests
}

/**
* @description Passthrough method to the default system assert class
*
* @param condition Boolean condition to evaluate
* @param msg String message to display if the assertion fails
*/
public static void isFalse(Boolean condition, String msg) {
System.Assert.isFalse(condition, msg);
}

/**
* @description Passthrough method to the default system assert class
*
* @param condition Boolean condition to evaluate
*/
public static void isFalse(Boolean condition) {
System.Assert.isFalse(condition);

Check warning on line 215 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L214-L215

Added lines #L214 - L215 were not covered by tests
}

/**
* @description Passthrough method to the default system assert class
*
* @param instance Object instance to evaluate
* @param expectedType Type expected type
* @param msg String message to display if the assertion fails
*/
public static void isInstanceOfType(

Check warning on line 225 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L225

Added line #L225 was not covered by tests
Object instance,
System.Type expectedType,
String msg
) {
System.Assert.isInstanceOfType(instance, expectedType, msg);

Check warning on line 230 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L230

Added line #L230 was not covered by tests
}

/**
* @description Passthrough method to the default system assert class
*
* @param instance Object instance to evaluate
* @param expectedType Type expected type
*/
public static void isInstanceOfType(

Check warning on line 239 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L239

Added line #L239 was not covered by tests
Object instance,
System.Type expectedType
) {
System.Assert.isInstanceOfType(instance, expectedType);

Check warning on line 243 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L243

Added line #L243 was not covered by tests
}

/**
* @description Passthrough method to the default system assert class
*
* @param instance Object instance to evaluate
* @param notExpectedType Object not expected type
* @param msg String message to display if the assertion fails
*/
public static void isNotInstanceOfType(

Check warning on line 253 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L253

Added line #L253 was not covered by tests
Object instance,
System.Type notExpectedType,
String msg
) {
System.Assert.isNotInstanceOfType(instance, notExpectedType, msg);

Check warning on line 258 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L258

Added line #L258 was not covered by tests
}

/**
* @description Passthrough method to the default system assert class
*
* @param instance Object instance to evaluate
* @param notExpectedType Object not expected type
*/
public static void isNotInstanceOfType(

Check warning on line 267 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L267

Added line #L267 was not covered by tests
Object instance,
System.Type notExpectedType
) {
System.Assert.isNotInstanceOfType(instance, notExpectedType);

Check warning on line 271 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L271

Added line #L271 was not covered by tests
}

/**
* @description Passthrough method to the default system assert class
*
* @param value Object value to evaluate
* @param msg String message to display if the assertion fails
*/
public static void isNotNull(Object value, String msg) {
System.Assert.isNotNull(value, msg);

Check warning on line 281 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L280-L281

Added lines #L280 - L281 were not covered by tests
}

/**
* @description Passthrough method to the default system assert class
*
* @param value Object value to evaluate
*/
public static void isNotNull(Object value) {
System.Assert.isNotNull(value);

Check warning on line 290 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L289-L290

Added lines #L289 - L290 were not covered by tests
}

/**
* @description Passthrough method to the default system assert class
*
* @param value Object value to evaluate
* @param msg String message to display if the assertion fails
*/
public static void isNull(Object value, String msg) {
System.Assert.isNull(value, msg);
}

/**
* @description Passthrough method to the default system assert class
*
* @param value Object value to evaluate
*/
public static void isNull(Object value) {
System.Assert.isNull(value);

Check warning on line 309 in force-app/main/default/classes/test utilities/Assert.cls

View check run for this annotation

Codecov / codecov/patch

force-app/main/default/classes/test utilities/Assert.cls#L308-L309

Added lines #L308 - L309 were not covered by tests
}

/**
* @description Passthrough method to the default system assert class
*
* @param condition Boolean condition to evaluate
* @param msg String message to display if the assertion fails
*/
public static void isTrue(Boolean condition, String msg) {
System.Assert.isTrue(condition, msg);
}

/**
* @description Passthrough method to the default system assert class
*
* @param condition Boolean condition to evaluate
*/
public static void isTrue(Boolean condition) {
System.Assert.isTrue(condition);
}
}

0 comments on commit 9321f4a

Please sign in to comment.