summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/administration.hpp1
-rw-r--r--libs/greatest/greatest.h6
-rw-r--r--src/administration.cpp7
-rw-r--r--src/administration_reader.cpp2
-rw-r--r--src/administration_writer.cpp45
-rw-r--r--tests/run_tests.cpp57
6 files changed, 73 insertions, 45 deletions
diff --git a/include/administration.hpp b/include/administration.hpp
index b782f7a..fd2d651 100644
--- a/include/administration.hpp
+++ b/include/administration.hpp
@@ -335,6 +335,7 @@ typedef struct
// Setup functions.
// =======================
void administration_create_empty(char* save_file);
+void administration_create_default(char* save_file);
void administration_create_from_file(char* save_file);
// Callback functions.
diff --git a/libs/greatest/greatest.h b/libs/greatest/greatest.h
index af0c053..58f12b5 100644
--- a/libs/greatest/greatest.h
+++ b/libs/greatest/greatest.h
@@ -777,7 +777,7 @@ clear: \
static void greatest_do_pass(void) { \
struct greatest_run_info *g = &greatest_info; \
if (GREATEST_IS_VERBOSE()) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, "PASS %s: %s", \
+ GREATEST_FPRINTF(GREATEST_STDOUT, "\033[0;32mPASS\033[0m %s: %s", \
g->name_buf, g->msg ? g->msg : ""); \
} else { \
GREATEST_FPRINTF(GREATEST_STDOUT, "."); \
@@ -789,7 +789,7 @@ static void greatest_do_fail(void) { \
struct greatest_run_info *g = &greatest_info; \
if (GREATEST_IS_VERBOSE()) { \
GREATEST_FPRINTF(GREATEST_STDOUT, \
- "FAIL %s: %s (%s:%u)", g->name_buf, \
+ "\033[0;31mFAIL\033[0m %s: %s (%s:%u)", g->name_buf, \
g->msg ? g->msg : "", g->fail_file, g->fail_line); \
} else { \
GREATEST_FPRINTF(GREATEST_STDOUT, "F"); \
@@ -798,7 +798,7 @@ static void greatest_do_fail(void) { \
GREATEST_FPRINTF(GREATEST_STDOUT, "\n"); \
g->col = 0; \
} \
- GREATEST_FPRINTF(GREATEST_STDOUT, "FAIL %s: %s (%s:%u)\n", \
+ GREATEST_FPRINTF(GREATEST_STDOUT, "\033[0;31mFAIL\033[0m %s: %s (%s:%u)\n", \
g->name_buf, g->msg ? g->msg : "", \
g->fail_file, g->fail_line); \
} \
diff --git a/src/administration.cpp b/src/administration.cpp
index c5f863b..30fbd76 100644
--- a/src/administration.cpp
+++ b/src/administration.cpp
@@ -500,11 +500,14 @@ void administration_create_empty(char* save_file)
strops_copy(g_administration.path, save_file, sizeof(g_administration.path));
strops_copy(g_administration.program_version, PROGRAM_VERSION, sizeof(g_administration.program_version));
- administration_company_info_set(administration_contact_create_empty());
+ administration_company_info_set(administration_contact_create_empty());
+}
+void administration_create_default(char* save_file)
+{
+ administration_create_empty(save_file);
administration_create_default_tax_brackets();
administration_create_default_cost_centers();
-
//administration_create_debug_data();
}
diff --git a/src/administration_reader.cpp b/src/administration_reader.cpp
index b3caeac..4fd1247 100644
--- a/src/administration_reader.cpp
+++ b/src/administration_reader.cpp
@@ -18,7 +18,7 @@ bool administration_reader_open_new()
if (!save_path) return false;
- administration_create_empty(save_path);
+ administration_create_default(save_path);
return true;
}
diff --git a/src/administration_writer.cpp b/src/administration_writer.cpp
index 4d4f5fe..618b36e 100644
--- a/src/administration_writer.cpp
+++ b/src/administration_writer.cpp
@@ -80,23 +80,14 @@ static char* administration_writer_copy_template(const char* template_str, int*
static bool administration_writer_entry_exists(char* entry)
{
- bool entry_exists = false;
struct zip_t *zip_read = zip_open(administration_file_path_get(), 0, 'r');
- int i, n = (int)zip_entries_total(zip_read);
- for (i = 0; i < n; ++i) {
- zip_entry_openbyindex(zip_read, i);
- const char *entry_name = zip_entry_name(zip_read);
- if (strcmp(entry_name, entry) == 0) {
- entry_exists = true;
- break;
- }
- zip_entry_close(zip_read);
- }
+ int result = zip_entry_open(zip_read, entry);
zip_close(zip_read);
- return entry_exists;
+
+ return result == 0;
}
-bool administration_writer_delete_entry(char* id)
+static bool _administration_writer_delete_entry_by_name(char* entry)
{
STOPWATCH_START;
@@ -104,41 +95,37 @@ bool administration_writer_delete_entry(char* id)
struct zip_t *zip_write = zip_open(administration_file_path_get(), 0, 'a');
if (!zip_write) zip_write = zip_open(administration_file_path_get(), 0, 'w');
- char final_path[50];
- snprintf(final_path, 50, "%s.xml", id);
-
- char* indices[1] = {final_path};
+ char* indices[1] = {entry};
if (zip_entries_delete(zip_write, indices, 1) < 0) result = 0;
zip_close(zip_write);
- log_add("Deleted entry '%s' in %.3fms.", id, STOPWATCH_TIME);
+ log_add("Deleted entry '%s' in %.3fms.", entry, STOPWATCH_TIME);
return result;
}
+bool administration_writer_delete_entry(char* id)
+{
+ char final_path[50];
+ snprintf(final_path, 50, "%s.xml", id);
+
+ return _administration_writer_delete_entry_by_name(final_path);
+}
+
static bool administration_writer_write_to_zip(char* entry_to_replace, char* orig_content, int final_length)
{
bool result = 1;
bool entry_exists = administration_writer_entry_exists(entry_to_replace);
+ if (entry_exists) _administration_writer_delete_entry_by_name(entry_to_replace);
struct zip_t *zip_write = zip_open(administration_file_path_get(), 0, 'a');
if (!zip_write) zip_write = zip_open(administration_file_path_get(), 0, 'w');
- if (entry_exists)
- {
- char* indices[1] = {entry_to_replace};
- zip_entries_delete(zip_write, indices, 1);
- zip_close(zip_write);
-
- zip_write = zip_open(administration_file_path_get(), 0, 'a');
- }
-
zip_entry_open(zip_write, entry_to_replace);
if (zip_entry_write(zip_write, orig_content, final_length) < 0) result = 0;
zip_entry_close(zip_write);
-
- zip_close(zip_write);
+ zip_close(zip_write);
return result;
}
diff --git a/tests/run_tests.cpp b/tests/run_tests.cpp
index 65ae47c..e9092a6 100644
--- a/tests/run_tests.cpp
+++ b/tests/run_tests.cpp
@@ -12,8 +12,18 @@
char* test_file_path = "C:\\Users\\aldri\\Desktop\\Vault\\Projects\\accounting\\build\\test.openbook";
-TEST _administration_rw_taxbrackets(void)
-{
+static void setup_cb(void *data) {
+ (void)data;
+ unlink(test_file_path);
+}
+
+static void teardown_cb(void *data) {
+ (void)data;
+ unlink(test_file_path);
+}
+
+TEST _administration_rw_taxbracket(void)
+{
country_tax_bracket pw;
country_tax_bracket pr;
u32 count;
@@ -43,8 +53,6 @@ TEST _administration_rw_taxbrackets(void)
ASSERT_EQ(pr.rate, pw.rate);
}
- unlink(test_file_path);
-
PASS();
}
@@ -77,8 +85,6 @@ TEST _administration_rw_costcenter(void)
ASSERT_STR_EQ(pr.description, pw.description);
}
- unlink(test_file_path);
-
PASS();
}
@@ -86,6 +92,7 @@ TEST _administration_rw_project(void)
{
project pw;
project pr;
+ u32 count;
administration_writer_create();
@@ -93,28 +100,58 @@ TEST _administration_rw_project(void)
{
pw = administration_project_create_empty();
strops_copy(pw.description, "Test Project", sizeof(pw.description));
+ count = administration_project_count();
administration_project_add(pw);
- ASSERT_EQ(1, administration_project_count());
+ ASSERT_EQ(count+1, administration_project_count());
}
administration_reader_open_existing(test_file_path);
{
- ASSERT_EQ(1, administration_project_count());
+ ASSERT_EQ(count+1, administration_project_count());
ASSERT_EQ(1, administration_project_get_by_id(&pr, pw.id));
ASSERT_STR_EQ(pr.id, pw.id);
ASSERT_STR_EQ(pr.description, pw.description);
}
- unlink(test_file_path);
+ PASS();
+}
+
+TEST _administration_rw_contact(void)
+{
+ contact pw;
+ contact pr;
+ u32 count;
+
+ administration_writer_create();
+
+ administration_create_empty(test_file_path);
+ {
+ pw = administration_contact_create_empty();
+ count = administration_contact_count();
+ administration_contact_add(pw);
+
+ ASSERT_EQ(count+1, administration_contact_count());
+ }
+
+ administration_reader_open_existing(test_file_path);
+ {
+ ASSERT_EQ(count+1, administration_contact_count());
+ ASSERT_EQ(1, administration_contact_get_by_id(&pr, pw.id));
+ ASSERT_STR_EQ(pr.id, pw.id);
+ }
PASS();
}
SUITE(administration_io) {
- RUN_TEST(_administration_rw_taxbrackets);
+ SET_SETUP(setup_cb, NULL);
+ SET_TEARDOWN(teardown_cb, NULL);
+
+ RUN_TEST(_administration_rw_taxbracket);
RUN_TEST(_administration_rw_costcenter);
RUN_TEST(_administration_rw_project);
+ RUN_TEST(_administration_rw_contact);
}
GREATEST_MAIN_DEFS();