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 2,466 other followers

Archive for the ‘Spring4D’ Category

Tuple class and records in the Spring4D framework

Posted by jpluimers on 2021/07/01

The Spring Framework for Delphi has some cool tuple support.

The [WayBack] Tuple class has 3 create methods that build generic Tuple<> records with 2, 3 and 4 fields:

–jeroen

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

Delphi TestInsight: when supporting it, test if it is running at all. Same library has a deferer pattern.

Posted by jpluimers on 2021/04/08

Interesting idea by Cesar Romero (who has some interesting repositories at [WayBack] cesarliws (Cesar Romero) · GitHub and [WayBack] cesarliws — Bitbucket) when using TestInsight for Delphi: first test if TestInsight is running at all: [WayBack] foundation-4-delphi/Foundation.Test.Utils.pas at master · cesarliws/foundation-4-delphi · GitHub.

function IsTestInsightRunning: Boolean;
{$IFDEF TESTINSIGHT}
var
  TestInsightClient: ITestInsightClient;
begin
  TestInsightClient := TTestInsightRestClient.Create;
  TestInsightClient.StartedTesting(0);
  Result := not TestInsightClient.HasError;
end;
{$ELSE}
begin
  Result := False;
end;
{$ENDIF}

procedure RunRegisteredTests;
begin
  ReportMemoryLeaksOnShutdown := True;

{$IFDEF TESTINSIGHT}
  if IsTestInsightRunning then
    TestInsight.DUnit.RunRegisteredTests
  else
{$ENDIF}
    DUnitTestRunner.RunRegisteredTests;
end;

Another interesting bit from the same library is the deferer pattern (which is different from the promise pattern!)  in [WayBack] foundation-4-delphi/Foundation.System.pas at master · cesarliws/foundation-4-delphi · GitHub with the below code examples.

I think a better name might be DeferExecutionToEndOfScopeFor.

procedure ProcessFile (const FileName: string);
var
  File: TFile;
begin
  File: = TFile.Open (FileName);
  Defer (File.Close);
  while not File.EOF
  begin
    // ... process file
  end;
end; // Defer will be executed here [File.Close]

procedure ExecSql (const ConnectionString, Sql: string);
var
  Database: TDatabase;
  Exec: IDeferred;
  Query: TQuery;
begin
  Database: = TDatabase.Create (ConnectionString);
  Exec: = Defer (Database.Free);
  Database.Open;
  Exec.Defer (Database.Close);

  Query: = Database.Query (SQL);
  Exec.Defer (Query.Free);
  Exec.Defer (Query.Close);
  if Query.IsEmpty then
   Exit;

  while not Query.EOF
  begin
    // ... process query
  end;

  Exec.Defer (
    procedure
    begin
      Writeln ('Finished ExecSql');
    end
  );
end; // Defer will be executed here [Writeln, Query.Close, Database.Close, Database.Free]

–jeroen

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

Want to use SmartPointers in Delphi? Use the Spring4D one: it is unit tested and optimised

Posted by jpluimers on 2021/01/06

A while ago, there was a nice discussion on smart pointers at [WayBack] Smart Pointers – Generics vrs non-generic implementastion – RTL and Delphi Object Pascal – Delphi-PRAXiS [en].

Conclusion from that:

  • many people think that reference counted interfaces are the same as Smart Pointers
    (basically Smart Pointers are the next level and of course they are based on reference counting)
  • there are a lot of Smart Pointer implementations, but few have a test suite, nor are optimised , nor easy to use
  • The combo Shared/IShared<T>/TShared<T> from Spring4D has all of the above advantages
  • in order to optmise Smart Pointer implementations, you really have to well know the effects of modern Delphi language constructs on the compiler in various target platforms

The discussion mentioned above includes both feature and speed comparisons.

I was a bit amazed that at CodeRage 2018, Marco Cantu introduced yet another smart pointer implementation: one worse than existing implementations, and one with only basic demonstration code, leaving out a test suite.

There have many posts on my blog about smart pointers (see the list below), but Spring4D smart pointer implementation has been around for such a long time that any well respected Delphi developer by now should use them. The source is at  Shared/IShared (search for {$REGION 'Shared smart pointer'} at the current repository).

