FILE* f = fopen("afilename", "r");
ASSERT_TRUE(f != NULL);
?
--
Zhanyong
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