Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
0aacd0cf
Commit
0aacd0cf
authored
May 05, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replace EXPECT_STDOUT and EXPECT_STDERR with a single macro EXPECT_WRITE.
parent
098f8ac3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
29 deletions
+48
-29
test/format-test.cc
test/format-test.cc
+1
-2
test/gtest-extra-test.cc
test/gtest-extra-test.cc
+43
-19
test/gtest-extra.h
test/gtest-extra.h
+4
-8
No files found.
test/format-test.cc
View file @
0aacd0cf
...
@@ -1815,8 +1815,7 @@ TEST(FormatIntTest, FormatDec) {
...
@@ -1815,8 +1815,7 @@ TEST(FormatIntTest, FormatDec) {
#ifdef FMT_USE_FILE_DESCRIPTORS
#ifdef FMT_USE_FILE_DESCRIPTORS
TEST
(
FormatTest
,
PrintColored
)
{
TEST
(
FormatTest
,
PrintColored
)
{
EXPECT_STDOUT
(
EXPECT_WRITE
(
stdout
,
fmt
::
PrintColored
(
fmt
::
RED
,
"Hello, {}!
\n
"
)
<<
"world"
,
fmt
::
PrintColored
(
fmt
::
RED
,
"Hello, {}!
\n
"
)
<<
"world"
,
"
\x1b
[31mHello, world!
\n\x1b
[0m"
);
"
\x1b
[31mHello, world!
\n\x1b
[0m"
);
}
}
...
...
test/gtest-extra-test.cc
View file @
0aacd0cf
...
@@ -83,17 +83,20 @@ class SingleEvaluationTest : public ::testing::Test {
...
@@ -83,17 +83,20 @@ class SingleEvaluationTest : public ::testing::Test {
SingleEvaluationTest
()
{
SingleEvaluationTest
()
{
p_
=
s_
;
p_
=
s_
;
a_
=
0
;
a_
=
0
;
b_
=
0
;
}
}
static
const
char
*
const
s_
;
static
const
char
*
const
s_
;
static
const
char
*
p_
;
static
const
char
*
p_
;
static
int
a_
;
static
int
a_
;
static
int
b_
;
};
};
const
char
*
const
SingleEvaluationTest
::
s_
=
"01234"
;
const
char
*
const
SingleEvaluationTest
::
s_
=
"01234"
;
const
char
*
SingleEvaluationTest
::
p_
;
const
char
*
SingleEvaluationTest
::
p_
;
int
SingleEvaluationTest
::
a_
;
int
SingleEvaluationTest
::
a_
;
int
SingleEvaluationTest
::
b_
;
void
ThrowNothing
()
{}
void
ThrowNothing
()
{}
...
@@ -109,10 +112,11 @@ TEST_F(SingleEvaluationTest, FailedEXPECT_THROW_MSG) {
...
@@ -109,10 +112,11 @@ TEST_F(SingleEvaluationTest, FailedEXPECT_THROW_MSG) {
EXPECT_EQ
(
s_
+
1
,
p_
);
EXPECT_EQ
(
s_
+
1
,
p_
);
}
}
// Tests that when EXPECT_
STDOUT
fails, it evaluates its message argument
// Tests that when EXPECT_
WRITE
fails, it evaluates its message argument
// exactly once.
// exactly once.
TEST_F
(
SingleEvaluationTest
,
FailedEXPECT_STDOUT
)
{
TEST_F
(
SingleEvaluationTest
,
FailedEXPECT_WRITE
)
{
EXPECT_NONFATAL_FAILURE
(
EXPECT_STDOUT
(
std
::
printf
(
"test"
),
p_
++
),
"01234"
);
EXPECT_NONFATAL_FAILURE
(
EXPECT_WRITE
(
stdout
,
std
::
printf
(
"test"
),
p_
++
),
"01234"
);
EXPECT_EQ
(
s_
+
1
,
p_
);
EXPECT_EQ
(
s_
+
1
,
p_
);
}
}
...
@@ -122,29 +126,55 @@ TEST_F(SingleEvaluationTest, ExceptionTests) {
...
@@ -122,29 +126,55 @@ TEST_F(SingleEvaluationTest, ExceptionTests) {
EXPECT_THROW_MSG
({
// NOLINT
EXPECT_THROW_MSG
({
// NOLINT
a_
++
;
a_
++
;
ThrowException
();
ThrowException
();
},
std
::
exception
,
"test"
);
},
std
::
exception
,
(
b_
++
,
"test"
)
);
EXPECT_EQ
(
1
,
a_
);
EXPECT_EQ
(
1
,
a_
);
EXPECT_EQ
(
1
,
b_
);
// failed EXPECT_THROW_MSG, throws different type
// failed EXPECT_THROW_MSG, throws different type
EXPECT_NONFATAL_FAILURE
(
EXPECT_THROW_MSG
({
// NOLINT
EXPECT_NONFATAL_FAILURE
(
EXPECT_THROW_MSG
({
// NOLINT
a_
++
;
a_
++
;
ThrowException
();
ThrowException
();
},
std
::
logic_error
,
"test"
),
"throws a different type"
);
},
std
::
logic_error
,
(
b_
++
,
"test"
)
),
"throws a different type"
);
EXPECT_EQ
(
2
,
a_
);
EXPECT_EQ
(
2
,
a_
);
EXPECT_EQ
(
2
,
b_
);
// failed EXPECT_THROW_MSG, throws an exception with different message
// failed EXPECT_THROW_MSG, throws an exception with different message
EXPECT_NONFATAL_FAILURE
(
EXPECT_THROW_MSG
({
// NOLINT
EXPECT_NONFATAL_FAILURE
(
EXPECT_THROW_MSG
({
// NOLINT
a_
++
;
a_
++
;
ThrowException
();
ThrowException
();
},
std
::
exception
,
"other"
),
"throws an exception with a different message"
);
},
std
::
exception
,
(
b_
++
,
"other"
)),
"throws an exception with a different message"
);
EXPECT_EQ
(
3
,
a_
);
EXPECT_EQ
(
3
,
a_
);
EXPECT_EQ
(
3
,
b_
);
// failed EXPECT_THROW_MSG, throws nothing
// failed EXPECT_THROW_MSG, throws nothing
EXPECT_NONFATAL_FAILURE
(
EXPECT_NONFATAL_FAILURE
(
EXPECT_THROW_MSG
(
a_
++
,
std
::
exception
,
"test"
),
"throws nothing"
);
EXPECT_THROW_MSG
(
a_
++
,
std
::
exception
,
(
b_
++
,
"test"
)
),
"throws nothing"
);
EXPECT_EQ
(
4
,
a_
);
EXPECT_EQ
(
4
,
a_
);
EXPECT_EQ
(
4
,
b_
);
}
// Tests that assertion arguments are evaluated exactly once.
TEST_F
(
SingleEvaluationTest
,
WriteTests
)
{
// successful EXPECT_WRITE
EXPECT_WRITE
(
stdout
,
{
// NOLINT
a_
++
;
std
::
printf
(
"test"
);
},
(
b_
++
,
"test"
));
EXPECT_EQ
(
1
,
a_
);
EXPECT_EQ
(
1
,
b_
);
// failed EXPECT_WRITE
EXPECT_NONFATAL_FAILURE
(
EXPECT_WRITE
(
stdout
,
{
// NOLINT
a_
++
;
std
::
printf
(
"test"
);
},
(
b_
++
,
"other"
)),
"Actual: test"
);
EXPECT_EQ
(
2
,
a_
);
EXPECT_EQ
(
2
,
b_
);
}
}
// TODO: test EXPECT_STD*
// Tests that the compiler will not complain about unreachable code in the
// Tests that the compiler will not complain about unreachable code in the
// EXPECT_THROW_MSG macro.
// EXPECT_THROW_MSG macro.
TEST
(
ExpectThrowTest
,
DoesNotGenerateUnreachableCodeWarning
)
{
TEST
(
ExpectThrowTest
,
DoesNotGenerateUnreachableCodeWarning
)
{
...
@@ -291,11 +321,11 @@ TEST_F(BufferedFileTest, CloseFileInDtor) {
...
@@ -291,11 +321,11 @@ TEST_F(BufferedFileTest, CloseFileInDtor) {
TEST_F
(
BufferedFileTest
,
CloseErrorInDtor
)
{
TEST_F
(
BufferedFileTest
,
CloseErrorInDtor
)
{
BufferedFile
*
f
=
new
BufferedFile
(
OpenFile
(
".travis.yml"
));
BufferedFile
*
f
=
new
BufferedFile
(
OpenFile
(
".travis.yml"
));
// The close function must be called inside EXPECT_
STDERR
, otherwise
// The close function must be called inside EXPECT_
WRITE
, otherwise
// the system may recycle closed file descriptor when redirecting the
// the system may recycle closed file descriptor when redirecting the
// output in EXPECT_STDERR and the second close will break output
// output in EXPECT_STDERR and the second close will break output
// redirection.
// redirection.
EXPECT_
STDERR
(
close
(
fileno
(
f
->
get
()));
delete
f
,
EXPECT_
WRITE
(
stderr
,
close
(
fileno
(
f
->
get
()));
delete
f
,
FormatSystemErrorMessage
(
EBADF
,
"cannot close file"
)
+
"
\n
"
);
FormatSystemErrorMessage
(
EBADF
,
"cannot close file"
)
+
"
\n
"
);
}
}
...
@@ -380,11 +410,11 @@ TEST(FileTest, CloseFileInDtor) {
...
@@ -380,11 +410,11 @@ TEST(FileTest, CloseFileInDtor) {
TEST
(
FileTest
,
CloseErrorInDtor
)
{
TEST
(
FileTest
,
CloseErrorInDtor
)
{
File
*
f
=
new
File
(
".travis.yml"
,
File
::
RDONLY
);
File
*
f
=
new
File
(
".travis.yml"
,
File
::
RDONLY
);
#ifndef _WIN32
#ifndef _WIN32
// The close function must be called inside EXPECT_
STDERR
, otherwise
// The close function must be called inside EXPECT_
WRITE
, otherwise
// the system may recycle closed file descriptor when redirecting the
// the system may recycle closed file descriptor when redirecting the
// output in EXPECT_STDERR and the second close will break output
// output in EXPECT_STDERR and the second close will break output
// redirection.
// redirection.
EXPECT_
STDERR
(
FMT_POSIX
(
close
(
f
->
descriptor
()));
delete
f
,
EXPECT_
WRITE
(
stderr
,
FMT_POSIX
(
close
(
f
->
descriptor
()));
delete
f
,
FormatSystemErrorMessage
(
EBADF
,
"cannot close file"
)
+
"
\n
"
);
FormatSystemErrorMessage
(
EBADF
,
"cannot close file"
)
+
"
\n
"
);
#else
#else
close
(
f
->
descriptor
());
close
(
f
->
descriptor
());
...
@@ -404,10 +434,6 @@ TEST(FileTest, Close) {
...
@@ -404,10 +434,6 @@ TEST(FileTest, Close) {
TEST
(
FileTest
,
CloseError
)
{
TEST
(
FileTest
,
CloseError
)
{
File
f
(
".travis.yml"
,
File
::
RDONLY
);
File
f
(
".travis.yml"
,
File
::
RDONLY
);
#ifndef _WIN32
#ifndef _WIN32
// The close function must be called inside EXPECT_STDERR, otherwise
// the system may recycle closed file descriptor when redirecting the
// output in EXPECT_STDERR and the second close will break output
// redirection.
close
(
f
.
descriptor
());
close
(
f
.
descriptor
());
EXPECT_SYSTEM_ERROR
(
f
.
close
(),
EBADF
,
"cannot close file"
);
EXPECT_SYSTEM_ERROR
(
f
.
close
(),
EBADF
,
"cannot close file"
);
EXPECT_EQ
(
-
1
,
f
.
descriptor
());
EXPECT_EQ
(
-
1
,
f
.
descriptor
());
...
@@ -615,11 +641,11 @@ TEST(OutputRedirectTest, ErrorInDtor) {
...
@@ -615,11 +641,11 @@ TEST(OutputRedirectTest, ErrorInDtor) {
// Put a character in a file buffer.
// Put a character in a file buffer.
EXPECT_EQ
(
'x'
,
fputc
(
'x'
,
f
.
get
()));
EXPECT_EQ
(
'x'
,
fputc
(
'x'
,
f
.
get
()));
#ifndef _WIN32
#ifndef _WIN32
// The close function must be called inside EXPECT_
STDERR
, otherwise
// The close function must be called inside EXPECT_
WRITE
, otherwise
// the system may recycle closed file descriptor when redirecting the
// the system may recycle closed file descriptor when redirecting the
// output in EXPECT_STDERR and the second close will break output
// output in EXPECT_STDERR and the second close will break output
// redirection.
// redirection.
EXPECT_
STDERR
(
close
(
write_fd
);
delete
redir
,
EXPECT_
WRITE
(
stderr
,
close
(
write_fd
);
delete
redir
,
FormatSystemErrorMessage
(
EBADF
,
"cannot flush stream"
));
FormatSystemErrorMessage
(
EBADF
,
"cannot flush stream"
));
#else
#else
close
(
write_fd
);
close
(
write_fd
);
...
@@ -628,8 +654,6 @@ TEST(OutputRedirectTest, ErrorInDtor) {
...
@@ -628,8 +654,6 @@ TEST(OutputRedirectTest, ErrorInDtor) {
write_dup
.
dup2
(
write_fd
);
// "undo" close or dtor of BufferedFile will fail
write_dup
.
dup2
(
write_fd
);
// "undo" close or dtor of BufferedFile will fail
}
}
// TODO: test EXPECT_STDOUT and EXPECT_STDERR
// TODO: compile both in C++11 & C++98 mode
// TODO: compile both in C++11 & C++98 mode
#endif
#endif
...
...
test/gtest-extra.h
View file @
0aacd0cf
...
@@ -337,7 +337,7 @@ class OutputRedirect {
...
@@ -337,7 +337,7 @@ class OutputRedirect {
std
::
string
RestoreAndRead
();
std
::
string
RestoreAndRead
();
};
};
#define FMT_TEST_
PRINT
_(statement, expected_output, file, fail) \
#define FMT_TEST_
WRITE
_(statement, expected_output, file, fail) \
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \
if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \
std::string gtest_expected_output = expected_output; \
std::string gtest_expected_output = expected_output; \
...
@@ -355,13 +355,9 @@ class OutputRedirect {
...
@@ -355,13 +355,9 @@ class OutputRedirect {
GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
fail(gtest_ar.failure_message())
fail(gtest_ar.failure_message())
// Tests that the statement prints the expected output to stdout.
// Tests that the statement writes the expected output to file.
#define EXPECT_STDOUT(statement, expected_output) \
#define EXPECT_WRITE(file, statement, expected_output) \
FMT_TEST_PRINT_(statement, expected_output, stdout, GTEST_NONFATAL_FAILURE_)
FMT_TEST_WRITE_(statement, expected_output, file, GTEST_NONFATAL_FAILURE_)
// Tests that the statement prints the expected output to stderr.
#define EXPECT_STDERR(statement, expected_output) \
FMT_TEST_PRINT_(statement, expected_output, stderr, GTEST_NONFATAL_FAILURE_)
#endif // FMT_USE_FILE_DESCRIPTORS
#endif // FMT_USE_FILE_DESCRIPTORS
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment