Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-07

fpcUnit testing Exceptions

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

Is it possible to test the code below using the AssertException() call? I am sure it must be, I am just not sure how to use it.

This is the work-around I have now, untill I can figure out how to use
AssertException() correctly.
------------------------
  try
    si.SetSlideName('006~sa.swf');
    si.SlideTypeDB;  { <<== method raises an error if type unknown }
  except
    on E: exception do
    begin
      AssertEquals('Failing on 3', EUnknownSlideType.ClassName, E.ClassName);
    end;
  end;
------------------------

I tried the following, but got a compiler error on each one.

-----------------------------
  si.SetSlideName('006~sa.swf');
  { none of the following worked... }
  AssertException('Failing on 3', EUnknownSlideType, @si.SlideTypeDB);
  AssertException('Failing on 3', EUnknownSlideType, );
  AssertException('Failing on 3', EUnknownSlideType, si.^SlideTypeDB);
-----------------------------

Regards,
Graeme.

There's no place like 127.0.0.1

Reply by email

Unknown author
to Graeme Geldenhuys

Graeme,

I would try and test this:

AssertException('Failing on 3', EUnknownSlideType,
TRunMethod(@si.SlideTypeDB));

Haven't been able to test though, but it's what comes to mind. Perhaps it helps.

Darius

Show quoted text 40 lines
Hi, Is it possible to test the code below using the AssertException() call? I am sure it must be, I am just not sure how to use it. This is the work-around I have now, untill I can figure out how to use AssertException() correctly. ------------------------ try si.SetSlideName('006~sa.swf'); si.SlideTypeDB; { <<== method raises an error if type unknown } except on E: exception do begin AssertEquals('Failing on 3', EUnknownSlideType.ClassName, E.ClassName); end; end; ------------------------ I tried the following, but got a compiler error on each one. ----------------------------- si.SetSlideName('006~sa.swf'); { none of the following worked... } AssertException('Failing on 3', EUnknownSlideType, @si.SlideTypeDB); AssertException('Failing on 3', EUnknownSlideType, ); AssertException('Failing on 3', EUnknownSlideType, si.^SlideTypeDB); ----------------------------- Regards, Graeme. -- There's no place like 127.0.0.1 _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Graeme Geldenhuys
to Unknown author
I would try and test this: AssertException('Failing on 3', EUnknownSlideType, TRunMethod(@si.SlideTypeDB));

It gives a "Variable identifier expected" just before the dot.

I even tried putting the si.SlideTypeDB inside a Local Procedure and passing @<procedurename> but that also didn't work. :-(

Example

procedue Testcase;
    procedure LocalCall;
    begin
      si.SlideTypeDB;
    end
begin
   AssertException('Failing on 3', EUnknownSlideType, @LocalCall);
end;

Graeme.

There's no place like 127.0.0.1

Reply by email

Dean Zobec
to Graeme Geldenhuys: Is it possible to test the code below using the AssertException() call? I am sure it…
Graeme Geldenhuys wrote 29 lines
Hi, Is it possible to test the code below using the AssertException() call? I am sure it must be, I am just not sure how to use it. This is the work-around I have now, untill I can figure out how to use AssertException() correctly. ------------------------ try si.SetSlideName('006~sa.swf'); si.SlideTypeDB; { <<== method raises an error if type unknown } except on E: exception do begin AssertEquals('Failing on 3', EUnknownSlideType.ClassName, E.ClassName); end; end; ------------------------ I tried the following, but got a compiler error on each one. ----------------------------- si.SetSlideName('006~sa.swf'); { none of the following worked... } AssertException('Failing on 3', EUnknownSlideType, @si.SlideTypeDB); AssertException('Failing on 3', EUnknownSlideType, ); AssertException('Failing on 3', EUnknownSlideType, si.^SlideTypeDB); -----------------------------

from the FPCUnit code:

class procedure AssertException(const AMessage: string; AExceptionClass:
ExceptClass; AMethod: TRunMethod); overload;
where
TRunMethod = procedure of object;

How is SlideTypeDB defined?

the correct way should be

AssertException('Failing on 3', EUnknownSlideType, @si.SlideTypeDB);

provided SlideTypeDB is a simple procedure with no parameters and you are using
{$mode objfpc}

See the examples and the tests in the fcl/fpcunit directory on how to use AssertException

Btw, there is an elegant way to test that a proper exception is raised without using AssertException e.g. (from fcl/fpcunit/tests/asserttest.pp):

procedure TAssertTest.TestAssertNull;
var
  obj: TObject;
begin
  AssertNull(nil);
  obj := TObject.Create;
  try
    AssertNull(obj);
  except
    on E: EAssertionFailedError do
    begin
      obj.Free;
      Exit;
    end;
  end;
  obj.Free;
  Fail('failure: obj is not null!');
end;

Ciao,
Dean

Reply by email

Graeme Geldenhuys
to Dean Zobec

Hi,

Show quoted text 12 lines
where TRunMethod = procedure of object; How is SlideTypeDB defined? the correct way should be AssertException('Failing on 3', EUnknownSlideType, @si.SlideTypeDB); provided SlideTypeDB is a simple procedure with no parameters and you are using {$mode objfpc}

si = Is a Slide Information class. You give it a slide name, and can then retrieve all kinds of information from it.

si.SlideTypeDB = I think is a read-only property returning a string (I don't have the code infront of me). The property getter method raises an exception, if it can't figure out the Slide Type from the name.

Yes I always code in {$mode objfpc}.

I think the problem is the SlideTypeDB which is a property! I will change it to a standard function returning a string and try @si.SlideTypeDB again.

Regards,
Graeme.

There's no place like 127.0.0.1

Reply by email

Dean Zobec
to Graeme Geldenhuys
Graeme Geldenhuys wrote
I think the problem is the SlideTypeDB which is a property! I will change it to a standard function returning a string and try @si.SlideTypeDB again.

I guess it won't work, since assertException expects a procedure (TRunMethod = procedure of Object).

I would create a procedure in the TTestCase class that would internaly access this property and pass this procedure instead.

Regards,
Dean

Reply by email

Graeme Geldenhuys
to Dean Zobec
Dean Zobec wrote
I would create a procedure in the TTestCase class that would internaly access this property and pass this procedure instead.

I created a local procedure and tried to pass that (See a message I posted 2 replies ago). It didn't work, but I was on the right track... ;-) I will try again on for the
input.

Regards,
Graeme.

There's no place like 127.0.0.1

Reply by email