dodo/tests/tests.c

207 lines
3.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
// printf Formating
#define X_RED "\x1B[31m"
#define X_GREEN "\x1B[32m"
#define X_RST "\x1B[0m"
// INSERT HEADERS FOR FUNCS
#include "sqlite3.h"
#include "x_string.h"
#include "x_curses.h"
#include "dodo.h"
// SET THE NUMBER OF TESTS
#define NUM_TESTS 8
// DEFINE REUSABLE TEST SETUPS
int set_up_db(sqlite3 **db){
char* home_dir = getenv("HOME");
char* filename = x_strconcat(home_dir, DB_PATH);
return opendb(db, filename);
}
// DEFINE TESTS
int test_add_new_task_no_date(){
printf("%s... ", __func__);
int rc = 0;
sqlite3 *db;
char *argv[3] = {"./dodo", "new", "jello"};
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"};
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"};
if ( (rc = set_up_db(&db)) ){
return rc;
}
if ( rc = add_new_task(db, 3, argv_1) ){
return rc;
}
if ( rc = del_task(db, 3, argv_2) ){
return rc;
}
return 0;
}
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_display_task(){
printf("%s... ", __func__);
FILE *fd1;
FILE *stdout_org;
sqlite3 *db;
int rc = 0;
if ( (rc = set_up_db(&db)) ){
return rc;
}
stdout_org = stdout;
errno = 0;
fd1 = fopen("/dev/null", "w");
if(fd1 == NULL){
return -1;
}
stdout = fd1;
if ( (rc = view_tasks(db)) ){
return rc;
}
stdout = stdout_org;
fclose(fd1);
return 0;
}
int test_update_task(){
printf("%s... ", __func__);
int rc = 0;
sqlite3 *db;
char *argv_1[4] = {"./dodo", "update", "jello", "hello"};
if ( (rc = set_up_db(&db)) ){
return rc;
}
return update_task(db, 4, argv_1);
}
int test_date_difference(){
printf("%s... ", __func__);
char d[10] = "10-10-2024";
set_color_of_date(d);
return 1;
}
// DUMMY TEST
int fail(){
printf("%s... ", __func__);
return -1;
}
// END OF TEST DEFINITIONS
void run_tests(int (**tests)()){
int i = 0;
int errors = 0;
for ( i = 0; i < NUM_TESTS; i++ ){
printf("Test #%d: ", i);
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;
tests[1] = test_update_task;
tests[2] = test_add_new_task_no_date;
tests[3] = test_del_task;
tests[4] = test_add_new_task_with_date_and_delete;
tests[5] = test_add_then_update_task_status_then_delete;
tests[6] = test_date_difference;
tests[7] = test_display_task;
// END OF PASSING TESTS
// RUN TESTS
run_tests(tests);
return 0;
}