Skip to content

Commit

Permalink
Lots of internal changes, NUnit logger rewritten, test results now po…
Browse files Browse the repository at this point in the history
…pulated
  • Loading branch information
vincentparrett committed Sep 18, 2013
1 parent d0e5b56 commit 267111f
Show file tree
Hide file tree
Showing 27 changed files with 1,323 additions and 691 deletions.
296 changes: 296 additions & 0 deletions DUnitX.FixtureResult.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
unit DUnitX.FixtureResult;

interface

uses
classes,
TimeSpan,
DUnitX.Generics,
DUnitX.TestFramework,
DUnitX.InternalInterfaces;

type
TDUnitXFixtureResult = class(TInterfacedObject,IFixtureResult,IFixtureResultBuilder)
private
FChildren : IList<IFixtureResult>;
FTestResults : IList<ITestResult>;
FFixture : ITestFixtureInfo;

FAllPassed : boolean;
FErrorCount : integer;
FFailureCount : integer;
FPassCount : integer;
FIgnoredCount : integer;
FTotalCount : integer;

FStartTime : TDateTime;
FFinishTime : TDateTime;
FDuration : TTimeSpan;


protected
procedure Reduce;


function GetChildCount : Integer;
function GetChildren : IList<IFixtureResult>;
function GetErrorCount : Integer;
function GetErrors : IList<ITestError>;
function GetFailureCount : Integer;
function GetFailures : IList<ITestResult>;
function GetFixture : ITestFixtureInfo;
function GetHasFailures : Boolean;
function GetPassCount : Integer;
function GetPasses : IList<ITestResult>;
function GetTestResultCount : Integer;
function GetTestResults : IList<ITestResult>;
function GetIgnoredCount : Integer;

function GetStartTime: TDateTime;
function GetFinishTime: TDateTime;
function GetDuration: TTimeSpan;

procedure AddChild(const AFixtureResult: IFixtureResult);
procedure AddTestResult(const AResult: ITestResult);
procedure RecordTestResult(const AResult : ITestResult);
procedure RollUpResults;
public
constructor Create(const AParentResult : IFixtureResult; const AFixture : ITestFixtureInfo);
end;

implementation

uses
DateUtils,
SysUtils;



{ TDUnitXFixtureResult }

procedure TDUnitXFixtureResult.AddChild(const AFixtureResult: IFixtureResult);
begin
if FChildren = nil then
FChildren := TDUnitXList<IFixtureResult>.Create;
FChildren.Add(AFixtureResult);
end;

procedure TDUnitXFixtureResult.AddTestResult(const AResult: ITestResult);
begin
if FTestResults = nil then
FTestResults := TDUnitXList<ITestResult>.Create;
FTestResults.Add(AResult);
RecordTestResult(AResult);
end;

constructor TDUnitXFixtureResult.Create(const AParentResult : IFixtureResult;const AFixture: ITestFixtureInfo);
begin
FFixture := AFixture;
FStartTime := Now;
//Don't create collections here.. we'll lazy create;
FChildren := nil;
FTestResults := nil;

if AParentResult <> nil then
(AParentResult as IFixtureResultBuilder).AddChild(Self);

end;


function TDUnitXFixtureResult.GetChildCount: Integer;
begin
if FChildren <> nil then
result := FChildren.Count
else
result := 0;
end;

function TDUnitXFixtureResult.GetChildren: IList<IFixtureResult>;
begin
//Don't pass nill back???
if FChildren = nil then
FChildren := TDUnitXList<IFixtureResult>.Create;
result := FChildren;
end;

function TDUnitXFixtureResult.GetDuration: TTimeSpan;
begin
result := FDuration;
end;

function TDUnitXFixtureResult.GetErrorCount: Integer;
begin
result := FErrorCount;
end;


function TDUnitXFixtureResult.GetErrors: IList<ITestError>;
var
test : ITestResult;
error : ITestError;
begin
result := TDUnitXList<ITestError>.Create;
if FTestResults = nil then
exit;

for test in FTestResults do
begin
if Supports(test,ITestError,error) then
result.Add(error);
end;
end;

function TDUnitXFixtureResult.GetFailureCount: Integer;
begin
result := FFailureCount;
end;

function TDUnitXFixtureResult.GetFailures: IList<ITestResult>;
var
test : ITestResult;
begin
result := TDUnitXList<ITestResult>.Create;
if FTestResults = nil then
exit;

for test in FTestResults do
begin
if test.ResultType = TTestResultType.Failure then
result.Add(test);
end;
end;

function TDUnitXFixtureResult.GetFinishTime: TDateTime;
begin
result := FFinishTime;
end;

function TDUnitXFixtureResult.GetFixture: ITestFixtureInfo;
begin
result := FFixture;
end;

function TDUnitXFixtureResult.GetHasFailures: Boolean;
begin
result := FFailureCount > 0;
end;

function TDUnitXFixtureResult.GetIgnoredCount: Integer;
begin
result := FIgnoredCount;
end;

function TDUnitXFixtureResult.GetPassCount: Integer;
begin
result := FPassCount;
end;

function TDUnitXFixtureResult.GetPasses: IList<ITestResult>;
var
test : ITestResult;
begin
result := TDUnitXList<ITestResult>.Create;
if FTestResults = nil then
exit;

for test in FTestResults do
begin
if test.ResultType = TTestResultType.Pass then
result.Add(test);
end;
end;

