tcspecs.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 2021 by Michael Van Canneyt ([email protected])
  4. testcase for official Mustache tests
  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 tcspecs;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, fpcunit, testregistry, fpmustache, fpjson, jsonparser;
  16. Type
  17. { TTestMustacheSpecs }
  18. TTestMustacheSpecs = class(TTestCase)
  19. private
  20. FTests: TJSONArray;
  21. procedure RunMustacheTest(aIndex: Integer; aTest: TJSONObject);
  22. Public
  23. class var BaseDir : string;
  24. Public
  25. Procedure Setup; override;
  26. Procedure TearDown; override;
  27. Procedure DoTest(aFileName : string);
  28. Property Tests : TJSONArray Read FTests;
  29. Published
  30. Procedure TestComments;
  31. Procedure TestDelimiters;
  32. Procedure TestInterpolation;
  33. Procedure TestInverted;
  34. Procedure TestPartials;
  35. Procedure TestSections;
  36. Procedure TestInheritance;
  37. end;
  38. implementation
  39. { TTestMustacheSpecs }
  40. procedure TTestMustacheSpecs.RunMustacheTest(aIndex : Integer; aTest : TJSONObject);
  41. Var
  42. M : TMustache;
  43. aTempl,aErr,aRes,aName : TMustacheString;
  44. Parts : TJSONObject;
  45. I : Integer;
  46. Ok : Boolean;
  47. Procedure TreeDump;
  48. begin
  49. if not OK then
  50. begin
  51. Writeln('Tree dump:');
  52. Writeln(M.Dump);
  53. end;
  54. end;
  55. Procedure InputDump;
  56. begin
  57. Writeln('Test : ',aIndex);
  58. writeln(aTempl);
  59. writeln(StringReplace(StringReplace(aTempl,#10,' ',[rfReplaceAll]),#13,' ',[rfReplaceAll]));
  60. aName:='';
  61. While Length(aName)<Length(aTempl) do
  62. aName:=AName+'1234567890';
  63. Writeln(aName);
  64. end;
  65. begin
  66. OK:=False;
  67. aTempl:=aTest.Get('template','');
  68. // InputDump;
  69. M:=TMustache.CreateMustache(Nil,aTempl);
  70. try
  71. // Load partials
  72. Parts:=aTest.Get('partials',TJSONObject(Nil));
  73. if Assigned(Parts) then
  74. for I:=0 to Parts.Count-1 do
  75. M.Partials.Add(Parts.Names[i]+'='+Parts.Items[i].AsString);
  76. // Set test name and run tests
  77. aName:='Test '+IntToStr(aIndex)+': '+aTest.Get('name','');
  78. Try
  79. aErr:='';
  80. aRes:=m.Render(aTest.Get('data',TJSONObject(Nil)));
  81. except
  82. on e : exception do
  83. aErr:=E.ClassName+' '+E.message;
  84. end;
  85. if aErr<>'' then
  86. Fail(aName+': Unexpected error: '+aErr);
  87. AssertEquals(aName,aTest.Get('expected',''),aRes);
  88. OK:=true;
  89. finally
  90. // TreeDump;
  91. M.Free;
  92. end;
  93. end;
  94. procedure TTestMustacheSpecs.Setup;
  95. begin
  96. inherited Setup;
  97. end;
  98. procedure TTestMustacheSpecs.TearDown;
  99. begin
  100. inherited TearDown;
  101. end;
  102. procedure TTestMustacheSpecs.DoTest(aFileName: string);
  103. Var
  104. I : Integer;
  105. F : TFileStream;
  106. D : TJSONData;
  107. FN : String;
  108. begin
  109. D:=Nil;
  110. FN:=IncludeTrailingPathDelimiter(BaseDir)+aFileName+'.json';
  111. F:=TFileStream.Create(FN,fmOpenRead or fmShareDenyWrite);
  112. try
  113. D:=GetJSON(F);
  114. if D is TJSONObject then
  115. begin
  116. Ftests:=(D as TJSONObject).Get('tests',TJSONArray(Nil));
  117. if (FTests=Nil) then
  118. Fail('Invalid mustache tests in '+FN);
  119. end
  120. else
  121. Fail('Invalid JSON object in '+FN);
  122. For I:=0 to Tests.Count-1 do
  123. RunMustacheTest(I,Tests.Items[i] as TJSONObject);
  124. finally
  125. D.Free;
  126. F.Free;
  127. end;
  128. end;
  129. procedure TTestMustacheSpecs.TestComments;
  130. begin
  131. DoTest('comments');
  132. end;
  133. procedure TTestMustacheSpecs.TestDelimiters;
  134. begin
  135. DoTest('delimiters');
  136. end;
  137. procedure TTestMustacheSpecs.TestInterpolation;
  138. begin
  139. DoTest('interpolation');
  140. end;
  141. procedure TTestMustacheSpecs.TestInverted;
  142. begin
  143. DoTest('inverted');
  144. end;
  145. procedure TTestMustacheSpecs.TestPartials;
  146. begin
  147. DoTest('partials');
  148. end;
  149. procedure TTestMustacheSpecs.TestSections;
  150. begin
  151. DoTest('sections');
  152. end;
  153. procedure TTestMustacheSpecs.TestInheritance;
  154. begin
  155. DoTest('inheritance');
  156. end;
  157. begin
  158. TTestMustacheSpecs.BaseDir:='spec/';
  159. RegisterTest(TTestMustacheSpecs);
  160. end.