dodo/tests/tests.c

155 lines
2.7 KiB
C
Raw Normal View History

2024-09-10 04:59:39 +00:00
#include <stdio.h>
2024-09-13 05:48:15 +00:00
#include <stdlib.h>
2024-09-10 04:59:39 +00:00
// printf Formating
#define X_RED "\x1B[31m"
#define X_GREEN "\x1B[32m"
#define X_RST "\x1B[0m"
// INSERT HEADERS FOR FUNCS
2024-09-13 05:48:15 +00:00
#include "sqlite3.h"
#include "x_string.h"
#include "x_curses.h"
#include "dodo.h"
2024-09-10 04:59:39 +00:00
// SET THE NUMBER OF TESTS
#define NUM_TESTS 5
2024-09-13 05:48:15 +00:00
// DEFINE REUSABLE TEST SETUPS
2024-09-13 05:48:15 +00:00
int set_up_db(sqlite3 **db){
char* home_dir = getenv("HOME");
char* filename = x_strconcat(home_dir, DB_PATH);
return opendb(db, filename);
}
2024-09-10 04:59:39 +00:00
// DEFINE TESTS
2024-09-13 05:48:15 +00:00
int test_add_new_task_no_date(){
printf("%s... ", __func__);
int rc = 0;
sqlite3 *db;
char *argv[3] = {"./dodo", "new", "jello"};
2024-09-13 05:48:15 +00:00
if ( (rc = set_up_db(&db)) ){
return rc;
}
return add_new_task(db, 3, argv);
}
int test_del_task(){
printf("%s... ", __func__);
int rc = 0;
sqlite3 *db;
char *argv[3] = {"./dodo", "del", "jello"};
2024-09-10 04:59:39 +00:00
2024-09-13 05:48:15 +00:00
if ( rc = set_up_db(&db) ){
return rc;
}
return del_task(db, 3, argv);
}
int test_add_new_task_with_date_and_delete(){
printf("%s... ", __func__);
int rc = 0;
sqlite3 *db;
char *argv_1[3] = {"./dodo", "new", "jello"};
char *argv_2[3] = {"./dodo", "del", "jello"};
2024-09-13 05:48:15 +00:00
if ( (rc = set_up_db(&db)) ){
return rc;
}
if ( rc = add_new_task(db, 3, argv_1) ){
2024-09-13 05:48:15 +00:00
return rc;
}
if ( rc = del_task(db, 3, argv_2) ){
2024-09-13 05:48:15 +00:00
return rc;
}
return 0;
}
2024-09-10 04:59:39 +00:00
int test_add_then_update_task_status_then_delete(){
printf("%s... ", __func__);
int rc = 0;
sqlite3 *db;
char *argv_1[3] = {"./dodo", "new", "jello"};
char *argv_2[3] = {"./dodo", "today", "jello"};
char *argv_3[3] = {"./dodo", "del", "jello"};
if ( (rc = set_up_db(&db)) ){
return rc;
}
if ( rc = add_new_task(db, 3, argv_1) ){
return rc;
}
if ( rc = update_task_status(db, 3, argv_2) ){
return rc;
}
if ( rc = del_task(db, 3, argv_3) ){
return rc;
}
return 0;
}
int test_update_task(){
printf("%s... ", __func__);
int rc = 0;
sqlite3 *db;
}
2024-09-10 04:59:39 +00:00
// DUMMY TEST
int fail(){
2024-09-13 05:48:15 +00:00
printf("%s... ", __func__);
2024-09-10 04:59:39 +00:00
return -1;
}
// END OF TEST DEFINITIONS
void run_tests(int (**tests)()){
int i = 0;
int errors = 0;
for ( i = 0; i < NUM_TESTS; i++ ){
2024-09-13 05:48:15 +00:00
printf("Test #%d: ", i);
2024-09-10 04:59:39 +00:00
if ( tests[i]() == 0){
printf("%spassed%s\n", X_GREEN, X_RST);
}
else {
printf("%sfailed%s\n", X_RED, X_RST);
errors++;
}
}
printf("Tests completed with %s%d passed%s", X_GREEN, NUM_TESTS - errors, X_RST);
printf(" and %s%d failed%s\n", X_RED, errors, X_RST);
}
int main(){
int (*tests[NUM_TESTS])();
// PASS TESTS INTO ARRAY
tests[0] = fail;
2024-09-13 05:48:15 +00:00
tests[1] = test_add_new_task_no_date;
tests[2] = test_del_task;
tests[3] = test_add_new_task_with_date_and_delete;
tests[4] = test_add_then_update_task_status_then_delete;
2024-09-10 04:59:39 +00:00
// END OF PASSING TESTS
// RUN TESTS
run_tests(tests);
return 0;
}