Skip to content

Commit 939a5de

Browse files
committed
Extract tests into separate file
1 parent 591b998 commit 939a5de

2 files changed

Lines changed: 156 additions & 146 deletions

File tree

src/root.zig

Lines changed: 2 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const std = @import("std");
22
const testing = std.testing;
33
const Writer = std.Io.Writer;
4-
const builtin = @import("builtin");
54

65
const formatter = @import("./formatter.zig");
76
const Formatter = formatter.Formatter;
@@ -110,149 +109,6 @@ pub fn initRootLogger(alloc: std.mem.Allocator, options: Options) !*Logger {
110109
return Logger.init(options.root_logger_name, spec, log_handler, alloc);
111110
}
112111

113-
test "text logger" {
114-
var tl = try TestingLogger.init(.{
115-
.log_spec = SpecSource{ .from_string = "warn" },
116-
.color = .always,
117-
});
118-
defer tl.deinit();
119-
120-
var log2 = try tl.logger.initChildLogger("kid1");
121-
122-
tl.logger.info("info test aa11", .{ .field1 = "value1", .name = "John", .age = 30 });
123-
log2.trace("Hello, aa22", .{ .field1 = "value1", .name = "John", .age = 30 });
124-
log2.debug("Hello, aa33", .{ .field1 = "value1", .name = "John", .age = 30 });
125-
log2.info("Hello, aa44", .{ .field1 = "value1", .name = "John", .age = 30e2 });
126-
log2.warn("Hello, aa55", .{ .field1 = "value1", .name = "John", .age = 30.34534 });
127-
log2.err("Hello, aa66", .{ .field1 = "value1", .name = "John Smith", .age = 30, .active = true, .nothing = null });
128-
129-
var log3 = try log2.initChildLogger("kid1-1");
130-
var log4 = try log2.initChildLogger("kid1-2");
131-
try testing.expectEqual(2, log2.kids.items.len);
132-
log3.warn("abc77", .{ .f1 = "v1" });
133-
log4.err("abc88", .{ .f1 = "v1" });
134-
135-
log3.deinit();
136-
try testing.expectEqual(1, log2.kids.items.len);
137-
138-
try tl.hasNotPattern("aa11");
139-
try tl.hasNotPattern("aa22");
140-
try tl.hasNotPattern("aa33");
141-
try tl.hasNotPattern("aa44");
142-
try tl.hasPattern("aa55");
143-
try tl.hasPattern("aa66");
144-
try tl.hasPattern("abc77");
145-
try tl.hasPattern("abc88");
146-
}
147-
148-
test "json logger" {
149-
var tl = try TestingLogger.init(.{ .formatter = .json });
150-
tl.logger.info("Hello\tslog!", .{ .field1 = "value1", .field2 = "value1", .rate = 30 });
151-
defer tl.deinit();
152-
153-
try tl.hasPattern("message\":\"Hello\\t");
154-
try tl.hasPattern("field1\":\"value1\"");
155-
}
156-
157-
const TestingLogger = struct {
158-
allocating: *Writer.Allocating,
159-
logger: *Logger,
160-
161-
fn init(options: Options) !TestingLogger {
162-
var al = try testing.allocator.create(Writer.Allocating);
163-
al.* = Writer.Allocating.init(testing.allocator);
164-
165-
var opt = options;
166-
opt.output = .{ .writer = &al.writer };
167-
return .{
168-
.allocating = al,
169-
.logger = try initRootLogger(testing.allocator, opt),
170-
};
171-
}
172-
173-
pub fn deinit(self: *TestingLogger) void {
174-
const al = self.allocating.allocator;
175-
self.allocating.deinit();
176-
self.logger.deinit();
177-
al.destroy(self.allocating);
178-
}
179-
180-
fn hasPattern(self: TestingLogger, pattern: []const u8) !void {
181-
try testing.expect(std.mem.indexOf(u8, self.allocating.written(), pattern).? >= 0);
182-
}
183-
184-
fn hasNotPattern(self: TestingLogger, pattern: []const u8) !void {
185-
try testing.expect(std.mem.indexOf(u8, self.allocating.written(), pattern) == null);
186-
}
187-
};
188-
189-
test "root logger level abc" {
190-
var tl = try TestingLogger.init(.{
191-
.root_logger_name = "abc",
192-
.log_spec = SpecSource{ .from_string = "info,abc=debug,n1=trace" },
193-
});
194-
defer tl.deinit();
195-
tl.logger.debug("abcd", .{});
196-
197-
try tl.hasPattern("abcd");
198-
}
199-
200-
test "root logger level 2" {
201-
var tl = try TestingLogger.init(.{
202-
.root_logger_name = "abc",
203-
.log_spec = SpecSource{ .from_string = "abc=debug,n1=trace" },
204-
});
205-
defer tl.deinit();
206-
tl.logger.debug("abcd", .{});
207-
208-
try tl.hasPattern("abcd");
209-
}
210-
211-
test "logger with dots in name" {
212-
var tl = try TestingLogger.init(.{
213-
.root_logger_name = "ab.cd.ef",
214-
.log_spec = SpecSource{ .from_string = "error,ab=info" },
215-
});
216-
defer tl.deinit();
217-
tl.logger.info("text1", .{});
218-
tl.logger.debug("text2", .{});
219-
220-
try tl.hasPattern("text1");
221-
try tl.hasNotPattern("text2");
222-
}
223-
224-
test "logger with dots in name: deep spec" {
225-
var tl = try TestingLogger.init(.{
226-
.root_logger_name = "ab.cd.ef",
227-
.log_spec = SpecSource{ .from_string = "error,ab.cd=debug,ab.cd.ef.gh=warn" },
228-
});
229-
defer tl.deinit();
230-
tl.logger.info("text1", .{});
231-
tl.logger.debug("text2", .{});
232-
233-
try tl.hasPattern("text1");
234-
try tl.hasPattern("text2");
235-
236-
var log2 = try tl.logger.initChildLogger("gh");
237-
log2.info("text3", .{});
238-
try tl.hasNotPattern("text3");
239-
}
240-
241-
test "logger with dots in name: child logger" {
242-
var tl = try TestingLogger.init(.{
243-
.log_spec = SpecSource{ .from_string = "error,ab.cd=info,ab.cd.ef=debug" },
244-
});
245-
defer tl.deinit();
246-
247-
var log2 = try tl.logger.initChildLogger("ab.cd");
248-
log2.info("text1", .{});
249-
log2.debug("text2", .{});
250-
try tl.hasPattern("text1");
251-
try tl.hasNotPattern("text2");
252-
253-
var log3 = try log2.initChildLogger("ef");
254-
log3.info("text3", .{});
255-
log3.debug("text4", .{});
256-
try tl.hasPattern("text3");
257-
try tl.hasPattern("text4");
112+
comptime {
113+
_ = @import("./tests.zig");
258114
}

src/tests.zig

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
const std = @import("std");
2+
const Writer = std.Io.Writer;
3+
const Logger = @import("./Logger.zig");
4+
const Options = @import("./root.zig").Options;
5+
const initRootLogger = @import("./root.zig").initRootLogger;
6+
const SpecSource = @import("./root.zig").SpecSource;
7+
const testing = std.testing;
8+
9+
const TestingLogger = struct {
10+
allocating: *Writer.Allocating,
11+
logger: *Logger,
12+
13+
fn init(options: Options) !TestingLogger {
14+
var al = try testing.allocator.create(Writer.Allocating);
15+
al.* = Writer.Allocating.init(testing.allocator);
16+
17+
var opt = options;
18+
opt.output = .{ .writer = &al.writer };
19+
return .{
20+
.allocating = al,
21+
.logger = try initRootLogger(testing.allocator, opt),
22+
};
23+
}
24+
25+
pub fn deinit(self: *TestingLogger) void {
26+
const al = self.allocating.allocator;
27+
self.allocating.deinit();
28+
self.logger.deinit();
29+
al.destroy(self.allocating);
30+
}
31+
32+
fn hasPattern(self: TestingLogger, pattern: []const u8) !void {
33+
try testing.expect(std.mem.indexOf(u8, self.allocating.written(), pattern).? >= 0);
34+
}
35+
36+
fn hasNotPattern(self: TestingLogger, pattern: []const u8) !void {
37+
try testing.expect(std.mem.indexOf(u8, self.allocating.written(), pattern) == null);
38+
}
39+
};
40+
41+
test "text logger" {
42+
var tl = try TestingLogger.init(.{
43+
.log_spec = SpecSource{ .from_string = "warn" },
44+
.color = .always,
45+
});
46+
defer tl.deinit();
47+
48+
var log2 = try tl.logger.initChildLogger("kid1");
49+
50+
tl.logger.info("info test aa11", .{ .field1 = "value1", .name = "John", .age = 30 });
51+
log2.trace("Hello, aa22", .{ .field1 = "value1", .name = "John", .age = 30 });
52+
log2.debug("Hello, aa33", .{ .field1 = "value1", .name = "John", .age = 30 });
53+
log2.info("Hello, aa44", .{ .field1 = "value1", .name = "John", .age = 30e2 });
54+
log2.warn("Hello, aa55", .{ .field1 = "value1", .name = "John", .age = 30.34534 });
55+
log2.err("Hello, aa66", .{ .field1 = "value1", .name = "John Smith", .age = 30, .active = true, .nothing = null });
56+
57+
var log3 = try log2.initChildLogger("kid1-1");
58+
var log4 = try log2.initChildLogger("kid1-2");
59+
try testing.expectEqual(2, log2.kids.items.len);
60+
log3.warn("abc77", .{ .f1 = "v1" });
61+
log4.err("abc88", .{ .f1 = "v1" });
62+
63+
log3.deinit();
64+
try testing.expectEqual(1, log2.kids.items.len);
65+
66+
try tl.hasNotPattern("aa11");
67+
try tl.hasNotPattern("aa22");
68+
try tl.hasNotPattern("aa33");
69+
try tl.hasNotPattern("aa44");
70+
try tl.hasPattern("aa55");
71+
try tl.hasPattern("aa66");
72+
try tl.hasPattern("abc77");
73+
try tl.hasPattern("abc88");
74+
}
75+
76+
test "json logger" {
77+
var tl = try TestingLogger.init(.{ .formatter = .json });
78+
tl.logger.info("Hello\tslog!", .{ .field1 = "value1", .field2 = "value1", .rate = 30 });
79+
defer tl.deinit();
80+
81+
try tl.hasPattern("message\":\"Hello\\t");
82+
try tl.hasPattern("field1\":\"value1\"");
83+
}
84+
85+
test "root logger level abc" {
86+
var tl = try TestingLogger.init(.{
87+
.root_logger_name = "abc",
88+
.log_spec = SpecSource{ .from_string = "info,abc=debug,n1=trace" },
89+
});
90+
defer tl.deinit();
91+
tl.logger.debug("abcd", .{});
92+
93+
try tl.hasPattern("abcd");
94+
}
95+
96+
test "root logger level 2" {
97+
var tl = try TestingLogger.init(.{
98+
.root_logger_name = "abc",
99+
.log_spec = SpecSource{ .from_string = "abc=debug,n1=trace" },
100+
});
101+
defer tl.deinit();
102+
tl.logger.debug("abcd", .{});
103+
104+
try tl.hasPattern("abcd");
105+
}
106+
107+
test "logger with dots in name" {
108+
var tl = try TestingLogger.init(.{
109+
.root_logger_name = "ab.cd.ef",
110+
.log_spec = SpecSource{ .from_string = "error,ab=info" },
111+
});
112+
defer tl.deinit();
113+
tl.logger.info("text1", .{});
114+
tl.logger.debug("text2", .{});
115+
116+
try tl.hasPattern("text1");
117+
try tl.hasNotPattern("text2");
118+
}
119+
120+
test "logger with dots in name: deep spec" {
121+
var tl = try TestingLogger.init(.{
122+
.root_logger_name = "ab.cd.ef",
123+
.log_spec = SpecSource{ .from_string = "error,ab.cd=debug,ab.cd.ef.gh=warn" },
124+
});
125+
defer tl.deinit();
126+
tl.logger.info("text1", .{});
127+
tl.logger.debug("text2", .{});
128+
129+
try tl.hasPattern("text1");
130+
try tl.hasPattern("text2");
131+
132+
var log2 = try tl.logger.initChildLogger("gh");
133+
log2.info("text3", .{});
134+
try tl.hasNotPattern("text3");
135+
}
136+
137+
test "logger with dots in name: child logger" {
138+
var tl = try TestingLogger.init(.{
139+
.log_spec = SpecSource{ .from_string = "error,ab.cd=info,ab.cd.ef=debug" },
140+
});
141+
defer tl.deinit();
142+
143+
var log2 = try tl.logger.initChildLogger("ab.cd");
144+
log2.info("text1", .{});
145+
log2.debug("text2", .{});
146+
try tl.hasPattern("text1");
147+
try tl.hasNotPattern("text2");
148+
149+
var log3 = try log2.initChildLogger("ef");
150+
log3.info("text3", .{});
151+
log3.debug("text4", .{});
152+
try tl.hasPattern("text3");
153+
try tl.hasPattern("text4");
154+
}

0 commit comments

Comments
 (0)