aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/printable/printabletest.cpp
blob: 03e3c777a252856282614d2d6b55ed09372dec6e (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/util/printable.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <vespa/vespalib/testkit/testapp.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;
        }
    }
};

class Test : public vespalib::TestApp
{
public:
    void testSimple();
    void testAsciiVariant();
    int Main() override;
};

void
Test::testSimple()
{
    Foo foo(3, "myval");
    Bar bar(7, 3, "otherval");

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

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

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

void
Test::testAsciiVariant()
{
    AsciiFoo foo(19);

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

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

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

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

int
Test::Main()
{
    TEST_INIT("printabletest");
    testSimple();
    testAsciiVariant();
    TEST_DONE();
}

TEST_APPHOOK(Test)