Skip to content

Commit

Permalink
UT for functions parsing error
Browse files Browse the repository at this point in the history
  • Loading branch information
cdoucy committed Oct 28, 2023
1 parent 3bf7050 commit b1c5e73
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(fmt)

set(CMAKE_CXX_FLAGS "-g3 -std=c++20 -Wpedantic -Wall -Wextra ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "-std=c++20 -Wpedantic -Wall -Wextra ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g3 ${CMAKE_CXX_FLAGS_DEBUG}")

if (${CMAKE_BUILD_TYPE} STREQUAL "test")
Expand Down
5 changes: 5 additions & 0 deletions error-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fnc f() int {
}

f()();
print(f());
14 changes: 13 additions & 1 deletion functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,16 @@ fnc rec(int start) int {
int r = div(4, 2);
print(r);

rec(10);
fnc power(int x, int n) int {
if (n == 0) {
return 1;
}
if (n == 1) {
return x;
}
return power(x, n - 1) * x;
}

print(power(2, 32));

rec(power(2, 8));
1 change: 0 additions & 1 deletion input.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

int i = 0;

while (i < 0) {
Expand Down
75 changes: 75 additions & 0 deletions tst/parser/functions_error_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <gtest/gtest.h>
#include "Parser.hpp"

struct FunctionsErrorTest
{
std::string description;
std::string program;
};

void testFunctionsError(const std::vector<FunctionsErrorTest> &testCases)
{
for (const auto &tc: testCases) {
std::cout << tc.description << std::endl;

Parser parser;

EXPECT_THROW(parser.feed(tc.program), SyntaxError);
}
}

TEST(ParserTest, FunctionsDeclarationError)
{
const std::vector<FunctionsErrorTest> testCases{
FunctionsErrorTest{
.description = "1. Function declaration without identifier",
.program = "fnc () {}"
},
FunctionsErrorTest{
.description = "1. Function declaration without identifier",
.program = "fnc () {}"
},
FunctionsErrorTest{
.description = "2. Function declaration without parenthesis",
.program = "fnc f {}"
},
FunctionsErrorTest{
.description = "3. Function declaration with already used parameter name",
.program = "fnc f (int x, int x){}"
},
FunctionsErrorTest{
.description = "4. Function declaration without close parenthesis",
.program = "fnc f (int x, int y {}"
},
FunctionsErrorTest{
.description = "5. Function declaration without body",
.program = "fnc f (int x, int y)"
},
FunctionsErrorTest{
.description = "6. Function declaration with return type and without body",
.program = "fnc f (int x, int y) int"
}
};

testFunctionsError(testCases);
}

TEST(ParserTest, CallError)
{
const std::vector<FunctionsErrorTest> testCases{
FunctionsErrorTest{
.description = "1. Missing param or closed parenthesis",
.program = "f(;"
},
FunctionsErrorTest{
.description = "2. Missing closed parenthesis",
.program = "f(1, 2;"
},
FunctionsErrorTest{
.description = "3. Missing expression after comma",
.program = "f(1, );"
}
};

testFunctionsError(testCases);
}
1 change: 1 addition & 0 deletions tst/tst.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ set(
${PROJECT_ROOT}/tst/evaluator/evaluator_test.cpp
${PROJECT_ROOT}/tst/evaluator/statements_test.cpp
${PROJECT_ROOT}/tst/parser/parser_test.cpp
${PROJECT_ROOT}/tst/parser/functions_error_test.cpp
)

0 comments on commit b1c5e73

Please sign in to comment.