Menu

[r890]: / branches / new-backend / Tests / Src / DUnit / TestNsDatabaseUDataItem.pas  Maximize  Restore  History

Download this file

168 lines (135 with data), 4.0 kB

  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
{
Delphi DUnit Test Case for the NsDatabase.UDataItem Unit
--------------------------------------------------------
$Rev$
$Date$
}
unit TestNsDatabaseUDataItem;
interface
uses
TestFramework, NsDatabase.UCookies, NsDatabase.UDataItem, SysUtils;
type
TTestObject = class(TDBDataItem)
strict private
fID: Integer;
strict protected
procedure Finalize; override;
public
constructor Create(ID: Integer; Cookie: TDBCookie);
property ID: Integer read fID;
class var InstanceCount: Integer;
function ToString: string; override;
end;
// Not testing ownership / freeing since that tested for parent object
TestTDBDataItem = class(TTestCase)
public
procedure SetUp; override;
procedure TearDown; override;
procedure FreeAndNil(var O: TTestObject);
published
procedure TestFree;
procedure TestCookieProp;
end;
implementation
{ TTestObject }
constructor TTestObject.Create(ID: Integer; Cookie: TDBCookie);
begin
inherited Create(Cookie);
fID := ID;
Inc(InstanceCount);
end;
procedure TTestObject.Finalize;
begin
inherited;
Dec(InstanceCount);
end;
function TTestObject.ToString: string;
begin
Result := Format('%s %d', [ClassName, ID]);
end;
{ TestTDBDataItem }
procedure TestTDBDataItem.FreeAndNil(var O: TTestObject);
begin
if Assigned(O) then
begin
O.Owner := nil;
O.Free;
O := nil;
end;
end;
procedure TestTDBDataItem.SetUp;
begin
inherited;
end;
procedure TestTDBDataItem.TearDown;
begin
inherited;
end;
procedure TestTDBDataItem.TestCookieProp;
var
O1, O2, O0: TTestObject;
Cookie2: TDBCookie;
begin
Assert(TTestObject.InstanceCount = 0,
'TestTDBDataItem.TestFree: TTestObject.InstanceCount <> 0');
O1 := nil; O2 := nil; O0 := nil;
try
O1 := TTestObject.Create(100, TDBCookie.Create);
O2 := TTestObject.Create(200, TDBCookie.Create);
Check(O1.Cookie <> O2.Cookie,
Format('Expected %s cookie <> %s cookie', [O1.ToString, O2.ToString]));
Cookie2 := O2.Cookie;
O2.Free; // can do because not owned
O2 := nil;
O2 := TTestObject.Create(200, TDBCookie.Create);
Check(O2.Cookie <> Cookie2,
Format('Expected %s cookie to have changed', [O2.ToString]));
O0 := TTestObject.Create(0, TDBCookie.CreateNul);
Check(O0.Cookie.IsNul,
Format('Expected %s cookie to be nul', [O0.ToString]));
finally
FreeAndNil(O1);
FreeAndNil(O2);
FreeAndNil(O0);
end;
Assert(TTestObject.InstanceCount = 0,
'TestTDBDataItem.TestCookieProp: TTestObject.InstanceCount <> 0');
end;
procedure TestTDBDataItem.TestFree;
var
Obj: TTestObject;
begin
Assert(TTestObject.InstanceCount = 0,
'TestTDBDataItem.TestFree: TTestObject.InstanceCount <> 0');
// Check owned object
Obj := TTestObject.Create(100, TDBCookie.Create);
Obj.Owner := Self;
Check(TTestObject.InstanceCount = 1,
Format('Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
Obj.Free; // should not have been allowed
Check(TTestObject.InstanceCount = 1,
Format('Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
// Check object after removing owner
Obj.Owner := nil;
Obj.Free; // should have been allowed
Check(TTestObject.InstanceCount = 0,
Format('Expected Instance Count of 0, got %d',
[TTestObject.InstanceCount]));
// Checking object is not owned when created
Obj := TTestObject.Create(100, TDBCookie.Create);
Check(TTestObject.InstanceCount = 1,
Format('Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
Obj.Free; // should have been allowed
Check(TTestObject.InstanceCount = 0,
Format('Expected Instance Count of 0, got %d',
[TTestObject.InstanceCount]));
Assert(TTestObject.InstanceCount = 0,
'TestTDBDataItem.TestFree: TTestObject.InstanceCount <> 0');
end;
initialization
// Register test case with the test runner
RegisterTest(TestTDBDataItem.Suite);
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.