diff --git a/test/parser_integration_tests.c b/test/parser_integration_tests.c index 1916cd1..4b55e15 100644 --- a/test/parser_integration_tests.c +++ b/test/parser_integration_tests.c @@ -4,11 +4,10 @@ #include #include -void progStartKeyword() { - const char *mock_data = "==\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); +enum SYNTAX_ERROR setupError(const char *mockData) { + FILE *mockFile = fmemopen((void *)mockData, strlen(mockData), "r"); - if (mock_file == NULL) { + if (mockFile == NULL) { fprintf(stderr, "Error opening source file.\n"); exit(EXIT_FAILURE); } @@ -19,328 +18,94 @@ void progStartKeyword() { // for integration tests, prog itself needs a previously initialized parser // with a token too - struct Token token = lexerGetNextChar(mock_file, lineCount); + struct Token token = lexerGetNextChar(mockFile, lineCount); struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; + .fd = mockFile, .lineCount = lineCount, .token = token}; enum SYNTAX_ERROR error = prog(&parser); // example debugging: // printSyntaxError(error); + return error; +} + +void progStartKeyword() { + const char *mock_data = "==\n"; + enum SYNTAX_ERROR error = setupError("==\n"); assert(error == INVALID_PROG_START_KEYWORD); } void declListVarInvalidType() { - const char *mock_data = "const string\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file.\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("const string\n"); assert(error == INVALID_TYPE); } void declVarNoId() { - const char *mock_data = "const int 5\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file.\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("const int 5\n"); assert(error == NO_VAR_ID); } void declVarArrayInvalidSubscript() { - const char *mock_data = "const int i[2.2\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file.\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("const int i[2.2\n"); assert(error == INVALID_ARRAY_SUBSCRIPT_DEC); } void declVarArrayDidntClose() { - const char *mock_data = "const int i[2[\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file.\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("const int i[2[\n"); assert(error == INVALID_ARRAY_BRACKET_DEC_CLOSE); } void declVarBadInit() { - const char *mock_data = "const int i = i\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file.\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("const int i = i\n"); assert(error == INVALID_VAR_TYPE_INIT); } void declListVarMulti() { - const char *mock_data = "const int i = 1, a = 2\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file.\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("const int i = 1, a = 2\n"); assert(error == NO_ERROR); } void declListVarMultiFail() { - const char *mock_data = "const int i = 1, a = a\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file.\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("const int i = 1, a = a\n"); assert(error == INVALID_VAR_TYPE_INIT); } void declVarArrayMultiTooMany() { - const char *mock_data = "int i[2][3][4]\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file.\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("int i[2][3][4]\n"); assert(error == INVALID_ARRAY_DIMENSION_DECLARATION); } void declVarArrayBadInitCurly() { - const char *mock_data = "const int i[2] = [1\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file.\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("const int i[2] = [1\n"); assert(error == INVALID_ARRAY_INIT_CURLY_OPEN); } void declVarArrayInvalidTypeInit() { - const char *mock_data = "const int i[2] = {id\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file.\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("const int i[2] = {id\n"); assert(error == INVALID_ARRAY_TYPE_INIT); } void declVarArrayMultiInit() { - const char *mock_data = "int i[4] = {3, 4, 4, 4}\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file.\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("int i[4] = {3, 4, 4, 4}\n"); assert(error == NO_ERROR); } void declVarArrayBadClose() { - const char *mock_data = "int i[1] = {1{\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("int i[1] = {1{\n"); assert(error == INVALID_ARRAY_INIT_CURLY_CLOSE); } void declDefProcNoId() { - const char *mock_data = "prot 1\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("prot 1\n"); assert(error == NO_PROTO_ID); } void declDefProcProtNoOpenParen() { - const char *mock_data = "prot a -\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("prot a -\n"); assert(error == INVALID_PROTO_PAREN_OPEN); } void declDefProcProtoInvalidParamType() { - const char *mock_data = "prot a(&null)\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("prot a(&null)\n"); assert(error == INVALID_PROTO_PARAM_TYPE); } @@ -348,197 +113,46 @@ void declDefProcProtoInvalidParamType() { // changed the error check to correct this. // good for documentation for the grammar, i guess. void declDefProcProtoNoParamId() { - const char *mock_data = "prot b(int 1)\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("prot b(int 1)\n"); assert(error == NO_PROTO_VALID_TOKEN_AFTER_TYPE); } void declDefProcProtoNoValidTokenAfterType() { - const char *mock_data = "prot c(int{\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("prot c(int{\n"); assert(error == NO_PROTO_VALID_TOKEN_AFTER_TYPE); } void declDefProcProtoUnclosedArrayParam() { - const char *mock_data = "prot d(int[[\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("prot d(int[[\n"); assert(error == INVALID_ARRAY_PROTO_PARAM_BRACKET_CLOSE); } void declDefProcProtoInvalid2dArrayOpen() { - const char *mock_data = "prot e(int[]])\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("prot e(int[]])\n"); assert(error == NO_PROTO_VALID_TOKEN_AFTER_BRACKET_CLOSE); } void declDefProcProtoInvalid2dArrayClose() { - const char *mock_data = "prot f(int [][[)\n"; - FILE *mock_file = fmemopen((void *)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token}; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("prot f(int [][[)\n"); assert(error == INVALID_ARRAY_PROTO_PARAM_BRACKET_CLOSE); } void declDefProcProtoInvalid3dArray() { - const char *mock_data = "prot g(int [][][)\n"; - FILE *mock_file = fmemopen((void*)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token - }; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("prot g(int [][][)\n"); assert(error == INVALID_ARRAY_DIMENSION_DECLARATION); } void declDefProcProtoMultiParams() { - const char *mock_data = "prot h(int [], int[], int[][][])\n"; - - FILE *mock_file = fmemopen((void*)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token - }; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("prot h(int [], int[], int[][][])\n"); assert(error == INVALID_ARRAY_DIMENSION_DECLARATION); } void declDefProcProtoNoParenClose() { - const char *mock_data = "prot i(int(\n"; - - FILE *mock_file = fmemopen((void*)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token - }; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("prot i(int(\n"); assert(error == NO_PROTO_VALID_TOKEN_AFTER_TYPE); } void declDefProcProtoTwoProts() { - const char *mock_data = "prot j(int) prot k(2(\n"; - - FILE *mock_file = fmemopen((void*)mock_data, strlen(mock_data), "r"); - - if (mock_file == NULL) { - fprintf(stderr, "Error opening source file\n"); - exit(EXIT_FAILURE); - } - - int *lineCount; - int line = 1; - lineCount = &line; - - struct Token token = lexerGetNextChar(mock_file, lineCount); - struct Parser parser = { - .fd = mock_file, .lineCount = lineCount, .token = token - }; - - enum SYNTAX_ERROR error = prog(&parser); + enum SYNTAX_ERROR error = setupError("prot j(int) prot k(2(\n"); assert(error == INVALID_PROTO_PARAM_TYPE); } diff --git a/test/parser_integration_tests.h b/test/parser_integration_tests.h index c48eb45..15b908b 100644 --- a/test/parser_integration_tests.h +++ b/test/parser_integration_tests.h @@ -6,6 +6,8 @@ #include "../lexer/lexer.h" #include "../lexer/types.h" +enum SYNTAX_ERROR setupError(const char* mockData); + void progStartKeyword(); void declListVarInvalidType(); diff --git a/test/test.c b/test/test.c index fecb349..03165d9 100644 --- a/test/test.c +++ b/test/test.c @@ -4,12 +4,17 @@ #include #include +// ANSI escape codes +#define RESET "\033[0m" +#define RED "\033[31m" +#define GREEN "\033[32m" + int main(void) { lexerTest(); lexerCharconTest(); lexerCharconTest2(); - printf("--- Lexer tests passed\n"); + printf(GREEN "--- Lexer tests passed\n" RESET); opRelTest(); opRelTest2(); @@ -22,7 +27,7 @@ int main(void) { declVarArrayBadInitCurly(); // fatorArrayMultTest(); - printf("--- Parser unit tests passed\n"); + printf(GREEN "--- Parser unit tests passed\n" RESET); progStartKeyword(); @@ -52,7 +57,7 @@ int main(void) { declDefProcProtoNoParenClose(); declDefProcProtoTwoProts(); - printf("--- Parser integration tests passed\n"); + printf(GREEN "--- Parser integration tests passed\n" RESET); return EXIT_SUCCESS; }