Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Call all methods of an object

Subject: Call all methods of an object

From: per isakson

Date: 18 May, 2008 17:21:02

Message: 1 of 10

The tool for Unit Testing that I use, MUNIT (xtargets),
doesn't work under R2008a. Now I'm looking for a
replacement.

In such a tool one need to run all tests in a file, which
might be implemented as invoking "all" methods of an
object. What would be the best way to do that? The feature
demonstrated by this example looks useful. However, is it
documented?

/per

>> dm = DynamicReference2Method;
>> dm.('m1')(3)
Method, m1, running
>> dm.('m2')
Method, m2, running

classdef DynamicReference2Method < handle
   properties
   end
   methods
       function m1( this, ii )
           disp('Method, m1, running')
       end
       function m2( this )
           disp('Method, m2, running')
       end
   end
end

Subject: Re: Call all methods of an object

From: Doug Schwarz

Date: 18 May, 2008 21:54:05

Message: 2 of 10

In article <g0pohu$sji$1@fred.mathworks.com>,
 "per isakson" <poi.nospam@bimDOTkthDOT.se> wrote:

> The tool for Unit Testing that I use, MUNIT (xtargets),
> doesn't work under R2008a. Now I'm looking for a
> replacement.
>
> In such a tool one need to run all tests in a file, which
> might be implemented as invoking "all" methods of an
> object. What would be the best way to do that? The feature
> demonstrated by this example looks useful. However, is it
> documented?
>
> /per
>
> >> dm = DynamicReference2Method;
> >> dm.('m1')(3)
> Method, m1, running
> >> dm.('m2')
> Method, m2, running
>
> classdef DynamicReference2Method < handle
> properties
> end
> methods
> function m1( this, ii )
> disp('Method, m1, running')
> end
> function m2( this )
> disp('Method, m2, running')
> end
> end
> end

Yes, it's documented. I don't remember where exactly, but I learned
about it by reading the docs so it's in there somewhere. It may be in
the big pdf document on object oriented programming since I remember
reading through that.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Re: Call all methods of an object

From: Steven Lord

Date: 18 May, 2008 23:50:54

Message: 3 of 10


"per isakson" <poi.nospam@bimDOTkthDOT.se> wrote in message
news:g0pohu$sji$1@fred.mathworks.com...
> The tool for Unit Testing that I use, MUNIT (xtargets),
> doesn't work under R2008a. Now I'm looking for a
> replacement.
>
> In such a tool one need to run all tests in a file, which
> might be implemented as invoking "all" methods of an
> object. What would be the best way to do that? The feature
> demonstrated by this example looks useful. However, is it
> documented?

I'd do this using the METHODS function to obtain the list of methods, then
use FEVAL to evaluate the method or use STR2FUNC to create a function handle
to the method and invoke that using the regular functionHandle() syntax.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/methods.html

--
Steve Lord
slord@mathworks.com


Subject: Re: Call all methods of an object

From: ImageAnalyst

Date: 19 May, 2008 00:48:32

Message: 4 of 10

On May 18, 7:50=A0pm, "Steven Lord" <sl...@mathworks.com> wrote:
> "per isakson" <poi.nos...@bimDOTkthDOT.se> wrote in message
>
> news:g0pohu$sji$1@fred.mathworks.com...
>
> > The tool for Unit Testing that I use, MUNIT (xtargets),
> > doesn't work under R2008a. Now I'm looking for a
> > replacement.
>
> > In such a tool one need to run all tests in a file, which
> > might be implemented as invoking "all" methods of an
> > object. What would be the best way to do that? The feature
> > demonstrated by this example looks useful. However, is it
> > documented?
>
> I'd do this using the METHODS function to obtain the list of methods, then=

> use FEVAL to evaluate the method or use STR2FUNC to create a function hand=
le
> to the method and invoke that using the regular functionHandle() syntax.
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/methods.html
>
> --
> Steve Lord
> sl...@mathworks.com

-------------------------------------------------------
If you don't know the methods in advance, and have to get their names
from the METHODS function, then how do you know what arguments to pass
to those methods? Calling a method without passing it the proper
arguments will likely cause an exception.

Subject: Re: Call all methods of an object

From: Loren Shure

Date: 19 May, 2008 11:51:19

Message: 5 of 10

In article <a8af7cd9-686d-422f-afd6-1f184e74bb23
@m3g2000hsc.googlegroups.com>, imageanalyst@mailinator.com says...
> On May 18, 7:50=A0pm, "Steven Lord" <sl...@mathworks.com> wrote:
> > "per isakson" <poi.nos...@bimDOTkthDOT.se> wrote in message
> >
> > news:g0pohu$sji$1@fred.mathworks.com...
> >
> > > The tool for Unit Testing that I use, MUNIT (xtargets),
> > > doesn't work under R2008a. Now I'm looking for a
> > > replacement.
> >
> > > In such a tool one need to run all tests in a file, which
> > > might be implemented as invoking "all" methods of an
> > > object. What would be the best way to do that? The feature
> > > demonstrated by this example looks useful. However, is it
> > > documented?
> >
> > I'd do this using the METHODS function to obtain the list of methods, t=
hen
> > use FEVAL to evaluate the method or use STR2FUNC to create a function h=
andle
> > to the method and invoke that using the regular functionHandle() syntax=
.
> >
> > http://www.mathworks.com/access/helpdesk/help/techdoc/ref/methods.html
> >
> > --
> > Steve Lord
> > sl...@mathworks.com
>=20
> -------------------------------------------------------
> If you don't know the methods in advance, and have to get their names
> from the METHODS function, then how do you know what arguments to pass
> to those methods? Calling a method without passing it the proper
> arguments will likely cause an exception.
>=20

