Skip to content

Commit c43b8a5

Browse files
committed
Add beginning of testing harness
Added the beginnings of a testing harness for Liszt. See the 'tests' directory for me. Needs a lot of work.
1 parent 341749d commit c43b8a5

15 files changed

+415
-37
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
set(CMAKE_CXX_COMPILER "clang")
22

3+
set(CMAKE_BUILD_TYPE Debug)
4+
35
cmake_minimum_required(VERSION 3.10)
46

57
project(lizst VERSION 1.1.8)

src/install.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ int makeFiles() {
2828
// Make data_file.json
2929
char dataFilePath[MAX_LENGTH];
3030
strcpy(dataFilePath, tilde);
31-
strcat(dataFilePath, "/.liszt/background/data_file.json");
31+
strcat(dataFilePath, "/.liszt/background/data_file");
3232
toCreate = fopen(dataFilePath, "w");
3333
fclose(toCreate);
3434
if (stat(dataFilePath, &st) == -1) {
3535
printf("\033[1mLiszt\033[0m installation unsuccessful. Failed to install '%s'\n", dataFilePath);
3636
printf("Please try again later.\n");
3737
return -1;
38-
} else printf("Created ~/.liszt/background/data_file.json file\n");
38+
} else printf("Created ~/.liszt/background/data_file file\n");
3939

4040
// Make default file
4141
char defaultFilePath[MAX_LENGTH];

