2

I'm using DSharp mocks in Delphi XE5 (thanks Stefan!!) but have a problem with some enumerators. If I try to define an enumerator with specific values, the {$M+} directive causes the following error:

E2134 Type 'TMyEnum' has no type info

Here is a sample console app which sums it all up:

program DSharpMockEnum;

program DSharpMockEnum;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  DSharp.Testing.Mock,
  System.SysUtils;

type

  TBadEnum = (badEnum1 = 1, badEnum2); // <---- WONT COMPILE
  TGoodEnum = (goodEnum1, goodEnum2); // This compiles OK

  {$M+}
  IBadMock = interface
  ['{34B3904E-3EBA-4C6E-BBA8-A40A67A32E7F}']
    function GetEnum: TBadEnum;
    procedure SetEnum(Value: TBadEnum);
    property MyEnum: TBadEnum read GetEnum write SetEnum;
  end;

  {$M+}
  IGoodMock = interface
  ['{34B3904E-3EBA-4C6E-BBA8-A40A67A32E7F}']
    function GetEnum: TGoodEnum;
    procedure SetEnum(Value: TGoodEnum);
    property MyEnum: TGoodEnum read GetEnum write SetEnum;
  end;

var
  LGoodMock: Mock<IGoodMock>;
  LBadMock: Mock<IBadMock>;
begin
  try
    Writeln('Good Mock');
    LGoodMock.Setup.WillReturn(goodEnum1).Any.WhenCalling.MyEnum;

    Writeln('Bad Mock');
    LBadMock.Setup.WillReturn(badEnum1).Any.WhenCalling.MyEnum;

    Writeln('Stop');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Any ideas or suggestions? I prefer to avoid resetting all my enums since some are stored in the database and need specific values. Delphi XE5. Thanks. Rick.

Rick Wheeler
  • 1,142
  • 10
  • 22
  • These types are called enumerations, enumerator is the thing you need for `for in` loops - just saying ;) And here another answer about the missing type info from Barry: http://stackoverflow.com/a/1420649/587106 – Stefan Glienke May 29 '14 at 15:10
  • Thank you Stefan! I'm sure I'm not alone in how much I appreciate your work both here and your open source contributions. Simply awesome. – Rick Wheeler May 29 '14 at 19:47

1 Answers1

2

answer in error log. Enums with assigned values does not have RTTI. If u want enums with rtti and concrete values, u must use dummy emeration entries, like TBadEnum = (badDummy0, badEnum1, badEnum2,badDummy3, badEnum4);

Here badDummy0 effectively stores 0, and badDummy3 is 3

Other option is to use some integer type

AlekXL
  • 81
  • 1
  • As [documented](http://docwiki.embarcadero.com/RADStudio/XE6/en/Simple_Types#Enumerated_Types_with_Explicitly_Assigned_Ordinality) – Sir Rufo May 29 '14 at 06:35