function TDUnitXFixtureResult.GetStartTime: TDateTime;
begin
Result := FStartTime;
end;

function TDUnitXFixtureResult.GetTestResultCount: Integer;
begin
if FTestResults = nil then
Exit(0);
result := FTestResults.Count;
end;

function TDUnitXFixtureResult.GetTestResults: IList<DUnitX.TestFramework.ITestResult>;
begin
if FTestResults = nil then
FTestResults := TDUnitXList<ITestResult>.Create;
result := FTestResults;
end;

function Max(const a, b : TDateTime) : TDateTime;
begin
if a > b then
result := a
else
result := b;
end;


procedure TDUnitXFixtureResult.RecordTestResult(const AResult: ITestResult);
begin
Inc(FTotalCount);
case AResult.ResultType of
TTestResultType.Pass : Inc(FPassCount);
TTestResultType.Failure : Inc(FFailureCount);
TTestResultType.Error : Inc(FErrorCount);
TTestResultType.Ignored : Inc(FIgnoredCount);
end;

if AResult.ResultType <> Pass then
FAllPassed := False;
end;

procedure TDUnitXFixtureResult.Reduce;
var
fixtureRes : IFixtureResult;
begin
if (FChildren <> nil) and (FChildren.Count > 0) then
begin
//Reduce the children first.
for fixtureRes in FChildren do
fixtureRes.Reduce;

//if we have no tests and only one child, then we reduce to that child.
if (FChildren.Count = 1) and ((FTestResults = nil) or (FTestResults.Count = 0)) then
begin
fixtureRes := FChildren[0];
FFixture := fixtureRes.Fixture;
if FTestResults = nil then
FTestResults := TDUnitXList<ITestResult>.Create;
FTestResults.AddRange(fixtureRes.TestResults);
FChildren.Clear;
if fixtureRes.ChildCount > 0 then
FChildren.AddRange(fixtureRes.Children)
else
FChildren.Clear;
end;
end;
end;

procedure TDUnitXFixtureResult.RollUpResults;
var
fixture : IFixtureResult;
begin
if FChildren <> nil then
begin
FFinishTime := FStartTime;
for fixture in FChildren do
begin
(fixture as IFixtureResultBuilder).RollUpResults;
Inc(FErrorCount,fixture.ErrorCount);
Inc(FFailureCount,fixture.FailureCount);
Inc(FIgnoredCount,fixture.IgnoredCount);
Inc(FPassCount,fixture.PassCount);
FAllPassed := FAllPassed and (not fixture.HasFailures);
FFinishTime := Max(FFinishTime,fixture.FinishTime);
end;
end
else
FFinishTime := Now;
FDuration := TTimeSpan.FromMilliseconds(DateUtils.MilliSecondsBetween(FFinishTime,FStartTime));

end;

end.
36 changes: 23 additions & 13 deletions DUnitX.Generics.pas
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,34 @@ interface

type
//Delphi does not have reference counted collection types.. so we created one here.
//This will typically be used where we return IEnumerbable<T> from a function
//TODO: need unit tests!!!
IList<T> = interface(IEnumerable<T>)

IReadOnlyList<T> = interface(IEnumerable<T>)
function GetCapacity : integer;
procedure SetCapacity(const value : integer);
function GetCount : integer;
procedure SetCount(const value : integer);
function GetItem(index : integer) : T;
procedure SetItem(index : integer; value : T);

function GetOnNotify : TCollectionNotifyEvent<T>;
procedure SetOnNotify(value : TCollectionNotifyEvent<T>);
function First: T;
function Last: T;
function Contains(const Value: T): Boolean;
function IndexOf(const Value: T): Integer;
function LastIndexOf(const Value: T): Integer;
function BinarySearch(const Item: T; out Index: Integer): Boolean; overload;
function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload;
property Capacity: Integer read GetCapacity;
property Count: Integer read GetCount;
property Items[Index: Integer]: T read GetItem; default;
property OnNotify: TCollectionNotifyEvent<T> read GetOnNotify write SetOnNotify;

end;

IList<T> = interface(IReadOnlyList<T>)
procedure SetCapacity(const value : integer);
procedure SetCount(const value : integer);
procedure SetItem(index : integer; value : T);


function Add(const Value: T): Integer;
procedure AddRange(const Values: array of T); overload;
Expand All @@ -68,21 +85,14 @@ interface
procedure Exchange(Index1, Index2: Integer);
procedure Move(CurIndex, NewIndex: Integer);

function First: T;
function Last: T;

procedure Clear;

function Contains(const Value: T): Boolean;
function IndexOf(const Value: T): Integer;
function LastIndexOf(const Value: T): Integer;

procedure Reverse;

procedure Sort; overload;
procedure Sort(const AComparer: IComparer<T>); overload;
function BinarySearch(const Item: T; out Index: Integer): Boolean; overload;
function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload;

procedure TrimExcess;

Expand All @@ -104,7 +114,7 @@ TDUnitXEnumerable = class(TInterfacedObject, IEnumerable)
function GetNonGenEnumerator: IEnumerator; virtual; abstract;
end;

TDUnitXList<T> = class(TDUnitXEnumerable, IList<T>)
TDUnitXList<T> = class(TDUnitXEnumerable, IList<T>,IEnumerable<T>)
private
FList : TList<T>;
protected
Expand Down
Loading

0 comments on commit 267111f

Please sign in to comment.