fpcUnit testing Exceptions

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

fpcUnit testing Exceptions

Graeme Geldenhuys-2
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, si.@SlideTypeDB);
  AssertException('Failing on 3', EUnknownSlideType, si.^SlideTypeDB);
-----------------------------

Regards,
  Graeme.


--
There's no place like 127.0.0.1
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpcUnit testing Exceptions

Darius Blaszyk
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


> 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, si.@SlideTypeDB);
>   AssertException('Failing on 3', EUnknownSlideType, si.^SlideTypeDB);
> -----------------------------
>
> Regards,
>   Graeme.
>
>
> --
> There's no place like 127.0.0.1
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpcUnit testing Exceptions

Graeme Geldenhuys-2
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpcUnit testing Exceptions

Dean Zobec
In reply to this post by Graeme Geldenhuys-2
Graeme Geldenhuys wrote:

> 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, si.@SlideTypeDB);
>  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



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpcUnit testing Exceptions

Graeme Geldenhuys-2
Hi,

> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpcUnit testing Exceptions

Dean Zobec
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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpcUnit testing Exceptions

Graeme Geldenhuys-2
On 7/15/06, Dean Zobec <[hidden email]> 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 Monday at work.  Thanks for the
input.

Regards,
  Graeme.


--
There's no place like 127.0.0.1
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal