* handle booleans correctly in Typinfo.GetEnum*, resolves #11372

git-svn-id: trunk@11284 -
This commit is contained in:
florian 2008-06-26 20:01:31 +00:00
parent 048c7224b0
commit 67e811db60
3 changed files with 109 additions and 35 deletions

1
.gitattributes vendored
View File

@ -8351,6 +8351,7 @@ tests/webtbs/tw1132.pp svneol=native#text/plain
tests/webtbs/tw1133.pp svneol=native#text/plain
tests/webtbs/tw11349.pp svneol=native#text/plain
tests/webtbs/tw11354.pp svneol=native#text/plain
tests/webtbs/tw11372.pp svneol=native#text/plain
tests/webtbs/tw1152.pp svneol=native#text/plain
tests/webtbs/tw11543.pp svneol=native#text/plain
tests/webtbs/tw1157.pp svneol=native#text/plain

View File

@ -349,8 +349,17 @@ Function GetEnumName(TypeInfo : PTypeInfo;Value : Integer) : string;
begin
PT:=GetTypeData(TypeInfo);
// ^.BaseType);
// If PT^.MinValue<0 then Value:=Ord(Value<>0); {map to 0/1}
if TypeInfo^.Kind=tkBool then
begin
case Value of
0,1:
Result:=BooleanIdents[Boolean(Value)];
else
Result:='';
end;
end
else
begin
PS:=@PT^.NameList;
While Value>0 Do
begin
@ -359,6 +368,7 @@ begin
end;
Result:=PS^;
end;
end;
Function GetEnumValue(TypeInfo : PTypeInfo;const Name : string) : Integer;
@ -375,6 +385,17 @@ begin
PT:=GetTypeData(TypeInfo);
Count:=0;
Result:=-1;
if TypeInfo^.Kind=tkBool then
begin
If CompareText(BooleanIdents[false],Name)=0 then
result:=0
else if CompareText(BooleanIdents[true],Name)=0 then
result:=1;
end
else
begin
PS:=@PT^.NameList;
While (Result=-1) and (PByte(PS)^<>0) do
begin
@ -384,6 +405,7 @@ begin
Inc(Count);
end;
end;
end;
function GetEnumNameCount(enum1: PTypeInfo): SizeInt;
@ -393,6 +415,10 @@ var
Count: SizeInt;
begin
PT:=GetTypeData(enum1);
if enum1^.Kind=tkBool then
Result:=2
else
begin
Count:=0;
Result:=0;
@ -405,6 +431,7 @@ begin
Result := Count;
end;
end;
Function SetToString(PropInfo: PPropInfo; Value: Integer; Brackets: Boolean) : String;

46
tests/webtbs/tw11372.pp Normal file
View File

@ -0,0 +1,46 @@
program BoolAsEnumTest_FPC;
{$MODE Delphi}
{$APPTYPE CONSOLE}
uses
SysUtils,
TypInfo;
procedure Test_GetEnumName;
begin
writeln('Testing GetEnumName');
if TypInfo.GetEnumName(TypeInfo(Boolean),Ord(False))<>'False' then
halt(1);
if TypInfo.GetEnumName(TypeInfo(Boolean),Ord(True))<>'True' then
halt(1);
end;
procedure Test_GetEnumValue;
begin
writeln('Testing GetEnumValue');
if TypInfo.GetEnumValue(TypeInfo(Boolean),'false')<>0 then
halt(1);
if TypInfo.GetEnumValue(TypeInfo(Boolean),'true')<>1 then
halt(1);
end;
procedure Test_GetEnumCount;
begin
writeln('Testing GetEnumCount');
if TypInfo.GetEnumNameCount(TypeInfo(Boolean))<>Ord(High(Boolean))+1 then
halt(1);
end;
begin
Test_GetEnumCount;
Test_GetEnumValue;
Test_GetEnumName;
writeln('Ok');
end.