-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspectester.pas
56 lines (51 loc) · 1.68 KB
/
spectester.pas
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
// fpc -Fu/mnt/LinuxShared/lazarus-svn/components/lazutils/lib/x86_64-linux spectester.pas
program spectester;
{$mode objfpc}{$H+}
uses
Classes,SysUtils,FileUtil,fpjson,jsonparser,mustapas;
var
JSONFileContent: TStringList;
Template,Expected,Result: String;
JSON,Test,Context: TJSONObject;
e: TJSONEnum;
i: Integer;
Verdict: Boolean;
TemplateStream,ResultStream: TStringStream;
begin
with FindAllFiles('specs','*.json') do
try
for i := 0 to Count - 1 do begin
WriteLn('[' + Strings[i] + ']');
JSONFileContent := TStringList.Create;
JSONFileContent.LoadFromFile(Strings[i]);
JSON := GetJSON(JSONFileContent.Text) as TJSONObject;
for e in JSON['tests'] do begin
Test := e.Value as TJSONObject;
Template := Test['template'].AsString;
Context := Test['data'] as TJSONObject;
Expected := Test['expected'].AsString;
try
TemplateStream := TStringStream.Create(Template);
ResultStream := TStringStream.Create('');
Mustapas.Render(TemplateStream,Context,ResultStream);
Result := ResultStream.DataString;
TemplateStream.Free;
ResultStream.Free;
Verdict := Result = Expected;
WriteLn(Test['name'].AsString + ': ',Verdict);
if not Verdict then begin
WriteLn('Expected: ' + Escape(Expected));
WriteLn('Actual : ' + Escape(Result));
WriteLn;
end;
except
on e: Exception do begin
WriteLn(e.ClassName + ': ' + e.Message)
end;
end;
end;
end;
finally
Free;
end;
end.