deno.land / std@0.224.0 / internal / diff_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { diff, diffstr, DiffType } from "./diff.ts";import { assertEquals } from "../assert/assert_equals.ts";
Deno.test({ name: "diff() with empty values", fn() { assertEquals(diff([], []), []); },});
Deno.test({ name: 'diff() "a" vs "b"', fn() { assertEquals(diff(["a"], ["b"]), [ { type: DiffType.removed, value: "a" }, { type: DiffType.added, value: "b" }, ]); },});
Deno.test({ name: 'diff() "a" vs "a"', fn() { assertEquals(diff(["a"], ["a"]), [{ type: DiffType.common, value: "a" }]); },});
Deno.test({ name: 'diff() "a" vs ""', fn() { assertEquals(diff(["a"], []), [{ type: DiffType.removed, value: "a" }]); },});
Deno.test({ name: 'diff() "" vs "a"', fn() { assertEquals(diff([], ["a"]), [{ type: DiffType.added, value: "a" }]); },});
Deno.test({ name: 'diff() "a" vs "a, b"', fn() { assertEquals(diff(["a"], ["a", "b"]), [ { type: DiffType.common, value: "a" }, { type: DiffType.added, value: "b" }, ]); },});
Deno.test({ name: 'diff() "strength" vs "string"', fn() { assertEquals(diff(Array.from("strength"), Array.from("string")), [ { type: DiffType.common, value: "s" }, { type: DiffType.common, value: "t" }, { type: DiffType.common, value: "r" }, { type: DiffType.removed, value: "e" }, { type: DiffType.added, value: "i" }, { type: DiffType.common, value: "n" }, { type: DiffType.common, value: "g" }, { type: DiffType.removed, value: "t" }, { type: DiffType.removed, value: "h" }, ]); },});
Deno.test({ name: 'diff() "strength" vs ""', fn() { assertEquals(diff(Array.from("strength"), Array.from("")), [ { type: DiffType.removed, value: "s" }, { type: DiffType.removed, value: "t" }, { type: DiffType.removed, value: "r" }, { type: DiffType.removed, value: "e" }, { type: DiffType.removed, value: "n" }, { type: DiffType.removed, value: "g" }, { type: DiffType.removed, value: "t" }, { type: DiffType.removed, value: "h" }, ]); },});
Deno.test({ name: 'diff() "" vs "strength"', fn() { assertEquals(diff(Array.from(""), Array.from("strength")), [ { type: DiffType.added, value: "s" }, { type: DiffType.added, value: "t" }, { type: DiffType.added, value: "r" }, { type: DiffType.added, value: "e" }, { type: DiffType.added, value: "n" }, { type: DiffType.added, value: "g" }, { type: DiffType.added, value: "t" }, { type: DiffType.added, value: "h" }, ]); },});
Deno.test({ name: 'diff() "abc", "c" vs "abc", "bcd", "c"', fn() { assertEquals(diff(["abc", "c"], ["abc", "bcd", "c"]), [ { type: DiffType.common, value: "abc" }, { type: DiffType.added, value: "bcd" }, { type: DiffType.common, value: "c" }, ]); },});
Deno.test({ name: 'diff() "a b c d" vs "a b x d e" (diffstr)', fn() { const diffResult = diffstr( [..."abcd"].join("\n"), [..."abxde"].join("\n"), ); assertEquals(diffResult, [ { type: DiffType.common, value: "a\\n\n" }, { type: DiffType.common, value: "b\\n\n" }, { type: DiffType.added, value: "x\\n\n", details: [ { type: DiffType.added, value: "x" }, { type: DiffType.common, value: "\\" }, { type: DiffType.common, value: "n" }, { type: DiffType.common, value: "\n" }, ], }, { type: DiffType.added, value: "d\\n\n", details: [ { type: DiffType.common, value: "d" }, { type: DiffType.added, value: "\\" }, { type: DiffType.added, value: "n" }, { type: DiffType.common, value: "\n" }, ], }, { type: DiffType.added, value: "e\n" }, { type: DiffType.removed, value: "c\\n\n", details: [ { type: DiffType.removed, value: "c" }, { type: DiffType.common, value: "\\" }, { type: DiffType.common, value: "n" }, { type: DiffType.common, value: "\n" }, ], }, { type: DiffType.removed, value: "d\n", details: [ { type: DiffType.common, value: "d" }, { type: DiffType.common, value: "\n" }, ], }, ]); },});
Deno.test({ name: `diff() "3.14" vs "2.71" (diffstr)`, fn() { const diffResult = diffstr("3.14", "2.71"); assertEquals(diffResult, [ { type: DiffType.removed, value: "3.14\n", details: [ { type: DiffType.removed, value: "3", }, { type: DiffType.common, value: ".", }, { type: DiffType.removed, value: "14", }, { type: DiffType.common, value: "\n", }, ], }, { type: DiffType.added, value: "2.71\n", details: [ { type: DiffType.added, value: "2", }, { type: DiffType.common, value: ".", }, { type: DiffType.added, value: "71", }, { type: DiffType.common, value: "\n", }, ], }, ]); },});
Deno.test({ name: `diff() single line "a b" vs "c d" (diffstr)`, fn() { const diffResult = diffstr("a b", "c d"); assertEquals(diffResult, [ { type: DiffType.removed, value: "a b\n", details: [ { type: DiffType.removed, value: "a" }, { type: DiffType.removed, value: " " }, { type: DiffType.removed, value: "b" }, { type: DiffType.common, value: "\n" }, ], }, { type: DiffType.added, value: "c d\n", details: [ { type: DiffType.added, value: "c" }, { type: DiffType.added, value: " " }, { type: DiffType.added, value: "d" }, { type: DiffType.common, value: "\n" }, ], }, ]); },});
Deno.test({ name: `diff() single line, different word length "a bc" vs "cd e" (diffstr)`, fn() { const diffResult = diffstr("a bc", "cd e"); assertEquals(diffResult, [ { type: DiffType.removed, value: "a bc\n", details: [ { type: DiffType.removed, value: "a" }, { type: DiffType.removed, value: " " }, { type: DiffType.removed, value: "bc" }, { type: DiffType.common, value: "\n" }, ], }, { type: DiffType.added, value: "cd e\n", details: [ { type: DiffType.added, value: "cd" }, { type: DiffType.added, value: " " }, { type: DiffType.added, value: "e" }, { type: DiffType.common, value: "\n" }, ], }, ]); },});
Deno.test({ name: `diff() "\\b\\f\\r\\t\\v\\n" vs "\\r\\n" (diffstr)`, fn() { const diffResult = diffstr("\b\f\r\t\v\n", "\r\n"); assertEquals(diffResult, [ { type: DiffType.removed, value: "\\b\\f\\r\\t\\v\\n\n", details: [ { type: DiffType.common, value: "\\" }, { type: DiffType.removed, value: "b" }, { type: DiffType.removed, value: "\\" }, { type: DiffType.removed, value: "f" }, { type: DiffType.removed, value: "\\" }, { type: DiffType.common, value: "r" }, { type: DiffType.common, value: "\\" }, { type: DiffType.removed, value: "t" }, { type: DiffType.removed, value: "\\" }, { type: DiffType.removed, value: "v" }, { type: DiffType.removed, value: "\\" }, { type: DiffType.common, value: "n" }, { type: DiffType.common, value: "\n" }, ], }, { type: DiffType.added, value: "\\r\\n\r\n", details: [ { type: DiffType.common, value: "\\" }, { type: DiffType.common, value: "r" }, { type: DiffType.common, value: "\\" }, { type: DiffType.common, value: "n" }, { type: DiffType.added, value: "\r" }, { type: DiffType.common, value: "\n" }, ], }, { type: DiffType.common, value: "\n" }, ]); },});
Deno.test({ name: "diff() multiline with more removed lines", fn() { const diffResult = diffstr("a\na", "e"); assertEquals(diffResult, [ { type: DiffType.removed, value: "a\\n\n", }, { type: DiffType.removed, value: "a\n", details: [ { type: DiffType.removed, value: "a" }, { type: DiffType.common, value: "\n" }, ], }, { type: DiffType.added, value: "e\n", details: [ { type: DiffType.added, value: "e" }, { type: DiffType.common, value: "\n" }, ], }, ]); },});
Version Info