/
thomas-king
/
CodeGenerator
Обзор
Документация
Войти
/
thomas-king
/
CodeGenerator
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
test/FunctionDeclarationTests/FunctionDeclarationTests.cpp
451 строка
18 KB
Ace Rodstin
Task #49. Add support for class initializer.
26 окт 2023, 16:48
26 окт 2023, 16:48
c402876
Код
Авторство
О чём код?
// // FunctionDeclarationTests.cpp // CodeGenerator // // Created by Ace Rodstin on 10/23/23. // Updated by Ace Rodstin on 10/26/23. // // Copyright © 2023 Ace Rodstin. All rights reserved. // #include "Support.h" TEST(FunctionDeclarationTests, functionDeclaration) { // Given string expected = "void testFunction() {\n" "\n" "}"; // When Symbol functionSymbol { Type::Atomic::VOID, "testFunction" }; auto functionDeclaration = Node::make<FunctionDeclaration>(functionSymbol); auto functionDeclarationNode = Node::cast<Node>(functionDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionDeclarationNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionDeclarationWithIntegerParameter) { // Given string expected = "void testFunction(long int arg1) {\n" "\n" "}"; // When Symbol parameterSymbol { Type::Atomic::INTEGER_64, "arg1" }; Object parameterObject { Object::Type::VALUE, parameterSymbol }; auto parameterDeclaration = Node::make<ParameterDeclaration>(parameterObject); auto parameterDeclarationNode = Node::cast<Node>(parameterDeclaration); Symbol functionSymbol { Type::Atomic::VOID, "testFunction" }; auto functionDeclaration = Node::make<FunctionDeclaration>(functionSymbol); functionDeclaration->append(parameterDeclarationNode, FunctionDeclaration::Section::PARAMETERS); auto functionDeclarationNode = Node::cast<Node>(functionDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionDeclarationNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionDeclarationWithDoubleParameter) { // Given string expected = "void testFunction(double arg1) {\n" "\n" "}"; // When Symbol parameterSymbol { Type::Atomic::DOUBLE_32, "arg1" }; Object parameterObject { Object::Type::VALUE, parameterSymbol }; auto parameterDeclaration = Node::make<ParameterDeclaration>(parameterObject); auto parameterDeclarationNode = Node::cast<Node>(parameterDeclaration); Symbol functionSymbol { Type::Atomic::VOID, "testFunction" }; auto functionDeclaration = Node::make<FunctionDeclaration>(functionSymbol); functionDeclaration->append(parameterDeclarationNode, FunctionDeclaration::Section::PARAMETERS); auto functionDeclarationNode = Node::cast<Node>(functionDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionDeclarationNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionDeclarationWithStringParameter) { // Given string expected = "void testFunction(string arg1) {\n" "\n" "}"; // When Symbol parameterSymbol { Type::Atomic::STRING, "arg1" }; Object paramterObject { Object::Type::VALUE, parameterSymbol }; auto parameterDeclaration = Node::make<ParameterDeclaration>(paramterObject); auto parameterDeclarationNode = Node::cast<Node>(parameterDeclaration); Symbol functionSymbol { Type::Atomic::VOID, "testFunction" }; auto functionDeclaration = Node::make<FunctionDeclaration>(functionSymbol); functionDeclaration->append(parameterDeclarationNode, FunctionDeclaration::Section::PARAMETERS); auto functionDeclarationNode = Node::cast<Node>(functionDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionDeclarationNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionDeclarationWithParameters) { // Given string expected = "void testFunction(long int arg1, long int arg2) {\n" "\n" "}"; // When Symbol parameterSymbol1 { Type::Atomic::INTEGER_64, "arg1" }; Object paramterObject1 { Object::Type::VALUE, parameterSymbol1 }; auto parameterDeclaration1 = Node::make<ParameterDeclaration>(paramterObject1); auto parameterDeclarationNode1 = Node::cast<Node>(parameterDeclaration1); Symbol parameterSymbol2 { Type::Atomic::INTEGER_64, "arg2" }; Object paramterObject2 { Object::Type::VALUE, parameterSymbol2 }; auto parameterDeclaration2 = Node::make<ParameterDeclaration>(paramterObject2); auto parameterDeclarationNode2 = Node::cast<Node>(parameterDeclaration2); Symbol functionSymbol { Type::Atomic::VOID, "testFunction" }; auto functionDeclaration = Node::make<FunctionDeclaration>(functionSymbol); functionDeclaration->append(parameterDeclarationNode1, FunctionDeclaration::Section::PARAMETERS); functionDeclaration->append(parameterDeclarationNode2, FunctionDeclaration::Section::PARAMETERS); auto functionDeclarationNode = Node::cast<Node>(functionDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionDeclarationNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionDeclarationWithParameterWithBody) { // Given string expected = "long int testFunction(long int arg1) {\n" " return arg1;\n" "}"; // When Symbol parameterSymbol { Type::Atomic::INTEGER_64, "arg1" }; Object parameterObject { Object::Type::VALUE, parameterSymbol }; auto parameterDeclaration = Node::make<ParameterDeclaration>(parameterObject); auto parameterDeclarationNode = Node::cast<Node>(parameterDeclaration); Symbol returnSymbol { Type::Atomic::INTEGER_64, "arg1" }; auto returnStatement = Node::make<ReturnStatement>(returnSymbol); auto returnStatementNode = Node::cast<Node>(returnStatement); Symbol functionSymbol { Type::Atomic::INTEGER_64, "testFunction" }; auto functionDeclaration = Node::make<FunctionDeclaration>(functionSymbol); functionDeclaration->append(parameterDeclarationNode, FunctionDeclaration::Section::PARAMETERS); functionDeclaration->append(returnStatementNode, FunctionDeclaration::Section::BODY); auto functionDeclarationNode = Node::cast<Node>(functionDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionDeclarationNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionDeclarationWithBody) { // Given string expected = "long int testFunction() {\n" " return 1;\n" "}"; // When Literal literal { Type::Atomic::INTEGER_64, 1L }; RightValue value { RightValue::Kind::LITERAL, literal }; auto returnStatement = Node::make<ReturnStatement>(value); auto returnStatementNode = Node::cast<Node>(returnStatement); Symbol functionSymbol { Type::Atomic::INTEGER_64, "testFunction" }; auto functionDeclaration = Node::make<FunctionDeclaration>(functionSymbol); functionDeclaration->append(returnStatementNode, FunctionDeclaration::Section::BODY); auto functionDeclarationNode = Node::cast<Node>(functionDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionDeclarationNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionDeclarationWithParametersWithReturnAddition) { // Given string expected = "void testFunction(long int arg1, long int arg2) {\n" " return arg1 + arg2;\n" "}"; // When Symbol parameterSymbol1 { Type::Atomic::INTEGER_64, "arg1" }; Object paramterObject1 { Object::Type::VALUE, parameterSymbol1 }; auto parameterDeclaration1 = Node::make<ParameterDeclaration>(paramterObject1); auto parameterNode1 = Node::cast<Node>(parameterDeclaration1); Symbol parameterSymbol2 { Type::Atomic::INTEGER_64, "arg2" }; Object paramterObject2 { Object::Type::VALUE, parameterSymbol2 }; auto parameterDeclaration2 = Node::make<ParameterDeclaration>(paramterObject2); auto parameterNode2 = Node::cast<Node>(parameterDeclaration2); RightValue lhs { RightValue::Kind::OBJECT, paramterObject1 }; RightValue rhs { RightValue::Kind::OBJECT, paramterObject2 }; auto additionOperation = Node::make<AdditionOperation>(lhs, rhs); auto operationNode = Node::cast<Operation>(additionOperation); RightValue value { RightValue::Kind::OPERATION, operationNode }; auto returnStatement = Node::make<ReturnStatement>(value); auto returnStatementNode = Node::cast<Node>(returnStatement); Symbol functionSymbol { Type::Atomic::VOID, "testFunction" }; auto functionDeclaration = Node::make<FunctionDeclaration>(functionSymbol); functionDeclaration->append(parameterNode1, FunctionDeclaration::Section::PARAMETERS); functionDeclaration->append(parameterNode2, FunctionDeclaration::Section::PARAMETERS); functionDeclaration->append(returnStatementNode, FunctionDeclaration::Section::BODY); auto functionDeclarationNode = Node::cast<Node>(functionDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionDeclarationNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionDeclarationWithParametersWithReturnAdditionWithNumber) { // Given string expected = "void testFunction(long int arg1) {\n" " return arg1 + 1;\n" "}"; // When Symbol parameterSymbol { Type::Atomic::INTEGER_64, "arg1" }; Object parameterObject { Object::Type::VALUE, parameterSymbol }; auto parameterDeclaration = Node::make<ParameterDeclaration>(parameterObject); auto parameterDeclarationNode = Node::cast<Node>(parameterDeclaration); RightValue lhs { RightValue::Kind::OBJECT, parameterObject }; Literal literal { Type::Atomic::INTEGER_64, 1L }; RightValue rhs { RightValue::Kind::LITERAL, literal }; auto additionOperation = Node::make<AdditionOperation>(lhs, rhs); auto operationNode = Node::cast<Operation>(additionOperation); RightValue value { RightValue::Kind::OPERATION, operationNode }; auto returnStatement = Node::make<ReturnStatement>(value); auto returnStatementNode = Node::cast<Node>(returnStatement); Symbol functionSymbol { Type::Atomic::VOID, "testFunction" }; auto functionDeclaration = Node::make<FunctionDeclaration>(functionSymbol); functionDeclaration->append(parameterDeclarationNode, FunctionDeclaration::Section::PARAMETERS); functionDeclaration->append(returnStatementNode, FunctionDeclaration::Section::BODY); auto functionDeclarationNode = Node::cast<Node>(functionDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionDeclarationNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionCall) { // Given string expected = "testFunction();"; // When Symbol functionSymbol { Type::Atomic::VOID, "testFunction" }; Object functionObject { Object::Type::REFERENCE, functionSymbol }; auto functionCallExpression = Node::make<FunctionCallExpression>(functionObject); auto functionCallExpressionNode = Node::cast<Node>(functionCallExpression); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionCallExpressionNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionCallWithIntegerArgument) { // Given string expected = "testFunction(1);"; // When Literal literal { Type::Atomic::INTEGER_64, 1L }; RightValue argument { RightValue::Kind::LITERAL, literal }; Symbol functionSymbol { Type::Atomic::VOID, "testFunction" }; Object functionObject { Object::Type::REFERENCE, functionSymbol }; auto functionCallExpression = Node::make<FunctionCallExpression>(functionObject); functionCallExpression->append(argument); auto functionCallExpressionNode = Node::cast<Node>(functionCallExpression); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionCallExpressionNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionCallWithDoubleArgument) { // Given string expected = "testFunction(1.5);"; // When Literal literal { Type::Atomic::DOUBLE_32, 1.5 }; RightValue argument { RightValue::Kind::LITERAL, literal }; Symbol functionSymbol { Type::Atomic::VOID, "testFunction" }; Object functionObject { Object::Type::REFERENCE, functionSymbol }; auto functionCallExpression = Node::make<FunctionCallExpression>(functionObject); functionCallExpression->append(argument); auto functionCallExpressionNode = Node::cast<Node>(functionCallExpression); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionCallExpressionNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionCallWithStringArgument) { // Given string expected = "testFunction(\"test\");"; // When Literal literal { Type::Atomic::STRING, "test" }; RightValue argument { RightValue::Kind::LITERAL, literal }; Symbol functionSymbol { Type::Atomic::VOID, "testFunction" }; Object functionObject { Object::Type::REFERENCE, functionSymbol }; auto functionCallExpression = Node::make<FunctionCallExpression>(functionObject); functionCallExpression->append(argument); auto functionCallExpressionNode = Node::cast<Node>(functionCallExpression); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionCallExpressionNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionCallWithArguments) { // Given string expected = "testFunction(1, 2);"; // When Literal literal1 { Type::Atomic::INTEGER_64, 1L }; RightValue argument1 { RightValue::Kind::LITERAL, literal1 }; Literal literal2 { Type::Atomic::INTEGER_64, 2L }; RightValue argument2 { RightValue::Kind::LITERAL, literal2 }; Symbol functionSymbol { Type::Atomic::VOID, "testFunction" }; Object functionObject { Object::Type::REFERENCE, functionSymbol }; auto functionCallExpression = Node::make<FunctionCallExpression>(functionObject); functionCallExpression->append(argument1); functionCallExpression->append(argument2); auto functionCallExpressionNode = Node::cast<Node>(functionCallExpression); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionCallExpressionNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(FunctionDeclarationTests, functionCallInsideFunctionBody) { // Given string expected = "void testFunction() {\n" " example(1, 2);\n" "}"; // When Literal literal1 { Type::Atomic::INTEGER_64, 1L }; RightValue argument1 { RightValue::Kind::LITERAL, literal1 }; Literal literal2 { Type::Atomic::INTEGER_64, 2L }; RightValue argument2 { RightValue::Kind::LITERAL, literal2 }; Symbol functionCallSymbol { Type::Atomic::VOID, "example" }; Object functionCallObject { Object::Type::REFERENCE, functionCallSymbol }; auto functionCallExpression = Node::make<FunctionCallExpression>(functionCallObject); functionCallExpression->append(argument1); functionCallExpression->append(argument2); auto functionCallExpressionNode = Node::cast<Node>(functionCallExpression); Symbol functionSymbol { Type::Atomic::VOID, "testFunction" }; auto functionDeclaration = Node::make<FunctionDeclaration>(functionSymbol); functionDeclaration->append(functionCallExpressionNode, FunctionDeclaration::Section::BODY); auto functionDeclarationNode = Node::cast<Node>(functionDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(functionDeclarationNode, SourceFile::Section::FUNCTIONS); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); }