summaryrefslogtreecommitdiff
path: root/packages/fcl-mustache/tests/tcspecs.pas
blob: 5892733e3772bb1cb1f96b0b832ed1cc04b16ea1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
{
    This file is part of the Free Pascal Run time library.
    Copyright (c) 2021 by Michael Van Canneyt (michael@freepascal.org)

    testcase for official Mustache tests

    See the File COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
unit tcspecs;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpcunit, testregistry, fpmustache, fpjson, jsonparser;

Type

  { TTestMustacheSpecs }

  TTestMustacheSpecs = class(TTestCase)
  private
    FTests: TJSONArray;
    procedure RunMustacheTest(aIndex: Integer; aTest: TJSONObject);
  Public
    class var BaseDir : string;
  Public
    Procedure Setup; override;
    Procedure TearDown; override;
    Procedure DoTest(aFileName : string);
    Property Tests : TJSONArray Read FTests;
  Published
    Procedure TestComments;
    Procedure TestDelimiters;
    Procedure TestInterpolation;
    Procedure TestInverted;
    Procedure TestPartials;
    Procedure TestSections;
  end;


implementation

{ TTestMustacheSpecs }

procedure TTestMustacheSpecs.RunMustacheTest(aIndex : Integer; aTest : TJSONObject);

Var
  M : TMustache;
  aTempl,aErr,aRes,aName : TMustacheString;
  Parts : TJSONObject;
  I : Integer;
  Ok : Boolean;

  Procedure TreeDump;

  begin
    if not OK then
      begin
      Writeln('Tree dump:');
      Writeln(M.Dump);
      end;
  end;

  Procedure InputDump;

  begin
    Writeln('Test : ',aIndex);
    writeln(aTempl);
    writeln(StringReplace(StringReplace(aTempl,#10,' ',[rfReplaceAll]),#13,' ',[rfReplaceAll]));
    aName:='';
    While Length(aName)<Length(aTempl) do
      aName:=AName+'1234567890';
    Writeln(aName);
  end;

begin
  OK:=False;
  aTempl:=aTest.Get('template','');
  // InputDump;
  M:=TMustache.CreateMustache(Nil,aTempl);
  try
    // Load partials
    Parts:=aTest.Get('partials',TJSONObject(Nil));
    if Assigned(Parts) then
      for I:=0 to Parts.Count-1 do
        M.Partials.Add(Parts.Names[i]+'='+Parts.Items[i].AsString);
    // Set test name and run tests
    aName:='Test '+IntToStr(aIndex)+': '+aTest.Get('name','');
    Try
      aErr:='';
      aRes:=m.Render(aTest.Get('data',TJSONObject(Nil)));
    except
      on e : exception do
        aErr:=E.ClassName+' '+E.message;
    end;
    if aErr<>'' then
      Fail(aName+': Unexpected error: '+aErr);
    AssertEquals(aName,aTest.Get('expected',''),aRes);
    OK:=true;
  finally
    // TreeDump;
    M.Free;
  end;
end;

procedure TTestMustacheSpecs.Setup;
begin
  inherited Setup;
end;

procedure TTestMustacheSpecs.TearDown;
begin
  inherited TearDown;
end;

procedure TTestMustacheSpecs.DoTest(aFileName: string);

Var
  I : Integer;
  F : TFileStream;
  D : TJSONData;
  FN : String;

begin
  D:=Nil;
  FN:=IncludeTrailingPathDelimiter(BaseDir)+aFileName+'.json';
  F:=TFileStream.Create(FN,fmOpenRead or fmShareDenyWrite);
  try
    D:=GetJSON(F);
    if D is TJSONObject then
      begin
      Ftests:=(D as TJSONObject).Get('tests',TJSONArray(Nil));
      if (FTests=Nil) then
        Fail('Invalid mustache tests in '+FN);
      end
    else
      Fail('Invalid JSON object in '+FN);
    For I:=0 to Tests.Count-1 do
      RunMustacheTest(I,Tests.Items[i] as TJSONObject);
  finally
    D.Free;
    F.Free;
  end;
end;

procedure TTestMustacheSpecs.TestComments;
begin
  DoTest('comments');
end;

procedure TTestMustacheSpecs.TestDelimiters;
begin
  DoTest('delimiters');
end;

procedure TTestMustacheSpecs.TestInterpolation;
begin
  DoTest('interpolation');
end;

procedure TTestMustacheSpecs.TestInverted;
begin
  DoTest('inverted');
end;

procedure TTestMustacheSpecs.TestPartials;
begin
  DoTest('partials');
end;

procedure TTestMustacheSpecs.TestSections;
begin
  DoTest('sections');
end;

begin
  TTestMustacheSpecs.BaseDir:='spec/';
  RegisterTest(TTestMustacheSpecs);
end.