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
|
program testnullable;
{$mode objfpc}
{$h+}
uses sysutils, nullable;
Const
Val1 = 'Value 1';
Val2 = 'Value 2';
Function Testinit : String;
Var
A : specialize TNullable<String>;
begin
Result:='';
If a.HasValue then
Exit('May not have a value at start');
If Not a.IsNull then
Exit('May not have a value at start (null)');
end;
Function TestSetValue : String;
Var
A : specialize TNullable<String>;
begin
Result:='';
a.Value:=Val1;
If Not a.HasValue then
Exit('Setting value does not result in hasvalue');
If a.Value<>Val1 then
Exit('Setting value does not result in correct value stored');
end;
Function TestIsnull : String;
Var
A : specialize TNullable<String>;
begin
Result:='';
If Not a.IsNull then
Exit('Not null on init');
a.Value:=Val1;
If a.IsNull then
Exit('Setting value does not result in Not isNull');
end;
Function TestClear : String;
Var
A : specialize TNullable<String>;
begin
Result:='';
a.Value:=Val1;
If Not a.HasValue then
Exit('Setting value does not result in hasvalue');
A.Clear;
If a.HasValue then
Exit('Clear does not result in no value');
end;
Function TestGetEmptyValue : String;
Var
A : specialize TNullable<String>;
B : String;
begin
Result:='';
try
B:=a.Value;
Exit('Getting empty value does not result in exception : '+B);
except
on E : Exception do
if not (E is EConvertError) then
Exit('Getting empty value does not result in correct exception class');
end;
end;
Function TestGetEmptyValueOrDefault : String;
Var
A : specialize TNullable<String>;
B : String;
begin
Result:='';
try
B:=a.ValueOrDefault;
if B<>'' then
Exit('Getting empty value does not get empty value');
a.Value:=Val2;
B:=a.ValueOrDefault;
if B<>Val2 then
Exit('Getting set value does not get empty value');
except
on E : Exception do
Exit('Getting empty value or default results in exception !');
end;
end;
Function TestSetHasValue : String;
Var
A : specialize TNullable<String>;
begin
Result:='';
a.HasValue:=true;
if Not A.HasValue then
Exit('Setting hasvalue to true does not result in correct hasvalue value');
if Not (A.Value='') then
Exit('Setting hasvalue does not result in correct empty value');
A.HasValue:=False;
if A.HasValue then
Exit('Setting hasvalue to false does not result in correct hasvalue value');
end;
Function TestTypecast1 : string;
Var
A : specialize TNullable<String>;
B : String;
begin
Result:='';
a.Value:=Val1;
B:=String(A);
If not (B=Val1) then
Exit('Typecast not correct');
A.clear;
try
B:=String(A);
Exit('No exception raised');
Except
on E : Exception do
if not (E is EConvertError) then
Exit('Getting empty value does not result in correct exception class');
end;
end;
Function TestTypecast2 : string;
Var
A : specialize TNullable<String>;
B : String;
begin
Result:='';
B:=Val1;
A:=specialize TNullable<String>(B);
If Not (A.HasValue and (A.Value=Val1)) then
Exit('Typecast not correct');
end;
Function TestAssign : string;
Var
A : specialize TNullable<String>;
B : String;
begin
Result:='';
B:=Val1;
A:=B;
If Not (A.HasValue and (A.Value=Val1)) then
Exit('Assign not correct');
end;
Function TestAssign2 : string;
Var
A : specialize TNullable<String>;
B : String;
begin
Result:='';
A.Value:=Val1;
B:=A;
If Not (B=Val1) then
Exit('Assign not correct');
A.Clear;
try
B:=A;
Exit('No exception raised');
Except
on E : Exception do
if not (E is EConvertError) then
Exit('Getting empty value does not result in correct exception class');
end;
end;
Procedure DoTest(aTest,aResult : String);
begin
if aResult<>'' then
begin
writeln(aTest,' failed : ',aResult);
Halt(1);
end
else
Writeln(aTest,' OK.');
end;
begin
DoTest('TestInit',TestInit);
DoTest('TestSetValue',TestSetValue);
DoTest('TestClear',TestClear);
DoTest('TestSetHasValue',TestSetHasValue);
DoTest('TestIsNull',TestIsNull);
DoTest('TestTypeCast1',TestTypecast1);
DoTest('TestTypeCast2',TestTypecast2);
DoTest('TestAssign',TestAssign);
DoTest('TestAssign2',TestAssign2);
DoTest('TestGetEmptyValue',TestGetEmptyValue);
DoTest('TestGetEmptyValueOrDefault',TestGetEmptyValueOrDefault);
end.
|