Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarParra committed Oct 15, 2023
1 parent 4d9a335 commit a87d494
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
68 changes: 68 additions & 0 deletions expression-src/main/api/tests/EvaluatorTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1403,4 +1403,72 @@ private class EvaluatorTest {
Object result = Evaluator.run(formula);
Assert.areEqual(3, result);
}

@IsTest
private static void throwsRuntimeExceptionWhenPassingArgumentToSObjectGetExpr() {
Account testRecord = new Account(Name = 'Test');
insert testRecord;

try {
Evaluator.run('FETCH("Account", []) -> FIRST().Name("Test")',
testRecord.Id);
Assert.fail('Expected RuntimeException');
} catch (Exception e) {
Assert.isInstanceOfType(e, Exceptions.RuntimeException.class);
}
}

@IsTest
private static void throwsRuntimeExceptionWhenPassingArgumentToMapGetExpr() {
try {
Evaluator.run('{"Name": "Test"}.Name("Test")');
Assert.fail('Expected RuntimeException');
} catch (Exception e) {
Assert.isInstanceOfType(e, Exceptions.RuntimeException.class);
}
}

@IsTest
private static void throwsRuntimeExceptionWhenGettingFieldThatDoesNotExist() {
Account testRecord = new Account(Name = 'Test');
insert testRecord;

try {
Evaluator.run('FETCH("Account", []) -> FIRST().NonExistentField',
testRecord.Id);
Assert.fail('Expected RuntimeException');
} catch (Exception e) {
Assert.isInstanceOfType(e, Exceptions.RuntimeException.class);
}
}

@IsTest
private static void throwsRuntimeExceptionWhenGettingKeyThatDoesNotExist() {
try {
Evaluator.run('{"Name": "Test"}.NonExistentKey');
Assert.fail('Expected RuntimeException');
} catch (Exception e) {
Assert.isInstanceOfType(e, Exceptions.RuntimeException.class);
}
}

@IsTest
private static void throwsRuntimeExceptionWhenBubblingErrorsFromGlobalVariableResolvers() {
try {
Evaluator.run('$Action.Apex.NonExistentClass');
Assert.fail('Expected RuntimeException');
} catch (Exception e) {
Assert.isInstanceOfType(e, Exceptions.RuntimeException.class);
}
}

@IsTest
private static void throwsRuntimeExceptionWhenUsingGetExpressionInAnInvalidWay() {
try {
Evaluator.run('$DoesNotExist.DoSomething');
Assert.fail('Expected RuntimeException');
} catch (Exception e) {
Assert.isInstanceOfType(e, Exceptions.RuntimeException.class);
}
}
}
40 changes: 40 additions & 0 deletions expression-src/main/api/tests/OperatorTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ private class OperatorTest {
Assert.areEqual(true, Evaluator.run('1 = 1 && (2 = 2)'));
}

@IsTest
private static void throwsRuntimeExceptionWhenLeftSideOfDoubleAmpIsNotBoolean() {
try {
Evaluator.run('1 && 2');
Assert.fail('Expected RuntimeException');
} catch (Exception e) {
Assert.isInstanceOfType(e, Exceptions.RuntimeException.class);
}
}

@IsTest
private static void throwsRuntimeExceptionWhenRightSideOfDoubleAmpIsNotBoolean() {
try {
Evaluator.run('1 = 1 && 2');
Assert.fail('Expected RuntimeException');
} catch (Exception e) {
Assert.isInstanceOfType(e, Exceptions.RuntimeException.class);
}
}

@IsTest
private static void supportsAnd_leftFalse() {
Assert.areEqual(false, Evaluator.run('1 = 2 && 2 = 2'));
Expand All @@ -130,6 +150,26 @@ private class OperatorTest {
Assert.areEqual(true, Evaluator.run('((1 = 1) && (2 = 2)) || (3 = 3)'));
}

@IsTest
private static void throwsRuntimeExceptionWhenLeftSideOfDoublePipeIsNotBoolean() {
try {
Evaluator.run('1 || 2');
Assert.fail('Expected RuntimeException');
} catch (Exception e) {
Assert.isInstanceOfType(e, Exceptions.RuntimeException.class);
}
}

@IsTest
private static void throwsRuntimeExceptionWhenRightSideOfDoublePipeIsNotBoolean() {
try {
Evaluator.run('1 != 1 || 2');
Assert.fail('Expected RuntimeException');
} catch (Exception e) {
Assert.isInstanceOfType(e, Exceptions.RuntimeException.class);
}
}

@IsTest
private static void supportsOr() {
Assert.areEqual(true, Evaluator.run('1 = 1 || (2 = 2)'));
Expand Down
2 changes: 0 additions & 2 deletions expression-src/main/src/interpreter/Scanner.cls
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ public with sharing class Scanner {
// want the column of the first line.
List<String> textLines = text.split('\n');
Integer column = this.column - textLines[0].length();
System.debug('current col ' + this.column);
System.debug('text lines ' + textLines[0]);

tokens.add(new Token(type, text, literal, new Token.Position(line, column)));
}
Expand Down

0 comments on commit a87d494

Please sign in to comment.