From e6d9439ba99a764bd9884557f0d75a79d4f81ff2 Mon Sep 17 00:00:00 2001 From: IlushaShurupov Date: Sat, 12 Aug 2023 08:08:47 +0300 Subject: [PATCH] Adding simple test fot cf grammar --- Parser/tests/Tests.cpp | 60 +++++++++++++++++++++++++++++++++++++++- Utils/public/Testing.hpp | 1 + 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/Parser/tests/Tests.cpp b/Parser/tests/Tests.cpp index 7679e67..892394f 100644 --- a/Parser/tests/Tests.cpp +++ b/Parser/tests/Tests.cpp @@ -1,4 +1,62 @@ -int main() { +#include "NewPlacement.hpp" +#include "Testing.hpp" +#include "Parser.hpp" +using namespace tp; + +TEST_DEF(Simple) { + + String source = R"( +Start Body +Body : [ Function ] | List | &; +List : [ LIST ]; +)"; + + auto state = CfGrammar::initializeCfGrammarParser(); + + CfGrammar grammar; + + if (!grammar.parse(state, source)) { + TEST(0 && "Parsing is failed\n"); + CfGrammar::deinitializeCfGrammarParser(state); + return; + } + + if (!grammar.compile()) { + TEST(0 && "Compilation is failed\n"); + CfGrammar::deinitializeCfGrammarParser(state); + return; + } + + CfGrammar::deinitializeCfGrammarParser(state); + + printf("Grammar accepted.\n"); + + List sentences; + grammar.generateSentences(sentences); + + TEST_ASSERT(sentences.length() == 3); + TEST_ASSERT(sentences.first()->data.terms.length() == 1); + TEST_ASSERT(sentences.last()->data.terms.length() == 1); + + TEST(sentences.first()->data.terms.first()->data.id == "Function"); + TEST(sentences.last()->data.terms.first()->data.id == "LIST"); + + printf("Example sentences formed from grammar: \n"); + for (auto sentence : sentences) tp::CfGrammar::printSentence(sentence.data()); +} + +int main(int argc, const char* argv[]) { + tp::ModuleManifest* deps[] = { &tp::gModuleParser, nullptr }; + tp::ModuleManifest testModule("CommandLineTest", nullptr, nullptr, deps); + + if (!testModule.initialize()) { + return 1; + } + + testSimple(); + + testModule.deinitialize(); + return 0; } \ No newline at end of file diff --git a/Utils/public/Testing.hpp b/Utils/public/Testing.hpp index c8c1c1c..25f76c2 100644 --- a/Utils/public/Testing.hpp +++ b/Utils/public/Testing.hpp @@ -61,4 +61,5 @@ namespace tp { void Name##FunctorBody() #define TEST(expr) if (!(expr)) tp::gTesting.addFailedCheck({ #expr, __FILE__, __LINE__ }) +#define TEST_ASSERT(expr) TEST(expr); if (!(expr)) return #define TEST_EQUAL(l, r) if (!((l) == (r))) tp::gTesting.addFailedCheck({ #l" == "#r, __FILE__, __LINE__ })