From 02cf7ffb05288a9977d035f132e03c72b7e43d5c Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 20 Oct 2024 18:40:24 -0400 Subject: [PATCH] Add tests for format::desktop_entry --- format/desktop_entry/scan_test.ha | 145 +++++++++++++++++++++ format/desktop_entry/test_data/foo.desktop | 26 ++++ 2 files changed, 171 insertions(+) create mode 100644 format/desktop_entry/scan_test.ha create mode 100644 format/desktop_entry/test_data/foo.desktop diff --git a/format/desktop_entry/scan_test.ha b/format/desktop_entry/scan_test.ha new file mode 100644 index 0000000..dc145fc --- /dev/null +++ b/format/desktop_entry/scan_test.ha @@ -0,0 +1,145 @@ +use fmt; +use io; +use locale; +use os; + +@test fn next() void = { + let h_de = "Desktop Entry"; + let h_dag = "Desktop Action Gallery"; + let h_dac = "Desktop Action Create"; + let correct: []line = [ + "This is a comment": comment, + h_de: group_header, + entry_new(h_de, "Version", "1.0", locale::c), + entry_new(h_de, "Type", "Application", locale::c), + entry_new(h_de, "Name", "Foo Viewer", locale::c), + entry_new(h_de, "Comment", "The best viewer for Foo objects available!", locale::c), + entry_new(h_de, "TryExec", "fooview", locale::c), + entry_new(h_de, "Exec", "fooview %F", locale::c), + blank, + entry_new(h_de, "Icon", "fooview", locale::c), + entry_new(h_de, "MimeType", "image/x-foo;", locale::c), + entry_new(h_de, "Actions", "Gallery;Create;", locale::c), + blank, + h_dag: group_header, + entry_new(h_dag, "Exec", "fooview --gallery", locale::c), + entry_new(h_dag, "Name", "Browse Gallery", locale::c), + blank, + h_dac: group_header, + entry_new(h_dac, "Exec", "fooview --create-new", locale::c), + entry_new(h_dac, "Name", "Create a new Foo!", locale::c), + entry_new( + h_dac, "Name", "Create a new Foo!", + locale::parse("en_US")!), + entry_new( + h_dac, "Name", "Zweep zoop flooble glorp", + locale::parse("xx_XX.UTF-8")!), + "Another comment": comment, + entry_new(h_dac, "Icon", "fooview-new", locale::c), + ]; + + let file = os::open("format/desktop_entry/test_data/foo.desktop")!; + defer io::close(file)!; + let scanne = scan(file); + defer finish(&scanne); + + for (let correct .. correct) match(next(&scanne)!) { + case io::EOF => + abort("encountered EOF early"); + case blank => + assert(correct is blank); + case let ln: comment => + assert(ln == correct as comment); + case let ln: group_header => + assert(ln == correct as group_header); + case let ln: entry => + assert(entry_equal(ln, correct as entry)); + }; + + assert(next(&scanne) is io::EOF); +}; + +@test fn next_entry() void = { + let h_de = "Desktop Entry"; + let h_dag = "Desktop Action Gallery"; + let h_dac = "Desktop Action Create"; + let correct: []entry = [ + entry_new(h_de, "Version", "1.0", locale::c), + entry_new(h_de, "Type", "Application", locale::c), + entry_new(h_de, "Name", "Foo Viewer", locale::c), + entry_new(h_de, "Comment", "The best viewer for Foo objects available!", locale::c), + entry_new(h_de, "TryExec", "fooview", locale::c), + entry_new(h_de, "Exec", "fooview %F", locale::c), + entry_new(h_de, "Icon", "fooview", locale::c), + entry_new(h_de, "MimeType", "image/x-foo;", locale::c), + entry_new(h_de, "Actions", "Gallery;Create;", locale::c), + entry_new(h_dag, "Exec", "fooview --gallery", locale::c), + entry_new(h_dag, "Name", "Browse Gallery", locale::c), + entry_new(h_dac, "Exec", "fooview --create-new", locale::c), + entry_new(h_dac, "Name", "Create a new Foo!", locale::c), + entry_new( + h_dac, "Name", "Create a new Foo!", + locale::parse("en_US")!), + entry_new( + h_dac, "Name", "Zweep zoop flooble glorp", + locale::parse("xx_XX.UTF-8")!), + entry_new(h_dac, "Icon", "fooview-new", locale::c), + ]; + + let file = os::open("format/desktop_entry/test_data/foo.desktop")!; + defer io::close(file)!; + let scanne = scan(file); + defer finish(&scanne); + + for (let correct .. correct) { + assert(entry_equal(next_entry(&scanne)! as entry, correct)); + }; + + assert(next(&scanne) is io::EOF); +}; + +@test fn parse_entry() void = { + assert(entry_equal(parse_entry("hello=world")!, entry { + key = "hello", + value = "world", + locale = locale::c, + ... + })); + assert(entry_equal(parse_entry("hello[sr_YU.UTF-8@Latn]=world")!, entry { + key = "hello", + value = "world", + locale = locale::parse("sr_YU.UTF-8@Latn")!, + ... + })); +}; + +@test fn validate_entry_key() void = { + // ยง3.3 Only the characters A-Za-z0-9- may be used in key names. + assert(validate_entry_key("SomeName") is void); + assert(validate_entry_key("some-name") is void); + assert(validate_entry_key("1234567890some-0987654321naMe") is void); + assert(validate_entry_key("^&(*)[") is invalid_entry); + assert(validate_entry_key("]") is invalid_entry); + assert(validate_entry_key("&*%^") is invalid_entry); + assert(validate_entry_key("asj=hd@#*()") is invalid_entry); +}; + +@test fn validate_entry_locale() void = { + assert(validate_entry_locale("sr_YU.UTF-8@Latn") is void); + assert(validate_entry_locale("abcd[") is invalid_entry); + assert(validate_entry_locale("ab]cd") is invalid_entry); + assert(validate_entry_locale("a=bcd") is invalid_entry); +}; + +fn entry_equal(a: entry, b: entry) bool = + a.group == b.group && + a.key == b.key && + a.value == b.value && + locale::equal(a.locale, b.locale); + +fn entry_new(group: str, key: str, value: str, local: locale::locale) entry = entry { + group = group, + key = key, + value = value, + locale = local, +}; diff --git a/format/desktop_entry/test_data/foo.desktop b/format/desktop_entry/test_data/foo.desktop new file mode 100644 index 0000000..5dd3e3e --- /dev/null +++ b/format/desktop_entry/test_data/foo.desktop @@ -0,0 +1,26 @@ +#This is a comment + +[Desktop Entry] +Version=1.0 +Type=Application +Name=Foo Viewer +Comment=The best viewer for Foo objects available! +TryExec=fooview +Exec=fooview %F + +Icon=fooview +MimeType=image/x-foo; +Actions=Gallery;Create; + +Name[en_US]=foo.desktop + +[Desktop Action Gallery] +Exec=fooview --gallery +Name=Browse Gallery + +[Desktop Action Create] +Exec=fooview --create-new +Name=Create a new Foo! +Name[en_US]=Create a new Foo! +#Another comment +Icon=fooview-new