about summary refs log tree commit diff
path: root/third_party
diff options
context:
space:
mode:
authorAndreas Rammhold <andreas@rammhold.de>2020-05-25T19·24+0200
committertazjin <mail@tazj.in>2020-07-18T00·00+0000
commite4b7d6d5925208214927a9e8c8fd72c246bc70ff (patch)
tree375be43394618b61a8dd7889f967df9c0796772a /third_party
parent0b01c409516b5efb81ba97d927066fdc3cdf995a (diff)
test(3p/nix/libexpr): add json/xml tests r/1368
Backported from:
https://github.com/andir/nix/commit/c1371b6fbb91caf53ebc94d3ae0250dbe7eb7eae

Change-Id: Ida326cf1567081a56d747c4a72f036f55cfdea37
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1239
Tested-by: BuildkiteCI
Reviewed-by: Kane York <rikingcoding@gmail.com>
Diffstat (limited to 'third_party')
-rw-r--r--third_party/nix/src/tests/value-to-json.cc287
1 files changed, 287 insertions, 0 deletions
diff --git a/third_party/nix/src/tests/value-to-json.cc b/third_party/nix/src/tests/value-to-json.cc
new file mode 100644
index 000000000000..462d2fda0170
--- /dev/null
+++ b/third_party/nix/src/tests/value-to-json.cc
@@ -0,0 +1,287 @@
+#include "value-to-json.hh"
+
+#include <sstream>
+
+#include <gtest/gtest.h>
+
+#include "store-api.hh"
+#include "value-to-xml.hh"
+#include "value.hh"
+
+class ValueTest : public ::testing::Test {
+ protected:
+  static void SetUpTestCase() { nix::initGC(); }
+
+  static void TearDownTestCase() {}
+};
+
+class JSONValueTest : public ValueTest {};
+class XMLValueTest : public ValueTest {};
+
+namespace nix {
+
+class DummyStore : public Store {
+ public:
+  explicit DummyStore() : Store(Store::Params{}) {}
+
+  std::string getUri() { return ""; }
+  virtual void queryPathInfoUncached(const StorePath&) {}
+  virtual void queryPathInfoUncached(
+      const StorePath&,
+      nix::Callback<std::shared_ptr<const nix::ValidPathInfo>>) noexcept {}
+  std::optional<StorePath> queryPathFromHashPart(const std::string& hashPart) {
+    return {};
+  }
+  StorePath addToStore(const std::string&, const std::string&,
+                       const StorePathSet&, nix::RepairFlag) {
+    return StorePath::dummy.clone();
+  }
+  StorePath addToStore(const std::string&, const Path&, bool, nix::HashType,
+                       nix::PathFilter&, nix::RepairFlag) {
+    return StorePath::dummy.clone();
+  }
+  StorePath addTextToStore(const std::string&, const std::string&,
+                           const StorePathSet&, nix::RepairFlag) {
+    return StorePath::dummy.clone();
+  }
+
+  void narFromPath(const StorePath&, Sink&) {}
+  void ensurePath(const StorePath&) {}
+
+  BuildResult buildDerivation(const StorePath&, const BasicDerivation&,
+                              BuildMode) {
+    return BuildResult{};
+  }
+};
+
+TEST_F(JSONValueTest, null) {
+  std::stringstream ss;
+  Value v;
+  PathSet ps;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({}, ref<Store>(store), false);
+
+  mkNull(v);
+  printValueAsJSON(s, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), "null");
+}
+
+TEST_F(JSONValueTest, BoolFalse) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkBool(v, false);
+  printValueAsJSON(s, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), "false");
+}
+
+TEST_F(JSONValueTest, BoolTrue) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkBool(v, true);
+  printValueAsJSON(s, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), "true");
+}
+
+TEST_F(JSONValueTest, IntPositive) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkInt(v, 100);
+  printValueAsJSON(s, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), "100");
+}
+
+TEST_F(JSONValueTest, IntNegative) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkInt(v, -100);
+  printValueAsJSON(s, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), "-100");
+}
+
+TEST_F(JSONValueTest, String) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkString(v, "test");
+  printValueAsJSON(s, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), "\"test\"");
+}
+
+TEST_F(JSONValueTest, StringQuotes) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkString(v, "test\"");
+  printValueAsJSON(s, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), "\"test\\\"\"");
+}
+
+TEST_F(JSONValueTest, Path) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkPath(v, "test");
+  printValueAsJSON(s, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), "\"/nix/store/g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-x\"");
+}
+
+TEST_F(JSONValueTest, PathNoCopy) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkPathNoCopy(v, "test");
+  printValueAsJSON(s, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), "\"/nix/store/g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-x\"");
+}
+
+/*
+ * Value to XMl tests
+ */
+
+#define XML(v) \
+  ("<?xml version='1.0' encoding='utf-8'?>\n<expr>\n" v "\n</expr>\n")
+
+TEST_F(XMLValueTest, null) {
+  std::stringstream ss;
+  Value v;
+  PathSet ps;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({}, ref<Store>(store), false);
+
+  mkNull(v);
+  printValueAsXML(s, true, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), XML("  <null />"));
+}
+
+TEST_F(XMLValueTest, BoolFalse) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkBool(v, false);
+  printValueAsXML(s, true, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), XML("  <bool value=\"false\" />"));
+}
+
+TEST_F(XMLValueTest, BoolTrue) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkBool(v, true);
+  printValueAsXML(s, true, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), XML("  <bool value=\"true\" />"));
+}
+
+TEST_F(XMLValueTest, IntPositive) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkInt(v, 100);
+  printValueAsXML(s, true, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), XML("  <int value=\"100\" />"));
+}
+
+TEST_F(XMLValueTest, IntNegative) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkInt(v, -100);
+  printValueAsXML(s, true, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), XML("  <int value=\"-100\" />"));
+}
+
+TEST_F(XMLValueTest, String) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkString(v, "test-value");
+  printValueAsXML(s, true, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), XML("  <string value=\"test-value\" />"));
+}
+
+TEST_F(XMLValueTest, StringQuotes) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkString(v, "test-value\"");
+  printValueAsXML(s, true, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), XML("  <string value=\"test-value&quot;\" />"));
+}
+
+/*
+ * FIXME: This function returns the original input path while the JSON version
+ * of the same actually touches the store to create a /nix/store path.
+ */
+TEST_F(XMLValueTest, Path) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkPath(v, "some-path");
+  printValueAsXML(s, true, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), XML("  <path value=\"some-path\" />"));
+}
+
+/*
+ * FIXME: This function returns the original input path while the JSON version
+ * of the same actually touches the store to create a /nix/store path.
+ */
+TEST_F(XMLValueTest, PathNoCopy) {
+  std::stringstream ss;
+  auto store = std::make_shared<DummyStore>();
+  EvalState s({"."}, ref<Store>(store), false);
+  Value v;
+  PathSet ps;
+
+  mkPathNoCopy(v, "some-other-path");
+  printValueAsXML(s, true, true, v, ss, ps);
+  ASSERT_EQ(ss.str(), XML("  <path value=\"some-other-path\" />"));
+}
+}  // namespace nix