Skip to content

Commit

Permalink
fix: calling functions with empty arguments fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Dec 4, 2023
1 parent 6fd6a44 commit 65b1f03
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/builtin_functions/divide/Divide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "exceptions/SyntaxError.h"

SyntaxTreeNode Divide::evaluate(const std::vector<SyntaxTreeNode> &args) {
if (args.empty()) {
throw SyntaxError("At least 1 argument was expected but none were found");
}
int result = std::stoi(Evaluate::evaluate(args[0]).token.token);

for (int i = 1; i < args.size(); i++) {
Expand Down
3 changes: 3 additions & 0 deletions src/builtin_functions/subtract/subtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "exceptions/SyntaxError.h"

SyntaxTreeNode Subtract::evaluate(const std::vector<SyntaxTreeNode> &args) {
if (args.empty()) {
throw SyntaxError("At least 1 argument was expected but none were found");
}
int result = std::stoi(Evaluate::evaluate(args[0]).token.token);

for (int i = 1; i < args.size(); i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/eval/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SyntaxTreeNode Evaluate::evaluate(const SyntaxTreeNode &tree) {
{"/", (Function *) (new Divide())},
{"print", (Function *) (new Print())},
};
if (tree.children.empty()) {
if (tree.children.empty() && tree.token.type != Token::Symbol) {
return SyntaxTreeNode(tree.token);
}
if (builtin.find(tree.token.token) != builtin.end()) {
Expand Down
3 changes: 1 addition & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
#include "lexer/Lexer.h"
#include "parser/Parser.h"
#include "read.hpp"
#include "utils/printTokens.h"
#include "utils/StdOut.h"
#include "utils/printTokens.h"
#include <bits/getopt_core.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>

Expand Down
6 changes: 6 additions & 0 deletions tests/add_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ TEST(add_test, ThrowExceptionOnInvalidArguments) {
EXPECT_EQ(line == expression[1].token.line, true);
EXPECT_EQ(column == expression[1].token.column, true);
}

TEST(add_test, ShouldReturnZeroWhenNoArgumentsAreFound) {
auto expectedResult = SyntaxTreeNode(Token(Token::Integer, "0"));
auto actualResult = Add().evaluate({});
EXPECT_EQ(actualResult == expectedResult, true);
}
20 changes: 17 additions & 3 deletions tests/divide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "parser/SyntaxTreeNode.h"
#include <gtest/gtest.h>

TEST(add_test, ShouldDivideNumbers) {
TEST(divide_test, ShouldDivideNumbers) {
auto expression = {SyntaxTreeNode(Token(Token::Integer, "8")),
SyntaxTreeNode(Token(Token::Integer, "2"))};

Expand All @@ -13,7 +13,7 @@ TEST(add_test, ShouldDivideNumbers) {
EXPECT_EQ(expectedResult == actual, true);
}

TEST(add_test, ShouldEvaluateNestedDivision) {
TEST(divide_test, ShouldEvaluateNestedDivision) {
auto expression = {
SyntaxTreeNode(Token(Token::Integer, "18")),
SyntaxTreeNode(Token(Token::Integer, "/"),
Expand All @@ -26,7 +26,7 @@ TEST(add_test, ShouldEvaluateNestedDivision) {
EXPECT_EQ(expectedResult == actual, true);
}

TEST(add_test, ThrowExceptionOnDivisionWithInvalidArguments) {
TEST(divide_test, ThrowExceptionOnDivisionWithInvalidArguments) {
bool isCaught = false;
std::string errorMessage;
int line = 0;
Expand All @@ -52,3 +52,17 @@ TEST(add_test, ThrowExceptionOnDivisionWithInvalidArguments) {
EXPECT_EQ(column == expression[1].token.column, true);
}

TEST(divide_test, ThrowExceptionOnZeroArguments) {
bool isCaught = false;
std::string errorMessage;

try {
Divide().evaluate({});
} catch (SyntaxError &error) {
isCaught = true;
errorMessage = error.message;
}

EXPECT_EQ(isCaught, true);
EXPECT_EQ(errorMessage == "At least 1 argument was expected but none were found", true);
}
12 changes: 11 additions & 1 deletion tests/eval_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ TEST(eval_test, ShouldEvaluateNestedExpression) {
auto expectedResult = SyntaxTreeNode(Token(Token::Integer, "37"));
auto actualResult = Evaluate::evaluate(expression);
EXPECT_EQ(expectedResult == actualResult, true);
}
}

TEST(eval_test, ShouldCallFunctionWithNoArguments) {
auto expression = SyntaxTreeNode(
Token(Token::Symbol, "+")
);

auto expectedResult = SyntaxTreeNode(Token(Token::Integer, "0"));
auto actualResult = Evaluate::evaluate(expression);
EXPECT_EQ(expectedResult == actualResult, true);
}
10 changes: 8 additions & 2 deletions tests/multiply_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "parser/SyntaxTreeNode.h"
#include <gtest/gtest.h>

TEST(add_test, ShouldMultiplyNumbers) {
TEST(multiply_test, ShouldMultiplyNumbers) {
auto expression = {SyntaxTreeNode(Token(Token::Integer, "2")),
SyntaxTreeNode(Token(Token::Integer, "2"))};

Expand All @@ -26,7 +26,7 @@ TEST(add_test, ShouldEvaluateNestedMultiplication) {
EXPECT_EQ(expectedResult == actual, true);
}

TEST(add_test, ThrowExceptionOnMultiplyWithInvalidArguments) {
TEST(multiply_test, ThrowExceptionOnMultiplyWithInvalidArguments) {
bool isCaught = false;
std::string errorMessage;
int line = 0;
Expand All @@ -51,3 +51,9 @@ TEST(add_test, ThrowExceptionOnMultiplyWithInvalidArguments) {
EXPECT_EQ(line == expression[1].token.line, true);
EXPECT_EQ(column == expression[1].token.column, true);
}

TEST(multiply_test, ShouldReturnZeroWhenNoArgumentsAreFound) {
auto expectedResult = SyntaxTreeNode(Token(Token::Integer, "1"));
auto actualResult = Multiply().evaluate({});
EXPECT_EQ(actualResult == expectedResult, true);
}
21 changes: 18 additions & 3 deletions tests/subtract_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "parser/SyntaxTreeNode.h"
#include <gtest/gtest.h>

TEST(add_test, ShouldSubtractNumbers) {
TEST(subtract_test, ShouldSubtractNumbers) {
auto expression = {SyntaxTreeNode(Token(Token::Integer, "5")),
SyntaxTreeNode(Token(Token::Integer, "2"))};

Expand All @@ -13,7 +13,7 @@ TEST(add_test, ShouldSubtractNumbers) {
EXPECT_EQ(expectedResult == actual, true);
}

TEST(add_test, ShouldEvaluateNestedSubtraction) {
TEST(subtract_test, ShouldEvaluateNestedSubtraction) {
auto expression = {
SyntaxTreeNode(Token(Token::Integer, "9")),
SyntaxTreeNode(Token(Token::Integer, "-"),
Expand All @@ -26,7 +26,7 @@ TEST(add_test, ShouldEvaluateNestedSubtraction) {
EXPECT_EQ(expectedResult == actual, true);
}

TEST(add_test, ThrowExceptionOnInvalidArgumentsInSubtraction) {
TEST(subtract_test, ThrowExceptionOnInvalidArgumentsInSubtraction) {
bool isCaught = false;
std::string errorMessage;
int line = 0;
Expand All @@ -51,3 +51,18 @@ TEST(add_test, ThrowExceptionOnInvalidArgumentsInSubtraction) {
EXPECT_EQ(line == expression[1].token.line, true);
EXPECT_EQ(column == expression[1].token.column, true);
}

TEST(subtract_test, ThrowExceptionOnZeroArguments) {
bool isCaught = false;
std::string errorMessage;

try {
Subtract().evaluate({});
} catch (SyntaxError &error) {
isCaught = true;
errorMessage = error.message;
}

EXPECT_EQ(isCaught, true);
EXPECT_EQ(errorMessage == "At least 1 argument was expected but none were found", true);
}

0 comments on commit 65b1f03

Please sign in to comment.