-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlib-test.zig
More file actions
87 lines (71 loc) · 2.85 KB
/
lib-test.zig
File metadata and controls
87 lines (71 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const std = @import("std");
const c = @import("c");
test "parser create/destroy" {
var buffer: c.ini_Parser = undefined;
c.ini_create_buffer(&buffer, "", 0, "", 0);
c.ini_destroy(&buffer);
}
fn expectNull(record: c.ini_Record) !void {
try std.testing.expectEqual(c.INI_RECORD_NUL, record.type);
}
fn expectSection(heading: []const u8, record: c.ini_Record) !void {
try std.testing.expectEqual(c.INI_RECORD_SECTION, record.type);
try std.testing.expectEqualStrings(heading, std.mem.span(record.unnamed_0.section));
}
fn expectKeyValue(key: []const u8, value: []const u8, record: c.ini_Record) !void {
try std.testing.expectEqual(c.INI_RECORD_PROPERTY, record.type);
try std.testing.expectEqualStrings(key, std.mem.span(record.unnamed_0.property.key));
try std.testing.expectEqualStrings(value, std.mem.span(record.unnamed_0.property.value));
}
fn expectEnumeration(enumeration: []const u8, record: c.ini_Record) !void {
try std.testing.expectEqual(c.INI_RECORD_ENUMERATION, record.type);
try std.testing.expectEqualStrings(enumeration, std.mem.span(record.unnamed_0.enumeration));
}
fn parseNext(parser: *c.ini_Parser) !c.ini_Record {
var record: c.ini_Record = undefined;
const err = c.ini_next(parser, &record);
switch (err) {
c.INI_SUCCESS => return record,
c.INI_ERR_OUT_OF_MEMORY => return error.OutOfMemory,
c.INI_ERR_IO => return error.InputOutput,
c.INI_ERR_INVALID_DATA => return error.InvalidData,
else => unreachable,
}
}
fn commonTest(parser: *c.ini_Parser) !void {
try expectSection("Meta", try parseNext(parser));
try expectKeyValue("author", "xq", try parseNext(parser));
try expectKeyValue("library", "ini", try parseNext(parser));
try expectSection("Albums", try parseNext(parser));
try expectEnumeration("Thriller", try parseNext(parser));
try expectEnumeration("Back in Black", try parseNext(parser));
try expectEnumeration("Bat Out of Hell", try parseNext(parser));
try expectEnumeration("The Dark Side of the Moon", try parseNext(parser));
try expectNull(try parseNext(parser));
}
test "buffer parser" {
const slice =
\\[Meta]
\\author = xq
\\library = ini
\\
\\[Albums]
\\Thriller
\\Back in Black
\\Bat Out of Hell
\\The Dark Side of the Moon
;
var parser: c.ini_Parser = undefined;
c.ini_create_buffer(&parser, slice, slice.len, ";#", 2);
defer c.ini_destroy(&parser);
try commonTest(&parser);
}
test "file parser" {
const file = c.fopen("example/example.ini", "rb") orelse unreachable;
defer _ = c.fclose(file);
var parser: c.ini_Parser = undefined;
var read_buffer: [1024]u8 = undefined;
c.ini_create_file(&parser, &read_buffer, read_buffer.len, file, ";#", 2);
defer c.ini_destroy(&parser);
try commonTest(&parser);
}