aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-05-13 10:47:42 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-05-13 10:47:42 +0000
commit921c36728a09e2b3609ca68356ffcfb2c65c1b06 (patch)
tree859baff84ba5a39d70e028899e7eae1419b2a977
parent3fff5654a7df96f3cefc73461dd883129725af03 (diff)
downloadpaludis-921c36728a09e2b3609ca68356ffcfb2c65c1b06.tar.gz
paludis-921c36728a09e2b3609ca68356ffcfb2c65c1b06.tar.xz
More elegant output from DepSpecPrettyPrinter
-rw-r--r--paludis/dep_list/range_rewriter_TEST.cc2
-rw-r--r--paludis/dep_spec_pretty_printer.cc96
-rw-r--r--paludis/dep_spec_pretty_printer.hh24
-rw-r--r--paludis/dep_spec_pretty_printer_TEST.cc95
-rw-r--r--paludis/files.m42
-rw-r--r--paludis/portage_dep_parser_TEST.cc22
-rw-r--r--paludis/repositories/cran/cran_dep_parser_TEST.cc6
-rw-r--r--paludis/set_file_TEST.cc12
8 files changed, 207 insertions, 52 deletions
diff --git a/paludis/dep_list/range_rewriter_TEST.cc b/paludis/dep_list/range_rewriter_TEST.cc
index adb3cc8..650d085 100644
--- a/paludis/dep_list/range_rewriter_TEST.cc
+++ b/paludis/dep_list/range_rewriter_TEST.cc
@@ -46,7 +46,7 @@ namespace test_cases
DepSpecPrettyPrinter w(0, false);
r.spec()->accept(&w);
- TEST_CHECK_STRINGIFY_EQUAL(w, "a/b[=1|=2] ");
+ TEST_CHECK_STRINGIFY_EQUAL(w, "a/b[=1|=2]");
}
} test_range_rewriter;
}
diff --git a/paludis/dep_spec_pretty_printer.cc b/paludis/dep_spec_pretty_printer.cc
index d312759..7b663d4 100644
--- a/paludis/dep_spec_pretty_printer.cc
+++ b/paludis/dep_spec_pretty_printer.cc
@@ -30,74 +30,138 @@
using namespace paludis;
+namespace paludis
+{
+ template<>
+ struct Implementation<DepSpecPrettyPrinter>
+ {
+ std::stringstream s;
+ unsigned indent;
+ bool use_newlines;
+ bool outer_block;
+ mutable bool need_space;
+
+ Implementation(unsigned i, bool b) :
+ indent(i),
+ use_newlines(b),
+ outer_block(true),
+ need_space(false)
+ {
+ }
+ };
+}
+
+DepSpecPrettyPrinter::DepSpecPrettyPrinter(unsigned i, bool b) :
+ PrivateImplementationPattern<DepSpecPrettyPrinter>(new Implementation<DepSpecPrettyPrinter>(i, b))
+{
+}
+
+DepSpecPrettyPrinter::~DepSpecPrettyPrinter()
+{
+}
+
std::ostream &
paludis::operator<< (std::ostream & s, const DepSpecPrettyPrinter & p)
{
- s << p._s.str();
+ s << p._imp->s.str();
return s;
}
void
DepSpecPrettyPrinter::visit(const AllDepSpec * const a)
{
- _s << indent() << "(" << newline();
+ if (! _imp->outer_block)
{
- Save<unsigned> old_indent(&_indent, _indent + 4);
+ _imp->s << indent() << "(";
+ _imp->s << newline();
+ }
+
+ {
+ Save<unsigned> old_indent(&_imp->indent, _imp->outer_block ? _imp->indent : _imp->indent + 4);
std::for_each(a->begin(), a->end(), accept_visitor(this));
}
- _s << indent() << ")" << newline();
+
+ if (! _imp->outer_block)
+ {
+ _imp->s << indent() << ")";
+ _imp->s << newline();
+ }
}
void
DepSpecPrettyPrinter::visit(const AnyDepSpec * const a)
{
- _s << indent() << "|| (" << newline();
+ Save<bool> old_outer(&_imp->outer_block, false);
+
+ _imp->s << indent() << "|| (";
+ _imp->s << newline();
{
- Save<unsigned> old_indent(&_indent, _indent + 4);
+ Save<unsigned> old_indent(&_imp->indent, _imp->indent + 4);
std::for_each(a->begin(), a->end(), accept_visitor(this));
}
- _s << indent() << ")" << newline();
+ _imp->s << indent() << ")";
+ _imp->s << newline();
}
void
DepSpecPrettyPrinter::visit(const UseDepSpec * const a)
{
- _s << indent() << (a->inverse() ? "!" : "") <<
- a->flag() << "? (" << newline();
+ Save<bool> old_outer(&_imp->outer_block, false);
+
+ _imp->s << indent() << (a->inverse() ? "!" : "") << a->flag() << "? (";
+ _imp->s << newline();
{
- Save<unsigned> old_indent(&_indent, _indent + 4);
+ Save<unsigned> old_indent(&_imp->indent, _imp->indent + 4);
std::for_each(a->begin(), a->end(), accept_visitor(this));
}
- _s << indent() << ")" << newline();
+ _imp->s << indent() << ")";
+ _imp->s << newline();
}
void
DepSpecPrettyPrinter::visit(const PackageDepSpec * const p)
{
- _s << indent() << *p << newline();
+ _imp->s << indent() << *p;
+ _imp->s << newline();
}
void
DepSpecPrettyPrinter::visit(const PlainTextDepSpec * const p)
{
- _s << indent() << p->text() << newline();
+ _imp->s << indent() << p->text();
+ _imp->s << newline();
}
void
DepSpecPrettyPrinter::visit(const BlockDepSpec * const b)
{
- _s << indent() << "!" << *b->blocked_spec() << newline();
+ _imp->s << indent() << "!" << *b->blocked_spec();
+ _imp->s << newline();
}
std::string
DepSpecPrettyPrinter::newline() const
{
- return _use_newlines ? "\n" : " ";
+ if (_imp->use_newlines)
+ return "\n";
+ else
+ {
+ _imp->need_space = true;
+ return "";
+ }
}
std::string
DepSpecPrettyPrinter::indent() const
{
- return _use_newlines ? std::string(_indent, ' ') : "";
+ if (_imp->use_newlines)
+ return std::string(_imp->indent, ' ');
+ else if (_imp->need_space)
+ {
+ _imp->need_space = false;
+ return " ";
+ }
+ else
+ return "";
}
diff --git a/paludis/dep_spec_pretty_printer.hh b/paludis/dep_spec_pretty_printer.hh
index 8fde14d..86d32fe 100644
--- a/paludis/dep_spec_pretty_printer.hh
+++ b/paludis/dep_spec_pretty_printer.hh
@@ -37,28 +37,24 @@ namespace paludis
* \ingroup grpdepspecprettyprinter
*/
class PALUDIS_VISIBLE DepSpecPrettyPrinter :
- public DepSpecVisitorTypes::ConstVisitor
+ public DepSpecVisitorTypes::ConstVisitor,
+ private PrivateImplementationPattern<DepSpecPrettyPrinter>
{
friend std::ostream & operator<< (std::ostream &, const DepSpecPrettyPrinter &);
private:
- std::stringstream _s;
- unsigned _indent;
- bool _use_newlines;
-
std::string newline() const;
std::string indent() const;
public:
- /**
- * Constructor.
- */
- DepSpecPrettyPrinter(unsigned initial_indent,
- bool use_newlines = true) :
- _indent(initial_indent),
- _use_newlines(use_newlines)
- {
- }
+ ///\name Basic operations
+ ///\{
+
+ DepSpecPrettyPrinter(unsigned initial_indent, bool use_newlines = true);
+
+ ~DepSpecPrettyPrinter();
+
+ ///\}
/// \name Visit functions
///{
diff --git a/paludis/dep_spec_pretty_printer_TEST.cc b/paludis/dep_spec_pretty_printer_TEST.cc
new file mode 100644
index 0000000..61bbcec
--- /dev/null
+++ b/paludis/dep_spec_pretty_printer_TEST.cc
@@ -0,0 +1,95 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "dep_spec_pretty_printer.hh"
+#include "portage_dep_parser.hh"
+#include <test/test_runner.hh>
+#include <test/test_framework.hh>
+
+using namespace test;
+using namespace paludis;
+
+namespace test_cases
+{
+ struct PrettyPrinterNoIndentTest : TestCase
+ {
+ PrettyPrinterNoIndentTest() : TestCase("pretty printer no indent") { }
+
+ void run()
+ {
+ std::tr1::shared_ptr<const DepSpec> s1(PortageDepParser::parse("foo/bar bar/baz",
+ PortageDepParser::Policy::text_is_package_dep_spec(true, pds_pm_permissive)));
+ DepSpecPrettyPrinter p1(0, false);
+ s1->accept(&p1);
+ TEST_CHECK_STRINGIFY_EQUAL(p1, "foo/bar bar/baz");
+
+ std::tr1::shared_ptr<const DepSpec> s2(PortageDepParser::parse("foo/bar moo? ( bar/baz )",
+ PortageDepParser::Policy::text_is_package_dep_spec(true, pds_pm_permissive)));
+ DepSpecPrettyPrinter p2(0, false);
+ s2->accept(&p2);
+ TEST_CHECK_STRINGIFY_EQUAL(p2, "foo/bar moo? ( bar/baz )");
+
+ std::tr1::shared_ptr<const DepSpec> s3(PortageDepParser::parse("|| ( a/b ( c/d e/f ) )",
+ PortageDepParser::Policy::text_is_package_dep_spec(true, pds_pm_permissive)));
+ DepSpecPrettyPrinter p3(0, false);
+ s3->accept(&p3);
+ TEST_CHECK_STRINGIFY_EQUAL(p3, "|| ( a/b ( c/d e/f ) )");
+
+ std::tr1::shared_ptr<const DepSpec> s4(PortageDepParser::parse("( ( ( ) a ) b )",
+ PortageDepParser::Policy::text_is_text_dep_spec(true)));
+ DepSpecPrettyPrinter p4(0, false);
+ s4->accept(&p4);
+ TEST_CHECK_STRINGIFY_EQUAL(p4, "a b");
+ }
+ } test_pretty_printer_no_indent;
+
+ struct PrettyPrinterIndentTest : TestCase
+ {
+ PrettyPrinterIndentTest() : TestCase("pretty printer indent") { }
+
+ void run()
+ {
+ std::tr1::shared_ptr<const DepSpec> s1(PortageDepParser::parse("foo/bar bar/baz",
+ PortageDepParser::Policy::text_is_package_dep_spec(true, pds_pm_permissive)));
+ DepSpecPrettyPrinter p1(4);
+ s1->accept(&p1);
+ TEST_CHECK_STRINGIFY_EQUAL(p1, " foo/bar\n bar/baz\n");
+
+ std::tr1::shared_ptr<const DepSpec> s2(PortageDepParser::parse("foo/bar moo? ( bar/baz )",
+ PortageDepParser::Policy::text_is_package_dep_spec(true, pds_pm_permissive)));
+ DepSpecPrettyPrinter p2(4);
+ s2->accept(&p2);
+ TEST_CHECK_STRINGIFY_EQUAL(p2, " foo/bar\n moo? (\n bar/baz\n )\n");
+
+ std::tr1::shared_ptr<const DepSpec> s3(PortageDepParser::parse("|| ( a/b ( c/d e/f ) )",
+ PortageDepParser::Policy::text_is_package_dep_spec(true, pds_pm_permissive)));
+ DepSpecPrettyPrinter p3(4);
+ s3->accept(&p3);
+ TEST_CHECK_STRINGIFY_EQUAL(p3, " || (\n a/b\n (\n c/d\n"
+ " e/f\n )\n )\n");
+
+ std::tr1::shared_ptr<const DepSpec> s4(PortageDepParser::parse("( ( ( ) a ) b )",
+ PortageDepParser::Policy::text_is_text_dep_spec(true)));
+ DepSpecPrettyPrinter p4(4);
+ s4->accept(&p4);
+ TEST_CHECK_STRINGIFY_EQUAL(p4, " a\n b\n");
+ }
+ } test_pretty_printer_indent;
+}
+
diff --git a/paludis/files.m4 b/paludis/files.m4
index a3b6bf8..1e690a9 100644
--- a/paludis/files.m4
+++ b/paludis/files.m4
@@ -13,7 +13,7 @@ add(`config_file', `hh', `cc', `se', `test', `testscript')
add(`contents', `hh', `cc')
add(`dep_spec', `hh', `cc', `se', `test')
add(`dep_spec_flattener', `hh', `cc')
-add(`dep_spec_pretty_printer', `hh', `cc')
+add(`dep_spec_pretty_printer', `hh', `cc', `test')
add(`dep_tag', `hh', `cc', `sr')
add(`environment', `hh', `cc', `se')
add(`environment_implementation', `hh', `cc', `test')
diff --git a/paludis/portage_dep_parser_TEST.cc b/paludis/portage_dep_parser_TEST.cc
index 5469fbe..248305f 100644
--- a/paludis/portage_dep_parser_TEST.cc
+++ b/paludis/portage_dep_parser_TEST.cc
@@ -44,7 +44,7 @@ namespace test_cases
{
DepSpecPrettyPrinter d(0, false);
PortageDepParser::parse_depend("", pds_pm_permissive)->accept(&d);
- TEST_CHECK_EQUAL(stringify(d), "( ) ");
+ TEST_CHECK_EQUAL(stringify(d), "");
}
} test_dep_spec_parser_empty;
@@ -60,7 +60,7 @@ namespace test_cases
{
DepSpecPrettyPrinter d(0, false);
PortageDepParser::parse_depend(" \n \t", pds_pm_permissive)->accept(&d);
- TEST_CHECK_EQUAL(stringify(d), "( ) ");
+ TEST_CHECK_EQUAL(stringify(d), "");
}
} test_dep_spec_parser_blank;
@@ -76,7 +76,7 @@ namespace test_cases
{
DepSpecPrettyPrinter d(0, false);
PortageDepParser::parse_depend("app-editors/vim", pds_pm_permissive)->accept(&d);
- TEST_CHECK_EQUAL(stringify(d), "( app-editors/vim ) ");
+ TEST_CHECK_EQUAL(stringify(d), "app-editors/vim");
}
} test_dep_spec_parser_package;
@@ -92,15 +92,15 @@ namespace test_cases
{
DepSpecPrettyPrinter d1(0, false);
PortageDepParser::parse_depend(">=app-editors/vim-6.4_alpha", pds_pm_permissive)->accept(&d1);
- TEST_CHECK_EQUAL(stringify(d1), "( >=app-editors/vim-6.4_alpha ) ");
+ TEST_CHECK_EQUAL(stringify(d1), ">=app-editors/vim-6.4_alpha");
DepSpecPrettyPrinter d2(0, false);
PortageDepParser::parse_depend("=app-editors/vim-6.4_alpha-r1", pds_pm_permissive)->accept(&d2);
- TEST_CHECK_EQUAL(stringify(d2), "( =app-editors/vim-6.4_alpha-r1 ) ");
+ TEST_CHECK_EQUAL(stringify(d2), "=app-editors/vim-6.4_alpha-r1");
DepSpecPrettyPrinter d3(0, false);
PortageDepParser::parse_depend(">=app-editors/vim-6.4_alpha:one", pds_pm_permissive)->accept(&d3);
- TEST_CHECK_EQUAL(stringify(d3), "( >=app-editors/vim-6.4_alpha:one ) ");
+ TEST_CHECK_EQUAL(stringify(d3), ">=app-editors/vim-6.4_alpha:one");
}
} test_dep_spec_parser_decorated_package;
@@ -116,7 +116,7 @@ namespace test_cases
{
DepSpecPrettyPrinter d(0, false);
PortageDepParser::parse_depend("app-editors/vim app-misc/hilite \nsys-apps/findutils", pds_pm_permissive)->accept(&d);
- TEST_CHECK_EQUAL(stringify(d), "( app-editors/vim app-misc/hilite sys-apps/findutils ) ");
+ TEST_CHECK_EQUAL(stringify(d), "app-editors/vim app-misc/hilite sys-apps/findutils");
}
} test_dep_spec_parser_packages;
@@ -132,7 +132,7 @@ namespace test_cases
{
DepSpecPrettyPrinter d(0, false);
PortageDepParser::parse_depend("|| ( one/one two/two )", pds_pm_permissive)->accept(&d);
- TEST_CHECK_EQUAL(stringify(d), "( || ( one/one two/two ) ) ");
+ TEST_CHECK_EQUAL(stringify(d), "|| ( one/one two/two )");
}
} test_dep_spec_parser_any;
@@ -148,7 +148,7 @@ namespace test_cases
{
DepSpecPrettyPrinter d(0, false);
PortageDepParser::parse_depend(" ( one/one two/two ) ", pds_pm_permissive)->accept(&d);
- TEST_CHECK_EQUAL(stringify(d), "( ( one/one two/two ) ) ");
+ TEST_CHECK_EQUAL(stringify(d), "one/one two/two");
}
} test_dep_spec_parser_all;
@@ -164,7 +164,7 @@ namespace test_cases
{
DepSpecPrettyPrinter d(0, false);
PortageDepParser::parse_depend("foo? ( one/one )", pds_pm_permissive)->accept(&d);
- TEST_CHECK_EQUAL(stringify(d), "( foo? ( one/one ) ) ");
+ TEST_CHECK_EQUAL(stringify(d), "foo? ( one/one )");
}
} test_dep_spec_parser_use;
@@ -180,7 +180,7 @@ namespace test_cases
{
DepSpecPrettyPrinter d(0, false);
PortageDepParser::parse_depend("!foo? ( one/one )", pds_pm_permissive)->accept(&d);
- TEST_CHECK_EQUAL(stringify(d), "( !foo? ( one/one ) ) ");
+ TEST_CHECK_EQUAL(stringify(d), "!foo? ( one/one )");
}
} test_dep_spec_parser_inv_use;
diff --git a/paludis/repositories/cran/cran_dep_parser_TEST.cc b/paludis/repositories/cran/cran_dep_parser_TEST.cc
index 524086a..7354276 100644
--- a/paludis/repositories/cran/cran_dep_parser_TEST.cc
+++ b/paludis/repositories/cran/cran_dep_parser_TEST.cc
@@ -50,15 +50,15 @@ namespace test_cases
// test R dependency
std::string dep1("R (>= 2.0.0)");
CRANDepParser::parse(dep1, pds_pm_permissive)->accept(&d1);
- TEST_CHECK_EQUAL(stringify(d1), "( >=dev-lang/R-2.0.0 ) ");
+ TEST_CHECK_EQUAL(stringify(d1), ">=dev-lang/R-2.0.0");
// test varying whitespaces
std::string dep2("testpackage1 \t(<1.9)");
CRANDepParser::parse(dep2, pds_pm_permissive)->accept(&d2);
- TEST_CHECK_EQUAL(stringify(d2), "( <cran/testpackage1-1.9 ) ");
+ TEST_CHECK_EQUAL(stringify(d2), "<cran/testpackage1-1.9");
// test for package-name and version normalisation
std::string dep3("R.matlab (>= 2.3-1)");
CRANDepParser::parse(dep3, pds_pm_permissive)->accept(&d3);
- TEST_CHECK_EQUAL(stringify(d3), "( >=cran/R-matlab-2.3.1 ) ");
+ TEST_CHECK_EQUAL(stringify(d3), ">=cran/R-matlab-2.3.1");
}
} test_cran_dep_parser;
}
diff --git a/paludis/set_file_TEST.cc b/paludis/set_file_TEST.cc
index 3519308..2e76962 100644
--- a/paludis/set_file_TEST.cc
+++ b/paludis/set_file_TEST.cc
@@ -45,7 +45,7 @@ namespace test_cases
{
DepSpecPrettyPrinter p(0, false);
f.contents()->accept(&p);
- TEST_CHECK_STRINGIFY_EQUAL(p, "( foo/bar >=bar/baz-1.23 ) ");
+ TEST_CHECK_STRINGIFY_EQUAL(p, "foo/bar >=bar/baz-1.23");
}
f.add("foo/bar");
@@ -53,7 +53,7 @@ namespace test_cases
{
DepSpecPrettyPrinter p(0, false);
f.contents()->accept(&p);
- TEST_CHECK_STRINGIFY_EQUAL(p, "( foo/bar >=bar/baz-1.23 moo/oink ) ");
+ TEST_CHECK_STRINGIFY_EQUAL(p, "foo/bar >=bar/baz-1.23 moo/oink");
}
f.rewrite();
@@ -71,7 +71,7 @@ namespace test_cases
{
DepSpecPrettyPrinter p(0, false);
f.contents()->accept(&p);
- TEST_CHECK_STRINGIFY_EQUAL(p, "( foo/bar moo/oink ) ");
+ TEST_CHECK_STRINGIFY_EQUAL(p, "foo/bar moo/oink");
}
f.rewrite();
@@ -106,7 +106,7 @@ namespace test_cases
{
DepSpecPrettyPrinter p(0, false);
f.contents()->accept(&p);
- TEST_CHECK_STRINGIFY_EQUAL(p, "( >=bar/baz-1.23 ) ");
+ TEST_CHECK_STRINGIFY_EQUAL(p, ">=bar/baz-1.23");
}
f.add("foo/bar");
@@ -114,7 +114,7 @@ namespace test_cases
{
DepSpecPrettyPrinter p(0, false);
f.contents()->accept(&p);
- TEST_CHECK_STRINGIFY_EQUAL(p, "( >=bar/baz-1.23 moo/oink ) ");
+ TEST_CHECK_STRINGIFY_EQUAL(p, ">=bar/baz-1.23 moo/oink");
}
f.rewrite();
@@ -132,7 +132,7 @@ namespace test_cases
{
DepSpecPrettyPrinter p(0, false);
f.contents()->accept(&p);
- TEST_CHECK_STRINGIFY_EQUAL(p, "( moo/oink ) ");
+ TEST_CHECK_STRINGIFY_EQUAL(p, "moo/oink");
}
f.rewrite();