From 40a544bac1f8799d40df377a90fb89d24fc2803c Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Fri, 19 Jul 2024 21:31:04 +0200 Subject: test: Add testing Also add blank space between line being merged together. --- src/main.zig | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/main.zig b/src/main.zig index 14eb65c..21103ff 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,11 +1,12 @@ const std = @import("std"); const File = std.fs.File; +const Reader = std.fs.File.Reader; const Writer = std.fs.File.Writer; pub fn main() !void { const in = std.io.getStdIn(); const out = std.io.getStdOut(); - Uf.init().streamCopy(in, out) catch |err| { + Uf.init().streamCopy(in.reader(), out.writer()) catch |err| { try std.io.getStdErr().writer().print("Error {}", .{err}); }; } @@ -22,12 +23,12 @@ const Uf = struct { return Self{}; } - fn flush(_: Self, out: Writer) !void { + fn flush(_: Self, out: anytype) !void { _ = try out.write(out_buf[0..out_index]); out_index = 0; } - fn writeChar(self: Self, out: Writer, c: u8) !void { + fn writeChar(self: Self, out: anytype, c: u8) !void { if (out_index >= buf_size) { try self.flush(out); } @@ -35,10 +36,7 @@ const Uf = struct { out_index += 1; } - fn streamCopy(self: Self, in: File, out: File) !void { - const reader = in.reader(); - const writer = out.writer(); - + fn streamCopy(self: Self, reader: anytype, writer: anytype) !void { var hit = false; var second = false; var in_buf: [buf_size]u8 = undefined; @@ -55,6 +53,8 @@ const Uf = struct { } else if (hit and c != '\n') { if (second) { try self.writeChar(writer, '\n'); + } else { + try self.writeChar(writer, ' '); } hit = false; second = false; @@ -73,3 +73,32 @@ const Uf = struct { } } }; + +test "Test format" { + const text_in = + \\# This is a markdown + \\ + \\Lorem ipsum dolor sit amet, + \\consectetur adipiscing elit. + \\ + \\ + \\ + \\Praesent pharetra sit amet ante sit amet consequat. + ; + + const text_out = + \\# This is a markdown + \\ + \\Lorem ipsum dolor sit amet, consectetur adipiscing elit. + \\ + \\Praesent pharetra sit amet ante sit amet consequat. + ; + + var stream = std.io.fixedBufferStream(text_in); + + var list = std.ArrayList(u8).init(std.testing.allocator); + defer list.deinit(); + + try Uf.init().streamCopy(stream.reader(), list.writer()); + try std.testing.expect(std.mem.eql(u8, text_out, list.items)); +} -- cgit v1.2.3