File: form_auto_newline.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (94 lines) | stat: -rw-r--r-- 3,196 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 *             Copyright Andrey Semashev 2019.
 * Distributed under the Boost Software License, Version 1.0.
 *    (See accompanying file LICENSE_1_0.txt or copy at
 *          https://www.boost.org/LICENSE_1_0.txt)
 */
/*!
 * \file   form_auto_newline.cpp
 * \author Andrey Semashev
 * \date   23.06.2019
 *
 * \brief  This header contains tests for the auto_newline formatter.
 */

#define BOOST_TEST_MODULE form_auto_newline

#include <string>
#include <boost/test/unit_test.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/formatting_ostream.hpp>
#include "char_definitions.hpp"
#include "make_record.hpp"

namespace logging = boost::log;
namespace expr = logging::expressions;

// Test appending a newline to a non-empty string
BOOST_AUTO_TEST_CASE_TEMPLATE(append_to_non_empty_string, CharT, char_types)
{
    typedef CharT char_type;
    typedef std::basic_ostringstream< char_type > ostream_type;
    typedef logging::basic_formatting_ostream< char_type > formatting_ostream_type;
    typedef typename formatting_ostream_type::string_type string_type;
    typedef logging::record_view record_view;
    typedef logging::basic_formatter< char_type > formatter;

    record_view rec = make_record_view();
    string_type str_fmt;
    formatting_ostream_type strm_fmt(str_fmt);

    formatter f = expr::stream << "Hello" << expr::auto_newline;
    f(rec, strm_fmt);

    ostream_type strm_correct;
    strm_correct << "Hello\n";

    BOOST_CHECK(equal_strings(strm_fmt.str(), strm_correct.str()));
}

// Test appending a newline to an empty string
BOOST_AUTO_TEST_CASE_TEMPLATE(append_to_empty_string, CharT, char_types)
{
    typedef CharT char_type;
    typedef std::basic_ostringstream< char_type > ostream_type;
    typedef logging::basic_formatting_ostream< char_type > formatting_ostream_type;
    typedef typename formatting_ostream_type::string_type string_type;
    typedef logging::record_view record_view;
    typedef logging::basic_formatter< char_type > formatter;

    record_view rec = make_record_view();
    string_type str_fmt;
    formatting_ostream_type strm_fmt(str_fmt);

    formatter f = expr::stream << expr::auto_newline;
    f(rec, strm_fmt);

    ostream_type strm_correct;
    strm_correct << "\n";

    BOOST_CHECK(equal_strings(strm_fmt.str(), strm_correct.str()));
}

// Test not appending a newline to a non-empty string which already ends with a newline
BOOST_AUTO_TEST_CASE_TEMPLATE(not_append_if_ends_with_a_newline, CharT, char_types)
{
    typedef CharT char_type;
    typedef std::basic_ostringstream< char_type > ostream_type;
    typedef logging::basic_formatting_ostream< char_type > formatting_ostream_type;
    typedef typename formatting_ostream_type::string_type string_type;
    typedef logging::record_view record_view;
    typedef logging::basic_formatter< char_type > formatter;

    record_view rec = make_record_view();
    string_type str_fmt;
    formatting_ostream_type strm_fmt(str_fmt);

    formatter f = expr::stream << "Hello\n" << expr::auto_newline;
    f(rec, strm_fmt);

    ostream_type strm_correct;
    strm_correct << "Hello\n";

    BOOST_CHECK(equal_strings(strm_fmt.str(), strm_correct.str()));
}