summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/contract/test/detail/oteststream.hpp
blob: 5bf5a031c7f68b29d66c4700c42f342ae4d5c949 (plain)
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
#ifndef BOOST_CONTRACT_TEST_DETAIL_OTESTSTREAM_HPP_
#define BOOST_CONTRACT_TEST_DETAIL_OTESTSTREAM_HPP_

// Copyright (C) 2008-2018 Lorenzo Caminiti
// Distributed under the Boost Software License, Version 1.0 (see accompanying
// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html

#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>

namespace boost { namespace contract { namespace test { namespace detail {

namespace oteststream_ {
    struct oss_base { // Wrap oss data member for proper initialization order.
    protected:
        std::ostringstream oss_;
    };
}

// Print to clog plus build internal string (using ostringstream) for checking.
struct oteststream :
    private oteststream_::oss_base,
    public boost::iostreams::stream<boost::iostreams::tee_device<std::ostream,
            std::ostringstream> >
{
    oteststream() :
        oteststream_::oss_base(),
        boost::iostreams::stream<boost::iostreams::tee_device<
                std::ostream, std::ostringstream> >(
            boost::iostreams::tee_device<std::ostream, std::ostringstream>(
                    std::clog, oss_)
        )
    {}

    std::string str() const { return oss_.str(); }
    void str(std::string const& s) { oss_.str(s); }

    bool eq(std::string const& s) { return eq(str(), s); }
    
    // Also display mismatching characters.
    static bool eq(std::string const& r, std::string const& s) {
        std::string::size_type i = 0;
        for(; i < r.size() && i < s.size(); ++i) if(r[i] != s[i]) break;
        if(i < r.size() || i < s.size()) {
            std::cout << std::endl;
            std::cout <<
                "Error: Following strings differ at position " << i <<
                ", because '" << r[i] << "' != '" << s[i] << "':" << std::endl
            ;
            std::cout << std::endl;
            std::cout
                << r.substr(0, i)
                << "(((" << r[i] << ")))"
                // Extra () to avoid clashes with MSVC min macro.
                << r.substr((std::min)(i + 1, r.size()), r.size())
                << std::endl
            ;
            std::cout << std::endl;
            std::cout
                << s.substr(0, i)
                << "(((" << s[i] << ")))"
                // Extra () to avoid clashes with MSVC min macro.
                << s.substr((std::min)(i + 1, s.size()), s.size())
                << std::endl
            ;
            std::cout << std::endl;
            return false;
        }
        return true;
    }
};

} } } } // namespace

#endif // #include guard