format::desktop_entry: Add tests for file parsing

This commit is contained in:
Sasha Koshka 2024-10-22 13:17:21 -04:00
parent 0bfa8f6914
commit 2033df5335

View File

@ -0,0 +1,93 @@
use fmt;
use io;
use locale;
use os;
@test fn parse() void = {
let file = os::open("format/desktop_entry/test_data/foo.desktop")!;
defer io::close(file)!;
let file = parse(file)!;
defer file_finish(&file);
assert(file.typ == "Application");
assert(file.version == "1.0");
assert(locale_string_equal(file.name, [
(locale::c, "Foo Viewer"),
]));
assert(locale_string_equal(file.generic_name, [])); // TODO
assert(file.no_display == false);
assert(locale_string_equal(file.comment, [
(locale::c, "The best viewer for Foo objects available!"),
]));
assert(locale_string_equal(file.icon, [
(locale::c, "fooview"),
]));
assert(file.hidden == false);
assert(strings_equal(file.only_show_in, [])); // TODO
assert(strings_equal(file.not_show_in, [])); // TODO
assert(file.dbus_activatable == false);
assert(file.try_exec == "fooview");
assert(file.exec == "fooview %F");
assert(file.path == "");
assert(file.terminal == false);
assert(strings_equal(file.mime_type, ["image/x-foo"]));
assert(strings_equal(file.categories, [])); // TODO
assert(strings_equal(file.implements, [])); // TODO
assert(locale_strings_equal(file.keywords, [])); // TODO
assert(file.startup_notify == false);
assert(file.url == "");
assert(file.prefers_non_default_gpu == false);
assert(file.single_main_window == false);
for (let actio .. file.actions) switch (actio.key) {
case "Gallery" =>
assert(locale_string_equal(actio.name, [
(locale::c, "Browse Gallery"),
]));
assert(locale_string_equal(actio.icon, [])); // TODO
assert(actio.exec == "fooview --gallery");
case "Create" =>
assert(locale_string_equal(actio.name, [
(locale::c, "Create a new Foo!"),
(locale::parse("en_US")!, "Create a new Foo!"),
(locale::parse("xx_XX.UTF-8")!, "Zweep zoop flooble glorp"),
]));
assert(locale_string_equal(actio.icon, [
(locale::c, "fooview-new"),
]));
assert(actio.exec == "fooview --create-new");
case =>
fmt::println("unexpected action", actio.key)!;
abort("unexpected action");
};
};
fn strings_equal(a: []str, b: []str) bool = {
if (len(a) != len(b)) return false;
for (let index = 0z; index < len(a); index += 1) {
if (a[index] != b[index]) return false;
};
return true;
};
fn locale_string_equal(a: locale::string, b: locale::string) bool = {
if (len(a) != len(b)) return false;
for (let index = 0z; index < len(a); index += 1) {
let a = a[index];
let b = b[index];
if (!locale::equal(a.0, b.0)) return false;
if (a.1 != b.1 ) return false;
};
return true;
};
fn locale_strings_equal(a: locale::strings, b: locale::strings) bool = {
if (len(a) != len(b)) return false;
for (let index = 0z; index < len(a); index += 1) {
let a = a[index];
let b = b[index];
if (!locale::equal(a.0, b.0)) return false;
if (!strings_equal(a.1, b.1)) return false;
};
return true;
};