Skip to content

Commit

Permalink
improved error messages #202
Browse files Browse the repository at this point in the history
  • Loading branch information
darnocian committed Sep 21, 2024
1 parent 8852553 commit c803a6d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
7 changes: 5 additions & 2 deletions src/Sempare.Template.Context.pas
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ interface
procedure Unmanage(const AObject: TObject);
end;

TGetRttiContext = reference to function : PRttiContext;
TGetRttiContext = reference to function: PRttiContext;

ITemplateContext = interface
['{979D955C-B4BD-46BB-9430-1E74CBB999D4}']
Expand Down Expand Up @@ -428,7 +428,10 @@ procedure TTemplateContext.ClearTemplates;

constructor TTemplateContext.Create(const AOptions: TTemplateEvaluationOptions);
begin
FRttiContext := function : PRttiContext begin exit(@GRttiContext) end;
FRttiContext := function: PRttiContext
begin
exit(@GRttiContext)
end;
FOptions := AOptions + [eoFlattenTemplate, eoOptimiseTemplate];
FMaxRuntimeMs := GDefaultRuntimeMS;
FPrettyPrintOutput := GPrettyPrintOutput;
Expand Down
2 changes: 2 additions & 0 deletions src/Sempare.Template.Evaluate.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,8 @@ procedure TEvaluationTemplateVisitor.Visit(const AExpr: IMethodCallExpr);
LResult := '';
FEvalStack.push(LResult);
except
on ETemplateEvaluationError do
raise;
on e: exception do
RaiseError(AExpr, AExpr.Method + ':' + e.Message);
end;
Expand Down
26 changes: 21 additions & 5 deletions tests/Sempare.Template.Test.pas
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ TTestTemplate = class

[Test]
procedure TestVariableResolver;

[Test]
procedure TestErrorClosingScript;
end;

type
Expand Down Expand Up @@ -713,19 +716,21 @@ procedure TTestTemplate.TestDecimalEncodingErrorWithLists;
ctx.DecimalSeparator := ',';
ctx.ValueSeparator := ';';
Assert.AreEqual('', Template.Eval(ctx, '<% a := ["a"; "b"] %>'));
Assert.WillRaise(
Assert.WillRaiseWithMessage(
procedure
begin // expecting ;

Assert.AreEqual('', Template.Eval(ctx, '<% a := ["a", "b"] %>'));
end);

end, ETemplateEvaluationError, ' (Line 1, Column 14) Parsing error. Expecting: ;');
ctx.DecimalSeparator := '.';
ctx.ValueSeparator := ',';
Assert.AreEqual('', Template.Eval(ctx, '<% a := ["a", "b"] %>'));
Assert.WillRaise(
procedure
begin // expecting ,
Assert.AreEqual('', Template.Eval(ctx, '<% a := ["a"; "b"] %>'));
end);
end, ETemplateEvaluationError, ' (Line 1, Column 14) Parsing error. Expecting: ,');
end;

procedure TTestTemplate.TestDecimalEncodingErrorWithListsDefaultValueSeparator;
Expand Down Expand Up @@ -790,6 +795,15 @@ procedure TTestTemplate.TestEmpty;
Assert.AreEqual('', Template.Eval(''));
end;

procedure TTestTemplate.TestErrorClosingScript;
begin
Assert.WillRaiseWithMessage(
procedure
begin
Template.Parse('<% %');
end, ETemplateEvaluationError, ' (Line 1, Column 4) Parsing error. Expecting: %>');
end;

procedure TTestTemplate.TestException;
var
LContext: ITemplateContext;
Expand Down Expand Up @@ -866,11 +880,13 @@ procedure TTestTemplate.TestVariableNotFoundException;
begin
LCtx := Template.Context();
LCtx.Options := LCtx.Options + [eoRaiseErrorWhenVariableNotFound];
Assert.WillRaise(
Assert.WillRaiseWithMessage(
procedure
begin // expects abc

Assert.AreEqual('', Template.Eval(LCtx, '<% abc %>'));
end);

end, ETemplateEvaluationError, ' (Line 1, Column 4) Cannot find variable ''abc''');
end;

procedure TTestTemplate.TestVariableResolver;
Expand Down
22 changes: 6 additions & 16 deletions tests/Sempare.Template.TestFunctions.pas
Original file line number Diff line number Diff line change
Expand Up @@ -726,32 +726,22 @@ procedure TFunctionTest.TestCallingNonExistingMethod;
var
LRec1: TMyId1;
begin
Assert.WillRaise(
Assert.WillRaiseWithMessage(
procedure
begin
try
Template.Eval('<% _.nonexisting() %>', LRec1);
except
on e: exception do
raise;
end;
end);
Template.Eval('<% _.nonexisting() %>', LRec1);
end, ETemplateEvaluationError, ' (Line 1, Column 18) Method TMyId1.nonexisting does not exist.');
end;

procedure TFunctionTest.TestCallingNonExistingFunction;
var
LRec1: TMyId1;
begin
Assert.WillRaise(
Assert.WillRaiseWithMessage(
procedure
begin
try
Template.Eval('<% nonexisting() %>', LRec1);
except
on e: exception do
raise;
end;
end);
Template.Eval('<% nonexisting() %>', LRec1);
end, ETemplateEvaluationError, ' (Line 1, Column 16) Function nonexisting not registered in context.');
end;

initialization
Expand Down

0 comments on commit c803a6d

Please sign in to comment.