tests: compress unit name tests and add more asserts

parent 7742f7e9
...@@ -27,151 +27,75 @@ ...@@ -27,151 +27,75 @@
#include "util.h" #include "util.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
char *t, *k;
#define expect(pattern, repl, expected) \
t = unit_name_replace_instance("foo@.service", "waldo"); { \
puts(t); char _cleanup_free_ *t = \
free(t); unit_name_replace_instance(pattern, repl); \
puts(t); \
t = unit_name_replace_instance("foo@xyz.service", "waldo"); assert(streq(t, expected)); \
puts(t); }
free(t);
expect("foo@.service", "waldo", "foo@waldo.service");
t = unit_name_replace_instance("xyz", "waldo"); expect("foo@xyz.service", "waldo", "foo@waldo.service");
puts(t); expect("xyz", "waldo", "xyz");
free(t); expect("", "waldo", "");
expect("foo.service", "waldo", "foo.service");
t = unit_name_replace_instance("", "waldo"); expect(".service", "waldo", ".service");
puts(t); expect("foo@", "waldo", "foo@waldo");
free(t); expect("@bar", "waldo", "@waldo");
t = unit_name_replace_instance("", ""); puts("-------------------------------------------------");
puts(t); #undef expect
free(t); #define expect(path, suffix, expected) \
{ \
t = unit_name_replace_instance("foo.service", "waldo"); char _cleanup_free_ *k, *t = \
puts(t); unit_name_from_path(path, suffix); \
free(t); puts(t); \
k = unit_name_to_path(t); \
t = unit_name_replace_instance(".service", "waldo"); puts(k); \
puts(t); assert(streq(k, expected ? expected : path)); \
free(t); }
t = unit_name_replace_instance("foo@bar", "waldo"); expect("/waldo", ".mount", NULL);
puts(t); expect("/waldo/quuix", ".mount", NULL);
free(t); expect("/waldo/quuix/", ".mount", "/waldo/quuix");
expect("/", ".mount", NULL);
t = unit_name_replace_instance("foo@", "waldo"); expect("///", ".mount", "/");
puts(t);
free(t); puts("-------------------------------------------------");
#undef expect
t = unit_name_replace_instance("@", "waldo"); #define expect(pattern, path, suffix, expected) \
puts(t); { \
free(t); char _cleanup_free_ *t = \
unit_name_from_path_instance(pattern, path, suffix); \
t = unit_name_replace_instance("@bar", "waldo"); puts(t); \
puts(t); assert(streq(t, expected)); \
free(t); }
t = unit_name_from_path("/waldo", ".mount"); expect("waldo", "/waldo", ".mount", "waldo@waldo.mount");
puts(t); expect("waldo", "/waldo////quuix////", ".mount", "waldo@waldo-quuix.mount");
k = unit_name_to_path(t); expect("waldo", "/", ".mount", "waldo@-.mount");
puts(k); expect("wa--ldo", "/--", ".mount", "wa--ldo@\\x2d\\x2d.mount");
free(k);
free(t); puts("-------------------------------------------------");
#undef expect
t = unit_name_from_path("/waldo/quuix", ".mount"); #define expect(pattern) \
puts(t); { \
k = unit_name_to_path(t); char _cleanup_free_ *k, *t; \
puts(k); assert_se(t = unit_name_mangle(pattern)); \
free(k); assert_se(k = unit_name_mangle(t)); \
free(t); puts(t); \
assert_se(streq(t, k)); \
t = unit_name_from_path("/waldo/quuix/", ".mount"); }
puts(t);
k = unit_name_to_path(t); expect("/home");
puts(k); expect("/dev/sda");
free(k); expect("üxknürz.service");
free(t); expect("foobar-meh...waldi.service");
expect("_____####----.....service");
t = unit_name_from_path("/", ".mount"); expect("_____##@;;;,,,##----.....service");
puts(t); expect("xxx@@@@/////\\\\\\\\\\yyy.service");
k = unit_name_to_path(t);
puts(k);
free(k);
free(t);
t = unit_name_from_path("///", ".mount");
puts(t);
k = unit_name_to_path(t);
puts(k);
free(k);
free(t);
t = unit_name_from_path_instance("waldo", "/waldo", ".mount");
puts(t);
free(t);
t = unit_name_from_path_instance("waldo", "/waldo////quuix////", ".mount");
puts(t);
free(t);
t = unit_name_from_path_instance("waldo", "/", ".mount");
puts(t);
free(t);
t = unit_name_from_path_instance("wa--ldo", "/--", ".mount");
puts(t);
free(t);
assert_se(t = unit_name_mangle("/home"));
assert_se(k = unit_name_mangle(t));
puts(t);
assert_se(streq(t, k));
free(t);
free(k);
assert_se(t = unit_name_mangle("/dev/sda"));
assert_se(k = unit_name_mangle(t));
puts(t);
assert_se(streq(t, k));
free(t);
free(k);
assert_se(t = unit_name_mangle("üxknürz.service"));
assert_se(k = unit_name_mangle(t));
puts(t);
assert_se(streq(t, k));
free(t);
free(k);
assert_se(t = unit_name_mangle("foobar-meh...waldi.service"));
assert_se(k = unit_name_mangle(t));
puts(t);
assert_se(streq(t, k));
free(t);
free(k);
assert_se(t = unit_name_mangle("_____####----.....service"));
assert_se(k = unit_name_mangle(t));
puts(t);
assert_se(streq(t, k));
free(t);
free(k);
assert_se(t = unit_name_mangle("_____##@;;;,,,##----.....service"));
assert_se(k = unit_name_mangle(t));
puts(t);
assert_se(streq(t, k));
free(t);
free(k);
assert_se(t = unit_name_mangle("xxx@@@@/////\\\\\\\\\\yyy.service"));
assert_se(k = unit_name_mangle(t));
puts(t);
assert_se(streq(t, k));
free(t);
free(k);
return 0; return 0;
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment