Skip to content

Commit

Permalink
Pass the whole unit tests on Node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Mar 30, 2024
1 parent 38cfbc3 commit 31d077e
Show file tree
Hide file tree
Showing 9 changed files with 902 additions and 115 deletions.
860 changes: 794 additions & 66 deletions codegen/__snapshots__/class.test.ts.snap

Large diffs are not rendered by default.

21 changes: 20 additions & 1 deletion codegen/inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export async function* generateInspector(
inspect: typeof Deno.inspect,
options: Deno.InspectOptions,
): string => "URL " + inspect(this.id!.href, options),
[Symbol.for("nodejs.util.inspect.custom")]: (
_depth: number,
options: unknown,
inspect: (value: unknown, options: unknown) => string,
): string => "URL " + inspect(this.id!.href, options),
};
}
`;
Expand All @@ -37,7 +42,12 @@ export async function* generateInspector(
[Symbol.for("Deno.customInspect")]: (
inspect: typeof Deno.inspect,
options: Deno.InspectOptions,
): string => "URL " + inspect(v.href, options)
): string => "URL " + inspect(v.href, options),
[Symbol.for("nodejs.util.inspect.custom")]: (
_depth: number,
options: unknown,
inspect: (value: unknown, options: unknown) => string,
): string => "URL " + inspect(v.href, options),
}
: v);
`;
Expand Down Expand Up @@ -70,5 +80,14 @@ export async function* generateInspector(
const proxy = this._getCustomInspectProxy();
return ${JSON.stringify(type.name + " ")} + inspect(proxy, options);
}
[Symbol.for("nodejs.util.inspect.custom")](
_depth: number,
options: unknown,
inspect: (value: unknown, options: unknown) => string,
): string {
const proxy = this._getCustomInspectProxy();
return ${JSON.stringify(type.name + " ")} + inspect(proxy, options);
}
`;
}
10 changes: 8 additions & 2 deletions codegen/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,14 @@ export class SchemaError extends Error {

async function loadSchemaValidator(): Promise<Validator> {
const thisFile = new URL(import.meta.url);
const response = await fetch(url.join(url.dirname(thisFile), "schema.yaml"));
const content = await response.text();
const schemaFile = url.join(url.dirname(thisFile), "schema.yaml");
let content: string;
if (schemaFile.protocol !== "file:") {
const response = await fetch(schemaFile);
content = await response.text();
} else {
content = await Deno.readTextFile(schemaFile);
}
const schemaObject = parse(content);
return new Validator(schemaObject as JsonSchema);
}
Expand Down
4 changes: 3 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"imports": {
"@cfworker/json-schema": "npm:@cfworker/json-schema@^1.12.8",
"@david/which-runtime": "jsr:@david/which-runtime@^0.2.0",
"@deno/dnt": "jsr:@deno/dnt@^0.41.1",
"@fedify/fedify": "./mod.ts",
"@fedify/fedify/federation": "./federation/mod.ts",
Expand Down Expand Up @@ -62,7 +63,8 @@
"test": "deno task codegen && deno task test-without-codegen",
"coverage": "rm -rf coverage/ && deno task test --coverage && deno coverage --html coverage",
"apidoc": "deno task codegen && deno doc --html --name=Fedify --output=apidoc/ mod.ts",
"publish": "deno task codegen && deno publish"
"publish": "deno task codegen && deno publish",
"dnt": "deno task codegen && deno run -A dnt.ts"
},
"unstable": [
"kv"
Expand Down
5 changes: 5 additions & 0 deletions dnt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ await build({
"npm/esm/testing/fixtures",
{ overwrite: true },
);
for await (const entry of Deno.readDir("vocab")) {
if (!entry.isFile || !entry.name.endsWith(".yaml")) continue;
await Deno.copyFile(`vocab/${entry.name}`, `npm/esm/vocab/${entry.name}`);
}
await Deno.copyFile("codegen/schema.yaml", "npm/esm/codegen/schema.yaml");
await Deno.copyFile("CHANGES.md", "npm/CHANGES.md");
await Deno.copyFile("LICENSE", "npm/LICENSE");
await Deno.copyFile("logo.svg", "npm/logo.svg");
Expand Down
3 changes: 3 additions & 0 deletions examples/blog/import_map.g.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"imports": {
"@cfworker/json-schema": "npm:@cfworker/json-schema@^1.12.8",
"@david/which-runtime": "jsr:@david/which-runtime@^0.2.0",
"@deno/dnt": "jsr:@deno/dnt@^0.41.1",
"@fedify/fedify": "../../mod.ts",
"@fedify/fedify/federation": "../../federation/mod.ts",
Expand All @@ -10,13 +11,15 @@
"@fedify/fedify/vocab": "../../vocab/mod.ts",
"@fedify/fedify/webfinger": "../../webfinger/mod.ts",
"@fedify/fedify/x/fresh": "../../x/fresh.ts",
"@hongminhee/aitertools": "jsr:@hongminhee/aitertools@^0.6.0",
"@js-temporal/polyfill": "npm:@js-temporal/polyfill@^0.4.4",
"@phensley/language-tag": "npm:@phensley/language-tag@^1.8.0",
"@std/assert": "jsr:@std/assert@^0.220.1",
"@std/bytes": "jsr:@std/bytes@^0.220.1",
"@std/collections": "jsr:@std/collections@^0.220.1",
"@std/encoding": "jsr:@std/encoding@^0.220.1",
"@std/encoding/base64": "jsr:@std/encoding@^0.220.1/base64",
"@std/fs": "jsr:@std/fs@^0.220.1",
"@std/http/negotiation": "jsr:@std/http@^0.220.1/negotiation",
"@std/json/common": "jsr:@std/json@^0.220.1/common",
"@std/path": "jsr:@std/path@^0.220.1",
Expand Down
7 changes: 5 additions & 2 deletions runtime/langstr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Deno.test("new LanguageString()", () => {
});

Deno.test("Deno.inspect(LanguageString)", () => {
const langStr = new LanguageString("Hello", "en");
assertEquals(Deno.inspect(langStr, { colors: false }), '<en> "Hello"');
const langStr = new LanguageString("Hello, 'world'", "en");
assertEquals(
Deno.inspect(langStr, { colors: false }),
"<en> \"Hello, 'world'\"",
);
});
8 changes: 8 additions & 0 deletions runtime/langstr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ export class LanguageString extends String {
): string {
return `<${this.language.compact()}> ${inspect(this.toString(), options)}`;
}

[Symbol.for("nodejs.util.inspect.custom")](
_depth: number,
options: unknown,
inspect: (value: unknown, options: unknown) => string,
): string {
return `<${this.language.compact()}> ${inspect(this.toString(), options)}`;
}
}
99 changes: 56 additions & 43 deletions vocab/vocab.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isDeno } from "@david/which-runtime";
import { toArray } from "@hongminhee/aitertools";
import { Temporal } from "@js-temporal/polyfill";
import { parseLanguageTag } from "@phensley/language-tag";
Expand Down Expand Up @@ -245,15 +246,25 @@ Deno.test("Deno.inspect(Object)", () => {
});
assertEquals(
Deno.inspect(obj, { colors: false, sorted: true, compact: false }),
"Object {\n" +
' attribution: URL "https://example.com/foo",\n' +
" contents: [\n" +
' <en> "Hello",\n' +
' <zh> "你好"\n' +
" ],\n" +
' id: URL "https://example.com/",\n' +
' name: "Test"\n' +
"}",
isDeno
? "Object {\n" +
' attribution: URL "https://example.com/foo",\n' +
" contents: [\n" +
' <en> "Hello",\n' +
' <zh> "你好"\n' +
" ],\n" +
' id: URL "https://example.com/",\n' +
' name: "Test"\n' +
"}"
: "Object {\n" +
" attribution: URL 'https://example.com/foo',\n" +
" contents: [\n" +
" <en> 'Hello',\n" +
" <zh> '你好'\n" +
" ],\n" +
" id: URL 'https://example.com/',\n" +
" name: 'Test'\n" +
"}",
);
});

Expand Down Expand Up @@ -711,41 +722,43 @@ for (const typeUri in types) {
assertEquals(restored3, instance2);
});

Deno.test(`Deno.inspect(${type.name}) [auto]`, async (t) => {
const empty = new cls({});
assertEquals(Deno.inspect(empty), `${type.name} {}`);

const instance = new cls({
id: new URL("https://example.com/"),
...initValues,
});
await assertSnapshot(t, Deno.inspect(instance));
if (isDeno) {
Deno.test(`Deno.inspect(${type.name}) [auto]`, async (t) => {
const empty = new cls({});
assertEquals(Deno.inspect(empty), `${type.name} {}`);

const instance2 = instance.clone(
globalThis.Object.fromEntries(
type.properties.filter((p) => !areAllScalarTypes(p.range, types)).map(
(p) =>
p.functional
? [p.singularName, new URL("https://example.com/")]
: [p.pluralName, [new URL("https://example.com/")]],
const instance = new cls({
id: new URL("https://example.com/"),
...initValues,
});
await assertSnapshot(t, Deno.inspect(instance));

const instance2 = instance.clone(
globalThis.Object.fromEntries(
type.properties.filter((p) => !areAllScalarTypes(p.range, types)).map(
(p) =>
p.functional
? [p.singularName, new URL("https://example.com/")]
: [p.pluralName, [new URL("https://example.com/")]],
),
),
),
);
await assertSnapshot(t, Deno.inspect(instance2));

const instance3 = instance.clone(
globalThis.Object.fromEntries(
type.properties.filter((p) => !p.functional).map(
(p) => {
assertFalse(p.functional);
return [
p.pluralName,
[sampleValues[p.range[0]], sampleValues[p.range[0]]],
];
},
);
await assertSnapshot(t, Deno.inspect(instance2));

const instance3 = instance.clone(
globalThis.Object.fromEntries(
type.properties.filter((p) => !p.functional).map(
(p) => {
assertFalse(p.functional);
return [
p.pluralName,
[sampleValues[p.range[0]], sampleValues[p.range[0]]],
];
},
),
),
),
);
await assertSnapshot(t, Deno.inspect(instance3));
});
);
await assertSnapshot(t, Deno.inspect(instance3));
});
}
}

0 comments on commit 31d077e

Please sign in to comment.