[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: testing error messages with functions needing a file id
From: |
PhilipNienhuis |
Subject: |
Re: testing error messages with functions needing a file id |
Date: |
Mon, 18 Mar 2013 15:11:24 -0700 (PDT) |
John W. Eaton wrote
> On 03/17/2013 06:37 AM, PhilipNienhuis wrote:
>> Daniel Sebald wrote
>>> On 03/16/2013 06:07 PM, PhilipNienhuis wrote:
>>>> In textscan.m I'd like to have a test like:
>>>>
>>>> %!error
>>>
> <missing or illegal value for>
>>> textscan (fid, "%s", "headerlines")
>>>>
>>>> but there are a few stumbling blocks:
>>>>
>>>> 1. I wouldn't know how to supply a valid file identifier to textscan so
>>>> that
>>>> the test can be made to work (apparently the error message test is
>>>> self-contained (left-adjusted against "%!") and cannot comprise
>>>> multiple
>>>> lines).
>>>
>>> I'm not completely understanding what the issue is.
>>
>> Sorry to be so vague. Another try:
>>
>> 1. Octave's error message tests seem to be exclusively one-liners only.
>> See
>> the last 3 lines in textscan's test suite.
>> 2. I want to check an error message that textscan.m only emits when
>> reading
>> from file.
>> 3. textscan.m doesn't accept file names but rather file identifiers
>> previously made with fopen()
>> 4. I wouldn't know how to squeeze an fopen and an fclose statement plus a
>> call to textscan in one line.
>>
>> Or does the error message test accept
> <statement>
> ;
> <statement>
> ;
>> constructs? (with semicolons) (hmm didn't try that)
>>
>>
>>> However, the second
>>> last sample test in that file creates a temporary file, writes some data
>>> to it, then rewinds the file and passes the FID onto the textscan test.
>>
>> Yes, I wrote that test myself a few days ago:-)
>
> I think you should be able to do something like this:
>
> %!shared fid, d, A, msg
> %! [fid, name] = mkstemp ("oct-XXXXXX", true);
> %! d = rand (1, 4);
> %! fprintf (fid, " %f %f %f %f ", d);
> %! fseek (fid, 0, "bof");
> %! A = textscan (fid, "%f %f");
> %! frewind (fid);
> %! msg = "foo error"
> %!assert (A{1}, [d(1); d(3)], 1e-6);
> %!assert (A{2}, [d(2); d(4)], 1e-6);
> %!error
> <foo error>
> error (msg)
Thanks,
I finally came up with the following, IMO convoluted code snippet:
%!test
%! f = tmpnam ();
%! fid = fopen (f, "w+");
%! msg1 = "Missing or illegal value for 'headerlines'";
%! try
%! A = textscan (fid, "", "headerlines", "hh");
%! end_try_catch;
%! fclose (fid);
%! unlink (f);
%! assert (msg1, lasterr);
...but OK it works and it's just a test.
Philip
--
View this message in context:
http://octave.1599824.n4.nabble.com/testing-error-messages-with-functions-needing-a-file-id-tp4650879p4650954.html
Sent from the Octave - Maintainers mailing list archive at Nabble.com.