This list below on my Smart Pointer related blog posts might not be fully complete, but at least mentions that by now you should be using Spring4D.

Some comments on the CodeRage 2018 demos

Read the rest of this entry »

Posted in Conference Topics, Conferences, Delphi, Development, Event, Software Development, Spring4D | Leave a Comment »

Spring4D Mock – Delphi Unit Testing : Writing a simple spy for the CUT – Stack Overflow

Posted by jpluimers on 2020/10/28

Reminder to self: write a longer article on Delphi mocking as Spring4D mocking is much better than Delphi Mocks, especially because of code-completion reasons.

Spring4D has a Mock record that can return a Mock<T> on which you can verify using methods like Received.

See:

I got some code that I need to dig up from an old project with many more Spring4D Mock examples.

Note that:

For now, these are a start [WayBack] Delphi Unit Testing : Writing a simple spy for the CUT – Stack Overflow:

Sounds like a use case for a mock (I am using the term mock here because most frameworks refer to their various kinds of test doubles as mock)

In the following example I am using DUnit but it should not make any difference for DUnitX. I am also using the mocking feature from Spring4D 1.2 (I did not check if Delphi Mocks supports this)

unit MyClass;

interface

type
  TMyClass = class
  private
    fCounter: Integer;
  protected
    procedure MyProcedure; virtual;
  public
    property Counter: Integer read fCounter;
  end;

implementation

procedure TMyClass.MyProcedure;
begin
  Inc(fCounter);
end;

end.

program Tests;

uses
  TestFramework,
  TestInsight.DUnit,
  Spring.Mocking,
  MyClass in 'MyClass.pas';

type
  TMyClass = class(MyClass.TMyClass)
  public
    // just to make it accessible for the test
    procedure MyProcedure; override;
  end;

  TMyTest = class(TTestCase)
  published
    procedure Test1;
  end;

procedure TMyClass.MyProcedure;
begin
  inherited;
end;

procedure TMyTest.Test1;
var
  // the mock is getting auto initialized on its first use
  // and defaults to TMockBehavior.Dynamic which means it lets all calls happen
  m: Mock<TMyClass>;
  o: TMyClass;
begin
  // set this to true to actually call the "real" method
  m.CallBase := True;
  // do something with o
  o := m;
  o.MyProcedure;

  // check if the expected call actually did happen
  m.Received(Times.Once).MyProcedure;

  // to prove that it actually did call the "real" method
  CheckEquals(1, o.Counter);
end;

begin
  RegisterTest(TMyTest.Suite);
  RunRegisteredTests();
end.

Keep in mind though that this only works for virtual methods.

  • In case you need to test a method from a base class which cannot be affected by the $RTTI directive, you can use a little trick and redefine it in subclass in public section as override; abstract; this will cause the RTTI to be generated. – Honza RFeb 5 ’16 at 8:02

–jeroen

Posted in Conference Topics, Conferences, Delphi, Development, Event, Software Development, Spring4D | Leave a Comment »

Spring4d Marshmallow ORM – ensuring the right identifier quoting is used

Posted by jpluimers on 2019/10/29

When an ORM generates the SQL for a certain database back-end, it needs to use the right quoting.

Normally, the Spring4D Marshmallow project derives that from the back-end specific adapter used.

But if you use a generic TDriverConnectionAdapter, then you need to manually set the QueryLanguage property in your IDBConnection using a valid value from Spring.Persistence.SQL.Interfaces. This then uses the correct SQLGenerator from the TSQLGeneratorRegister to generate the SQL including correct quotes.

To quote identifiers, most back-ends use a double quote, but MySQL uses a backtick.

via:

You obviously already modified that code as the commented out code is the correct one.
I guess you already got that mistake when you started using MySQL via ADO as MySQL needs the backtick.

Set QueryLanguage of your IDBConnection properly (the valid values are in Spring.Persistence.SQL.Interfaces). Then the correct SQLGenerator will be used.
If you are using an adapter that is specific to a certain database then it sets the QueryLanguage properly but if you are using an adapter for components that can connect to multiple databases (like ADO or FireDAC) it does not, hence the need you specify it.

 

–jeroen

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

 
%d bloggers like this: