aboutsummaryrefslogtreecommitdiff
path: root/tests/manifest-serializer/driver.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2015-06-11 16:53:03 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2015-06-11 16:53:03 +0200
commitf4660720e3ab0dc70d31fd39d48199590810ab03 (patch)
treeb8396646af57e03855aa93d798920d987fa3b0b7 /tests/manifest-serializer/driver.cxx
parent2ffe5ac2998b90c004de4f13e199c7ee965c3f75 (diff)
Implement manifest serializer
Diffstat (limited to 'tests/manifest-serializer/driver.cxx')
-rw-r--r--tests/manifest-serializer/driver.cxx245
1 files changed, 245 insertions, 0 deletions
diff --git a/tests/manifest-serializer/driver.cxx b/tests/manifest-serializer/driver.cxx
new file mode 100644
index 0000000..21bca08
--- /dev/null
+++ b/tests/manifest-serializer/driver.cxx
@@ -0,0 +1,245 @@
+// file : tests/manifest-serializer/driver.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <vector>
+#include <string>
+#include <utility> // pair
+#include <cassert>
+#include <sstream>
+#include <iostream>
+
+#include <bpkg/manifest-serializer>
+
+using namespace std;
+using namespace bpkg;
+
+using pairs = vector<pair<string, string>>;
+
+static bool
+test (const pairs& manifest, const string& expected);
+
+static bool
+fail (const pairs& manifest);
+
+int
+main ()
+{
+ // Comments.
+ //
+ assert (test ({{"#", ""}}, "#\n"));
+ assert (test ({{"#", "x"}}, "# x\n"));
+ assert (test ({{"#", "x"},{"#", "y"},{"#", ""}}, "# x\n# y\n#\n"));
+ assert (fail ({{"",""},{"#", "x"}})); // serialization after eos
+
+ // Empty manifest stream.
+ //
+ assert (test ({}, ""));
+ assert (test ({{"",""}}, ""));
+
+ // Empty manifest.
+ //
+ assert (test ({{"","1"},{"",""},{"",""}}, ": 1\n"));
+ assert (test ({{"","1"},{"",""},{"","1"},{"",""},{"",""}}, ": 1\n:\n"));
+
+ // Invalid manifests.
+ //
+ assert (fail ({{"a",""}})); // format version pair expected
+ assert (fail ({{"","1"},{"",""},{"a",""}})); // format version pair expected
+ assert (fail ({{"","9"}})); // unsupported format version 9
+ assert (fail ({{"","1"},{"","x"}})); // non-empty value in end pair
+ assert (fail ({{"",""},{"","1"}})); // serialization after eos
+
+ // Single manifest.
+ //
+ assert (test ({{"","1"},{"a","x"},{"",""},{"",""}}, ": 1\na: x\n"));
+ assert (test ({{"","1"},{"a","x"},{"b","y"},{"",""},{"",""}},
+ ": 1\na: x\nb: y\n"));
+ assert (test ({{"","1"},{"#","c"},{"a","x"},{"",""},{"",""}},
+ ": 1\n# c\na: x\n"));
+
+ // Multiple manifests.
+ //
+ assert (test ({{"","1"},{"a","x"},{"",""},
+ {"","1"},{"b","y"},{"",""},{"",""}}, ": 1\na: x\n:\nb: y\n"));
+ assert (test ({{"","1"},{"a","x"},{"",""},
+ {"","1"},{"b","y"},{"",""},
+ {"","1"},{"c","z"},{"",""},{"",""}},
+ ": 1\na: x\n:\nb: y\n:\nc: z\n"));
+
+ // Invalid name.
+ //
+ assert (fail ({{"","1"},{"#a",""}}));
+ assert (fail ({{"","1"},{"a:b",""}}));
+ assert (fail ({{"","1"},{"a b",""}}));
+ assert (fail ({{"","1"},{"a\tb",""}}));
+ assert (fail ({{"","1"},{"a\n",""}}));
+
+ // Simple value.
+ //
+ assert (test ({{"","1"},{"a",""},{"",""},{"",""}}, ": 1\na:\n"));
+ assert (test ({{"","1"},{"a","x y z"},{"",""},{"",""}}, ": 1\na: x y z\n"));
+
+ // Long simple value (newline escaping).
+ //
+
+ // "Solid" text/hard break.
+ //
+ string l1 ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+ "Yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
+ "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
+ "Zzzzzzzzzzzzzzzz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yyyy"
+ "yyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
+ " Zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
+ "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
+
+ string e3 ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+ "xxxxxxxxxxxxxxxxxxx\\\n"
+ " Yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
+ "yyyyyyyyyyyyyyyyyyyyyyyyyyyyy\\\n"
+ " Zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
+ "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
+
+ // Space with a better one/soft break.
+ //
+ string l4 ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+ "xxxxxxxxx xxxxxxxxx"
+ " Yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
+ "yyyyyyyyyyyyyyyyyy yyyyyyyyyy"
+ " Zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
+ "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
+
+ string e4 ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+ "xxxxxxxxx xxxxxxxxx\\\n"
+ " Yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
+ "yyyyyyyyyyyyyyyyyy yyyyyyyyyy\\\n"
+ " Zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
+ "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
+
+ assert (test ({{"","1"},{"a",l1},{"",""},{"",""}}, ": 1\na: " + e1 + "\n"));
+ assert (test ({{"","1"},{"a",l2},{"",""},{"",""}}, ": 1\na: " + e2 + "\n"));
+ assert (test ({{"","1"},{"a",l3},{"",""},{"",""}}, ": 1\na: " + e3 + "\n"));
+ assert (test ({{"","1"},{"a",l4},{"",""},{"",""}}, ": 1\na: " + e4 + "\n"));
+
+
+ // Multi-line value.
+ //
+ string n ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
+ assert (test ({{"","1"},{n,"x"},{"",""},{"",""}},
+ ": 1\n" + n + ": \\\nx\n\\\n"));
+ assert (test ({{"","1"},{"a","\n"},{"",""},{"",""}},
+ ": 1\na: \\\n\n\n\\\n"));
+ assert (test ({{"","1"},{"a","\n\n"},{"",""},{"",""}},
+ ": 1\na: \\\n\n\n\n\\\n"));
+ assert (test ({{"","1"},{"a","\nx\n"},{"",""},{"",""}},
+ ": 1\na: \\\n\nx\n\n\\\n"));
+ assert (test ({{"","1"},{"a","x\ny\nz"},{"",""},{"",""}},
+ ": 1\na: \\\nx\ny\nz\n\\\n"));
+ assert (test ({{"","1"},{"a"," x"},{"",""},{"",""}},
+ ": 1\na: \\\n x\n\\\n"));
+ assert (test ({{"","1"},{"a","x "},{"",""},{"",""}},
+ ": 1\na: \\\nx \n\\\n"));
+ assert (test ({{"","1"},{"a"," x "},{"",""},{"",""}},
+ ": 1\na: \\\n x \n\\\n"));
+
+ // Extra three x's are for the leading name part ("a: ") that we
+ // don't have.
+ //
+ assert (test ({{"","1"},{"a","\nxxx" + l1},{"",""},{"",""}},
+ ": 1\na: \\\n\nxxx" + e1 + "\n\\\n"));
+ assert (test ({{"","1"},{"a","\nxxx" + l2},{"",""},{"",""}},
+ ": 1\na: \\\n\nxxx" + e2 + "\n\\\n"));
+ assert (test ({{"","1"},{"a","\nxxx" + l3},{"",""},{"",""}},
+ ": 1\na: \\\n\nxxx" + e3 + "\n\\\n"));
+ assert (test ({{"","1"},{"a","\nxxx" + l4},{"",""},{"",""}},
+ ": 1\na: \\\n\nxxx" + e4 + "\n\\\n"));
+
+ // Backslash escaping (simple and multi-line).
+ //
+ assert (test ({{"","1"},{"a","c:\\"},{"",""},{"",""}},
+ ": 1\na: c:\\\\\n"));
+ assert (test ({{"","1"},{"a","c:\\\nd:\\"},{"",""},{"",""}},
+ ": 1\na: \\\nc:\\\\\nd:\\\\\n\\\n"));
+}
+
+static string
+serialize (const pairs& m)
+{
+ ostringstream os;
+ os.exceptions (istream::failbit | istream::badbit);
+ manifest_serializer s (os, "");
+
+ for (const auto& p: m)
+ {
+ if (p.first != "#")
+ s.next (p.first, p.second);
+ else
+ s.comment (p.second);
+ }
+
+ return os.str ();
+}
+
+static bool
+test (const pairs& m, const string& e)
+{
+ string r (serialize (m));
+
+ if (r != e)
+ {
+ cerr << "actual:" << endl << "'" << r << "'"<< endl
+ << "expect:" << endl << "'" << e << "'"<< endl;
+
+ return false;
+ }
+
+ return true;
+}
+
+static bool
+fail (const pairs& m)
+{
+ try
+ {
+ string r (serialize (m));
+ cerr << "nofail: " << r << endl;
+ return false;
+ }
+ catch (const manifest_serialization& e)
+ {
+ //cerr << e.what () << endl;
+ }
+
+ return true;
+}