Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
#6031: Add test assuming that a parsed syntax tree can be converted b…
…ack to the exact source string it has been parsed from.
  • Loading branch information
codereader committed Aug 12, 2022
1 parent e530be6 commit e6c5342
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
33 changes: 29 additions & 4 deletions test/DefBlockSyntaxParser.cpp
@@ -1,6 +1,7 @@
#include "gtest/gtest.h"
#include "RadiantTest.h"

#include "parser/DefBlockSyntaxParser.h"
#include "algorithm/FileUtils.h"

namespace test
{
Expand Down Expand Up @@ -117,16 +118,29 @@ TEST(DefBlockSyntaxTokeniser, TokenSequences)
{ parser::DefSyntaxToken::Type::BracedBlock, "{\n \"some to//kens {{\" \"containing /* control characters */\" // test\n}" },
{ parser::DefSyntaxToken::Type::Whitespace, "\r\n\r\n" },
});

expectTokenSequence("test\n{\n\"some\" \"keyvalue\" // line comment\n\n}\r\n\r\ntest2\n{\n\"some\" \"keyvalue\"\n\n}\n",
{
{ parser::DefSyntaxToken::Type::Token, "test" },
{ parser::DefSyntaxToken::Type::Whitespace, "\n" },
{ parser::DefSyntaxToken::Type::BracedBlock, "{\n\"some\" \"keyvalue\" // line comment\n\n}" },
{ parser::DefSyntaxToken::Type::Whitespace, "\r\n\r\n" },
{ parser::DefSyntaxToken::Type::Token, "test2" },
{ parser::DefSyntaxToken::Type::Whitespace, "\n" },
{ parser::DefSyntaxToken::Type::BracedBlock, "{\n\"some\" \"keyvalue\"\n\n}" },
{ parser::DefSyntaxToken::Type::Whitespace, "\n" },
});
}

TEST(DefBlockSyntaxParser, EmptyText)
using DefBlockSyntaxParserTest = RadiantTest;

TEST_F(DefBlockSyntaxParserTest, EmptyText)
{
auto syntaxTree = parseText("");

EXPECT_TRUE(syntaxTree) << "Syntax Root must not be null";
}

TEST(DefBlockSyntaxParser, Whitespace)
TEST_F(DefBlockSyntaxParserTest, Whitespace)
{
auto syntaxTree = parseText(" ");
EXPECT_EQ(syntaxTree->root->getChildren().size(), 1) << "Expected 1 whitespace node";
Expand All @@ -149,4 +163,15 @@ TEST(DefBlockSyntaxParser, Whitespace)
EXPECT_EQ(syntaxTree->root->getChildren().front()->getString(), "\r\n \r\n");
}

// Attempt to parse a whole file and reconstruct it from the syntax tree
TEST_F(DefBlockSyntaxParserTest, ReconstructFileFromSyntaxTree)
{
auto originalText = algorithm::loadTextFromVfsFile("testdecls/removal_tests.decl");

auto syntaxTree = parseText(originalText);

auto reconstructedText = syntaxTree->root->getString();
EXPECT_EQ(reconstructedText, originalText) << "Parsed file couldn't be reconstructed";
}

}
1 change: 1 addition & 0 deletions test/algorithm/FileUtils.h
@@ -1,6 +1,7 @@
#pragma once

#include <string>
#include <fstream>
#include "ifilesystem.h"
#include "os/fs.h"
#include "string/replace.h"
Expand Down

0 comments on commit e6c5342

Please sign in to comment.