src/memory.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,20 @@ void clearMemories() {
156156
char prompt[] = "Are you sure you want to clear your memories on your current note? \033[1mThere is no going back (y/n): \033[0m";
157157
char decision[50];
158158
requestUserPermission(prompt, decision);
159-
if (strcmp(decision, "y") == 0) {
159+
if (!strcmp(decision, "y")) {
160160
fclose(fopen(current_note_path, "w"));
161161
printf("Memories cleared\n");
162162
} else printf("Memory clearing aborted\n");
163+
free(note);
164+
free(current_note_path);
163165
}
164166

165167

166168
void addMemory(char* args[], int numArgs) {
167-
char* current_note_path;
168-
char *note = getCurrentNote(&current_note_path);
169-
169+
char* current_note_path = getCurrentNotePath();
170170
char *sans_first_word = parseUnaryArgs(args, numArgs);
171171
if (sans_first_word == NULL) {
172-
appendMemory(args[1], note);
172+
appendMemory(args[1], current_note_path);
173173
printf("Remembered '%s'\n", args[1]);
174174
} else {
175175
char memory[MAX_LENGTH];
@@ -179,10 +179,11 @@ void addMemory(char* args[], int numArgs) {
179179
strcat(memory, " ");
180180
strcat(memory, sans_first_word);
181181

182-
appendMemory(memory, note);
182+
appendMemory(memory, current_note_path);
183183
printf("Remembered '%s'\n", memory);
184184
}
185185
free(sans_first_word);
186+
free(current_note_path);
186187
}
187188

188189

@@ -198,6 +199,8 @@ void listMemories() {
198199
int numLines = getFileSize(note_path);
199200
if (numLines == 0) {
200201
printf("You have no memories on this note\n");
202+
free(note_path);
203+
free(note_name);
201204
return;
202205
}
203206
printf("\033[1m\033[3mFound %d memories on '%s'\033[0m\n", numLines, note_name);
@@ -213,5 +216,7 @@ void listMemories() {
213216
}
214217

215218
fclose(toRead);
219+
free(note_path);
220+
free(note_name);
216221
}
217222

src/note.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,13 @@ void addNote(char* args[], int numArgs) {
283283

284284
result = makeNote(note_path);
285285
if (result == -1) {
286+
free(note);
287+
free(note_path);
286288
return;
287289
}
288290
result = changeNoteHelper(note);
289291
printf("Added new note '%s'\n", note);
292+
free(note);
290293
free(note_path);
291294
}
292295

@@ -459,11 +462,19 @@ int changeNoteHelper(char* note) {
459462
// stop if the note to be changed to is the current note
460463
if (!strcmp(current_note_name, note)) {
461464
printf("The note you are trying to change to is already the current note.\n");
465+
free(current_note_path);
466+
free(current_note_name);
467+
free(data_file);
462468
return -1;
463469
}
464470
// stop if the note to be changed to is 'default'
465471
int result = checkDefault(note);
466-
if (result == -1) return -1;
472+
if (result == -1) {
473+
free(current_note_path);
474+
free(current_note_name);
475+
free(data_file);
476+
return -1;
477+
}
467478

468479
struct stat st = {0};
469480

@@ -472,10 +483,14 @@ int changeNoteHelper(char* note) {
472483

473484
if (stat(note_path, &st) == -1) {
474485
printf("Hmmm. The note you entered doesn't seem to exist. Please try again.\n");
486+
free(current_note_path);
487+
free(current_note_name);
488+
free(data_file);
489+
free(note_path);
475490
return -1;
476491
}
477492

478-
if (strcmp(current_note_name, "default") == 0) {
493+
if (!strcmp(current_note_name, "default")) {
479494
int numMemories = getFileSize(current_note_path);
480495
if (numMemories > 0) {
481496
char new_note[MAX_LENGTH];
@@ -487,7 +502,14 @@ int changeNoteHelper(char* note) {
487502
char *new_note_path = getNotePath(dirName, new_note);
488503

489504
result = makeNote(new_note_path);
490-
if (result == -1) return -1;
505+
if (result == -1) {
506+
free(current_note_path);
507+
free(current_note_name);
508+
free(data_file);
509+
free(note_path);
510+
free(new_note_path);
511+
return -1;
512+
}
491513
copyFile(current_note_path, new_note_path);
492514
// the following is for clearing 'default'
493515
FILE* toClear = fopen(current_note_path, "w");
@@ -500,6 +522,7 @@ int changeNoteHelper(char* note) {
500522
free(note_path);
501523
free(current_note_path);
502524
free(current_note_name);
525+
free(data_file);
503526
return 0;
504527
}
505528

src/util.c

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,32 @@ char *getCurrentNote(char **current_note_path) {
5252
return current_note_name;
5353
}
5454

55+
5556
char *getCurrentNotePath() {
5657
char *data_file = getDataFile();
5758

58-
FILE *toRead;
59-
toRead = fopen(data_file, "r");
60-
char line[MAX_LENGTH];
61-
char dataStream[MAX_LENGTH];
59+
FILE *to_read;
60+
to_read = fopen(data_file, "r");
61+
//char dataStream[MAX_LENGTH];
6262

63-
fgets(line, sizeof(line), toRead);
64-
strcpy(dataStream, line);
65-
while (fgets(line, sizeof(line), toRead)) {
63+
//fgets(line, sizeof(line), toRead);
64+
//strcpy(dataStream, line);
65+
char line[MAX_LENGTH];
66+
char *current_note_path = strdup(fgets(line, MAX_LENGTH * sizeof(char), to_read));
67+
/*
68+
while (fgets(line, MAX_LENGTH * sizeof(char), toRead)) {
6669
strcat(dataStream, line);
6770
}
6871
fclose(toRead);
6972
7073
cJSON *data = cJSON_Parse(dataStream);
7174
cJSON *currentNote = cJSON_GetObjectItem(data, "current_note");
7275
char *current_note_path = cJSON_PrintUnformatted(currentNote);
73-
current_note_path[strlen(current_note_path) + 1] = '\0';
74-
cJSON_Delete(data);
76+
*/
77+
current_note_path[strlen(current_note_path)] = '\0'; // to get rid of the newline
78+
//cJSON_Delete(data);
7579
free(data_file);
80+
fclose(to_read);
7681
return current_note_path;
7782
}
7883

@@ -104,7 +109,7 @@ void setToDefault() {
104109

105110

106111
char *getDataFile() {
107-
return getNotePath("background", "data_file.json");
112+
return getNotePath("background", "data_file");
108113
}
109114

110115

@@ -201,26 +206,27 @@ long checkRow(char *filename, char *char_row) {
201206
return row;
202207
}
203208

204-
209+
// the following this screwed up (intentionally)
210+
// needs to be fixed for the purpose of adding collections
205211
void overwriteFilenameToDataFile(char *filename, char *dirname) {
206212
// get path to data_file.json
207213
char *data_file = getDataFile();
208214

209-
cJSON *data = cJSON_CreateObject();
210-
cJSON *note = cJSON_CreateString(filename);
211-
cJSON_AddItemToObject(data, "current_note", note);
215+
//cJSON *data = cJSON_CreateObject();
216+
//cJSON *note = cJSON_CreateString(filename);
217+
//cJSON_AddItemToObject(data, "current_note", note);
212218

213-
cJSON *collection = cJSON_CreateString(dirname);
214-
cJSON_AddItemToObject(data, "current_collection", collection);
219+
//cJSON *collection = cJSON_CreateString(dirname);
220+
//cJSON_AddItemToObject(data, "current_collection", collection);
215221

216-
char *stringJSON = cJSON_Print(data);
222+
//char *stringJSON = cJSON_Print(data);
217223

218224
FILE *toWrite;
219225
toWrite = fopen(data_file, "w");
220-
fprintf(toWrite, "%s", stringJSON);
226+
fprintf(toWrite, "%s", filename);
221227
fclose(toWrite);
222228

223-
cJSON_Delete(data);
229+
//cJSON_Delete(data);
224230
free(data_file);
225231

226232
}
@@ -232,6 +238,7 @@ void writeFilenameToDataFile(char *filename) {
232238

233239
// open data_file and read contents into dataStream
234240
// should be decomposed
241+
/*
235242
FILE *toRead;
236243
toRead = fopen(data_file, "r");
237244
char line[MAX_LENGTH];
@@ -252,13 +259,14 @@ void writeFilenameToDataFile(char *filename) {
252259
cJSON_ReplaceItemInObjectCaseSensitive(data, "current_note", file);
253260
254261
char *newStream = cJSON_Print(data);
262+
*/
255263

256264
FILE *toWrite;
257265
toWrite = fopen(data_file, "w");
258-
fprintf(toWrite, "%s", newStream);
266+
fprintf(toWrite, "%s", filename);
259267
fclose(toWrite);
260268

261-
cJSON_Delete(data);
269+
// cJSON_Delete(data);
262270
free(data_file);
263271
}
264272

@@ -355,8 +363,7 @@ int makeNote(char *filePath) {
355363
printf("A note with this name already exists. Please choose a different name, delete the other note, or rename the other note.\n");
356364
return -1;
357365
}
358-
FILE *toCreate;
359-
toCreate = fopen(filePath, "w");
366+
FILE *toCreate = fopen(filePath, "w");
360367
fclose(toCreate);
361368
return 0;
362369
}

src/util.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#ifndef HELPER_FUNC_H
2-
#define HELPER_FUNC_H
1+
#ifndef UTIL_H
2+
#define UTIL_H
33
/*
44
* This file contains function declarations
55
* for helper functions for the Liszt project.
6-
* See helper_func.c for the definitions.
6+
* See util.c for the definitions.
77
*/
88

99

tests/note_tests.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include "note_tests.h"
5+
#include "test_counter.h"
6+
7+
void test_add_note(test_counter_t *count) {
8+
char *expected = "Added new note 'random'\n";
9+
system("../lst -a random > test");
10+
11+
FILE *fp = fopen("test", "r");
12+
char line[MAX_LINE_LEN];
13+
fgets(line, MAX_LINE_LEN * sizeof(char), fp);
14+
15+
if (strcmp(expected, line)) {
16+
printf("%s%sERROR%s: %s\n", BOLD, RED, RESET, line);
17+
printf("%sEXPECTED:%s %s\n", BOLD, RESET, expected);
18+
} else {
19+
printf("%s%sPASS%s: %s", BOLD, BLUE, RESET, expected);
20+
(count->num_tests_passed)++;
21+
}
22+
fclose(fp);
23+
(count->total_tests)++;
24+
}
25+
26+
void test_listing_notes(test_counter_t *count) {
27+
/*
28+
char expected[MAX_LINE_LEN];
29+
30+
scandir
31+
32+
fclose(fp);
33+
strcpy(expected, BOLD);
34+
strcat(expected, ITALICS);
35+
strcat(expected, "Found 1 memories on '");
36+
strcat(expected, line);
37+
strcat(expected, "'");
38+
strcat(expected, RESET);
39+
strcat(expected, "\n");
40+
strcat(expected, BOLD);
41+
strcat(expected, "1.");
42+
strcat(expected, RESET);
43+
strcat(expected, " trying to add a sentence with multiple words\n");
44+
system("../lst -l > test");
45+
fp = fopen("test", "r");
46+
char multi_lines[MAX_LINE_LEN * 2];
47+
strcpy(multi_lines, fgets(line, MAX_LINE_LEN * sizeof(char), fp));
48+
strcat(multi_lines, fgets(line, MAX_LINE_LEN * sizeof(char), fp));
49+
if (strcmp(expected, multi_lines)) {
50+
printf("%s%sERROR%s: %s\n", BOLD, RED, RESET, multi_lines);
51+
printf("%sEXPECTED:%s %s\n", BOLD, RESET, expected);
52+
} else {
53+
printf("%s%sPASS%s: %s", BOLD, BLUE, RESET, expected);
54+
(count->num_tests_passed)++;
55+
}
56+
(count->total_tests)++;
57+
fclose(fp);
58+
*/
59+
}
60+
61+
62+
/*
63+
void test_removing_thoughts(test_counter_t *count) {
64+
char *expected = "Removed memory 'trying to add a sentence with multiple words'\n";
65+
char line[MAX_LINE_LEN];
66+
system("../lst -r 1 > test");
67+
68+
FILE *fp = fopen("test", "r");
69+
fgets(line, MAX_LINE_LEN * sizeof(char), fp);
70+
71+
if (strcmp(expected, line)) {
72+
printf("%s%sERROR%s: %s\n", BOLD, RED, RESET, line);
73+
printf("%sEXPECTED:%s %s\n", BOLD, RESET, expected);
74+
} else {
75+
printf("%s%sPASS%s: %s", BOLD, BLUE, RESET, expected);
76+
(count->num_tests_passed)++;
77+
}
78+
fclose(fp);
79+
(count->total_tests)++;
80+
}
81+
*/

0 commit comments

Comments
 (0)