authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-06-30 17:43:00+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-02 10:35:44+02:00
log64bd134818b77ba56deb613112ec8cdb0c967703
treea4c442992629ce0b6f80cfcf2229c01291031600
parent8b82c40104802c9351b30ef7c5a41e7829ab6d2a

Add Dir.Iterator tests

This commit adds some `std.fs.Dir.Iterator` tests.

1 files changed, 38 insertions(+), 1 deletions(-)

lib/std/fs/test.zig+38-1
...@@ -3,10 +3,47 @@ const testing = std.testing;...@@ -3,10 +3,47 @@ const testing = std.testing;
3const builtin = std.builtin;3const builtin = std.builtin;
4const fs = std.fs;4const fs = std.fs;
5const mem = std.mem;5const mem = std.mem;
6const wasi = std.os.wasi;
67
8const Dir = std.fs.Dir;
7const File = std.fs.File;9const File = std.fs.File;
8const tmpDir = testing.tmpDir;10const tmpDir = testing.tmpDir;
911
12test "Dir.Iterator" {
13 var tmp_dir = tmpDir(.{ .iterate = true });
14 defer tmp_dir.cleanup();
15
16 // First, create a couple of entries to iterate over.
17 const file = try tmp_dir.dir.createFile("some_file", .{});
18 file.close();
19
20 try tmp_dir.dir.makeDir("some_dir");
21
22 // Create iterator.
23 var iter = tmp_dir.dir.iterate();
24 var entries = std.ArrayList(Dir.Entry).init(testing.allocator);
25 defer entries.deinit();
26
27 while (try iter.next()) |entry| {
28 try entries.append(entry);
29 }
30
31 testing.expect(entries.items.len == 2); // note that the Iterator skips '.' and '..'
32 testing.expect(contains(&entries, Dir.Entry{ .name = "some_file", .kind = Dir.Entry.Kind.File }));
33 testing.expect(contains(&entries, Dir.Entry{ .name = "some_dir", .kind = Dir.Entry.Kind.Directory }));
34}
35
36fn entry_eql(lhs: Dir.Entry, rhs: Dir.Entry) bool {
37 return mem.eql(u8, lhs.name, rhs.name) and lhs.kind == rhs.kind;
38}
39
40fn contains(entries: *const std.ArrayList(Dir.Entry), el: Dir.Entry) bool {
41 for (entries.items) |entry| {
42 if (entry_eql(entry, el)) return true;
43 }
44 return false;
45}
46
10test "readAllAlloc" {47test "readAllAlloc" {
11 var tmp_dir = tmpDir(.{});48 var tmp_dir = tmpDir(.{});
12 defer tmp_dir.cleanup();49 defer tmp_dir.cleanup();
...@@ -237,7 +274,7 @@ test "fs.copyFile" {...@@ -237,7 +274,7 @@ test "fs.copyFile" {
237 try expectFileContents(tmp.dir, dest_file2, data);274 try expectFileContents(tmp.dir, dest_file2, data);
238}275}
239276
240fn expectFileContents(dir: fs.Dir, file_path: []const u8, data: []const u8) !void {277fn expectFileContents(dir: Dir, file_path: []const u8, data: []const u8) !void {
241 const contents = try dir.readFileAlloc(testing.allocator, file_path, 1000);278 const contents = try dir.readFileAlloc(testing.allocator, file_path, 1000);
242 defer testing.allocator.free(contents);279 defer testing.allocator.free(contents);
243280