diff options
author | Jade Lovelace <lix@jade.fyi> | 2024-03-09 23:59:50 -0800 |
---|---|---|
committer | Jade Lovelace <lix@jade.fyi> | 2024-03-14 14:30:38 -0700 |
commit | 38571c50e6dc0ee910e9e7619e482fdbbfd644e1 (patch) | |
tree | 2f1635b17b71ef3aa0ed3c8dac7bc9ffd2165d07 /tests/functional | |
parent | 84727bebb42ded17c128567e34636c4cdc7eed45 (diff) |
Implement a parser for a literate testing system for the repl
This parser can be reused for other purposes. It's inspired by
https://bitheap.org/cram/
Although eelco's impostor exists https://github.com/mobusoperandi/eelco,
it is not very nice to depend on out of tree testing frameworks with no
way to customize them.
Change-Id: Ifca50177e09730182baf0ebf829c3505bbb0274a
Diffstat (limited to 'tests/functional')
-rw-r--r-- | tests/functional/local.mk | 4 | ||||
-rw-r--r-- | tests/functional/repl_characterization/.gitignore | 1 | ||||
-rw-r--r-- | tests/functional/repl_characterization/data/basic.ast | 11 | ||||
-rw-r--r-- | tests/functional/repl_characterization/data/basic.test | 11 | ||||
-rw-r--r-- | tests/functional/repl_characterization/local.mk | 15 | ||||
-rw-r--r-- | tests/functional/repl_characterization/repl_characterization.cc | 42 | ||||
-rw-r--r-- | tests/functional/test-repl-characterization.sh | 7 |
7 files changed, 90 insertions, 1 deletions
diff --git a/tests/functional/local.mk b/tests/functional/local.mk index f94f514c9..af2a55e58 100644 --- a/tests/functional/local.mk +++ b/tests/functional/local.mk @@ -131,7 +131,7 @@ ifeq ($(HAVE_LIBCPUID), 1) endif ifeq ($(ENABLE_BUILD), yes) - nix_tests += test-libstoreconsumer.sh + nix_tests += test-libstoreconsumer.sh test-repl-characterization.sh ifeq ($(BUILD_SHARED_LIBS), 1) nix_tests += plugins.sh @@ -142,6 +142,8 @@ $(d)/test-libstoreconsumer.sh.test $(d)/test-libstoreconsumer.sh.test-debug: \ $(buildprefix)$(d)/test-libstoreconsumer/test-libstoreconsumer $(d)/plugins.sh.test $(d)/plugins.sh.test-debug: \ $(buildprefix)$(d)/plugins/libplugintest.$(SO_EXT) +$(d)/test-repl-characterization.sh.test $(d)/test-repl-characterization.sh.test-debug: \ + $(buildprefix)$(d)/repl_characterization/test-repl-characterization install-tests += $(foreach x, $(nix_tests), $(d)/$(x)) diff --git a/tests/functional/repl_characterization/.gitignore b/tests/functional/repl_characterization/.gitignore new file mode 100644 index 000000000..4c6412c2f --- /dev/null +++ b/tests/functional/repl_characterization/.gitignore @@ -0,0 +1 @@ +test-repl-characterization diff --git a/tests/functional/repl_characterization/data/basic.ast b/tests/functional/repl_characterization/data/basic.ast new file mode 100644 index 000000000..d494b00aa --- /dev/null +++ b/tests/functional/repl_characterization/data/basic.ast @@ -0,0 +1,11 @@ +Commentary "meow meow meow" +Command "command" +Output "output output one" +Output "" +Output "" +Output "output output two" +Commentary "meow meow" +Command "command two" +Output "output output output" +Commentary "commentary" +Output "output output output" diff --git a/tests/functional/repl_characterization/data/basic.test b/tests/functional/repl_characterization/data/basic.test new file mode 100644 index 000000000..d6b8427b4 --- /dev/null +++ b/tests/functional/repl_characterization/data/basic.test @@ -0,0 +1,11 @@ +meow meow meow + nix-repl> command + output output one + + + output output two +meow meow + nix-repl> command two + output output output +commentary + output output output diff --git a/tests/functional/repl_characterization/local.mk b/tests/functional/repl_characterization/local.mk new file mode 100644 index 000000000..aa0cf3615 --- /dev/null +++ b/tests/functional/repl_characterization/local.mk @@ -0,0 +1,15 @@ +programs += test-repl-characterization + +test-repl-characterization_DIR := $(d) + +# do not install +test-repl-characterization_INSTALL_DIR := + +test-repl-characterization_SOURCES := \ + $(wildcard $(d)/*.cc) \ + +test-repl-characterization_CXXFLAGS += -I src/libutil -I tests/unit/libutil-support -DNIX_BIN_DIR="\"$(bindir)\"" + +test-repl-characterization_LIBS = libutil libutil-test-support + +test-repl-characterization_LDFLAGS = $(THREAD_LDFLAGS) $(SODIUM_LIBS) $(EDITLINE_LIBS) $(BOOST_LDFLAGS) $(LOWDOWN_LIBS) $(GTEST_LIBS) diff --git a/tests/functional/repl_characterization/repl_characterization.cc b/tests/functional/repl_characterization/repl_characterization.cc new file mode 100644 index 000000000..5b73e7a89 --- /dev/null +++ b/tests/functional/repl_characterization/repl_characterization.cc @@ -0,0 +1,42 @@ +#include <gtest/gtest.h> + +#include <string> +#include <string_view> +#include <optional> +#include <unistd.h> + +#include "tests/characterization.hh" +#include "tests/cli-literate-parser.hh" + +using namespace std::string_literals; + +namespace nix { + +static constexpr const char * REPL_PROMPT = "nix-repl> "; + +class ReplSessionTest : public CharacterizationTest +{ + Path unitTestData = getUnitTestData(); + +public: + Path goldenMaster(std::string_view testStem) const override + { + return unitTestData + "/" + testStem; + } +}; + +TEST_F(ReplSessionTest, parses) +{ + writeTest("basic.ast", [this]() { + const std::string content = readFile(goldenMaster("basic.test")); + auto parser = CLILiterateParser{REPL_PROMPT}; + parser.feed(content); + + std::ostringstream out{}; + for (auto & bit : parser.syntax()) { + out << bit.print() << "\n"; + } + return out.str(); + }); +} +}; diff --git a/tests/functional/test-repl-characterization.sh b/tests/functional/test-repl-characterization.sh new file mode 100644 index 000000000..78dcab5fc --- /dev/null +++ b/tests/functional/test-repl-characterization.sh @@ -0,0 +1,7 @@ +source common.sh + +# You might think that this can trivially be moved into a makefile, however, +# there is various environmental initialization of this shell framework that +# seems load bearing and seemingly prevents the tests working inside the +# builder. +_NIX_TEST_UNIT_DATA=$(pwd)/repl_characterization/data ./repl_characterization/test-repl-characterization |