summaryrefslogtreecommitdiff
path: root/test/lib
diff options
context:
space:
mode:
Diffstat (limited to 'test/lib')
-rw-r--r--test/lib/lib.zig54
1 files changed, 54 insertions, 0 deletions
diff --git a/test/lib/lib.zig b/test/lib/lib.zig
index a3f99b56..7e297920 100644
--- a/test/lib/lib.zig
+++ b/test/lib/lib.zig
@@ -102,6 +102,60 @@ test "a C and a Zig source are coloured, outlined and walked as pardes has them,
try std.testing.expectEqual(null, pardes.theme("no such theme"));
}
+test "a diff is a language: its hunks coloured as their file's, its added and removed rows on a theme's grounds" {
+ const gpa = std.testing.allocator;
+ pardes.start(gpa);
+ defer pardes.stop();
+ const patch =
+ \\diff --git a/main.c b/main.c
+ \\--- a/main.c
+ \\+++ b/main.c
+ \\@@ -1,3 +1,3 @@
+ \\ int main(void) {
+ \\- return 1;
+ \\+ return 0;
+ \\ }
+ \\
+ ;
+ const styles = try pardes.highlightDiff(gpa, patch, 0, patch.len);
+ defer gpa.free(styles);
+ try std.testing.expectEqual(patch.len, styles.len);
+ const syn = struct {
+ fn at(s: []const u8, text: []const u8, needle: []const u8) pardes.Syn {
+ return @fromBackingInt(s[std.mem.indexOf(u8, text, needle).?] & pardes.syn_bits);
+ }
+ }.at;
+ try std.testing.expectEqual(pardes.Syn.comment, syn(styles, patch, "--- a/main.c"));
+ try std.testing.expectEqual(pardes.Syn.keyword, syn(styles, patch, "@@ -1,3"));
+ // each side's code as C, behind its marker
+ try std.testing.expectEqual(pardes.Syn.keyword, syn(styles, patch, "return 1"));
+ try std.testing.expectEqual(pardes.Syn.keyword, syn(styles, patch, "return 0"));
+ try std.testing.expectEqual(pardes.Syn.number, syn(styles, patch, "0;"));
+ const added = std.mem.indexOf(u8, patch, "+ return 0").?;
+ const removed = std.mem.indexOf(u8, patch, "- return 1").?;
+ try std.testing.expectEqual(pardes.ground_add | pardes.ground_emph, styles[added]);
+ try std.testing.expect(styles[added + 5] & pardes.ground_add != 0 and styles[added + 5] & pardes.ground_emph == 0);
+ try std.testing.expectEqual(pardes.ground_del | pardes.ground_emph, styles[removed]);
+ try std.testing.expectEqual(0, styles[std.mem.indexOf(u8, patch, " int main").?] & (pardes.ground_add | pardes.ground_del));
+
+ // a row's hue in a theme: a ground between the page and the theme's green, a stronger and a fainter one
+ const th = pardes.theme("Orchard").?;
+ const g = pardes.Grounds.of(th);
+ const ground = g.forStyle(styles[added]).?;
+ try std.testing.expectEqual(g.add, ground);
+ try std.testing.expectEqual(g.del, g.forStyle(styles[removed]).?);
+ try std.testing.expectEqual(null, g.forStyle(@backingInt(pardes.Syn.keyword)));
+ try std.testing.expect(!std.mem.eql(u8, &ground.row, &pardes.page(th)) and !std.mem.eql(u8, &ground.row, &ground.emph) and !std.mem.eql(u8, &ground.row, &ground.line));
+
+ // what a row points at: the new line a `+` row is
+ var lines: std.ArrayList([]const u8) = .empty;
+ defer lines.deinit(gpa);
+ var it = std.mem.splitScalar(u8, patch, '\n');
+ while (it.next()) |line| try lines.append(gpa, line);
+ var buf: [64]u8 = undefined;
+ try std.testing.expectEqualStrings("main.c:2", pardes.diff.targetAt(lines.items, 6, false).?.address(&buf).?);
+}
+
test "every name the library has compiles outside pardes" {
std.testing.refAllDecls(pardes);
}