tcexmustache.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 2021 by Michael Van Canneyt ([email protected])
  4. Test cases for expression parser support
  5. See the File COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit tcexmustache;
  12. {$mode ObjFPC}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, fpcunit, fpjson, testregistry, fpmustache, tcbasemustache, fpexmustache, fpexprpars;
  16. Type
  17. { TTestExMustacheParser }
  18. TTestExMustacheParser = Class(TBaseMustacheTest)
  19. private
  20. FExpr: TFPExpressionParser;
  21. FOutput: TMustacheStringOutput;
  22. FContext : TMustacheJSONContext;
  23. FData : TJSONData;
  24. procedure GetVar(var Result: TFPExpressionResult; ConstRef
  25. AName: ShortString);
  26. Public
  27. Procedure SetUp; override;
  28. Procedure TearDown; override;
  29. Function CreateParser: TMustacheParser; override;
  30. Property Expr : TFPExpressionParser Read FExpr;
  31. Property Output : TMustacheStringOutput Read FOutput;
  32. Published
  33. Procedure TestSimple;
  34. Procedure TestRenderSimple;
  35. Procedure TestRenderSection;
  36. end;
  37. { TTestMustacheExpr }
  38. TTestMustacheExpr = Class(TTestCase)
  39. private
  40. FJSON: TJSONObject;
  41. FMustache: TMustacheExpr;
  42. public
  43. Procedure SetUp; override;
  44. Procedure TearDown; override;
  45. Property Mustache : TMustacheExpr Read FMustache;
  46. Property JSON : TJSONObject Read FJSON;
  47. Published
  48. Procedure TestEmpty;
  49. Procedure TestRegisterVariables;
  50. Procedure TestRenderSection;
  51. procedure TestRenderSectionStaticVariables;
  52. end;
  53. implementation
  54. Const
  55. STestJSON = '{ "data" : [ { "name": "me", "age" : 10}, { "name": "you", "age" : 12 }, { "name": "he", "age" : 13 } ] }';
  56. { TTestMustacheExpr }
  57. procedure TTestMustacheExpr.SetUp;
  58. begin
  59. inherited SetUp;
  60. FMustache:=TMustacheExpr.Create(Nil);
  61. FJSON:=GetJSON(STestJSON) as TJSONObject;
  62. end;
  63. procedure TTestMustacheExpr.TearDown;
  64. begin
  65. FreeAndNil(FJSON);
  66. FreeAndNil(FMustache);
  67. inherited TearDown;
  68. end;
  69. procedure TTestMustacheExpr.TestEmpty;
  70. begin
  71. AssertNotNull('Have mustache instance',Mustache);
  72. AssertNotNull('Have mustache expression engine instance',Mustache.ExpressionParser);
  73. end;
  74. procedure TTestMustacheExpr.TestRegisterVariables;
  75. begin
  76. Mustache.RegisterVariables(JSON,'data[0]',True);
  77. AssertEquals('Variable count',2,Mustache.ExpressionParser.Identifiers.Count);
  78. AssertEquals('Variable 0','name',Mustache.ExpressionParser.Identifiers[0].Name);
  79. AssertEquals('Variable 1','age',Mustache.ExpressionParser.Identifiers[1].Name);
  80. AssertTrue('Variable 0 type',rtString=Mustache.ExpressionParser.Identifiers[0].ResultType);
  81. AssertTrue('Variable 1 type',rtInteger=Mustache.ExpressionParser.Identifiers[1].ResultType);
  82. end;
  83. procedure TTestMustacheExpr.TestRenderSection;
  84. Var
  85. S : String;
  86. Const
  87. Template = '{{#data}}{{[name]}}:{{[age>11]}} {{/data}}';
  88. begin
  89. Mustache.Template:=Template;
  90. Mustache.RegisterVariables(JSON,'data[0]',True);
  91. S:=Mustache.Render(JSON);
  92. AssertEquals('Correct result','me:False you:True he:True ',S);
  93. end;
  94. procedure TTestMustacheExpr.TestRenderSectionStaticVariables;
  95. Var
  96. S : String;
  97. Const
  98. Template = '{{#data}}{{[name]}}:{{[age>11]}} {{/data}}';
  99. begin
  100. Mustache.Template:=Template;
  101. Mustache.RegisterVariables(JSON,'data[0]',False);
  102. S:=Mustache.Render(JSON);
  103. AssertEquals('Correct result','me:False me:False me:False ',S);
  104. end;
  105. { TTestExMustacheParser }
  106. procedure TTestExMustacheParser.SetUp;
  107. begin
  108. FExpr:=TFPExpressionParser.Create(Nil);
  109. Foutput:=TMustacheStringOutput.Create;
  110. inherited SetUp;
  111. end;
  112. procedure TTestExMustacheParser.TearDown;
  113. begin
  114. inherited TearDown;
  115. FreeAndNil(FExpr);
  116. FreeAndNil(Foutput);
  117. FreeAndNil(FContext);
  118. FreeAndNil(FData);
  119. end;
  120. function TTestExMustacheParser.CreateParser: TMustacheParser;
  121. Var
  122. P : TMustacheExprParser;
  123. begin
  124. P:=TMustacheExprParser.Create;
  125. P.ExprParser:=FExpr;
  126. Result:=P;
  127. end;
  128. procedure TTestExMustacheParser.TestSimple;
  129. begin
  130. Template:='{{[1+2]}}';
  131. CallParser;
  132. AssertElement(0,metVariable,'1+2',TMustacheExprElement);
  133. end;
  134. procedure TTestExMustacheParser.TestRenderSimple;
  135. begin
  136. TestSimple;
  137. ParseResult.Children[0].Render(Nil,Output,Nil,'',False);
  138. AssertEquals('Correct result','3',Output.Data);
  139. end;
  140. procedure TTestExMustacheParser.GetVar(Var Result : TFPExpressionResult; ConstRef AName : ShortString);
  141. begin
  142. Result.ResultType:=rtInteger;
  143. Result.ResInteger:=StrToINt(FContext.GetTextValue('age'));
  144. end;
  145. procedure TTestExMustacheParser.TestRenderSection;
  146. begin
  147. FData:=GetJSON(STestJSON);
  148. FContext:=TMustacheJSONContext.Create(FData,Nil);
  149. FExpr.Identifiers.AddVariable('age',rtInteger,@GetVar);
  150. Template:='{{#data}}{{{name}}}:{{[age>11]}} {{/data}}';
  151. CallParser;
  152. ParseResult.Render(FContext,Output,Nil,'',False);
  153. AssertEquals('Correct result','me:False you:True he:True ',Output.Data);
  154. end;
  155. initialization
  156. RegisterTests([TTestExMustacheParser,TTestMustacheExpr]);
  157. end.