about summary refs log tree commit diff
path: root/third_party/nix/src/tests/language-tests.cc
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-07-19T14·57+0100
committertazjin <mail@tazj.in>2020-07-19T16·33+0000
commit61a7b97c3927f28ef588196bc2efbd8c6cceb8c4 (patch)
treee1d5e5c273f1c0fcfce9df4044c346abf78ba59e /third_party/nix/src/tests/language-tests.cc
parent97e649922542ef66c9d80cb1b41817ed07c2c5a6 (diff)
test(3p/nix): Enable output comparison for evaluator success tests r/1398
Enables loading of the expected output of evaluator tests from the
corresponding .exp files, and checks that the output matches.

This again leaves some tests behind in the disabled folder, but we now
have almost the entire suite up and running so I can get around to
cleaning up the disabled ones.

Other note: Some tests had XML output, despite not being related to
XML testing at all - I'm not sure why they chose to do this, but have
converted those test outputs to normal Nix instead.

We have a separate test suite for JSON & XML serialisation already,
which was contributed by andi-.

Change-Id: Id7c42c836edfec4c22db9d893e35489f3e6dd559
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1285
Tested-by: BuildkiteCI
Reviewed-by: isomer <isomer@tvl.fyi>
Reviewed-by: glittershark <grfn@gws.fyi>
Diffstat (limited to '')
-rw-r--r--third_party/nix/src/tests/language-tests.cc33
1 files changed, 30 insertions, 3 deletions
diff --git a/third_party/nix/src/tests/language-tests.cc b/third_party/nix/src/tests/language-tests.cc
index daa2fa233e..cac5aee584 100644
--- a/third_party/nix/src/tests/language-tests.cc
+++ b/third_party/nix/src/tests/language-tests.cc
@@ -23,9 +23,12 @@
 
 #include <algorithm>
 #include <filesystem>
+#include <fstream>
 #include <iostream>
+#include <iterator>
 #include <memory>
 #include <optional>
+#include <sstream>
 #include <string>
 
 #include <absl/strings/ascii.h>
@@ -86,6 +89,20 @@ std::string TestNameFor(
   return name;
 }
 
+// Load the expected output of a given test as a string.
+std::string ExpectedOutputFor(absl::string_view stem) {
+  std::filesystem::path path(
+      absl::StrCat(NIX_SRC_DIR, "/src/tests/lang/", stem, ".exp"));
+
+  EXPECT_TRUE(std::filesystem::exists(path))
+      << stem << ": expected output file should exist";
+
+  std::ifstream input(path);
+  std::stringstream buffer;
+  buffer << input.rdbuf();
+  return std::string(absl::StripTrailingAsciiWhitespace(buffer.str()));
+}
+
 }  // namespace
 
 using nix::tests::DummyStore;
@@ -189,7 +206,7 @@ INSTANTIATE_TEST_SUITE_P(Eval, EvalFailureTest,
 
 class EvalSuccessTest : public testing::TestWithParam<std::filesystem::path> {};
 
-// Test pattern for files that should fail to evaluate.
+// Test pattern for files that should evaluate successfully.
 TEST_P(EvalSuccessTest, Fails) {
   std::shared_ptr<Store> store = std::make_shared<DummyStore>();
   EvalState state({}, ref<Store>(store));
@@ -200,8 +217,18 @@ TEST_P(EvalSuccessTest, Fails) {
       << path.stem().string() << ": should parse successfully";
 
   Value result;
-  state.eval(expr, result);
-  state.forceValue(result);
+
+  EXPECT_NO_THROW({
+    state.eval(expr, result);
+    state.forceValueDeep(result);
+  }) << path.stem().string()
+     << ": should evaluate successfully";
+
+  auto expected = ExpectedOutputFor(path.stem().string());
+  std::ostringstream value_str;
+  value_str << result;
+
+  EXPECT_EQ(expected, value_str.str()) << "evaluator output should match";
 }
 
 INSTANTIATE_TEST_SUITE_P(Eval, EvalSuccessTest,