/
thomas-king
/
CodeGenerator
Обзор
Документация
Войти
/
thomas-king
/
CodeGenerator
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
test/VariableDeclarationTests/VariableDeclarationTests.cpp
425 строк
16 KB
Ace Rodstin
Task #49. Add support for class initializer.
26 окт 2023, 16:48
26 окт 2023, 16:48
c402876
Код
Авторство
О чём код?
// // VariableDeclarationTests.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(VariableDeclarationTests, constantDeclaration) { // Given string expected = "const long int value;"; // When Symbol constantSymbol { Type::Atomic::INTEGER_64, "value" }; Object constantObject { Object::Type::VALUE, constantSymbol }; auto constantDeclaration = Node::make<ConstantDeclaration>(constantObject); auto constantDeclarationNode = Node::cast<Node>(constantDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(constantDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, constantDeclarationWithInteger32Value) { // Given string expected = "const int value = 1;"; // When Symbol constantSymbol { Type::Atomic::INTEGER_32, "value" }; Object constantObject { Object::Type::VALUE, constantSymbol }; Literal constantLiteral { Type::Atomic::INTEGER_32, 1 }; RightValue constantValue { RightValue::Kind::LITERAL, constantLiteral }; auto constantDeclaration = Node::make<ConstantDeclaration>(constantObject, constantValue); auto constantDeclarationNode = Node::cast<Node>(constantDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(constantDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, constantDeclarationWithInteger64Value) { // Given string expected = "const long int value = 1;"; // When Symbol constantSymbol { Type::Atomic::INTEGER_64, "value" }; Object constantObject { Object::Type::VALUE, constantSymbol }; Literal constantLiteral { Type::Atomic::INTEGER_64, 1L }; RightValue constantValue { RightValue::Kind::LITERAL, constantLiteral }; auto constantDeclaration = Node::make<ConstantDeclaration>(constantObject, constantValue); auto constantDeclarationNode = Node::cast<Node>(constantDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(constantDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, constantDeclarationWithDouble32Value) { // Given string expected = "const double value = 1.123456;"; // When Symbol constantSymbol { Type::Atomic::DOUBLE_32, "value" }; Object constantObject { Object::Type::VALUE, constantSymbol }; Literal constantLiteral { Type::Atomic::DOUBLE_32, 1.123456 }; RightValue constantValue { RightValue::Kind::LITERAL, constantLiteral }; auto constantDeclaration = Node::make<ConstantDeclaration>(constantObject, constantValue); auto constantDeclarationNode = Node::cast<Node>(constantDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(constantDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, constantDeclarationWithDouble64Value) { // Given string expected = "const long double value = 1.123456789;"; // When Symbol constantSymbol { Type::Atomic::DOUBLE_64, "value" }; Object constantObject { Object::Type::VALUE, constantSymbol }; Literal constantLiteral { Type::Atomic::DOUBLE_64, 1.123456789L }; RightValue constantValue { RightValue::Kind::LITERAL, constantLiteral }; auto constantDeclaration = Node::make<ConstantDeclaration>(constantObject, constantValue); auto constantDeclarationNode = Node::cast<Node>(constantDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(constantDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, constantDeclarationWithStringValue) { // Given string expected = "const string value = \"test\";"; // When Symbol constantSymbol { Type::Atomic::STRING, "value" }; Object constantObject { Object::Type::VALUE, constantSymbol }; Literal constantLiteral { Type::Atomic::STRING, "test" }; RightValue constantValue { RightValue::Kind::LITERAL, constantLiteral }; auto constantDeclaration = Node::make<ConstantDeclaration>(constantObject, constantValue); auto constantDeclarationNode = Node::cast<Node>(constantDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(constantDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, constantDeclarationWithBooleanValue) { // Given string expected = "const bool value = false;"; // When Symbol constantSymbol { Type::Atomic::BOOLEAN, "value" }; Object constantObject { Object::Type::VALUE, constantSymbol }; Literal constantLiteral { Type::Atomic::BOOLEAN, false }; RightValue constantValue { RightValue::Kind::LITERAL, constantLiteral }; auto constantDeclaration = Node::make<ConstantDeclaration>(constantObject, constantValue); auto constantDeclarationNode = Node::cast<Node>(constantDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(constantDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, variableDeclaration) { // Given string expected = "long int value;"; // When Symbol variableSymbol { Type::Atomic::INTEGER_64, "value" }; Object variableObject { Object::Type::VALUE, variableSymbol }; auto variableDeclaration = Node::make<VariableDeclaration>(variableObject); auto variableDeclarationNode = Node::cast<Node>(variableDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(variableDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, variableDeclarationWithIntegerValue) { // Given string expected = "long int value = 1;"; // When Symbol variableSymbol { Type::Atomic::INTEGER_64, "value" }; Object variableObject { Object::Type::VALUE, variableSymbol }; Literal variableLiteral { Type::Atomic::INTEGER_64, 1L }; RightValue variableValue { RightValue::Kind::LITERAL, variableLiteral }; auto variableDeclaration = Node::make<VariableDeclaration>(variableObject, variableValue); auto variableDeclarationNode = Node::cast<Node>(variableDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(variableDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, variableDeclarationBySymbol) { // Given string expected = "long int value = another;"; // When Symbol variableSymbol { Type::Atomic::INTEGER_64, "value" }; Object variableObject { Object::Type::VALUE, variableSymbol }; Symbol anotherSymbol { { Type::Atomic::INTEGER_64 }, "another" }; Object anotherObject { Object::Type::VALUE, anotherSymbol }; RightValue anotherValue { RightValue::Kind::OBJECT, anotherObject }; auto variableDeclaration = Node::make<VariableDeclaration>(variableObject, anotherValue); auto variableDeclarationNode = Node::cast<Node>(variableDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(variableDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, variableDeclarationByAddition) { // Given string expected = "long int value = 1 + 2;"; // When Literal lhsLiteral { Type::Atomic::INTEGER_64, 1L }; RightValue lhs { RightValue::Kind::LITERAL, lhsLiteral }; Literal rhsLiteral { Type::Atomic::INTEGER_64, 2L }; RightValue rhs { RightValue::Kind::LITERAL, rhsLiteral }; auto additionOperation = Node::make<AdditionOperation>(lhs, rhs); auto operation = Node::cast<Operation>(additionOperation); Symbol variableSymbol { Type::Atomic::INTEGER_64, "value" }; Object variableObject { Object::Type::VALUE, variableSymbol }; RightValue variableValue { RightValue::Kind::OPERATION, operation }; auto variableDeclaration = Node::make<VariableDeclaration>(variableObject, variableValue); auto variableDeclarationNode = Node::cast<Node>(variableDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(variableDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, variableDeclarationByAdditions) { // Given string expected = "long int value = 1 + 2 + 3;"; // When Literal literal1 { Type::Atomic::INTEGER_64, 1L }; RightValue rightValue1 { RightValue::Kind::LITERAL, literal1 }; Literal literal2 { Type::Atomic::INTEGER_64, 2L }; RightValue rightValue2 { RightValue::Kind::LITERAL, literal2 }; Literal literal3 { Type::Atomic::INTEGER_64, 3L }; RightValue rightValue3 { RightValue::Kind::LITERAL, literal3 }; auto additionOperation1 = Node::make<AdditionOperation>(rightValue2, rightValue3); auto operation1 = Node::cast<Operation>(additionOperation1); RightValue rightValue4 { RightValue::Kind::OPERATION, operation1 }; auto additionOperation2 = Node::make<AdditionOperation>(rightValue1, rightValue4); auto operation2 = Node::cast<Operation>(additionOperation2); Symbol variableSymbol { Type::Atomic::INTEGER_64, "value" }; Object variableObject { Object::Type::VALUE, variableSymbol }; RightValue variableValue { RightValue::Kind::OPERATION, operation2 }; auto variableDeclaration = Node::make<VariableDeclaration>(variableObject, variableValue); auto variableDeclarationNode = Node::cast<Node>(variableDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(variableDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, variableDeclarationByAdditionWithSymbol) { // Given string expected = "long int value = 1 + arg1;"; // When Literal lhsLiteral { Type::Atomic::INTEGER_64, 1L }; RightValue lhs { RightValue::Kind::LITERAL, lhsLiteral }; Symbol rhsSymbol { Type::Atomic::INTEGER_64, "arg1" }; Object rhsObject { Object::Type::VALUE, rhsSymbol }; RightValue rhs { RightValue::Kind::OBJECT, rhsObject }; auto additionOperation = Node::make<AdditionOperation>(lhs, rhs); auto operation = Node::cast<Operation>(additionOperation); Symbol variableSymbol { Type::Atomic::INTEGER_64, "value" }; Object variableObject { Object::Type::VALUE, variableSymbol }; RightValue variableValue { RightValue::Kind::OPERATION, operation }; auto variableDeclaration = Node::make<VariableDeclaration>(variableObject, variableValue); auto variableDeclarationNode = Node::cast<Node>(variableDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(variableDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, variableDeclarationByMultiplication) { // Given string expected = "long int value = 1 * 2;"; // When Literal lhsLiteral { Type::Atomic::INTEGER_64, 1L }; RightValue lhs { RightValue::Kind::LITERAL, lhsLiteral }; Literal rhsLiteral { Type::Atomic::INTEGER_64, 2L }; RightValue rhs { RightValue::Kind::LITERAL, rhsLiteral }; auto multiplicationOperation = Node::make<MultiplicationOperation>(lhs, rhs); auto operation = Node::cast<Operation>(multiplicationOperation); Symbol variableSymbol { Type::Atomic::INTEGER_64, "value" }; Object variableObject { Object::Type::VALUE, variableSymbol }; RightValue variableValue { RightValue::Kind::OPERATION, operation }; auto variableDeclaration = Node::make<VariableDeclaration>(variableObject, variableValue); auto variableDeclarationNode = Node::cast<Node>(variableDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(variableDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, variableDeclarationBySubtraction) { // Given string expected = "long int value = 1 - 2;"; // When Literal lhsLiteral { Type::Atomic::INTEGER_64, 1L }; RightValue lhs { RightValue::Kind::LITERAL, lhsLiteral }; Literal rhsLiteral { Type::Atomic::INTEGER_64, 2L }; RightValue rhs { RightValue::Kind::LITERAL, rhsLiteral }; auto subtractionOperation = Node::make<SubtractionOperation>(lhs, rhs); auto operation = Node::cast<Operation>(subtractionOperation); Symbol variableSymbol { Type::Atomic::INTEGER_64, "value" }; Object variableObject { Object::Type::VALUE, variableSymbol }; RightValue variableValue { RightValue::Kind::OPERATION, operation }; auto variableDeclaration = Node::make<VariableDeclaration>(variableObject, variableValue); auto variableDeclarationNode = Node::cast<Node>(variableDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(variableDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); } TEST(VariableDeclarationTests, variableDeclarationByDivision) { // Given string expected = "long int value = 1 / 2;"; // When Literal lhsLiteral { Type::Atomic::INTEGER_64, 1L }; RightValue lhs { RightValue::Kind::LITERAL, lhsLiteral }; Literal rhsLiteral { Type::Atomic::INTEGER_64, 2L }; RightValue rhs { RightValue::Kind::LITERAL, rhsLiteral }; auto divisionOperation = Node::make<DivisionOperation>(lhs, rhs); auto operation = Node::cast<Operation>(divisionOperation); Symbol variableSymbol { Type::Atomic::INTEGER_64, "value" }; Object variableObject { Object::Type::VALUE, variableSymbol }; RightValue variableValue { RightValue::Kind::OPERATION, operation }; auto variableDeclaration = Node::make<VariableDeclaration>(variableObject, variableValue); auto variableDeclarationNode = Node::cast<Node>(variableDeclaration); auto [module, moduleSourceFile] = createModule(); moduleSourceFile->append(variableDeclarationNode, SourceFile::Section::PROPERTIES); auto [package, packageSourceFile] = translate(module); auto result = packageSourceFile.sourceCode; // Then EXPECT_STREQ(result.c_str(), expected.c_str()); }