summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/lib/build.zig18
-rw-r--r--test/lib/build.zig.zon10
-rw-r--r--test/lib/lib.zig107
3 files changed, 135 insertions, 0 deletions
diff --git a/test/lib/build.zig b/test/lib/build.zig
new file mode 100644
index 00000000..6cd8d6e5
--- /dev/null
+++ b/test/lib/build.zig
@@ -0,0 +1,18 @@
+//! A package that depends on pardes for its library, as a program that draws
+//! source text itself would: pardes's `lib-test` step runs `zig build test`
+//! here, so the `pardes-syntax` module is built the way such a package
+//! builds it (build.zig's `library`), with the minimal grammars.
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+ const target = b.standardTargetOptions(.{});
+ const optimize = b.standardOptimizeOption(.{});
+ const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, .@"tree-sitter" = .minimal });
+ const tests = b.addTest(.{ .root_module = b.createModule(.{
+ .target = target,
+ .optimize = optimize,
+ .root_source_file = b.path("lib.zig"),
+ .imports = &.{.{ .name = "pardes-syntax", .module = pardes.module("pardes-syntax") }},
+ }) });
+ b.step("test", "colour, outline and walk a C and a Zig source through pardes's library").dependOn(&b.addRunArtifact(tests).step);
+}
diff --git a/test/lib/build.zig.zon b/test/lib/build.zig.zon
new file mode 100644
index 00000000..5bb45dae
--- /dev/null
+++ b/test/lib/build.zig.zon
@@ -0,0 +1,10 @@
+.{
+ .name = .pardes_lib_test,
+ .version = "0.0.0",
+ .dependencies = .{
+ .pardes = .{ .path = "../.." },
+ },
+ .minimum_zig_version = "0.17.0",
+ .paths = .{""},
+ .fingerprint = 0x3f9948734dd377c7,
+}
diff --git a/test/lib/lib.zig b/test/lib/lib.zig
new file mode 100644
index 00000000..a3f99b56
--- /dev/null
+++ b/test/lib/lib.zig
@@ -0,0 +1,107 @@
+//! 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 "every name the library has compiles outside pardes" {
+ std.testing.refAllDecls(pardes);
+}