summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2018-05-17 10:55:21 +0200
committerSven Eden <yamakuzure@gmx.net>2018-08-24 16:47:08 +0200
commit0d3fd8ae03eb180e1309f8b0bfd2fbce6c3cfdcc (patch)
treea7c87610bcd4b82b6d140f791f00c784b2f5996a /src/test
parentcfbe7aeb009b9e220fde7ee61424e77fc37f18f6 (diff)
basic/string-util: add a convenience function to cescape mostly-ascii fields
It's not supposed to be the most efficient, but instead fast and simple to use. I kept the logic in ellipsize_mem() to use unicode ellipsis even in non-unicode locales. I'm not quite convinced things should be this way, especially that with this patch it'd actually be simpler to always use "…" in unicode locale and "..." otherwise, but Lennart wanted it this way for some reason.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test-string-util.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c
index a7c863c89..8c7226e5f 100644
--- a/src/test/test-string-util.c
+++ b/src/test/test-string-util.c
@@ -6,6 +6,7 @@
***/
#include "alloc-util.h"
+//#include "locale-util.h"
#include "macro.h"
#include "string-util.h"
#include "strv.h"
@@ -79,6 +80,29 @@ static void test_ascii_strcasecmp_nn(void) {
}
#endif // 0
+static void test_cellescape(void) {
+ char buf[40];
+
+ assert_se(streq(cellescape(buf, 10, "1"), "1"));
+ assert_se(streq(cellescape(buf, 10, "12"), "12"));
+ assert_se(streq(cellescape(buf, 10, "123"), is_locale_utf8() ? "1…" : "1..."));
+
+ assert_se(streq(cellescape(buf, 10, "1\011"), "1\\t"));
+ assert_se(streq(cellescape(buf, 10, "1\020"), "1\\020"));
+ assert_se(streq(cellescape(buf, 10, "1\020x"), is_locale_utf8() ? "1…" : "1..."));
+
+ assert_se(streq(cellescape(buf, 40, "1\020"), "1\\020"));
+ assert_se(streq(cellescape(buf, 40, "1\020x"), "1\\020x"));
+
+ assert_se(streq(cellescape(buf, 40, "\a\b\f\n\r\t\v\\\"'"), "\\a\\b\\f\\n\\r\\t\\v\\\\\\\"\\'"));
+ assert_se(streq(cellescape(buf, 10, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a..."));
+ assert_se(streq(cellescape(buf, 11, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a..."));
+ assert_se(streq(cellescape(buf, 12, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a\\b…" : "\\a\\b..."));
+
+ assert_se(streq(cellescape(buf, sizeof buf, "1\020"), "1\\020"));
+ assert_se(streq(cellescape(buf, sizeof buf, "1\020x"), "1\\020x"));
+}
+
static void test_streq_ptr(void) {
assert_se(streq_ptr(NULL, NULL));
assert_se(!streq_ptr("abc", "cdef"));
@@ -432,6 +456,7 @@ int main(int argc, char *argv[]) {
test_ascii_strcasecmp_n();
test_ascii_strcasecmp_nn();
#endif // 0
+ test_cellescape();
test_streq_ptr();
test_strstrip();
test_strextend();