summaryrefslogtreecommitdiff
path: root/tests/msgs.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/msgs.c')
-rw-r--r--tests/msgs.c276
1 files changed, 276 insertions, 0 deletions
diff --git a/tests/msgs.c b/tests/msgs.c
new file mode 100644
index 0000000..37c8890
--- /dev/null
+++ b/tests/msgs.c
@@ -0,0 +1,276 @@
+#include "../src/msgs.c"
+
+#include "../src/testing.h"
+#include "slurp.h"
+
+
+
+enum TEST_MSGCATALOG_ID {
+ MSG_X_FIRST,
+ MSG_X_1,
+ MSG_X_2,
+ MSG_X_LAST,
+ MSG_STANDALONE,
+};
+
+static const char *const
+TEST_MSGS[] = {
+ [MSG_X_FIRST]="First line\n",
+ [MSG_X_1]="a second\n",
+ [MSG_X_2]="a third\n",
+ [MSG_X_LAST]="and the last one\n",
+ [MSG_STANDALONE]="single line message\n",
+ NULL
+};
+
+static size_t
+TEST_MSGS_LEN = nelem(TEST_MSGS);
+
+static const char
+FNAME[] = __FILE__ ".txt";
+
+
+
+static int
+test_msgs_init(void) {
+ int rc = -1;
+
+ test_start("msgs_init()");
+
+ {
+ testing("simple call without touching the environment");
+
+ const int should_overwrite = 1;
+ if (setenv(NLSPATH_KEY, "src/%N.en.cat", should_overwrite)) {
+ logerr("setenv(): %s", strerror(errno));
+ goto out;
+ }
+
+ if (msgs_init(TEST_MSGS)) {
+ logerr("msgs_init()");
+ goto out;
+ }
+ assert(msgs_length == TEST_MSGS_LEN - 1U);
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ if (msgs_end()) {
+ logerr("msgs_end()");
+ rc = -1;
+ }
+ return rc;
+}
+
+static int
+test_msgs_print_ids(void) {
+ int rc = -1;
+
+ test_start("msgs_print_ids()");
+ FILE *file = NULL;
+ char *str = NULL;
+
+ {
+ testing("message in range");
+
+ file = fopen(FNAME, "w");
+ if (file == NULL) {
+ perror("fopen(FNAME, \"w\")");
+ goto out;
+ }
+
+ if (msgs_print_ids(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)) {
+ logerr("msgs_print_ids()");
+ goto out;
+ }
+
+ const int ret = fclose(file);
+ file = NULL;
+ if (ret) {
+ logerr("fclose(): %s", strerror(errno));
+ goto out;
+ }
+
+ if (slurp_for_tests(FNAME, &str)) {
+ logerr("slurp_for_tests()");
+ goto out;
+ }
+
+ const char expected[] =
+ "First line\n"
+ "a second\n"
+ "a third\n"
+ "and the last one\n"
+ ;
+
+ assert(strcmp(expected, str) == 0);
+
+ free(str);
+ str = NULL;
+
+ test_ok();
+ }
+ {
+ testing("range begin and end is the same");
+
+ file = fopen(FNAME, "w");
+ if (!file) {
+ logerr("fopen(): %s", strerror(errno));
+ goto out;
+ }
+
+ if (msgs_print_ids(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)) {
+ logerr("msgs_print_ids()");
+ goto out;
+ }
+
+ const int ret = fclose(file);
+ file = NULL;
+ if (ret) {
+ logerr("fclose(): %s", strerror(errno));
+ goto out;
+ }
+
+ if (slurp_for_tests(FNAME, &str)) {
+ logerr("slurp_for_tests()");
+ goto out;
+ }
+
+ const char expected[] =
+ "First line\n";
+
+ assert(strcmp(expected, str) == 0);
+
+ free(str);
+ str = NULL;
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ if (str != NULL) {
+ free(str);
+ str = NULL;
+ }
+ if (file != NULL) {
+ if (fclose(file)) {
+ logerr("fclose(): %s", strerror(errno));
+ rc = -1;
+ }
+ }
+ return rc;
+}
+
+static int
+test_i18n_destroy(void) {
+ int rc = -1;
+
+ test_start("i18n_destroy()");
+
+ {
+ testing("simple call without init first");
+
+ if (msgs_end()) {
+ logerr("msgs_end()");
+ goto out;
+ }
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ return rc;
+}
+
+static int
+test_msgs_print_id(void) {
+ int rc = -1;
+
+ test_start("msgs_print_id()");
+ FILE *file = NULL;
+ char *str = NULL;
+
+ {
+ testing("simple individual message");
+
+ file = fopen(FNAME, "w");
+ if (!file) {
+ logerr("fopen(): %s", strerror(errno));
+ goto out;
+ }
+
+ if (msgs_print_id(TEST_MSGS, file, MSG_STANDALONE)) {
+ logerr("msgs_print_id()");
+ goto out;
+ }
+
+ const int ret = fclose(file);
+ file = NULL;
+ if (ret) {
+ logerr("fopen(): %s", strerror(errno));
+ goto out;
+ }
+
+ if (slurp_for_tests(FNAME, &str)) {
+ logerr("slurp_for_tests()");
+ goto out;
+ }
+
+ const char expected[] =
+ "single line message\n";
+
+ assert(strcmp(expected, str) == 0);
+
+ free(str);
+ str = NULL;
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ if (str != NULL) {
+ free(str);
+ str = NULL;
+ }
+ if (file != NULL) {
+ if (fclose(file)) {
+ logerr("fclose(): %s", strerror(errno));
+ rc = -1;
+ }
+ }
+ return rc;
+}
+
+int
+main(void) {
+ int rc = EXIT_FAILURE;
+
+ if (test_msgs_init()) {
+ logerr("test_msgs_init()");
+ goto out;
+ }
+
+ if (test_i18n_destroy()) {
+ logerr("test_i18n_destroy()");
+ goto out;
+ }
+
+ if (test_msgs_print_ids()) {
+ logerr("test_msgs_print_ids()");
+ goto out;
+ }
+
+ if (test_msgs_print_id()) {
+ logerr("test_msgs_print_id()");
+ goto out;
+ }
+
+ rc = EXIT_SUCCESS;
+out:
+ return rc;
+}