tcspecs.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. end;
  37. implementation
  38. { TTestMustacheSpecs }
  39. procedure TTestMustacheSpecs.RunMustacheTest(aIndex : Integer; aTest : TJSONObject);
  40. Var
  41. M : TMustache;
  42. aTempl,aErr,aRes,aName : TMustacheString;
  43. Parts : TJSONObject;
  44. I : Integer;
  45. Ok : Boolean;
  46. Procedure TreeDump;
  47. begin
  48. if not OK then
  49. begin
  50. Writeln('Tree dump:');
  51. Writeln(M.Dump);
  52. end;
  53. end;
  54. Procedure InputDump;
  55. begin
  56. Writeln('Test : ',aIndex);
  57. writeln(aTempl);
  58. writeln(StringReplace(StringReplace(aTempl,#10,' ',[rfReplaceAll]),#13,' ',[rfReplaceAll]));
  59. aName:='';
  60. While Length(aName)<Length(aTempl) do
  61. aName:=AName+'1234567890';
  62. Writeln(aName);
  63. end;
  64. begin
  65. OK:=False;
  66. aTempl:=aTest.Get('template','');
  67. // InputDump;
  68. M:=TMustache.CreateMustache(Nil,aTempl);
  69. try
  70. // Load partials
  71. Parts:=aTest.Get('partials',TJSONObject(Nil));
  72. if Assigned(Parts) then
  73. for I:=0 to Parts.Count-1 do
  74. M.Partials.Add(Parts.Names[i]+'='+Parts.Items[i].AsString);
  75. // Set test name and run tests
  76. aName:='Test '+IntToStr(aIndex)+': '+aTest.Get('name','');
  77. Try
  78. aErr:='';
  79. aRes:=m.Render(aTest.Get('data',TJSONObject(Nil)));
  80. except
  81. on e : exception do
  82. aErr:=E.ClassName+' '+E.message;
  83. end;
  84. if aErr<>'' then
  85. Fail(aName+': Unexpected error: '+aErr);
  86. AssertEquals(aName,aTest.Get('expected',''),aRes);
  87. OK:=true;
  88. finally
  89. // TreeDump;
  90. M.Free;
  91. end;
  92. end;
  93. procedure TTestMustacheSpecs.Setup;
  94. begin
  95. inherited Setup;
  96. end;
  97. procedure TTestMustacheSpecs.TearDown;
  98. begin
  99. inherited TearDown;
  100. end;
  101. procedure TTestMustacheSpecs.DoTest(aFileName: string);
  102. Var
  103. I : Integer;
  104. F : TFileStream;
  105. D : TJSONData;
  106. FN : String;
  107. begin
  108. D:=Nil;
  109. FN:=IncludeTrailingPathDelimiter(BaseDir)+aFileName+'.json';
  110. F:=TFileStream.Create(FN,fmOpenRead or fmShareDenyWrite);
  111. try
  112. D:=GetJSON(F);
  113. if D is TJSONObject then
  114. begin
  115. Ftests:=(D as TJSONObject).Get('tests',TJSONArray(Nil));
  116. if (FTests=Nil) then
  117. Fail('Invalid mustache tests in '+FN);
  118. end
  119. else
  120. Fail('Invalid JSON object in '+FN);
  121. For I:=0 to Tests.Count-1 do
  122. RunMustacheTest(I,Tests.Items[i] as TJSONObject);
  123. finally
  124. D.Free;
  125. F.Free;
  126. end;
  127. end;
  128. procedure TTestMustacheSpecs.TestComments;
  129. begin
  130. DoTest('comments');
  131. end;
  132. procedure TTestMustacheSpecs.TestDelimiters;
  133. begin
  134. DoTest('delimiters');
  135. end;
  136. procedure TTestMustacheSpecs.TestInterpolation;
  137. begin
  138. DoTest('interpolation');
  139. end;
  140. procedure TTestMustacheSpecs.TestInverted;
  141. begin
  142. DoTest('inverted');
  143. end;
  144. procedure TTestMustacheSpecs.TestPartials;
  145. begin
  146. DoTest('partials');
  147. end;
  148. procedure TTestMustacheSpecs.TestSections;
  149. begin
  150. DoTest('sections');
  151. end;
  152. begin
  153. TTestMustacheSpecs.BaseDir:='spec/';
  154. RegisterTest(TTestMustacheSpecs);
  155. end.