summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/consumer/lib.zig25
-rw-r--r--test/consumer/wasm.mjs38
2 files changed, 63 insertions, 0 deletions
diff --git a/test/consumer/lib.zig b/test/consumer/lib.zig
index fa0f8aee..ea0d56fd 100644
--- a/test/consumer/lib.zig
+++ b/test/consumer/lib.zig
@@ -254,3 +254,28 @@ test "a diff stacked with its file handed in: the declaration it stands in, from
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);
}
+
+test "a row's text is its line without its end: a CRLF diff's rows have no \\r, and a CRLF file handed in still matches" {
+ const gpa = std.testing.allocator;
+ pardes.start(gpa);
+ defer pardes.stop();
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const patch_crlf = try std.mem.replaceOwned(u8, a, patch_c, "\n", "\r\n");
+ const Given = struct {
+ var text: []const u8 = "";
+ fn source(_: *const pardes.PatchCtx, _: []const u8, path: []const u8) ?pardes.PatchSource {
+ return .{ .path = path, .bytes = text };
+ }
+ };
+ Given.text = try std.mem.replaceOwned(u8, a, main_c, "\n", "\r\n");
+ // no file: the rows as an LF diff's
+ const plain = try pardes.formatPatch(&.{ .arena = a }, "", patch_crlf);
+ 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", plain.text);
+ try std.testing.expect(plain.rows[6].no_newline);
+ // a CRLF file: its lines compare with the diff's, so its declaration is found
+ const given = try pardes.formatPatch(&.{ .arena = a, .source = Given.source, .declarations = pardes.PatchCtx.parsed }, "", patch_crlf);
+ 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", given.text);
+ try std.testing.expectEqual(pardes.PatchRow.Kind.declaration, given.rows[0].kind);
+}
diff --git a/test/consumer/wasm.mjs b/test/consumer/wasm.mjs
index 9e76af0f..97b0ba6b 100644
--- a/test/consumer/wasm.mjs
+++ b/test/consumer/wasm.mjs
@@ -56,6 +56,20 @@ function patch(m, diff) {
const [p, n] = put(m, diff);
const h = m.ex.patch(p, n);
m.ex.release(p, n);
+ return read(m, h);
+}
+
+// patchFile: the same, with the text of the file the diff's first section names
+function patchFile(m, diff, text) {
+ const [p, n] = put(m, diff);
+ const [t, tn] = put(m, text);
+ const h = m.ex.patchFile(p, n, t, tn);
+ m.ex.release(p, n);
+ m.ex.release(t, tn);
+ return read(m, h);
+}
+
+function read(m, h) {
const v = new DataView(m.mem());
const [text, textLen, styles, rows, count] = [0, 4, 8, 12, 16].map((o) => v.getUint32(h + o, true));
const bytes = new Uint8Array(m.mem(), text);
@@ -109,5 +123,29 @@ for (const file of process.argv.slice(2)) {
expect(`${file} highlight another language`, [...new Uint8Array(m.mem(), other, tn)].every((b) => b === 0), true);
}
}
+// patchFile (a module with a language): xit's bare `@@`, and the file's
+// text: the declaration the hunk sits in, from the file's own parse
+const oldRs = "fn total(items: &[u32]) -> u32 {\n let mut sum = 0;\n let mut n = 0;\n for i in items { sum += i; }\n n += 1;\n sum\n}\n";
+const newRs = "// sums\n" + oldRs.replace("sum += i;", "sum += *i;");
+const bare = "diff --git a/src/lib.rs b/src/lib.rs\n--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ -0,0 +1 @@\n+// sums\n@@ -3,3 +4,3 @@\n let mut n = 0;\n- for i in items { sum += i; }\n+ for i in items { sum += *i; }\n n += 1;\n";
+const more = bare + "diff --git a/src/b.rs b/src/b.rs\n--- a/src/b.rs\n+++ b/src/b.rs\n@@ -3,3 +4,3 @@\n let mut n = 0;\n- for i in items { sum += i; }\n+ for i in items { sum += *i; }\n n += 1;\n";
+const language = process.argv.slice(2).map(load).find((m) => m.ex.highlight);
+if (language) {
+ const lines = (p) => p.rows.map((r) => [r.kind, r.text]);
+ const plain = lines(patch(language, bare));
+ // the new file's text: its numbers
+ expect("patchFile new text", lines(patchFile(language, bare, newRs)), [
+ ["hunk", "src/lib.rs:1"], ["added", "+// sums"],
+ ["declaration", "src/lib.rs:2"], ["declaration", " fn total(items: &[u32]) -> u32 {"],
+ ["hunk", "src/lib.rs:5 *"], ["context", " let mut n = 0;"], ["removed", "- for i in items { sum += i; }"], ["added", "+ for i in items { sum += *i; }"], ["context", " n += 1;"],
+ ]);
+ // the old file's: found on its side, and numbered so
+ expect("patchFile old text", lines(patchFile(language, bare, oldRs)).slice(2, 5), [["declaration", "src/lib.rs:1"], ["declaration", " fn total(items: &[u32]) -> u32 {"], ["hunk", "src/lib.rs:4 *"]]);
+ // a text the diff is not of: the rows as with none
+ expect("patchFile another text", lines(patchFile(language, bare, "nothing like it\n")), plain);
+ // a second file: as with no text
+ const two = lines(patchFile(language, more, newRs));
+ expect("patchFile second file", two.slice(9), lines(patch(language, more)).slice(plain.length));
+}
if (failed) process.exit(1);
console.log("wasm: the modules run");