diff --git a/format/desktop_entry/desktop_entry_test.ha b/format/desktop_entry/desktop_entry_test.ha new file mode 100644 index 0000000..87fe8c7 --- /dev/null +++ b/format/desktop_entry/desktop_entry_test.ha @@ -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; +};