Assertion for File.Exists()

3,759 views
Skip to first unread message

Charles

unread,
Apr 21, 2009, 11:45:24 PM4/21/09
to Google C++ Testing Framework
In our unit test, we sometime requires a check to ensure a particular
file is present before reading it. Currently it requires something
like this:

FILE f = fopen("afilename");
if (f == NULL)
{
// assert fail
}
// otherwise, continue with test

Does anyone have a good suggestion on reducing the amount of typing
required for such a simple test? I want to explore the options before
suggesting adding ASSERT_FILE_EXIST() macro to Google Test.

Thanks,
Charles

Zhanyong Wan (λx.x x)

unread,
Apr 22, 2009, 12:12:38 AM4/22/09
to Charles, Google C++ Testing Framework
How about:

FILE* f = fopen("afilename", "r");
ASSERT_TRUE(f != NULL);

?

--
Zhanyong

Charles Chan

unread,
Apr 22, 2009, 12:20:43 AM4/22/09
to Zhanyong Wan (λx.x x), Google C++ Testing Framework
Yes. That would work, I guess I can even type in ASSERT_TRUE(fopen("afilename", "r") != NULL), but it does look very clean from a test perspective and there is always a cleanup required - using fclose().

Something like the following reads more naturally, but I am not sure if there is any benefit to others out there:

ASSERT_FILE_EXIST("afilename"); // fopen, test file handle, fclose

Anyone have similar needs or reasons against this?

Charles

2009/4/21 Zhanyong Wan (λx.x x) <w...@google.com>

Zhanyong Wan (λx.x x)

unread,
Apr 22, 2009, 12:33:23 AM4/22/09
to Charles Chan, Google C++ Testing Framework
2009/4/21 Charles Chan <charles...@gmail.com>:

> Yes. That would work, I guess I can even type in
> ASSERT_TRUE(fopen("afilename", "r") != NULL), but it does look very clean
> from a test perspective and there is always a cleanup required - using
> fclose().

You mean you don't plan to use the opened file pointer f in the code
that follows?

> Something like the following reads more naturally, but I am not sure if
> there is any benefit to others out there:
>
> ASSERT_FILE_EXIST("afilename"); // fopen, test file handle, fclose

No need to create a new macro every time you want to test a different
condition. Just write an ordinary function:

bool FileExists(const char* filename);

And then:

ASSERT_TRUE(FileExists("afilename"));

You can do whatever in FileExists().

Or, if you use Google Mock and prefer the ASSERT_THAT syntax, you can
define a matcher:

MATCHER(IsExistingFile) {
FILE* f = fopen(std::string(arg).c_str(), "r");
bool success = f != NULL;
fclose(f);
return success;
}
...
ASSERT_THAT("afilename", IsExistingFile());

Google Test and Google Mock are extensible. You can help yourself. :-)


>
> Anyone have similar needs or reasons against this?
>
> Charles
>
> 2009/4/21 Zhanyong Wan (λx.x x) <w...@google.com>
>>
>> How about:
>>
>>  FILE* f = fopen("afilename", "r");
>>  ASSERT_TRUE(f != NULL);
>>
>> ?
>>
>> On Tue, Apr 21, 2009 at 8:45 PM, Charles <charles...@gmail.com>
>> wrote:
>> >
>> >
>> > In our unit test, we sometime requires a check to ensure a particular
>> > file is present before reading it. Currently it requires something
>> > like this:
>> >
>> > FILE f = fopen("afilename");
>> > if (f == NULL)
>> > {
>> >  // assert fail
>> > }
>> > // otherwise, continue with test
>> >
>> > Does anyone have a good suggestion on reducing the amount of typing
>> > required for such a simple test? I want to explore the options before
>> > suggesting adding ASSERT_FILE_EXIST() macro to Google Test.
>> >
>> > Thanks,
>> > Charles
>> >
>>
>>
>>
>> --
>> Zhanyong
>
>

--
Zhanyong

Charles Chan

unread,
Apr 22, 2009, 10:08:26 AM4/22/09
to Zhanyong Wan (λx.x x), Google C++ Testing Framework
Yeah. That would do, it's sort of direction I am currently taking.

I haven't use Google Mock a whole log yet, but will take a look later.

Thanks.
Reply all
Reply to author
Forward
0 new messages