You may be able to use nargin and nargout to help with this, but it's=20
not a full solution:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nargin.html


--=20
Loren
http://blogs.mathworks.com/loren/

Subject: Re: Call all methods of an object

From: ImageAnalyst

Date: 19 May, 2008 14:54:51

Message: 6 of 10

On May 19, 7:51=A0am, Loren Shure <lo...@mathworks.com> wrote:
> In article <a8af7cd9-686d-422f-afd6-1f184e74bb23
> @m3g2000hsc.googlegroups.com>, imageanal...@mailinator.com says...
>
>
>
>
>
> > On May 18, 7:50=A0pm, "Steven Lord" <sl...@mathworks.com> wrote:
> > > "per isakson" <poi.nos...@bimDOTkthDOT.se> wrote in message
>
> > >news:g0pohu$sji$1@fred.mathworks.com...
>
> > > > The tool for Unit Testing that I use, MUNIT (xtargets),
> > > > doesn't work under R2008a. Now I'm looking for a
> > > > replacement.
>
> > > > In such a tool one need to run all tests in a file, which
> > > > might be implemented as invoking "all" methods of an
> > > > object. What would be the best way to do that? The feature
> > > > demonstrated by this example looks useful. However, is it
> > > > documented?
>
> > > I'd do this using the METHODS function to obtain the list of methods, =
then
> > > use FEVAL to evaluate the method or use STR2FUNC to create a function =
handle
> > > to the method and invoke that using the regular functionHandle() synta=
x.
>
> > >http://www.mathworks.com/access/helpdesk/help/techdoc/ref/methods.html
>
> > > --
> > > Steve Lord
> > > sl...@mathworks.com
>
> > -------------------------------------------------------
> > If you don't know the methods in advance, and have to get their names
> > from the METHODS function, then how do you know what arguments to pass
> > to those methods? =A0Calling a method without passing it the proper
> > arguments will likely cause an exception.
>
> You may be able to use nargin and nargout to help with this, but it's
> not a full solution:
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nargin.html
>
> --
> Lorenhttp://blogs.mathworks.com/loren/- Hide quoted text -
>
> - Show quoted text -

------------------------------------------------------------------------
That just gets you the number of arguments it's expecting.

I still assert that mindlessly calling a function without knowing what
the function expects, or what you should expect in return, is
worthless. You MUST know what you're doing.

