aboutsummaryrefslogtreecommitdiff
path: root/tests/unit
diff options
context:
space:
mode:
authoreldritch horrors <pennae@lix.systems>2024-03-21 15:54:41 +0100
committereldritch horrors <pennae@lix.systems>2024-06-23 11:52:49 +0000
commita9949f4760fea60ee790c27629e9620bf3ab5e3f (patch)
tree0e9f6d4160dd09a0d445e066ad6dcfed29800eb6 /tests/unit
parent39a1e248c9e7adea312c5b7e1743a8e55da63e6c (diff)
libutil: add some serialize.hh serializer tests
Change-Id: I0116265a18bc44bba16c07bf419af70d5195f07d
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/libutil/serialise.cc166
-rw-r--r--tests/unit/meson.build1
2 files changed, 167 insertions, 0 deletions
diff --git a/tests/unit/libutil/serialise.cc b/tests/unit/libutil/serialise.cc
new file mode 100644
index 000000000..95ae43115
--- /dev/null
+++ b/tests/unit/libutil/serialise.cc
@@ -0,0 +1,166 @@
+#include "serialise.hh"
+#include "error.hh"
+#include "fmt.hh"
+#include "pos-table.hh"
+#include "ref.hh"
+#include "types.hh"
+
+#include <limits.h>
+#include <gtest/gtest.h>
+
+#include <numeric>
+
+namespace nix {
+
+TEST(Sink, uint64_t)
+{
+ StringSink s;
+ s << 42;
+ ASSERT_EQ(s.s, std::string({42, 0, 0, 0, 0, 0, 0, 0}));
+}
+
+TEST(Sink, string_view)
+{
+ StringSink s;
+ s << "";
+ // clang-format off
+ ASSERT_EQ(
+ s.s,
+ std::string({
+ // length
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ // data (omitted)
+ })
+ );
+ // clang-format on
+
+ s = {};
+ s << "test";
+ // clang-format off
+ ASSERT_EQ(
+ s.s,
+ std::string({
+ // length
+ 4, 0, 0, 0, 0, 0, 0, 0,
+ // data
+ 't', 'e', 's', 't',
+ // padding
+ 0, 0, 0, 0,
+ })
+ );
+ // clang-format on
+
+ s = {};
+ s << "longer string";
+ // clang-format off
+ ASSERT_EQ(
+ s.s,
+ std::string({
+ // length
+ 13, 0, 0, 0, 0, 0, 0, 0,
+ // data
+ 'l', 'o', 'n', 'g', 'e', 'r', ' ', 's', 't', 'r', 'i', 'n', 'g',
+ // padding
+ 0, 0, 0,
+ })
+ );
+ // clang-format on
+}
+
+TEST(Sink, StringSet)
+{
+ StringSink s;
+ s << StringSet{};
+ // clang-format off
+ ASSERT_EQ(
+ s.s,
+ std::string({
+ // length
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ // data (omitted)
+ })
+ );
+ // clang-format on
+
+ s = {};
+ s << StringSet{"a", ""};
+ // clang-format off
+ ASSERT_EQ(
+ s.s,
+ std::string({
+ // length
+ 2, 0, 0, 0, 0, 0, 0, 0,
+ // data ""
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ // data "a"
+ 1, 0, 0, 0, 0, 0, 0, 0, 'a', 0, 0, 0, 0, 0, 0, 0,
+ })
+ );
+ // clang-format on
+}
+
+TEST(Sink, Strings)
+{
+ StringSink s;
+ s << Strings{};
+ // clang-format off
+ ASSERT_EQ(
+ s.s,
+ std::string({
+ // length
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ // data (omitted)
+ })
+ );
+ // clang-format on
+
+ s = {};
+ s << Strings{"a", ""};
+ // clang-format off
+ ASSERT_EQ(
+ s.s,
+ std::string({
+ // length
+ 2, 0, 0, 0, 0, 0, 0, 0,
+ // data "a"
+ 1, 0, 0, 0, 0, 0, 0, 0, 'a', 0, 0, 0, 0, 0, 0, 0,
+ // data ""
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ })
+ );
+ // clang-format on
+}
+
+TEST(Sink, Error)
+{
+ PosTable pt;
+ auto o = pt.addOrigin(Pos::String{make_ref<std::string>("test")}, 4);
+
+ StringSink s;
+ s << Error{ErrorInfo{
+ .level = lvlInfo,
+ .msg = HintFmt("foo"),
+ .pos = pt[pt.add(o, 1)],
+ .traces = {{.pos = pt[pt.add(o, 2)], .hint = HintFmt("b %1%", "foo")}},
+ }};
+ // NOTE position of the error and all traces are ignored
+ // by the wire format
+ // clang-format off
+ ASSERT_EQ(
+ s.s,
+ std::string({
+ 5, 0, 0, 0, 0, 0, 0, 0, 'E', 'r', 'r', 'o', 'r', 0, 0, 0,
+ 3, 0, 0, 0, 0, 0, 0, 0,
+ 5, 0, 0, 0, 0, 0, 0, 0, 'E', 'r', 'r', 'o', 'r', 0, 0, 0,
+ 3, 0, 0, 0, 0, 0, 0, 0, 'f', 'o', 'o', 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 1, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 16, 0, 0, 0, 0, 0, 0, 0,
+ 'b', ' ', '\x1b', '[', '3', '5', ';', '1', 'm', 'f', 'o', 'o', '\x1b', '[', '0', 'm',
+ })
+ );
+ // clang-format on
+}
+
+}
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 9f19e5fd9..2b5471526 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -49,6 +49,7 @@ libutil_tests_sources = files(
'libutil/paths-setting.cc',
'libutil/pool.cc',
'libutil/references.cc',
+ 'libutil/serialise.cc',
'libutil/suggestions.cc',
'libutil/tests.cc',
'libutil/url.cc',