diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/consumer/build.zig | 6 | ||||
| -rw-r--r-- | test/consumer/lib.zig | 95 | ||||
| -rw-r--r-- | test/consumer/none.zig | 56 |
3 files changed, 155 insertions, 2 deletions
diff --git a/test/consumer/build.zig b/test/consumer/build.zig index fbf19f61..f8f63712 100644 --- a/test/consumer/build.zig +++ b/test/consumer/build.zig @@ -26,14 +26,16 @@ pub fn build(b: *std.Build) void { const step = b.step("test", "colour, outline and walk a C and a Zig source through pardes's syntax, build it with one language named, and edit a text with its editor and a grammar of this package's"); // A file belongs to one module: the syntax alone, and the editor (which // has it as `.syntax`), each a test of its own. - for ([_]struct { []const u8, []const u8, *std.Build.Dependency }{ .{ "lib.zig", "pardes-syntax", syntax }, .{ "editor.zig", "pardes-editor", editor } }) |t| { + // The patch view with no grammar at all: the editor's package has + // `pardes-syntax` too. + for ([_]struct { []const u8, []const u8, *std.Build.Dependency }{ .{ "lib.zig", "pardes-syntax", syntax }, .{ "editor.zig", "pardes-editor", editor }, .{ "none.zig", "pardes-syntax", editor } }) |t| { const mod = b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path(t[0]), .imports = &.{.{ .name = t[1], .module = t[2].module(t[1]) }}, }); - if (t[2] == editor) mod.linkLibrary(grammar); + if (std.mem.eql(u8, t[1], "pardes-editor")) mod.linkLibrary(grammar); step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mod })).step); } diff --git a/test/consumer/lib.zig b/test/consumer/lib.zig index 7e297920..fa0f8aee 100644 --- a/test/consumer/lib.zig +++ b/test/consumer/lib.zig @@ -159,3 +159,98 @@ test "a diff is a language: its hunks coloured as their file's, its added and re test "every name the library has compiles outside pardes" { std.testing.refAllDecls(pardes); } + +// ---- the patch view (+Diff), without the editor ---- + +const patch_c = + \\diff --git a/main.c b/main.c + \\--- a/main.c + \\+++ b/main.c + \\@@ -2,4 +2,4 @@ int main(void) + \\ { + \\- int x = foo(1); + \\+ int x = bar(1); + \\ return x; + \\ } + \\\ No newline at end of file + \\ +; +/// main.c as the patch leaves it: what a reader hands in as its source. +const main_c = "int main(void)\n{\n int x = bar(1);\n return x;\n}"; + +/// Each byte of `text` that has `bit`, the rest as dots. +fn marked(buf: []u8, text: []const u8, styles: []const u8, bit: u8) []const u8 { + for (buf[0..text.len], text, styles) |*o, c, s| o.* = if (s & bit != 0) c else '.'; + return buf[0..text.len]; +} + +test "a diff stacked with no file to read: numbered by the diff, under git's function line, its changed words emphasised" { + const gpa = std.testing.allocator; + pardes.start(gpa); + defer pardes.stop(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const ctx: pardes.PatchCtx = .{ .arena = arena.allocator(), .colors = pardes.PatchCtx.languages }; + const p = try pardes.formatPatch(&ctx, "", patch_c); + try std.testing.expectEqualStrings("main.c:3\n int main(void)\n {\n- int x = foo(1);\n+ int x = bar(1);\n return x;\n }\n", p.text); + const Kind = pardes.PatchRow.Kind; + const want = [_]struct { Kind, ?usize, ?usize }{ + .{ .hunk, null, null }, + // the `@@`'s own words: the hunk's context + .{ .declaration, null, null }, + .{ .context, 2, 2 }, + .{ .removed, 3, null }, + .{ .added, null, 3 }, + .{ .context, 4, 4 }, + .{ .context, 5, 5 }, + }; + try std.testing.expectEqual(want.len, p.rows.len); + for (want, p.rows) |w, row| { + try std.testing.expectEqual(w[0], row.kind); + try std.testing.expectEqual(w[1], row.old); + try std.testing.expectEqual(w[2], row.new); + try std.testing.expectEqualStrings("main.c", row.path); + } + try std.testing.expectEqual(3, p.rows[0].line); + try std.testing.expectEqual("main.c:3".len, p.rows[0].location_end); + try std.testing.expect(p.rows[6].no_newline and !p.rows[5].no_newline); + var lines = std.mem.splitScalar(u8, p.text, '\n'); + var buf: [64]u8 = undefined; + for (p.rows) |row| { + const line = lines.next().?; + if (row.styles.len > 0) try std.testing.expectEqual(line.len, row.styles.len); + switch (row.kind) { + // the words a changed line changed, on both lines; each on its + // ground, its marker not on the line's own (it was paired) + .removed => try std.testing.expectEqualStrings(".............foo....", marked(&buf, line, row.styles, pardes.ground_emph)), + .added => try std.testing.expectEqualStrings(".............bar....", marked(&buf, line, row.styles, pardes.ground_emph)), + else => {}, + } + if (row.kind == .removed) try std.testing.expectEqual(pardes.ground_del, row.styles[0]); + if (row.kind == .added) try std.testing.expectEqual(pardes.ground_add, row.styles[0]); + } + // coloured as C: `return` a keyword + const ret = p.rows[5].styles[std.mem.indexOf(u8, " return x;", "return").?]; + try std.testing.expectEqual(pardes.Syn.keyword, @as(pardes.Syn, @fromBackingInt(ret & pardes.syn_bits))); +} + +test "a diff stacked with its file handed in: the declaration it stands in, from the file's own parse" { + const gpa = std.testing.allocator; + pardes.start(gpa); + defer pardes.stop(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const Given = struct { + fn source(_: *const pardes.PatchCtx, _: []const u8, path: []const u8) ?pardes.PatchSource { + return if (std.mem.eql(u8, path, "main.c")) .{ .path = path, .bytes = main_c } else null; + } + }; + const ctx: pardes.PatchCtx = .{ .arena = arena.allocator(), .source = Given.source, .declarations = pardes.PatchCtx.parsed, .colors = pardes.PatchCtx.languages }; + const p = try pardes.formatPatch(&ctx, "", patch_c); + try std.testing.expectEqualStrings("main.c:1\n int main(void)\nmain.c:3 *\n {\n- int x = foo(1);\n+ int x = bar(1);\n return x;\n }\n", p.text); + try std.testing.expectEqual(pardes.PatchRow.Kind.declaration, p.rows[0].kind); + try std.testing.expectEqual(1, p.rows[0].line); + try std.testing.expectEqual(pardes.PatchRow.Kind.declaration, p.rows[1].kind); + try std.testing.expectEqual(pardes.PatchRow.Kind.hunk, p.rows[2].kind); + try std.testing.expectEqual("main.c:3".len, p.rows[2].location_end); +} diff --git a/test/consumer/none.zig b/test/consumer/none.zig new file mode 100644 index 00000000..a389d429 --- /dev/null +++ b/test/consumer/none.zig @@ -0,0 +1,56 @@ +//! The patch view in a build with no grammar at all (`.grammars = .none`): +//! a diff of a file in a language it does not have still gets its grounds, +//! its changed words and its rows, and the colours it is not given stay +//! out. +const std = @import("std"); +const ps = @import("pardes-syntax"); + +const patch_rs = + \\diff --git a/src/lib.rs b/src/lib.rs + \\--- a/src/lib.rs + \\+++ b/src/lib.rs + \\@@ -10,3 +10,3 @@ fn total(items: &[u32]) -> u32 { + \\ let mut sum = 0; + \\- for i in items { sum += i; } + \\+ for i in items { sum += *i; } + \\ sum + \\ +; + +test "a diff with no grammar in the build: rows, grounds and changed words, no colours" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + try std.testing.expect(!ps.supportsPath("lib.rs")); + const ctx: ps.PatchCtx = .{ .arena = arena.allocator() }; + const p = try ps.formatPatch(&ctx, "", patch_rs); + try std.testing.expectEqualStrings("src/lib.rs:11\n fn total(items: &[u32]) -> u32 {\n let mut sum = 0;\n- for i in items { sum += i; }\n+ for i in items { sum += *i; }\n sum\n", p.text); + try std.testing.expectEqual(ps.PatchRow.Kind.hunk, p.rows[0].kind); + try std.testing.expectEqual(ps.PatchRow.Kind.declaration, p.rows[1].kind); + const removed = p.rows[3]; + const added = p.rows[4]; + try std.testing.expectEqual(.{ 11, null }, .{ removed.old, removed.new }); + try std.testing.expectEqual(.{ null, 11 }, .{ added.old, added.new }); + try std.testing.expectEqual(ps.ground_del, removed.styles[0]); + try std.testing.expectEqual(ps.ground_add, added.styles[0]); + // `*` added: the one word emphasised; no byte has a colour of its own + const star = std.mem.indexOfScalar(u8, "+ for i in items { sum += *i; }", '*').?; + for (added.styles, 0..) |s, i| try std.testing.expectEqual(i == star, s & ps.ground_emph != 0); + for (p.rows) |row| for (row.styles) |s| try std.testing.expectEqual(0, s & ps.syn_bits); +} + +test "with no grammar, a file handed in still places the hunk, and no declaration is made up" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const Given = struct { + fn source(_: *const ps.PatchCtx, _: []const u8, path: []const u8) ?ps.PatchSource { + return .{ .path = path, .bytes = "\n" ++ "\n" ++ "\n" ++ "\n" ++ "\n" ++ "\n" ++ "\n" ++ "\n" ++ + "fn total(items: &[u32]) -> u32 {\n let mut sum = 0;\n for i in items { sum += *i; }\n sum\n}\n" }; + } + }; + const ctx: ps.PatchCtx = .{ .arena = arena.allocator(), .source = Given.source, .declarations = ps.PatchCtx.parsed }; + const p = try ps.formatPatch(&ctx, "", patch_rs); + // found (no `@@` words then: git's guess would only contradict the + // file's own), and no grammar to read a declaration from + try std.testing.expectEqualStrings("src/lib.rs:11\n let mut sum = 0;\n- for i in items { sum += i; }\n+ for i in items { sum += *i; }\n sum\n", p.text); + for (p.rows) |row| try std.testing.expect(row.kind != .declaration); +} |