For example, maybe I can call function/method m1 and pass it a string
filename and it will be happy (say it just displays an image file),
but passing a string into m2 may throw an exception (maybe the file
doesn't exist or maybe m2 multiplies the argument by pi). And if you
don't know what the name is, then you don't know what it's expected to
do, so how can you possibly tell if it's operating properly by
checking its return value? Say I call m1 and pass in, oh say 35
(randomly pulled from my hat), and I get out, oh say 90. If I didn't
even know the name of the function, nor what it does, then how can I
be assured that 90 is the correct answer? So what's the point? This
is no way to check a function (do a "Performance Qualification with a
check sample" to use proper QA lingo) or do unit tests, unless I've
seriously misunderstood what the intent was.
Regards,
ImageAnalyst

Subject: Re: Call all methods of an object

From: per isakson

Date: 19 May, 2008 18:34:01

Message: 7 of 10

"Steven Lord" <slord@mathworks.com> wrote in message
<g0qfcu$tp$1@fred.mathworks.com>...
>
> "per isakson" <poi.nospam@bimDOTkthDOT.se> wrote in
message
> news:g0pohu$sji$1@fred.mathworks.com...
> > The tool for Unit Testing that I use, MUNIT (xtargets),
> > doesn't work under R2008a. Now I'm looking for a
> > replacement.
> >
> > In such a tool one need to run all tests in a file,
which
> > might be implemented as invoking "all" methods of an
> > object. What would be the best way to do that? The
feature
> > demonstrated by this example looks useful. However, is
it
> > documented?
>
> I'd do this using the METHODS function to obtain the list
of methods, then
> use FEVAL to evaluate the method or use STR2FUNC to
create a function handle
> to the method and invoke that using the regular
functionHandle() syntax.
>
>
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/me
thods.html
>
> --
> Steve Lord
> slord@mathworks.com
>

I guess I raised three different questions:
1. How to know for sure that an operational feature in
Matlab is NOT documented*?
2. Is the calling syntax: object.(‘method1’) documented?
3. A vague question on a design detail of a unit test
framework.

*) documented meaning that The Mathworks promises that the
feature will work in future versions of Matlab.

Background: I have a large number of test and a broken unit
testing framework. Three(?) years ago I thought that it was
safe to use “evalin(‘caller’,‘@name’)” (wishful thinking?).

I had something like the following in mind:

for ca = list_of_tests
    obj.setup()
    … obj.(ca{:})
    obj.teardown()
end

Where list_of_tests is a cell array of strings holding
names of methods, each of which implements a test.

Doug, I cannot find the text you refer to. The matter ought
to be treated in “MATLAB? 7 Classes and Object-Oriented
Programming” on page 7-9 or close.

Steve, you propose two alternatives to “object.(
strName )”, but avoid the question whether it is
documented. Thus, I should be wise and use one of your
alternatives.

Loren and ImageAnalyst, indeed the signatures of the
methods must be known beforehand. Thus, the methods, which
implement test, must honor a given signature.

Thank you for your answeres
Per


Subject: Re: Call all methods of an object

From: Doug Schwarz

Date: 20 May, 2008 02:13:18

Message: 8 of 10

In article <g0sh6p$2pq$1@fred.mathworks.com>,
 "per isakson" <poi.nospam@bimDOTkthDOT.se> wrote:

[snip]

> Doug, I cannot find the text you refer to. The matter ought
> to be treated in “MATLAB? 7 Classes and Object-Oriented
> Programming” on page 7-9 or close.

I found it. Quoting from matlab_oop.pdf for R2008a, beginning on page
7-10:


Referencing Names with Expressions‹Dynamic Reference

You can reference an object¹s properties or methods using an expression
in dot-parentheses syntax:

  obj.(expression)

The expression must evaluate to a string that is the name of a property
or a method. For example, the following statements are equivalent:

  obj.Property1
  obj.('Property1')

In this case, obj is an object of a class that defines a property called
Property1. Therefore, you can pass a string variable in the parentheses
to reference to property:

  propName = 'Property1';
  obj.(propName)

You can call a method and pass input arguments to the method using
another set of parentheses:

  obj.(expression)(arg1,arg2,...)

Using this notation, you can make dynamic references to properties and
methods in the same way you can create dynamic references to the fields
of structs (see ³Using Dynamic Field Names² for information on MATLAB
structures).



Per, I don't think you are looking at the right document.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Re: Call all methods of an object

From: per isakson

Date: 20 May, 2008 17:47:03

Message: 9 of 10

Doug Schwarz <see@sig.for.address.edu> wrote in message
<snip>
> Per, I don't think you are looking at the right document.
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Doug, you are perfectly right! I'm not looking the right
document and the reason is that The Mathworks has revised
matlab_oop.pdf sometimes after 02-Mar-2008. I downloaded
the current file, which is slightly bigger, and found the
text you refer to.

matlab_oop.pdf 02-Mar-2008 00:24:02 2337282
matlab_oop.pdf 20-May-2008 18:44:21 2341546

The revision is not noted on page ii.
Revision History
March 2008 Online only New for MATLAB 7.6 (Release 2008a)

Thank you for making me aware.
 
/ per


Subject: Re: Call all methods of an object

From: thomas.dohmke@gmail.com

Date: 23 May, 2008 21:20:29

Message: 10 of 10

On 18 Mai, 19:21, "per isakson" <poi.nos...@bimDOTkthDOT.se> wrote:
> The tool for Unit Testing that I use, MUNIT (xtargets),
> doesn't work under R2008a. Now I'm looking for a
> replacement.

I ported my own testing tool, named mlUnit, to R2008a some weeks ago.
The current release is mlUnit 2 Beta 1 (so far I haven't received any
bug reports, so it seems to be quite stable), see
http://sourceforge.net/projects/mlunit/

> In such a tool one need to run all tests in a file, which
> might be implemented as invoking "all" methods of an
> object.

Exactly this is possible with mlUnit 2. Example:

>> edit test.sin.m
    classdef test_sin < mlunit.test_case

        methods
            function self = test_sin(varargin)
                self = self@mlunit.test_case(varargin{:});
            end

            function self = test_null(self)
                mlunit.assert_equals(0, sin(0));
            end

            function self = test_sin_cos(self)
                mlunit.assert_equals(cos(0), sin(pi/2));
            end
        end
    end
>> runner = mlunit.text_test_runner(1, 1);
>> loader = mlunit.test_loader;
>> run(runner, load_tests_from_test_case(loader, 'test_sin'));

The last statement runs all methods of the class "test_sin", which
have a name beginning with "test". You can also create a method
"set_up" and a method "tear_down" to manage a test fixture, which is
shared by the test methods. A number of examples can be found in
mlUnit's own test cases:
http://mlunit.svn.sourceforge.net/viewvc/mlunit/mlunit2/trunk/test/

Hope that helps.

Thomas

Tags for this Thread

Everyone's Tags:

oop

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
oop per isakson 18 May, 2008 13:25:05
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics