summaryrefslogtreecommitdiff
path: root/test/consumer/lib.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-20 04:38:59 -0300
committerGabriel Schneider <[email protected]>2026-11-20 04:39:53 -0300
commit403b46922af6e4e2012cd15f5f3f5a5702eef7e7 (patch)
tree44e4df5fbc12e77210fbcafa50f29d23c93caa05 /test/consumer/lib.zig
parenta3634ab3dbf2fabd997f137c47eafd7bc6c442b0 (diff)
downloadpardes-403b46922af6e4e2012cd15f5f3f5a5702eef7e7.tar.gz
pardes-403b46922af6e4e2012cd15f5f3f5a5702eef7e7.zip
test: test/lib becomes test/consumer, the package a consumer would write, in both of build.zig's head-comment shapes (pardes-build-2 item 5; the start design's step 3): `pardes-syntax` with `.grammars = .minimal` (lib.zig, as before), and `pardes-editor` with `.grammars = .none, .editor = true` and a grammar of its own, C's, built by this package from the very package pardes's manifest pins (`ts_c`, its own build's `tree-sitter-c` artifact, static, ReleaseFast) and linked into the editor's test and the bench, so the case sketchbook's examples/live is (pardes compiles no grammar for it; the program hands one in at run time) is gated here and not only in sketchbook; no network: `lib-test` runs it with `--pkg-dir ../../zig-pkg`, which has that package; the package is named `pardes_consumer` (a new fingerprint, as zig asks for a renamed package); lib-test's run step is named for what it is, so `--summary all` shows `zig build test in test/consumer, a package that depends on pardes success`, under `unit-test` as before; the package's own run: 29/29 steps, 11/11 tests (3 of the syntax, 8 of the editor), its bench builds and runs; build/tools.zig, build.zig's head comment, core/lib_syntax.zig, docs/next-editor.md, the manual and the changelog name test/consumer; the stale `test/lib/zig-pkg` (1.1 GB, untracked, from a run without --pkg-dir) deleted with the old directory. Scripts and notes outside the tree that name test/lib (its cache among them: a wipe of `test/lib/.zig-cache` now misses `test/consumer/.zig-cache`) are listed in the agent's report for the coordinator
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'test/consumer/lib.zig')
-rw-r--r--test/consumer/lib.zig161
1 files changed, 161 insertions, 0 deletions
diff --git a/test/consumer/lib.zig b/test/consumer/lib.zig
new file mode 100644
index 00000000..7e297920
--- /dev/null
+++ b/test/consumer/lib.zig
@@ -0,0 +1,161 @@
+//! pardes's library from outside pardes (build.zig here): a C and a Zig
+//! source coloured, outlined and walked, and a theme's colours for them.
+const std = @import("std");
+const pardes = @import("pardes-syntax");
+
+const c_source =
+ \\#include <stdio.h>
+ \\
+ \\// say it
+ \\int main(void) {
+ \\ printf("hi %d\n", 42);
+ \\ return 0;
+ \\}
+ \\
+;
+
+const zig_source =
+ \\const std = @import("std");
+ \\
+ \\pub fn main() void {
+ \\ // say it
+ \\ std.debug.print("hi {d}\n", .{42});
+ \\}
+ \\
+;
+
+/// Every byte of `text`'s first occurrence in `source` is drawn as `want`.
+fn expectSyn(styles: []const u8, source: []const u8, text: []const u8, want: pardes.Syn) !void {
+ const at = std.mem.indexOf(u8, source, text).?;
+ for (styles[at..][0..text.len]) |style| try std.testing.expectEqual(want, @as(pardes.Syn, @fromBackingInt(style)));
+}
+
+test "a C and a Zig source are coloured, outlined and walked as pardes has them, in a named theme's colours" {
+ const gpa = std.testing.allocator;
+ // under the testing allocator, so what tree-sitter keeps is counted too
+ pardes.start(gpa);
+ defer pardes.stop();
+
+ try std.testing.expect(pardes.supportsPath("main.c") and pardes.supportsPath("main.zig"));
+ try std.testing.expect(!pardes.supportsPath("main.rs")); // minimal: c, c++, zig
+ try std.testing.expectEqualStrings("a.c", pardes.pathFor("C").?);
+ try std.testing.expectEqualStrings("a.cpp", pardes.pathFor("c++").?);
+ try std.testing.expectEqual(null, pardes.pathFor("klingon"));
+
+ var c = try pardes.analyzeSource(gpa, "main.c", c_source, true, true);
+ defer c.deinit(gpa);
+ try std.testing.expectEqual(c_source.len, c.colors.len);
+ try expectSyn(c.colors, c_source, "#include", .keyword);
+ try expectSyn(c.colors, c_source, "<stdio.h>", .string);
+ try expectSyn(c.colors, c_source, "// say it", .comment);
+ try expectSyn(c.colors, c_source, "main", .none);
+ try expectSyn(c.colors, c_source, "\"hi %d\\n\"", .string);
+ try expectSyn(c.colors, c_source, "42", .number);
+ try expectSyn(c.colors, c_source, "return", .keyword);
+ try std.testing.expectEqual(1, c.declarations.len);
+ const main = c.declarations[0];
+ try std.testing.expectEqualStrings("int main(void) {", c_source[main.start_byte..main.header_end_byte]);
+ try std.testing.expectEqual('}', c_source[main.end_byte - 1]);
+ try std.testing.expectEqual(3, main.start_line);
+ try std.testing.expectEqual(6, main.end_line);
+ try std.testing.expectEqualStrings("int main(void) {", pardes.lineSlice(c_source, main.start_line));
+ try std.testing.expectEqual(main.start_byte, pardes.lineStartOffset(c_source, main.start_line));
+ try std.testing.expectEqual(8, pardes.lineCount(c_source));
+
+ const zig = try pardes.highlightFileRange(gpa, pardes.pathFor("zig").?, zig_source, 0, zig_source.len);
+ defer gpa.free(zig);
+ try std.testing.expectEqual(zig_source.len, zig.len);
+ try expectSyn(zig, zig_source, "fn", .keyword);
+ try expectSyn(zig, zig_source, "\"std\"", .string);
+ try expectSyn(zig, zig_source, "// say it", .comment);
+ try expectSyn(zig, zig_source, "42", .number);
+ try expectSyn(zig, zig_source, "main", .none);
+ const outline = try pardes.analyzeSource(gpa, "main.zig", zig_source, true, false);
+ defer gpa.free(outline.declarations);
+ try std.testing.expectEqual(1, outline.declarations.len);
+ try std.testing.expectEqualStrings("pub fn main() void {", zig_source[outline.declarations[0].start_byte..outline.declarations[0].header_end_byte]);
+
+ // the tree: a statement from a byte in it, and the nodes themselves
+ const tree = pardes.parse("main.c", c_source).?;
+ defer tree.destroy();
+ try std.testing.expectEqualStrings("translation_unit", tree.rootNode().kind());
+ const at = std.mem.indexOf(u8, c_source, "return").?;
+ var span: [1]pardes.Span = undefined;
+ try std.testing.expectEqual(1, pardes.walkNodes(tree, .expand_node, at, at, at, &span));
+ try std.testing.expectEqualStrings("return", c_source[span[0].from..span[0].to]);
+ try std.testing.expectEqual(1, pardes.walkNodes(tree, .expand_node, span[0].from, span[0].to, at, &span));
+ try std.testing.expectEqualStrings("return 0;", c_source[span[0].from..span[0].to]);
+ try std.testing.expectEqual(null, pardes.parse("main.rs", "fn main() {}"));
+
+ const orchard = pardes.theme("Orchard").?;
+ try std.testing.expectEqual(orchard, &pardes.themes[0]);
+ try std.testing.expectEqual([3]u8{ 0x0d, 0x14, 0x10 }, pardes.page(orchard));
+ try std.testing.expectEqual([3]u8{ 0xe5, 0xee, 0xe5 }, pardes.ink(orchard));
+ try std.testing.expectEqual([3]u8{ 0xc9, 0xa7, 0xed }, pardes.synInk(orchard, .keyword).?);
+ try std.testing.expectEqual([3]u8{ 0xb8, 0xd8, 0x8a }, pardes.synInk(orchard, .string).?);
+ try std.testing.expectEqual([3]u8{ 0xf8, 0xc0, 0x82 }, pardes.synInk(orchard, .number).?);
+ try std.testing.expectEqual([3]u8{ 0xb9, 0xf4, 0x80 }, pardes.synInk(orchard, .comment).?);
+ try std.testing.expectEqual(null, pardes.synInk(orchard, .none));
+ // `dark` has no page of its own, and an imported palette is in the ring
+ try std.testing.expectEqual(pardes.theme("dark").?.tag_bg, pardes.page(pardes.theme("dark").?));
+ try std.testing.expect(pardes.theme("gruvbox") != null);
+ 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);
+}