hoolang/tests/bool_tests.cpp
2025-07-11 08:28:22 +05:30

72 lines
1.7 KiB
C++

#include "Compiler.hpp"
#include "Node.hpp"
#include "ParseErrorException.hpp"
#include "llvm/IR/Constants.h"
#include <gtest/gtest.h>
// Utility function to test boolean literal compilation
void testBoolLiteral(const std::string &input, bool expectedValue)
{
auto compiler = std::make_unique<Compiler>(input, "main");
auto result = compiler->compile();
auto boolNode = std::any_cast<Node>(result);
ASSERT_EQ(boolNode.getNodeType(), NODE_LITERAL);
ASSERT_EQ(boolNode.getDataType(), DATATYPE_BOOL);
auto value = boolNode.getValue();
ASSERT_NE(value, nullptr);
auto value_constant = llvm::dyn_cast<llvm::ConstantInt>(value);
ASSERT_NE(value_constant, nullptr);
ASSERT_EQ(value_constant->getValue().getLimitedValue(),
static_cast<uint64_t>(expectedValue));
}
// Test fixture for boolean tests
class BoolTest : public ::testing::Test
{
protected:
void SetUp() override
{
// Setup code if needed
}
void TearDown() override
{
// Cleanup code if needed
}
};
// Test cases
TEST_F(BoolTest, LiteralTrue)
{
testBoolLiteral("true;", true);
}
TEST_F(BoolTest, LiteralFalse)
{
testBoolLiteral("false;", false);
}
// Negative test cases
TEST_F(BoolTest, InvalidLiteral)
{
auto compiler = std::make_unique<Compiler>("notbool;", "main");
ASSERT_THROW(compiler->compile(), ParseCollectiveErrorException);
}
TEST_F(BoolTest, MissingSemicolon)
{
auto compiler = std::make_unique<Compiler>("true", "main");
ASSERT_THROW(compiler->compile(), ParseCollectiveErrorException);
}
TEST_F(BoolTest, MixedCaseLiteral)
{
auto compiler = std::make_unique<Compiler>("True;", "main");
ASSERT_THROW(compiler->compile(), ParseCollectiveErrorException);
}