aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/printable/printabletest.cpp
blob: 5b6c0058bf0f1b1caa002c8bde5064f3397c2497 (plain) (blame)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/util/printable.h>
#include <vespa/vespalib/stllike/asciistream.h>

struct Foo : public vespalib::Printable {
    int val;
    std::string other;

    Foo(int v, std::string o) : val(v), other(o) {}

    void print(std::ostream& out, bool verbose = false,
               const std::string& indent = "") const override
    {
        out << "Foo(val = " << val;
        if (verbose) {
            out << ", other:\n" << indent << "  " << other;
        } else {
            out << ", other size " << other.size();
        }
        out << ")";
    }
};

struct Bar : public Foo {
    int i;

    Bar(int j, int v, std::string o) : Foo(v, o), i(j) {}

    void print(std::ostream& out, bool verbose = false,
               const std::string& indent = "") const override
    {
        out << "Bar(" << i << ")";
        if (verbose) {
            out << " : ";
            Foo::print(out, verbose, indent + "  ");
        }
    }
};

struct AsciiFoo : public vespalib::AsciiPrintable {
    int val;

    AsciiFoo(int v) : val(v) {}

    void print(vespalib::asciistream& out,
               const PrintProperties& p) const override
    {
        if (p.verbose()) {
            out << "AsciiFoo(" << val << ")";
        } else {
            out << val;
        }
    }
};

struct AsciiBar : public vespalib::AsciiPrintable {
    AsciiFoo _foo;

    AsciiBar(int v) : _foo(v) {}

    void print(vespalib::asciistream& out,
               const PrintProperties& p) const override
    {
        if (p.verbose()) {
            out << "AsciiBar() {"
                << "\n" << p.indent(1);
            _foo.print(out, p.indentedCopy());
            out << "\n" << p.indent() << "}";
        } else {
            out << _foo;
        }
    }
};

TEST(PrintableTest, test_simple)
{
    Foo foo(3, "myval");
    Bar bar(7, 3, "otherval");

    EXPECT_EQ("Foo(val = 3, other size 5)", foo.toString());
    EXPECT_EQ("Foo(val = 3, other size 5)", foo.toString(false, "  "));
    EXPECT_EQ("Foo(val = 3, other:\n"
              "  myval)", foo.toString(true));
    EXPECT_EQ("Foo(val = 3, other:\n"
              "    myval)", foo.toString(true, "  "));

    std::ostringstream ost;
    ost << foo;
    EXPECT_EQ("Foo(val = 3, other size 5)", ost.str());

    EXPECT_EQ("Bar(7)", bar.toString());
    EXPECT_EQ("Bar(7)", bar.toString(false, "  "));
    EXPECT_EQ("Bar(7) : Foo(val = 3, other:\n"
              "    otherval)", bar.toString(true));
    EXPECT_EQ("Bar(7) : Foo(val = 3, other:\n"
              "      otherval)", bar.toString(true, "  "));
}

TEST(PrintableTest, test_ascii_variant)
{
    AsciiFoo foo(19);

    EXPECT_EQ("19", foo.toString());
    EXPECT_EQ("AsciiFoo(19)",
              foo.toString(vespalib::AsciiPrintable::VERBOSE));
    {
        vespalib::asciistream as;
        as << foo;
        EXPECT_EQ("19", as.str());

        std::ostringstream ost;
        ost << foo;
        EXPECT_EQ("19", ost.str());
    }

    AsciiBar bar(3);
    EXPECT_EQ("3", bar.toString());
    EXPECT_EQ("AsciiBar() {\n"
              "  AsciiFoo(3)\n"
              "}", bar.toString(vespalib::AsciiPrintable::VERBOSE));
    {
        vespalib::asciistream as;
        as << bar;
        EXPECT_EQ("3", as.str());

        std::ostringstream ost;
        ost << bar;
        EXPECT_EQ("3", ost.str());
    }
}

GTEST_MAIN_RUN_ALL_TESTS()