diff options
Diffstat (limited to 'vespalib/src/tests/printable')
-rw-r--r-- | vespalib/src/tests/printable/.gitignore | 4 | ||||
-rw-r--r-- | vespalib/src/tests/printable/CMakeLists.txt | 8 | ||||
-rw-r--r-- | vespalib/src/tests/printable/DESC | 1 | ||||
-rw-r--r-- | vespalib/src/tests/printable/FILES | 1 | ||||
-rw-r--r-- | vespalib/src/tests/printable/printabletest.cpp | 153 |
5 files changed, 167 insertions, 0 deletions
diff --git a/vespalib/src/tests/printable/.gitignore b/vespalib/src/tests/printable/.gitignore new file mode 100644 index 00000000000..e001a281449 --- /dev/null +++ b/vespalib/src/tests/printable/.gitignore @@ -0,0 +1,4 @@ +.depend +Makefile +printabletest +vespalib_printabletest_app diff --git a/vespalib/src/tests/printable/CMakeLists.txt b/vespalib/src/tests/printable/CMakeLists.txt new file mode 100644 index 00000000000..3aedbe7c011 --- /dev/null +++ b/vespalib/src/tests/printable/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(vespalib_printabletest_app + SOURCES + printabletest.cpp + DEPENDS + vespalib +) +vespa_add_test(NAME vespalib_printabletest_app COMMAND vespalib_printabletest_app) diff --git a/vespalib/src/tests/printable/DESC b/vespalib/src/tests/printable/DESC new file mode 100644 index 00000000000..a31bafc527f --- /dev/null +++ b/vespalib/src/tests/printable/DESC @@ -0,0 +1 @@ +Unit test for the Printable class diff --git a/vespalib/src/tests/printable/FILES b/vespalib/src/tests/printable/FILES new file mode 100644 index 00000000000..a3f259414b5 --- /dev/null +++ b/vespalib/src/tests/printable/FILES @@ -0,0 +1 @@ +printabletest.cpp diff --git a/vespalib/src/tests/printable/printabletest.cpp b/vespalib/src/tests/printable/printabletest.cpp new file mode 100644 index 00000000000..638060ebf49 --- /dev/null +++ b/vespalib/src/tests/printable/printabletest.cpp @@ -0,0 +1,153 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/fastos/fastos.h> +#include <vespa/vespalib/util/printable.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) {} + + virtual void print(std::ostream& out, bool verbose = false, + const std::string& indent = "") const + { + 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) {} + + virtual void print(std::ostream& out, bool verbose = false, + const std::string& indent = "") const + { + out << "Bar(" << i << ")"; + if (verbose) { + out << " : "; + Foo::print(out, verbose, indent + " "); + } + } +}; + +struct AsciiFoo : public vespalib::AsciiPrintable { + int val; + + AsciiFoo(int v) : val(v) {} + + virtual void print(vespalib::asciistream& out, + const PrintProperties& p) const + { + if (p.verbose()) { + out << "AsciiFoo(" << val << ")"; + } else { + out << val; + } + } +}; + +struct AsciiBar : public vespalib::AsciiPrintable { + AsciiFoo _foo; + + AsciiBar(int v) : _foo(v) {} + + virtual void print(vespalib::asciistream& out, + const PrintProperties& p) const + { + 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(); +}; + +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) |