Needed for #86.
Signed-off-by: Martin Milata <mmilata(a)redhat.com>
---
include/report_type.h | 8 +++++++
lib/report.c | 45 ++++++++++++++++++++++++++-------------
tests/Makefile.am | 1 +
tests/report.at | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++
tests/testsuite.at | 1 +
5 files changed, 99 insertions(+), 15 deletions(-)
create mode 100644 tests/report.at
diff --git a/include/report_type.h b/include/report_type.h
index 8df81a2..a51620d 100644
--- a/include/report_type.h
+++ b/include/report_type.h
@@ -37,6 +37,14 @@ enum sr_report_type
SR_REPORT_NUM
};
+/* returns malloc()ed string representation of report_type */
+char *
+sr_report_type_to_string(enum sr_report_type report_type);
+
+/* inverse function */
+enum sr_report_type
+sr_report_type_from_string(const char *report_type_str);
+
#ifdef __cplusplus
}
#endif
diff --git a/lib/report.c b/lib/report.c
index 2b3ace4..92b77cf 100644
--- a/lib/report.c
+++ b/lib/report.c
@@ -33,6 +33,17 @@
#include <string.h>
#include <assert.h>
+static char *report_types[] =
+{
+ [SR_REPORT_INVALID] = "invalid",
+ [SR_REPORT_CORE] = "core",
+ [SR_REPORT_PYTHON] = "python",
+ [SR_REPORT_KERNELOOPS] = "kerneloops",
+ [SR_REPORT_JAVA] = "java",
+ [SR_REPORT_GDB] = "gdb",
+ NULL
+};
+
struct sr_report *
sr_report_new()
{
@@ -256,21 +267,25 @@ sr_report_to_json(struct sr_report *report)
return sr_strbuf_free_nobuf(strbuf);
}
-static enum sr_report_type
-report_type_from_string(const char *report_type_str)
+enum sr_report_type
+sr_report_type_from_string(const char *report_type_str)
+{
+ for (int i = SR_REPORT_INVALID; i < SR_REPORT_NUM; i++)
+ {
+ if (0 == sr_strcmp0(report_types[i], report_type_str))
+ return i;
+ }
+
+ return SR_REPORT_INVALID;
+}
+
+char *
+sr_report_type_to_string(enum sr_report_type report_type)
{
- assert(report_type_str);
-
- if (0 == strcmp("core", report_type_str))
- return SR_REPORT_CORE;
- else if (0 == strcmp("python", report_type_str))
- return SR_REPORT_PYTHON;
- else if (0 == strcmp("kerneloops", report_type_str))
- return SR_REPORT_KERNELOOPS;
- else if (0 == strcmp("java", report_type_str))
- return SR_REPORT_JAVA;
- else
- return SR_REPORT_INVALID;
+ if (report_type < SR_REPORT_INVALID || report_type >= SR_REPORT_NUM)
+ return sr_strdup("invalid");
+
+ return sr_strdup(report_types[report_type]);
}
struct sr_report *
@@ -332,7 +347,7 @@ sr_report_from_json(struct sr_json_value *root, char **error_message)
if (!success)
goto fail;
- report->report_type = report_type_from_string(report_type);
+ report->report_type = sr_report_type_from_string(report_type);
/* User. */
struct sr_json_value *user = json_element(root, "user");
diff --git a/tests/Makefile.am b/tests/Makefile.am
index cef50ce..d69f493 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -55,6 +55,7 @@ TESTSUITE_AT = \
cluster.at \
rpm.at \
abrt.at \
+ report.at \
python_bindings.at
EXTRA_DIST += $(TESTSUITE_AT)
diff --git a/tests/report.at b/tests/report.at
new file mode 100644
index 0000000..8736137
--- /dev/null
+++ b/tests/report.at
@@ -0,0 +1,59 @@
+# Checking the satyr. -*- Autotest -*-
+
+AT_BANNER([Report])
+
+## ------------------------ ##
+## sr_report_type_to_string ##
+## ------------------------ ##
+
+AT_TESTFUN([sr_report_type_to_string],
+[[
+#include <assert.h>
+#include "report_type.h"
+
+void check(enum sr_report_type type, char *str)
+{
+ assert(0 == sr_strcmp0(sr_report_type_to_string(type), str));
+}
+
+int main(void)
+{
+ check(SR_REPORT_INVALID, "invalid");
+ check(SR_REPORT_CORE, "core");
+ check(SR_REPORT_KERNELOOPS, "kerneloops");
+ check(SR_REPORT_GDB, "gdb");
+ check(SR_REPORT_NUM, "invalid");
+ check(5000, "invalid");
+ check(-42, "invalid");
+
+ return 0;
+}
+]])
+
+## -------------------------- ##
+## sr_report_type_from_string ##
+## -------------------------- ##
+
+AT_TESTFUN([sr_report_type_from_string],
+[[
+#include <assert.h>
+#include <stdio.h>
+#include "report_type.h"
+
+void check(enum sr_report_type type, char *str)
+{
+ assert(sr_report_type_from_string(str) == type);
+}
+
+int main(void)
+{
+ check(SR_REPORT_INVALID, "invalid");
+ check(SR_REPORT_CORE, "core");
+ check(SR_REPORT_PYTHON, "python");
+ check(SR_REPORT_GDB, "gdb");
+ check(SR_REPORT_INVALID, NULL);
+ check(SR_REPORT_INVALID, "visual basic");
+
+ return 0;
+}
+]])
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 39d9a94..904fe64 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -21,4 +21,5 @@ m4_include([metrics.at])
m4_include([cluster.at])
m4_include([rpm.at])
m4_include([abrt.at])
+m4_include([report.at])
m4_include([python_bindings.at])
--
1.8.3.1