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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
unit TestInsightProtocol;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, fpJSON;
Type
ETestInsight = class(Exception);
TTestResultType = (rtUnknown, rtPassed, rtFailed, rtError, rtWarning, rtSkipped, rtRunning);
TTestPhase = (tpSetUp, tpRunTest, spTearDown, tpNothing);
{ TTestInsightResult }
TTestInsightResult = Class
Public
TestResult : TTestResultType;
TestName: string;
TestDuration: Cardinal;
TestUnitName: string;
TestClassName: string;
TestMethodName: string;
// Status
TestStatus: string;
TestExceptionMessage: string;
TestExceptionClass: string;
TestIsIgnored : Boolean;
TestPhase : TTestPhase;
// Failure info
FailureUnitName: string;
FailureMethodName: string;
FailureLineNumber: Integer;
FailureSourceUnitName : String; // Exception
FailureLocationInfo : String;
Constructor Create; virtual;
procedure FromJSON(const aJSON : TJSONStringType); overload;
procedure FromJSON(const aJSON : TJSONObject); overload;
function ToJSON: string;
Procedure ToJSON(aJSON : TJSONObject);
end;
TTestInsightResultClass = Class of TTestInsightResult;
TTestInsightResultArray = Array of TTestInsightResult;
{ TTestInsightOptions }
TTestInsightOptions = Class (TPersistent)
Public
ExecuteTests: Boolean;
ShowProgress: Boolean;
TestSuite : String;
Constructor Create; virtual;
Procedure Assign(Source : TPersistent); override;
procedure FromJSON(const aJSON : TJSONStringType); overload;
procedure FromJSON(const aJSON : TJSONObject); overload;
function ToJSON: string;
Procedure ToJSON(aJSON : TJSONObject);
end;
TTestInsightOptionsClass = Class of TTestInsightOptions;
Const
// Used in URLS
DefaultUrl = 'http://localhost:8081/tests';
pathTests = '/tests';
pathStarted = 'started';
pathFinished = 'finished';
pathResults = 'results';
pathOptions = 'options';
qryTotalCount = 'totalcount';
// Used in TTestInsightResult JSON encoding.
keyTestName = 'testname';
KeyTestUnitName = 'testunitname';
keyTestClassName = 'testclassname';
keyTestMethodName = 'testmethodname';
keyTestDuration = 'testduration';
keyResultType = 'testresulttype';
keyExceptionMessage = 'exceptionmessage';
KeyExceptionClass = 'exceptionclass';
keyfailureMethodName = 'failuremethodname';
keyFailureUnitName = 'failureunitname';
keyFailureLineNumber = 'failurelinenumber';
KeyFailureSourceUnitName = 'failuresourceunitname';
KeyFailureLocationInfo = 'location';
keyStatus = 'status';
keyIsIgnored = 'ignored';
KeyTestPhase = 'phase';
// Config file settings
SConfig = 'Config';
KeyBaseURL = 'BaseUrl';
// Used in TTestInsightOptions JSON encoding and Ini file
keyExecuteTests = 'ExecuteTests';
keyShowProgress = 'ShowProgress';
KeySuite = 'Suite';
ResultTypeNames: array[TTestResultType] of string
= ('?','Passed', 'Failed', 'Error', 'Warning', 'Skipped', 'Running');
TestPhaseNames: array[TTestPhase] of string
= ('Setup', 'Run', 'TearDown','');
Function StringToResultType(aValue : string) : TTestResultType;
Function ResultTypeToString(aValue : TTestResultType) : string;
Function StringToTestPhase(aValue : string) : TTestPhase;
Function TestPhaseToString(aValue : TTestPhase) : string;
// Convert CR/LF separated list name to expected JSON format
Function TestStringsToJSON(aContent : String): TJSONObject;
implementation
Function ResultTypeToString(aValue : TTestResultType) : string;
begin
Result:=ResultTypeNames[aValue];
end;
function StringToTestPhase(aValue: string): TTestPhase;
Var
T : TTestPhase;
begin
Result:=tpNothing;
For T in TTestPhase do
if SameText(TestPhaseNames[T],aValue) then
Exit(T);
end;
function TestPhaseToString(aValue: TTestPhase): string;
begin
Result:=TestPhaseNames[aValue];
end;
Function StringToResultType(aValue : string) : TTestResultType;
Var
T : TTestResultType;
begin
Result:=rtFailed;
For T in TTestResultType do
if SameText(ResultTypeNames[T],aValue) then
Exit(T);
end;
Procedure AddTests(aParent : TJSONObject; aList : TStrings; aPath : String; var aIdx : Integer);
Var
P : integer;
Up : Boolean;
Curr,Sub : String;
Obj : TJSONObject;
begin
Repeat
Curr:=aList[aIdx];
Up:=(aPath<>'') and Not SameText(Copy(Curr,1,Length(aPath)),aPath);
if not Up then
begin
Delete(Curr,1,Length(aPath));
if Curr[1]='.' then delete(Curr,1,1);
P:=Pos('.',Curr);
if P<>0 then
begin
Sub:=Copy(Curr,1,P-1);
Obj:=TJSONObject.Create;
aParent.Add(Sub,Obj);
if aPath<>'' then
AddTests(Obj,aList,APath+'.'+Sub,aIdx)
else
AddTests(Obj,aList,Sub,aIdx)
end
else
begin
aParent.Add(Curr,TJSONNull.Create);
Inc(aIdx);
end;
end
Until (aIdx>=aList.Count) or Up;
end;
Function TestStringsToJSON(aContent : String): TJSONObject;
Var
L : TStringList;
aPath : String;
aIdx : Integer;
begin
Result:=nil;
L:=TStringList.Create;
try
L.Text:=aContent;
L.Sort;
Result:=TJSONObject.Create;
aPath:='';
aIdx:=0;
AddTests(Result,L,aPath,aIdx);
finally
L.Free;
end;
end;
{ TTestInsightOptions }
constructor TTestInsightOptions.Create;
begin
ExecuteTests:=True;
ShowProgress:=True;
end;
procedure TTestInsightOptions.Assign(Source: TPersistent);
var
Src : TTestInsightOptions absolute source;
begin
if Source is TTestInsightOptions then
begin
ExecuteTests:=Src.ExecuteTests;
ShowProgress:=Src.ShowProgress;
TestSuite:=Src.TestSuite;
end
else
inherited Assign(Source);
end;
procedure TTestInsightOptions.FromJSON(const aJSON: TJSONStringType);
Var
D : TJSONData;
begin
D:=GetJSON(aJSON);
try
if D is TJSONObject then
FromJSON(D as TJSONObject);
finally
D.Free;
end;
end;
procedure TTestInsightOptions.FromJSON(const aJSON: TJSONObject);
begin
with aJSON do
begin
ExecuteTests:=Get(KeyExecuteTests,ExecuteTests);
ShowProgress:=Get(KeyShowProgress,ShowProgress);
TestSuite:=Get(KeySuite,TestSuite);
end;
end;
function TTestInsightOptions.ToJSON: string;
Var
Obj : TJSONObject;
begin
Obj:=TJSONObject.Create;
try
ToJSON(Obj);
Result := Obj.AsJSON;
finally
Obj.Free;
end;
end;
procedure TTestInsightOptions.ToJSON(aJSON: TJSONObject);
begin
with aJSON do
begin
Add(KeyExecuteTests,ExecuteTests);
Add(KeyShowProgress,ShowProgress);
Add(KeySuite,TestSuite);
end;
end;
{ TTestInsightResult }
constructor TTestInsightResult.Create;
begin
TestResult:=rtUnknown;
end;
procedure TTestInsightResult.FromJSON(const aJSON: TJSONStringType);
Var
D : TJSONData;
begin
D:=GetJSON(aJSON);
try
if D is TJSONObject then
FromJSON(D as TJSONObject);
finally
D.Free;
end;
end;
procedure TTestInsightResult.FromJSON(const aJSON: TJSONObject);
begin
With aJSON Do
begin
TestName:=Get(KeyTestName, TestName);
TestUnitName:=Get(KeyTestUnitName, TestUnitName);
TestClassName:=Get(KeyTestClassName, TestClassName);
TestMethodName:=Get(KeyTestMethodName, TestMethodName);
TestResult:=StringToResultType(Get(KeyResultType, ResultTypeNames[TestResult]));
TestDuration:=Get(KeyTestDuration, TestDuration);
TestStatus:=Get(Keystatus, TestStatus);
TestIsIgnored:=Get(keyIsIgnored,TestIsIgnored);
TestPhase:=StringToTestPhase(Get(KeyTestPhase,TestPhaseToString(TestPhase)));
TestExceptionMessage:=Get(KeyExceptionMessage, TestExceptionMessage);
TestExceptionClass:=Get(KeyExceptionClass, TestExceptionClass);
FailureUnitName:=Get(KeyFailureUnitName, FailureUnitName);
FailureMethodName:=Get(KeyFailureMethodName, FailureMethodName);
FailureLineNumber:=Get(KeyFailureLineNumber, FailureLineNumber);
FailureSourceUnitName:=Get(KeyFailureSourceUnitName,FailureSourceUnitName);
FailureLocationInfo:=Get(KeyFailureLocationInfo,FailureLocationInfo);
end;
end;
function TTestInsightResult.ToJSON: string;
Var
Obj : TJSONObject;
begin
Obj:=TJSONObject.Create;
try
ToJSON(Obj);
Result := Obj.AsJSON;
finally
Free;
end;
end;
procedure TTestInsightResult.ToJSON(aJSON: TJSONObject);
begin
With aJSON do
begin
Add(KeyTestname, TestName);
Add(KeyTestUnitName, TestUnitName);
Add(KeyTestClassName, TestClassName);
Add(KeyTestMethodName,TestMethodName);
Add(KeyResultType, ResultTypeNames[TestResult]);
Add(KeyTestDuration, TestDuration);
Add(KeyStatus, TestStatus);
Add(keyIsIgnored,TestIsIgnored);
Add(KeyTestPhase,TestPhaseToString(TestPhase));
Add(KeyExceptionMessage, TestExceptionMessage);
Add(KeyExceptionClass, TestExceptionClass);
Add(KeyFailureUnitName, FailureUnitName);
Add(KeyFailureMethodName, FailureMethodName);
Add(KeyFailureLinenumber, FailureLineNumber);
Add(KeyFailureSourceUnitName,FailureSourceUnitName);
Add(KeyFailureLocationInfo,FailureLocationInfo);
end;
end;
end.
|