The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,177 other subscribers

Archive for August 29th, 2018

DUnitX: now has a WillRaiseAttribute to ease defining tests around code that should throw exceptions

Posted by jpluimers on 2018/08/29

I stumbled over this commit message in [WayBack] “extended the TestAttribute with “Expected” property (#181)” which isn’t phrased correctly, but adds a very nice feature.

The feature is about WillRaiseAttribute:

constructor WillRaiseAttribute.Create(AExpectedException: ExceptClass; const AInheritance: TExceptionInheritance);

This allows tests like these:

    [WillRaise(EOutOfMemory)]
    procedure FailMe;

    [WillRaise(EHeapException, exDescendant)]
    procedure FailMeToo;

    [WillRaise(Exception, exDescendant)]
    procedure FailAny;

    [WillRaise(EOutOfMemory)]
    [Ignore('I am not behaving as I should')]
    procedure IgnoreMeCauseImWrong;

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »

Enum values in their own namespaces/scopes: Scoped Enums (Delphi)

Posted by jpluimers on 2018/08/29

A while ago, I needed several enum types in the same unit with overlapping enumeration values.

Putting each in an encompassing type wasn’t possible and I didn’t want to put each in their own unit.

Luckily, Delphi 2009 introduced the “scoped enum” feature effectively promoting the enumeration type into a scope or namespace.

It is only available at the source code level, as – at least up until Delphi 10.1 Berlin – it is not part of the compiler settings in the project options (see screenshot below).

Since the below was hard to find combined with the word “namespace” I’ve quoted it in full (note an earlier version of the post had a typo here as it was copied from the Delphi 2009 documentation which had SCOPEDEUNMS wrong):

Type
Switch
Syntax
{$SCOPEDENUMS ON}, or {$SCOPEDENUMS OFF}
Default
{$SCOPEDENUMS OFF}
Scope
Local

Remarks

The $SCOPEDENUMS directive enables or disables the use of scoped enumerations in Delphi code. More specifically, $SCOPEDENUMS affects only definitions of new enumerations, and only controls the addition of the enumeration’s value symbols to the global scope.

In the {$SCOPEDENUMS ON} state, enumerations are scoped, and enum values are not added to the global scope. To specify a member of a scoped enum, you must include the type of the enum. For example:

type
  TFoo = (A, B, Foo);
  {$SCOPEDENUMS ON}
  TBar = (A, B, Bar);
  {$SCOPEDENUMS OFF}

begin
  WriteLn(Integer(Foo)); 
  WriteLn(Integer(A)); // TFoo.A
  WriteLn(Integer(TBar.B));
  WriteLn(Integer(TBar.Bar));
  WriteLn(Integer(Bar)); // Error
end;

Note that this is also valid:

 Writeln(Integer(TFoo.A));

Even though TFoo was not declared with $SCOPEDENUMS ON, the A value can still be explicitly resolved using the enumeration name.

Read the rest of this entry »

Posted in Delphi, Development, Software Development | 4 Comments »

 
%d bloggers like this: