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
063d0077
Commit
063d0077
authored
Apr 29, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Test FileSink. Simplify tests.
parent
48be5c22
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
26 deletions
+33
-26
format-test.cc
format-test.cc
+27
-23
format.h
format.h
+2
-2
test/CMakeLists.txt
test/CMakeLists.txt
+4
-1
No files found.
format-test.cc
View file @
063d0077
...
@@ -123,6 +123,8 @@ using fmt::pad;
...
@@ -123,6 +123,8 @@ using fmt::pad;
FORMAT_TEST_THROW_(statement, expected_exception, expected_message, \
FORMAT_TEST_THROW_(statement, expected_exception, expected_message, \
GTEST_NONFATAL_FAILURE_)
GTEST_NONFATAL_FAILURE_)
namespace
{
// Checks if writing value to BasicWriter<Char> produces the same result
// Checks if writing value to BasicWriter<Char> produces the same result
// as writing it to std::basic_ostringstream<Char>.
// as writing it to std::basic_ostringstream<Char>.
template
<
typename
Char
,
typename
T
>
template
<
typename
Char
,
typename
T
>
...
@@ -188,6 +190,14 @@ void SPrintf(char *buffer, const char *format, ...) {
...
@@ -188,6 +190,14 @@ void SPrintf(char *buffer, const char *format, ...) {
va_end
(
args
);
va_end
(
args
);
}
}
std
::
string
ReadFile
(
fmt
::
StringRef
filename
)
{
std
::
ifstream
out
(
filename
.
c_str
());
std
::
stringstream
content
;
content
<<
out
.
rdbuf
();
return
content
.
str
();
}
}
TEST
(
UtilTest
,
Increment
)
{
TEST
(
UtilTest
,
Increment
)
{
char
s
[
10
]
=
"123"
;
char
s
[
10
]
=
"123"
;
Increment
(
s
);
Increment
(
s
);
...
@@ -1543,6 +1553,14 @@ TEST(FormatterTest, OutputNotWrittenOnError) {
...
@@ -1543,6 +1553,14 @@ TEST(FormatterTest, OutputNotWrittenOnError) {
EXPECT_EQ
(
0
,
num_writes
);
EXPECT_EQ
(
0
,
num_writes
);
}
}
TEST
(
FormatterTest
,
FileSink
)
{
FILE
*
f
=
std
::
fopen
(
"out"
,
"w"
);
fmt
::
FileSink
fs
(
f
);
fs
(
Writer
()
<<
"test"
);
std
::
fclose
(
f
);
EXPECT_EQ
(
"test"
,
ReadFile
(
"out"
));
}
// The test doesn't compile on older compilers which follow C++03 and
// The test doesn't compile on older compilers which follow C++03 and
// require an accessible copy constructor when binding a temporary to
// require an accessible copy constructor when binding a temporary to
// a const reference.
// a const reference.
...
@@ -1668,35 +1686,21 @@ TEST(FormatIntTest, FormatDec) {
...
@@ -1668,35 +1686,21 @@ TEST(FormatIntTest, FormatDec) {
#ifdef FMT_USE_DUP
#ifdef FMT_USE_DUP
// RAII for file descriptors.
class
File
{
private:
int
fd_
;
FMT_DISALLOW_COPY_AND_ASSIGN
(
File
);
public:
File
(
int
fd
)
:
fd_
(
fd
)
{}
~
File
()
{
close
(
fd_
);
}
int
fd
()
const
{
return
fd_
;
}
};
TEST
(
FormatTest
,
PrintColored
)
{
TEST
(
FormatTest
,
PrintColored
)
{
// Temporarily redirect stdout to a file and check if PrintColored adds
// Temporarily redirect stdout to a file and check if PrintColored adds
// necessary ANSI escape sequences.
// necessary ANSI escape sequences.
std
::
fflush
(
stdout
);
std
::
fflush
(
stdout
);
File
saved_stdio
(
dup
(
1
));
int
saved_stdio
=
dup
(
1
);
EXPECT_NE
(
-
1
,
saved_stdio
.
fd
());
EXPECT_NE
(
-
1
,
saved_stdio
);
{
int
out
=
open
(
"out"
,
O_WRONLY
|
O_CREAT
|
O_TRUNC
,
S_IRUSR
|
S_IWUSR
);
File
out
(
open
(
"out"
,
O_WRONLY
|
O_CREAT
|
O_TRUNC
,
S_IRUSR
|
S_IWUSR
));
EXPECT_NE
(
-
1
,
out
);
EXPECT_NE
(
-
1
,
out
.
fd
());
EXPECT_NE
(
-
1
,
dup2
(
out
,
1
));
EXPECT_NE
(
-
1
,
dup2
(
out
.
fd
(),
1
));
close
(
out
);
}
fmt
::
PrintColored
(
fmt
::
RED
,
"Hello, {}!
\n
"
)
<<
"world"
;
fmt
::
PrintColored
(
fmt
::
RED
,
"Hello, {}!
\n
"
)
<<
"world"
;
std
::
fflush
(
stdout
);
std
::
fflush
(
stdout
);
EXPECT_NE
(
-
1
,
dup2
(
saved_stdio
.
fd
(),
1
));
EXPECT_NE
(
-
1
,
dup2
(
saved_stdio
,
1
));
std
::
ifstream
out
(
"out"
);
close
(
saved_stdio
);
std
::
stringstream
content
;
EXPECT_EQ
(
"
\x1b
[31mHello, world!
\n\x1b
[0m"
,
ReadFile
(
"out"
));
content
<<
out
.
rdbuf
();
EXPECT_EQ
(
"
\x1b
[31mHello, world!
\n\x1b
[0m"
,
content
.
str
());
}
}
#endif
#endif
...
...
format.h
View file @
063d0077
...
@@ -1500,7 +1500,7 @@ class FileSink {
...
@@ -1500,7 +1500,7 @@ class FileSink {
std
::
FILE
*
file_
;
std
::
FILE
*
file_
;
public:
public:
FileSink
(
std
::
FILE
*
f
)
:
file_
(
f
)
{}
explicit
FileSink
(
std
::
FILE
*
f
)
:
file_
(
f
)
{}
/** Writes the output to a file. */
/** Writes the output to a file. */
void
operator
()(
const
BasicWriter
<
char
>
&
w
)
const
{
void
operator
()(
const
BasicWriter
<
char
>
&
w
)
const
{
...
@@ -1514,7 +1514,7 @@ class FileSink {
...
@@ -1514,7 +1514,7 @@ class FileSink {
// Print("Elapsed time: {0:.2f} seconds") << 1.23;
// Print("Elapsed time: {0:.2f} seconds") << 1.23;
// TODO: wchar overload
// TODO: wchar overload
inline
Formatter
<
FileSink
>
Print
(
StringRef
format
)
{
inline
Formatter
<
FileSink
>
Print
(
StringRef
format
)
{
Formatter
<
FileSink
>
f
(
format
,
stdout
);
Formatter
<
FileSink
>
f
(
format
,
FileSink
(
stdout
)
);
return
f
;
return
f
;
}
}
...
...
test/CMakeLists.txt
View file @
063d0077
...
@@ -39,4 +39,7 @@ expect_compile_error("fmt::Writer() << fmt::pad(\"abc\", 5, L' ');")
...
@@ -39,4 +39,7 @@ expect_compile_error("fmt::Writer() << fmt::pad(\"abc\", 5, L' ');")
expect_compile_error
(
"fmt::Writer() << fmt::pad(42, 5, L' ');"
)
expect_compile_error
(
"fmt::Writer() << fmt::pad(42, 5, L' ');"
)
# Formatting a wide character with a narrow format string is forbidden.
# Formatting a wide character with a narrow format string is forbidden.
expect_compile_error
(
"Format(
\"
{}
\"
) << L'a';"
)
expect_compile_error
(
"fmt::Format(
\"
{}
\"
) << L'a';"
)
# There is no implicit conversion from FILE* to FileSink.
expect_compile_error
(
"fmt::FileSink fs = 0;"
)
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