/
thomas-king
/
SyntacticAnalyzer
Обзор
Документация
Войти
/
thomas-king
/
SyntacticAnalyzer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
test/ParseConstantDeclarationTests.cpp
106 строк
3 KB
Ace Rodstin
Add ParseExpressionTests. Fix parenthesis parsing.
06 апр 2023, 13:52
06 апр 2023, 13:52
760e23e
Код
Авторство
О чём код?
// // ParseConstantDeclarationTests.cpp // Tests // // Created by Ace Rodstin on 4/6/23. // Copyright © 2023 Ace Rodstin. All rights reserved. // #include "gtest/gtest.h" #include "Services/SyntacticAnalyzer.h" using namespace std; using namespace ace; TEST(ParseConstantDeclarationTests, when_parse_then_identifierCorrect) { // Given string source = "val value = 1"; auto expected = "value"; // When TokensParser tokensParser { source }; SyntacticAnalyzer syntacticAnalyzer { tokensParser }; auto statement = syntacticAnalyzer.parse(); auto declaration = statement->getDeclaration(); auto constantDeclaration = declaration->getConstantDeclaration(); auto constantSpecification = constantDeclaration->getConstantSpecification(); auto identifierList = constantSpecification->getIdentifierList(); auto identifiers = identifierList->getIdentifiers(); auto& identifier = (*identifiers)[0]; auto output = identifier->getIdentifier(); // Then EXPECT_EQ(output, expected); } TEST(ParseConstantDeclarationTests, when_parse_then_identifierListCorrect) { // Given string source = "val value, another = 1"; vector<string> expected = { "value", "another" }; // When TokensParser tokensParser { source }; SyntacticAnalyzer syntacticAnalyzer { tokensParser }; auto statement = syntacticAnalyzer.parse(); auto declaration = statement->getDeclaration(); auto constantDeclaration = declaration->getConstantDeclaration(); auto constantSpecification = constantDeclaration->getConstantSpecification(); auto identifierList = constantSpecification->getIdentifierList(); auto identifiers = identifierList->getIdentifiers(); vector<string> output; for (auto& identifier: *identifiers) { output.push_back(identifier->getIdentifier()); } // Then EXPECT_EQ(output, expected); } TEST(ParseConstantDeclarationTests, when_parse_then_typeCorrect) { // Given string source = "val value: Integer = 1"; auto expected = "Integer"; // When TokensParser tokensParser { source }; SyntacticAnalyzer syntacticAnalyzer { tokensParser }; auto statement = syntacticAnalyzer.parse(); auto declaration = statement->getDeclaration(); auto constantDeclaration = declaration->getConstantDeclaration(); auto constantSpecification = constantDeclaration->getConstantSpecification(); auto type = constantSpecification->getType(); auto typeName = type->getTypeName(); auto identifier = typeName->getIdentifier(); auto output = identifier->getIdentifier(); // Then EXPECT_EQ(output, expected); } TEST(ParseConstantDeclarationTests, when_parse_then_expressionCorrect) { // Given string source = "val value = 1"; auto expected = "1"; // When TokensParser tokensParser { source }; SyntacticAnalyzer syntacticAnalyzer { tokensParser }; auto statement = syntacticAnalyzer.parse(); auto declaration = statement->getDeclaration(); auto constantDeclaration = declaration->getConstantDeclaration(); auto constantSpecification = constantDeclaration->getConstantSpecification(); auto expression = constantSpecification->getExpression(); auto unaryExpression = dynamic_cast<const UnaryExpression*>(expression); auto primaryExpression = unaryExpression->getPrimaryExpression(); auto operand = primaryExpression->getOperand(); auto literal = operand->getLiteral(); auto output = literal->getLiteral(); // Then EXPECT_EQ(output, expected); }