Unverified Commit a7b15339 authored by Hong Minhee's avatar Hong Minhee
Browse files

More tests

parent d37bc504
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
.cov.lcov
.DS_Store
.test-report.xml
coverage/
deno.lock
docs/*.css
docs/*.html
+17541 −0

File added.

Preview size limit exceeded, changes collapsed.

+15 −1
Original line number Diff line number Diff line
import { assertEquals } from "jsr:@std/assert@^0.218.2";
import { sortTopologically } from "./class.ts";
import { dirname, join } from "jsr:@std/path@^0.218.2";
import { assertSnapshot } from "jsr:@std/testing@^0.218.2/snapshot";
import { generateClasses, sortTopologically } from "./class.ts";
import { loadSchemaFiles } from "./schema.ts";

Deno.test("sortTopologically()", () => {
  const sorted = sortTopologically({
@@ -59,3 +62,14 @@ Deno.test("sortTopologically()", () => {
    ],
  );
});

Deno.test("generateClasses()", async (t) => {
  const schemaDir = join(dirname(import.meta.dirname!), "vocab");
  const runtimePath = "../runtime/";
  const types = await loadSchemaFiles(schemaDir);
  let entireCode = "";
  for await (const code of generateClasses(types, runtimePath)) {
    entireCode += code;
  }
  await assertSnapshot(t, entireCode);
});
+16 −6
Original line number Diff line number Diff line
@@ -49,7 +49,9 @@ export async function* generateEncoder(
    if (!areAllScalarTypes(property.range, types)) {
      yield 'v instanceof URL ? { "@id": v.href } : ';
    }
    for (const code of getEncoders(property.range, types, "v")) yield code;
    for (const code of getEncoders(property.range, types, "v", "options")) {
      yield code;
    }
    yield `
      );
    }
@@ -100,9 +102,13 @@ export async function* generateDecoder(
    if (globalThis.Object.keys(json).length == 0) {
      values = {};
    } else {
      const expanded = await jsonld.expand(json, options);
      const expanded = await jsonld.expand(json, {
        ...options,
        keepFreeFloatingNodes: true,
      });
      values =
        // deno-lint-ignore no-explicit-any
      values = expanded[0] as (Record<string, any[]> & { "@id"?: string });
        (expanded[0] ?? {}) as (Record<string, any[]> & { "@id"?: string });
    }
  `;
  const subtypes = getSubtypes(typeUri, types, true);
@@ -154,12 +160,16 @@ export async function* generateDecoder(
      `;
    }
    if (property.range.length == 1) {
      yield `${variable}.push(${getDecoder(property.range[0], types, "v")})`;
      yield `${variable}.push(${
        getDecoder(property.range[0], types, "v", "options")
      })`;
    } else {
      yield `
      const decoded =
      `;
      for (const code of getDecoders(property.range, types, "v")) yield code;
      for (const code of getDecoders(property.range, types, "v", "options")) {
        yield code;
      }
      yield `
      ;
      if (typeof decoded === "undefined") continue;
+12 −0
Original line number Diff line number Diff line
@@ -91,6 +91,9 @@ export async function* generateConstructor(
      yield `
        if ("${property.pluralName}" in values && \
            values.${property.pluralName} != null) {
      `;
      if (property.singularAccessor) {
        yield `
          if ("${property.singularName}" in values &&
              values.${property.singularName} != null) {
            throw new TypeError(
@@ -98,6 +101,9 @@ export async function* generateConstructor(
                "${property.pluralName} at the same time.",
            );
          }
        `;
      }
      yield `
          this.${fieldName} = values.${property.pluralName};
        }
      `;
@@ -144,6 +150,9 @@ export async function* generateCloner(
      yield `
        if ("${property.pluralName}" in values && \
            values.${property.pluralName} != null) {
      `;
      if (property.singularAccessor) {
        yield `
          if ("${property.singularName}" in values &&
              values.${property.singularName} != null) {
            throw new TypeError(
@@ -151,6 +160,9 @@ export async function* generateCloner(
                "${property.pluralName} at the same time.",
            );
          }
        `;
      }
      yield `
          clone.${fieldName} = values.${property.pluralName};
        }
      `;
Loading