hare-xdg/format/xdg/ini/scan_test.ha

147 lines
4.6 KiB
Hare

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,
blank,
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")!),
"Another comment": comment,
entry_new(h_dac, "Icon", "fooview-new", locale::c),
];
let file = os::open("format/xdg/ini/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")!),
entry_new(h_dac, "Icon", "fooview-new", locale::c),
];
let file = os::open("format/xdg/ini/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@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,
};