Browse Source

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

git-svn-id: trunk@11284 -
florian 17 years ago
parent
commit
67e811db60
3 changed files with 109 additions and 35 deletions
  1. 1 0
      .gitattributes
  2. 62 35
      rtl/objpas/typinfo.pp
  3. 46 0
      tests/webtbs/tw11372.pp

+ 1 - 0
.gitattributes

@@ -8351,6 +8351,7 @@ tests/webtbs/tw1132.pp svneol=native#text/plain
 tests/webtbs/tw1133.pp svneol=native#text/plain
 tests/webtbs/tw1133.pp svneol=native#text/plain
 tests/webtbs/tw11349.pp svneol=native#text/plain
 tests/webtbs/tw11349.pp svneol=native#text/plain
 tests/webtbs/tw11354.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/tw1152.pp svneol=native#text/plain
 tests/webtbs/tw11543.pp svneol=native#text/plain
 tests/webtbs/tw11543.pp svneol=native#text/plain
 tests/webtbs/tw1157.pp svneol=native#text/plain
 tests/webtbs/tw1157.pp svneol=native#text/plain

+ 62 - 35
rtl/objpas/typinfo.pp

@@ -65,7 +65,7 @@ unit typinfo;
 
 
    type
    type
       TTypeKinds = set of TTypeKind;
       TTypeKinds = set of TTypeKind;
-		  ShortStringBase = string[255];
+      ShortStringBase = string[255];
 
 
 {$PACKRECORDS 1}
 {$PACKRECORDS 1}
       TTypeInfo = record
       TTypeInfo = record
@@ -146,14 +146,14 @@ unit typinfo;
                RawIntfUnit: ShortString;
                RawIntfUnit: ShortString;
                IIDStr: ShortString;
                IIDStr: ShortString;
               );
               );
-			      tkDynArray:
-			        (
-			        elSize     : PtrUInt;
-			        elType2    : PPTypeInfo;
-			        varType    : Longint;
-			        elType     : PPTypeInfo;
-			        DynUnitName: ShortStringBase
-			        );
+            tkDynArray:
+              (
+              elSize     : PtrUInt;
+              elType2    : PPTypeInfo;
+              varType    : Longint;
+              elType     : PPTypeInfo;
+              DynUnitName: ShortStringBase
+              );
       end;
       end;
 
 
       // unsed, just for completeness
       // unsed, just for completeness
@@ -348,16 +348,26 @@ Function GetEnumName(TypeInfo : PTypeInfo;Value : Integer) : string;
       PT : PTypeData;
       PT : PTypeData;
 
 
 begin
 begin
- PT:=GetTypeData(TypeInfo);
- // ^.BaseType);
- //      If PT^.MinValue<0 then Value:=Ord(Value<>0); {map to 0/1}
- PS:=@PT^.NameList;
- While Value>0 Do
-  begin
-    PS:=PShortString(pointer(PS)+PByte(PS)^+1);
-    Dec(Value);
-  end;
- Result:=PS^;
+  PT:=GetTypeData(TypeInfo);
+  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
+         PS:=PShortString(pointer(PS)+PByte(PS)^+1);
+         Dec(Value);
+       end;
+     Result:=PS^;
+   end;
 end;
 end;
 
 
 
 
@@ -375,14 +385,26 @@ begin
   PT:=GetTypeData(TypeInfo);
   PT:=GetTypeData(TypeInfo);
   Count:=0;
   Count:=0;
   Result:=-1;
   Result:=-1;
-  PS:=@PT^.NameList;
-  While (Result=-1) and (PByte(PS)^<>0) do
+  
+  if TypeInfo^.Kind=tkBool then 
     begin
     begin
-      If ShortCompareText(PS^, sName) = 0 then
-        Result:=Count;
-      PS:=PShortString(pointer(PS)+PByte(PS)^+1);
-      Inc(Count);
-    end;
+    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
+         If ShortCompareText(PS^, sName) = 0 then
+           Result:=Count;
+         PS:=PShortString(pointer(PS)+PByte(PS)^+1);
+         Inc(Count);
+       end;
+   end;
 end;
 end;
 
 
 
 
@@ -393,17 +415,22 @@ var
   Count: SizeInt;
   Count: SizeInt;
 begin
 begin
   PT:=GetTypeData(enum1);
   PT:=GetTypeData(enum1);
-  Count:=0;
-  Result:=0;
-
-  PS:=@PT^.NameList;
-  While (PByte(PS)^<>0) do
+  if enum1^.Kind=tkBool then 
+    Result:=2
+  else
     begin
     begin
-      PS:=PShortString(pointer(PS)+PByte(PS)^+1);
-      Inc(Count);
+      Count:=0;
+      Result:=0;
+    
+      PS:=@PT^.NameList;
+      While (PByte(PS)^<>0) do
+        begin
+          PS:=PShortString(pointer(PS)+PByte(PS)^+1);
+          Inc(Count);
+        end;
+    
+      Result := Count;
     end;
     end;
-
-  Result := Count;
 end;
 end;
 
 
 
 

+ 46 - 0
tests/webtbs/tw11372.pp

@@ -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.