diff options
Diffstat (limited to 'test/lib')
| -rw-r--r-- | test/lib/bench.zig | 80 | ||||
| -rw-r--r-- | test/lib/build.zig | 34 | ||||
| -rw-r--r-- | test/lib/build.zig.zon | 10 | ||||
| -rw-r--r-- | test/lib/editor.zig | 241 | ||||
| -rw-r--r-- | test/lib/lib.zig | 161 |
5 files changed, 0 insertions, 526 deletions
diff --git a/test/lib/bench.zig b/test/lib/bench.zig deleted file mode 100644 index 7e945064..00000000 --- a/test/lib/bench.zig +++ /dev/null @@ -1,80 +0,0 @@ -//! What a key costs on the library (`zig build bench -- <KiB> <keys>`, Debug -//! or -Doptimize): a text of that size in the C grammar's shape, a key -//! typed into its middle `keys` times, each with an arena of its own; the -//! 4 KiB pages of the value a typed character changes; a full parse of the -//! text. `keys` 0 makes the text alone, to count what the keys add; a -//! third argument, `pages`, counts the pages (a copy and a compare a key, -//! which an instruction count of the keys leaves out). -const std = @import("std"); -const pe = @import("pardes-editor"); - -const Ed = pe.Editor(.{ .text = 128 * 1024, .undo = 512 * 1024 }); - -extern fn tree_sitter_c() *const pe.syntax.ts.Language; - -fn now() u64 { - var ts: std.c.timespec = undefined; - _ = std.c.clock_gettime(.MONOTONIC, &ts); - return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec)); -} - -pub fn main(init: std.process.Init) !void { - const gpa = std.heap.page_allocator; - const args = try init.minimal.args.toSlice(init.arena.allocator()); - const kib = try std.fmt.parseInt(usize, args[1], 10); - const keys = try std.fmt.parseInt(usize, args[2], 10); - const count_pages = args.len > 3; - var text: std.ArrayList(u8) = .empty; - var i: usize = 0; - while (text.items.len < kib * 1024) : (i += 1) try text.print(gpa, "int f{d}(int x) {{ return x + {d}; }}\n", .{ i, i }); - text.items.len = kib * 1024; - const ed = try gpa.create(Ed); - ed.* = .{}; - try ed.load(text.items); - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - // to the middle, into insert mode - for ([_]pe.Key{ .{ .cp = '%', .text = "%" }, .{ .cp = ';', .text = ";" } }) |k| _ = ed.key(arena.allocator(), null, k); - var to_middle: [8]u8 = undefined; - for (std.fmt.bufPrint(&to_middle, "{d}G", .{text.items.len / 2 / 32}) catch unreachable) |c| _ = ed.key(arena.allocator(), null, .{ .cp = c, .text = &.{c} }); - _ = ed.key(arena.allocator(), null, .{ .cp = 'i', .text = "i" }); - _ = arena.reset(.free_all); - if (keys == 0) return; - - const before = try gpa.create(Ed); - var pages_max: usize = 0; - var pages_sum: usize = 0; - var key_ns: u64 = 0; - for (0..keys) |n| { - if (count_pages) before.* = ed.*; - const t0 = now(); - const out = ed.key(arena.allocator(), null, .{ .cp = 'a' + @as(u21, @intCast(n % 26)), .text = &.{'a' + @as(u8, @intCast(n % 26))} }); - key_ns += now() - t0; - if (out != .done) return error.NotTyped; - _ = arena.reset(.retain_capacity); - if (!count_pages) continue; - const a = std.mem.asBytes(before); - const b = std.mem.asBytes(ed); - var pages: usize = 0; - var at: usize = 0; - while (at < a.len) : (at += 4096) { - const end = @min(at + 4096, a.len); - if (!std.mem.eql(u8, a[at..end], b[at..end])) pages += 1; - } - pages_max = @max(pages_max, pages); - pages_sum += pages; - } - const per_key = key_ns / keys; - - pe.syntax.start(std.heap.smp_allocator); // tree-sitter mallocs: not a page each - const lang: pe.Language = .{ .name = "c-as-dsl", .ext = ".cdsl", .ts = tree_sitter_c() }; - var syn: pe.Syntax = .{}; - // the parser and its queries made once, then the parse alone timed - _ = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree; - syn.tree.?.destroy(); - syn.tree = null; - const p0 = now(); - _ = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree; - const parse = now() - p0; - std.debug.print("{d} KiB: {d} keys, {d} ns a key, pages changed a key {d} max {d:.2} mean, value {d} bytes, full parse {d} ns\n", .{ kib, keys, per_key, pages_max, @as(f64, @floatFromInt(pages_sum)) / @as(f64, @floatFromInt(keys)), @sizeOf(Ed), parse }); -} diff --git a/test/lib/build.zig b/test/lib/build.zig deleted file mode 100644 index 171812e9..00000000 --- a/test/lib/build.zig +++ /dev/null @@ -1,34 +0,0 @@ -//! 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, and -//! `pardes-editor`, the text pane as a library, which it asks for. -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, .grammars = .minimal, .editor = true }); - const step = b.step("test", "colour, outline and walk a C and a Zig source through pardes's library, and edit a text with its editor"); - // A file belongs to one module: the syntax alone, and the editor (which - // has it as `.syntax`), each a test of its own. - for ([_][2][]const u8{ .{ "lib.zig", "pardes-syntax" }, .{ "editor.zig", "pardes-editor" } }) |t| { - const tests = b.addTest(.{ .root_module = b.createModule(.{ - .target = target, - .optimize = optimize, - .root_source_file = b.path(t[0]), - .imports = &.{.{ .name = t[1], .module = pardes.module(t[1]) }}, - }) }); - step.dependOn(&b.addRunArtifact(tests).step); - } - // What a key costs, a page count and a parse: `zig build bench -- <KiB> <keys>`. - const bench = b.addExecutable(.{ .name = "bench", .root_module = b.createModule(.{ - .target = target, - .optimize = optimize, - .root_source_file = b.path("bench.zig"), - .imports = &.{.{ .name = "pardes-editor", .module = pardes.module("pardes-editor") }}, - }) }); - const run_bench = b.addRunArtifact(bench); - run_bench.addPassthruArgs(); - b.step("bench", "a key typed into the library's editor at a size, its pages and a parse").dependOn(&run_bench.step); -} diff --git a/test/lib/build.zig.zon b/test/lib/build.zig.zon deleted file mode 100644 index d8c31c3f..00000000 --- a/test/lib/build.zig.zon +++ /dev/null @@ -1,10 +0,0 @@ -.{ - .name = .pardes_lib_test, - .version = "0.0.0", - .dependencies = .{ - .pardes = .{ .path = "../.." }, - }, - .minimum_zig_version = "0.17.1", - .paths = .{""}, - .fingerprint = 0x3f9948734dd377c7, -} diff --git a/test/lib/editor.zig b/test/lib/editor.zig deleted file mode 100644 index 22690915..00000000 --- a/test/lib/editor.zig +++ /dev/null @@ -1,241 +0,0 @@ -//! The editor as a package that depends on pardes takes it -//! (`pardes-editor`): a value of plain data a program keeps, typed into a -//! key at a time with an arena each call, copied anywhere, refused whole at -//! its capacities (docs/next-editor.md). -const std = @import("std"); -const pe = @import("pardes-editor"); - -const Ed = pe.Editor(.{ .text = 4096, .undo = 8192, .registers = 1024, .selections = 16 }); - -/// Keys in helix's notation, the few these tests type: printables, and -/// `<esc>`, `<ret>`, `<bs>`. -fn typeKeys(ed: anytype, gpa: std.mem.Allocator, keys: []const u8) !void { - var i: usize = 0; - while (i < keys.len) { - var k: pe.Key = .{ .cp = keys[i], .text = keys[i .. i + 1] }; - inline for (.{ .{ "<esc>", pe.Key.escape }, .{ "<ret>", pe.Key.enter }, .{ "<bs>", pe.Key.backspace } }) |special| { - if (std.mem.startsWith(u8, keys[i..], special[0])) k = .{ .cp = special[1] }; - } - i += if (k.text.len == 0) std.mem.indexOfScalarPos(u8, keys, i, '>').? + 1 - i else 1; - // every call its own arena, gone after it: nothing of a key outlives it - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - switch (ed.key(arena.allocator(), null, k)) { - .refused => return error.Refused, - else => {}, - } - } -} - -fn text(ed: anytype, buf: []u8) []const u8 { - return ed.read(buf); -} - -const first = "ifn main() {<ret> let x = 1;<ret>}<esc>ggxyp" ++ "wwCcfoo<esc>"; -const second = "%dpuuUjjAbar<esc>" ++ "ggiq<esc>u"; - -test "a value copied elsewhere, its old memory gone, types on as one never moved" { - const gpa = std.testing.allocator; - const still = try gpa.create(Ed); - defer gpa.destroy(still); - still.* = .{}; - try typeKeys(still, gpa, first); - try typeKeys(still, gpa, second); - - const before = try gpa.create(Ed); - before.* = .{}; - try typeKeys(before, gpa, first); - const moved = try gpa.create(Ed); - defer gpa.destroy(moved); - @memcpy(std.mem.asBytes(moved), std.mem.asBytes(before)); - @memset(std.mem.asBytes(before), 0xAA); - gpa.destroy(before); - try typeKeys(moved, gpa, second); - - try std.testing.expectEqualSlices(u8, std.mem.asBytes(still), std.mem.asBytes(moved)); - var buf: [4096]u8 = undefined; - // `%d` then `p`, both undone, the delete redone: `A` types into nothing - try std.testing.expectEqualStrings("bar", text(moved, &buf)); -} - -test "undo goes back as far as the ring holds, and redo all the way again" { - const gpa = std.testing.allocator; - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - const xs: [1000]u8 = @splat('x'); - try ed.load(&xs); - // thirty edits, each a snapshot of a thousand bytes: an 8 KiB ring - // keeps the last few - for (0..30) |_| try typeKeys(ed, gpa, "ia<esc>"); - var buf: [4096]u8 = undefined; - var last: [4096]u8 = undefined; - const final = text(ed, &last); - try std.testing.expectEqual(1030, final.len); - try std.testing.expect(ed.undo.count < 30); - const kept = ed.undo.count; - for (0..40) |_| try typeKeys(ed, gpa, "u"); - try std.testing.expectEqual(1030 - kept, text(ed, &buf).len); - for (0..40) |_| try typeKeys(ed, gpa, "U"); - try std.testing.expectEqualStrings(final, text(ed, &buf)); -} - -test "a key whose text would not fit is refused, and the value is as it was" { - const gpa = std.testing.allocator; - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - const ys: [5000]u8 = @splat('y'); - try std.testing.expectError(error.TextFull, ed.load(&ys)); - try ed.load(ys[0..4094]); - try typeKeys(ed, gpa, "iab"); - const kept = try gpa.create(Ed); - defer gpa.destroy(kept); - kept.* = ed.*; - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - try std.testing.expectEqual(pe.Outcome{ .refused = .text_full }, ed.key(arena.allocator(), null, .{ .cp = 'c', .text = "c" })); - try std.testing.expectEqualSlices(u8, std.mem.asBytes(kept), std.mem.asBytes(ed)); -} - -test "what is not the text's comes back, and the caller can do it" { - const gpa = std.testing.allocator; - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - try ed.load("one two one\n"); - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - const a = arena.allocator(); - try std.testing.expectEqual(pe.Role.command_line, ed.key(a, null, .{ .cp = ':', .text = ":" }).action.role); - _ = ed.key(a, null, .{ .cp = '%', .text = "%" }); - try std.testing.expectEqual(pe.Role.select_regex, ed.key(a, null, .{ .cp = 's', .text = "s" }).action.role); - _ = ed.selectRegex(a, "one", .select); - var out: [pe.max_selections]pe.Selection = undefined; - const got = try ed.ranges(a, &out); - try std.testing.expectEqual(2, got.n); - try std.testing.expectEqual(pe.Selection{ .anchor = 8, .head = 11 }, out[1]); - try std.testing.expectEqual(pe.Mode.normal, ed.mode()); - _ = ed.key(a, null, .{ .cp = 'g', .text = "g" }); - try std.testing.expectEqual(pe.Role.goto_prefix, ed.pending().?); -} - -extern fn tree_sitter_c() *const pe.syntax.ts.Language; - -test "a language handed in at run time parses and colours the text" { - const gpa = std.testing.allocator; - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - try ed.load("int f() { return 1; }\n"); - pe.syntax.start(gpa); - defer pe.syntax.stop(); - const lang: pe.Language = .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c(), .highlights = "\"return\" @keyword (number_literal) @number" }; - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - var syn: pe.Syntax = .{}; - defer syn.forget(); - const tree = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree; - try std.testing.expectEqualStrings("translation_unit", tree.rootNode().kind()); - const colors = try pe.Syntax.colors(arena.allocator(), ed, lang, 0, ed.len()); - try std.testing.expectEqual(pe.syntax.Syn.keyword, colors[10]); - try std.testing.expectEqual(pe.syntax.Syn.number, colors[17]); - try std.testing.expectEqual(pe.syntax.Syn.none, colors[0]); -} - -test "with a parse handed in, the tree-sitter walks run in the library; without, they are the caller's" { - const gpa = std.testing.allocator; - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - try ed.load("int f() { return 1 + 2; }\n"); - pe.syntax.start(gpa); - defer pe.syntax.stop(); - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - const a = arena.allocator(); - const alt_o: pe.Key = .{ .cp = 'o', .alt = true }; - const alt_i: pe.Key = .{ .cp = 'i', .alt = true }; - try std.testing.expectEqual(pe.Role.expand_node, ed.key(a, null, alt_o).action.role); - var syn: pe.Syntax = .{ .lang = .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c(), .textobjects = "(function_definition body: (_) @function.inside) @function.around" } }; - defer syn.forget(); - for ("21l") |c| _ = ed.key(a, &syn, .{ .cp = c, .text = &.{c} }); - var out: [pe.max_selections]pe.Selection = undefined; - // `2`, then `1 + 2`, then `return 1 + 2;`; Alt-i goes back the way it - // came (helix's object_selections: not the first child, `1`) - for ([_]struct { pe.Key, pe.Selection }{ - .{ alt_o, .{ .anchor = 17, .head = 22 } }, - .{ alt_o, .{ .anchor = 10, .head = 23 } }, - .{ alt_i, .{ .anchor = 17, .head = 22 } }, - .{ alt_i, .{ .anchor = 21, .head = 22 } }, - }) |step| { - try std.testing.expectEqual(pe.Outcome.done, ed.key(a, &syn, step[0])); - _ = try ed.ranges(a, &out); - try std.testing.expectEqual(step[1], out[0]); - } - // the grammar's textobjects handed in: `maf`, the function around - for ("maf") |c| _ = ed.key(a, &syn, .{ .cp = c, .text = &.{c} }); - _ = try ed.ranges(a, &out); - try std.testing.expectEqual(pe.Selection{ .anchor = 0, .head = 25 }, out[0]); -} - -test "a byte range replaced is one step to undo, the ranges following it; a drag joined is one step, refused whole when it does not fit" { - const gpa = std.testing.allocator; - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - try ed.load("a = 12 + 3;\n"); - try typeKeys(ed, gpa, "9l"); - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - const a = arena.allocator(); - var buf: [4096]u8 = undefined; - var out: [pe.max_selections]pe.Selection = undefined; - // a drag on the number: its first frame a step, the rest joined to it - try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 6, "130", false)); - _ = try ed.ranges(a, &out); - try std.testing.expectEqual(pe.Selection{ .anchor = 10, .head = 11 }, out[0]); // still on `3` - const steps = ed.undo.count; - try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "131", true)); - try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "14", true)); - try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf)); - try std.testing.expectEqual(steps, ed.undo.count); - _ = try ed.ranges(a, &out); - try std.testing.expectEqual(pe.Selection{ .anchor = 9, .head = 10 }, out[0]); - try typeKeys(ed, gpa, "u"); - try std.testing.expectEqualStrings("a = 12 + 3;\n", text(ed, &buf)); - try typeKeys(ed, gpa, "U"); - try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf)); - // what would not fit: refused, the value as it was - const kept = try gpa.create(Ed); - defer gpa.destroy(kept); - kept.* = ed.*; - const ys: [4096]u8 = @splat('y'); - try std.testing.expectEqual(pe.Outcome{ .refused = .text_full }, ed.replace(a, 0, 1, &ys, false)); - try std.testing.expectEqualSlices(u8, std.mem.asBytes(kept), std.mem.asBytes(ed)); -} - -test "a new line's indent comes from the parse handed in, where its language has an indent query; else the plain one" { - const gpa = std.testing.allocator; - pe.syntax.start(gpa); - defer pe.syntax.stop(); - var buf: [4096]u8 = undefined; - // the plain indent is a level more after `)`: what a language whose - // lines are calls does not want, and says by an indent query - for ([_]struct { ?pe.Language, []const u8 }{ - .{ null, "f(1)\n y" }, - .{ .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c() }, "f(1)\n y" }, - .{ .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c(), .indents = "(compound_statement) @indent \"}\" @outdent" }, "f(1)\ny" }, - }) |case| { - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - var syn: pe.Syntax = .{ .lang = case[0] }; - defer syn.forget(); - try ed.load("f(1)"); - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - for ([_]pe.Key{ .{ .cp = 'A', .text = "A" }, .{ .cp = pe.Key.enter }, .{ .cp = 'y', .text = "y" }, .{ .cp = pe.Key.escape } }) |k| _ = ed.key(arena.allocator(), &syn, k); - try std.testing.expectEqualStrings(case[1], text(ed, &buf)); - } -} diff --git a/test/lib/lib.zig b/test/lib/lib.zig deleted file mode 100644 index 7e297920..00000000 --- a/test/lib/lib.zig +++ /dev/null @@ -1,161 +0,0 @@ -//! 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); -} |
