Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(internal): improve test coverage #4779

Merged
merged 8 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions internal/build_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import type { DiffResult, DiffType } from "./types.ts";
*/
function createColor(
diffType: DiffType,
background = false,
): (s: string) => string {
/**
* TODO(@littledivy): Remove this when we can detect true color terminals. See
* https://github.com/denoland/deno_std/issues/2575.
*/
background = false;
background = false,
): (s: string) => string {
switch (diffType) {
case "added":
return (s) => background ? bgGreen(white(s)) : green(bold(s));
Expand Down Expand Up @@ -112,3 +111,9 @@ export function buildMessage(
messages.push(...(stringDiff ? [diffMessages.join("")] : diffMessages), "");
return messages;
}

/** Used internally for testing. */
export const _internals = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's export these functions instead of putting them in these _internals objects. It'll be much cleaner. Ditto for the other files in this package. This is fine to do here as this is purely an internal package. You'll have to add @example tags to the newly exported symbols to make the doc checker (deno task lint:docs) too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha!
for some reason i was unsure if we would ever want to export the other functions in this package.
implemented as suggested.
love the way how the doc checker works bth!

Copy link
Contributor

@iuioiua iuioiua May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you able to fix the functions in this file too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh... i'm so sorry.
cleanup for build_message is done as well.

createColor,
createSign,
};
43 changes: 43 additions & 0 deletions internal/build_message_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "@std/assert";
import { bgGreen, bgRed, bold, gray, green, red, white } from "@std/fmt/colors";
import { _internals, buildMessage } from "./build_message.ts";

const { createColor, createSign } = _internals;

Deno.test("buildMessage()", () => {
const messages = [
"",
"",
` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${
green(bold("Expected"))
}`,
"",
"",
];
assertEquals(buildMessage([]), [...messages, ""]);
assertEquals(
buildMessage([{ type: "added", value: "foo" }, {
type: "removed",
value: "bar",
}]),
[...messages, green(bold("+ foo")), red(bold("- bar")), ""],
);
});

Deno.test("createColor()", () => {
assertEquals(createColor("added")("foo"), green(bold("foo")));
assertEquals(createColor("removed")("foo"), red(bold("foo")));
assertEquals(createColor("common")("foo"), white("foo"));
assertEquals(createColor("added", true)("foo"), bgGreen(white("foo")));
assertEquals(createColor("removed", true)("foo"), bgRed(white("foo")));
assertEquals(createColor("common", true)("foo"), white("foo"));
});

Deno.test("createSign()", () => {
assertEquals(createSign("added"), "+ ");
assertEquals(createSign("removed"), "- ");
assertEquals(createSign("common"), " ");
// deno-lint-ignore no-explicit-any
assertEquals(createSign("unknown" as any), " ");
});
28 changes: 17 additions & 11 deletions internal/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ function createCommon<T>(A: T[], B: T[]): T[] {
}

function assertFp(value: unknown): asserts value is FarthestPoint {
if (value === undefined) {
if (
value == null ||
typeof value !== "object" ||
typeof (value as FarthestPoint)?.y !== "number" ||
typeof (value as FarthestPoint)?.id !== "number"
) {
throw new Error("Unexpected missing FarthestPoint");
}
}
Expand Down Expand Up @@ -147,22 +152,17 @@ function createFp(
*/
export function diff<T>(A: T[], B: T[]): DiffResult<T>[] {
const prefixCommon = createCommon(A, B);
const suffixCommon = createCommon(
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
A.slice(prefixCommon.length),
B.slice(prefixCommon.length),
);
A = A.slice(prefixCommon.length, -suffixCommon.length || undefined);
B = B.slice(prefixCommon.length, -suffixCommon.length || undefined);
A = A.slice(prefixCommon.length);
B = B.slice(prefixCommon.length);
const swapped = B.length > A.length;
[A, B] = swapped ? [B, A] : [A, B];
const M = A.length;
const N = B.length;
if (!M && !N && !suffixCommon.length && !prefixCommon.length) return [];
if (!M && !N && !prefixCommon.length) return [];
if (!N) {
return [
...prefixCommon.map((value) => ({ type: "common", value })),
...A.map((value) => ({ type: swapped ? "added" : "removed", value })),
...suffixCommon.map((value) => ({ type: "common", value })),
] as DiffResult<T>[];
}
const offset = N;
Expand All @@ -187,7 +187,6 @@ export function diff<T>(A: T[], B: T[]): DiffResult<T>[] {
): FarthestPoint {
const M = A.length;
const N = B.length;
if (k < -N || M < k) return { y: -1, id: -1 };
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
const fp = createFp(k, M, routes, diffTypesPtrOffset, ptr, slide, down);
ptr = fp.id;
while (fp.y + k < M && fp.y < N && A[fp.y + k] === B[fp.y]) {
Expand Down Expand Up @@ -222,6 +221,13 @@ export function diff<T>(A: T[], B: T[]): DiffResult<T>[] {
return [
...prefixCommon.map((value) => ({ type: "common", value })),
...backTrace(A, B, currentFp, swapped, routes, diffTypesPtrOffset),
...suffixCommon.map((value) => ({ type: "common", value })),
] as DiffResult<T>[];
}

/** Used internally for testing */
export const _internals = {
assertFp,
backTrace,
createCommon,
createFp,
};
34 changes: 13 additions & 21 deletions internal/diff_str.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ function unescape(string: string): string {
}

const WHITESPACE_SYMBOLS = /([^\S\r\n]+|[()[\]{}'"\r\n]|\b)/;
const EXT_LATIN_CHARS =
/^[a-zA-Z\u{C0}-\u{FF}\u{D8}-\u{F6}\u{F8}-\u{2C6}\u{2C8}-\u{2D7}\u{2DE}-\u{2FF}\u{1E00}-\u{1EFF}]+$/u;

/**
* Tokenizes a string into an array of tokens.
Expand All @@ -38,22 +36,9 @@ const EXT_LATIN_CHARS =
*/
function tokenize(string: string, wordDiff = false): string[] {
if (wordDiff) {
const tokens = string.split(WHITESPACE_SYMBOLS).filter((token) => token);
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
for (let i = 0; i < tokens.length - 1; i++) {
const token = tokens[i];
const tokenPlusTwo = tokens[i + 2];
if (
!tokens[i + 1] &&
token &&
tokenPlusTwo &&
EXT_LATIN_CHARS.test(token) &&
EXT_LATIN_CHARS.test(tokenPlusTwo)
) {
tokens[i] += tokenPlusTwo;
tokens.splice(i + 1, 2);
i--;
}
}
const tokens = string
.split(WHITESPACE_SYMBOLS)
.filter((token) => token);
return tokens;
}
mbhrznr marked this conversation as resolved.
Show resolved Hide resolved
const tokens: string[] = [];
Expand All @@ -80,8 +65,8 @@ function tokenize(string: string, wordDiff = false): string[] {
*/
function createDetails(
line: DiffResult<string>,
tokens: Array<DiffResult<string>>,
) {
tokens: DiffResult<string>[],
): DiffResult<string>[] {
return tokens.filter(({ type }) => type === line.type || type === "common")
.map((result, i, t) => {
const token = t[i - 1];
Expand Down Expand Up @@ -163,7 +148,7 @@ export function diffStr(A: string, B: string): DiffResult<string>[] {
b = bLines.shift();
const tokenized = [
tokenize(a.value, true),
tokenize(b?.value ?? "", true),
tokenize(b!.value, true),
] as [string[], string[]];
if (hasMoreRemovedLines) tokenized.reverse();
tokens = diff(tokenized[0], tokenized[1]);
Expand All @@ -184,3 +169,10 @@ export function diffStr(A: string, B: string): DiffResult<string>[] {

return diffResult;
}

/** Used internally for testing */
export const _internals = {
createDetails,
tokenize,
unescape,
};
72 changes: 71 additions & 1 deletion internal/diff_str_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { diffStr } from "./diff_str.ts";
import { _internals, diffStr } from "./diff_str.ts";
import { assertEquals } from "@std/assert/assert-equals";

const { createDetails, tokenize, unescape } = _internals;

Deno.test({
name: 'diff() "a" vs "b" (diffstr)',
fn() {
const diffResult = diffStr("a", "b");
assertEquals(diffResult, [
{
details: [
{ type: "removed", value: "a" },
{ type: "common", value: "\n" },
],
type: "removed",
value: "a\n",
},
{
details: [
{ type: "added", value: "b" },
{ type: "common", value: "\n" },
],
type: "added",
value: "b\n",
},
]);
},
});

Deno.test({
name: 'diff() "a b c d" vs "a b x d e" (diffStr)',
fn() {
Expand Down Expand Up @@ -236,3 +263,46 @@ Deno.test({
]);
},
});

Deno.test({
name: "createDetails()",
fn() {
const tokens = [
{ type: "added", value: "a" },
{ type: "removed", value: "b" },
{ type: "common", value: "c" },
] as const;
for (const token of tokens) {
assertEquals(
createDetails(token, [...tokens]),
tokens.filter(({ type }) => type === token.type || type === "common"),
);
}
},
});

Deno.test({
name: "tokenize()",
fn() {
assertEquals(tokenize("a\nb"), ["a\n", "b"]);
assertEquals(tokenize("a\r\nb"), ["a\r\n", "b"]);
assertEquals(tokenize("a\nb\n"), ["a\n", "b\n"]);
assertEquals(tokenize("a b"), ["a b"]);
assertEquals(tokenize("a b", true), ["a", " ", "b"]);
assertEquals(tokenize("abc bcd", true), ["abc", " ", "bcd"]);
assertEquals(tokenize("abc ", true), ["abc", " "]);
},
});

Deno.test({
name: "unescape()",
fn() {
assertEquals(unescape("a\b"), "a\\b");
assertEquals(unescape("a\f"), "a\\f");
assertEquals(unescape("a\t"), "a\\t");
assertEquals(unescape("a\v"), "a\\v");
assertEquals(unescape("a\r"), "a\\r");
assertEquals(unescape("a\n"), "a\\n\n");
assertEquals(unescape("a\r\n"), "a\\r\\n\r\n");
},
});
Loading