Skip to content

Commit 7a9b1a1

Browse files
committed
raise exception when method is not found #201
1 parent 9c5e2aa commit 7a9b1a1

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

src/Sempare.Template.Common.pas

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,11 @@ function Position(const APositional: IPosition): string; overload;
171171
end;
172172

173173
procedure RaiseError(const APositional: IPosition; const AFormat: string; const AArgs: array of const); overload;
174+
var
175+
LStr: string;
174176
begin
175-
raise ETemplateEvaluationError.Create(APositional, Position(APositional) + format(AFormat, AArgs));
177+
LStr := format(AFormat, AArgs);
178+
raise ETemplateEvaluationError.Create(APositional, Position(APositional) + LStr);
176179
end;
177180

178181
procedure RaiseError(const APositional: IPosition; const AFormat: string); overload;

src/Sempare.Template.Evaluate.pas

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,6 @@ function TEvaluationTemplateVisitor.Invoke(const AExpr: IMethodCallExpr; const A
11151115
LIntf: IInterface;
11161116
begin
11171117
LObjType := FContext.RttiContext().GetType(AObject.TypeInfo);
1118-
LMethod := LObjType.GetMethod(AExpr.Method);
11191118
LObject := AObject;
11201119
if LObject.Kind = tkInterface then
11211120
begin
@@ -1129,7 +1128,10 @@ function TEvaluationTemplateVisitor.Invoke(const AExpr: IMethodCallExpr; const A
11291128
LObject := AObject.AsType<TValue>;
11301129
end;
11311130
LMethod := LObjType.GetMethod(AExpr.Method);
1132-
1131+
if LMethod = nil then
1132+
begin
1133+
RaiseErrorRes(AExpr, @SMethodNotRegisteredOnObject, [LObject.TypeInfo.Name, AExpr.Method]);
1134+
end;
11331135
exit(DoInvoke(AExpr, LMethod, LObject, AArgs, AHasResult));
11341136
end;
11351137

src/Sempare.Template.ResourceStrings.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ interface
6565
SEndNotExpected = 'End not expected';
6666
SUnexpectedToken = 'UnexpectedToken';
6767
SFunctionNotRegisteredInContext = 'Function %s not registered in context.';
68+
SMethodNotRegisteredOnObject = 'Method %s.%s does not exist.';
6869
SParsingErrorExpecting = 'Parsing error. Expecting: %s';
6970
SStringOrNumericTypesExpected = 'String or numeric types expected';
7071
SBinOpNotSupported = 'Binop not supported';

tests/Sempare.Template.TestFunctions.pas

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ TFunctionTest = class
137137
procedure TestDomId;
138138
[Test]
139139
procedure TestFormatWithFloat;
140+
[Test]
141+
procedure TestCallingNonExistingMethod;
142+
[Test]
143+
procedure TestCallingNonExistingFunction;
140144
end;
141145

142146
type
@@ -718,6 +722,38 @@ procedure TFunctionTest.TestDomId;
718722
end;
719723
end;
720724

725+
procedure TFunctionTest.TestCallingNonExistingMethod;
726+
var
727+
LRec1: TMyId1;
728+
begin
729+
Assert.WillRaise(
730+
procedure
731+
begin
732+
try
733+
Template.Eval('<% _.nonexisting() %>', LRec1);
734+
except
735+
on e: exception do
736+
raise;
737+
end;
738+
end);
739+
end;
740+
741+
procedure TFunctionTest.TestCallingNonExistingFunction;
742+
var
743+
LRec1: TMyId1;
744+
begin
745+
Assert.WillRaise(
746+
procedure
747+
begin
748+
try
749+
Template.Eval('<% nonexisting() %>', LRec1);
750+
except
751+
on e: exception do
752+
raise;
753+
end;
754+
end);
755+
end;
756+
721757
initialization
722758

723759
TDUnitX.RegisterTestFixture(TFunctionTest);

0 commit comments

Comments
 (0)