Skip to content

Commit

Permalink
test(egf): add/update tests (char escapes)
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 19, 2020
1 parent 3c676aa commit 7691650
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 29 deletions.
28 changes: 14 additions & 14 deletions packages/egf/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ export const toEGFNode = (
return res.join("\n");
};

export const toEGFProp = (_: string, x: any) =>
isString(x)
? x.indexOf("\n") >= 0
? `>>>${x}<<<`
: x
: isNumber(x)
? `#num ${x}`
: isDate(x)
? `#date ${x.toISOString()}`
: isTypedArray(x)
export const toEGFProp = (_: string, val: any) =>
isString(val)
? val.indexOf("\n") >= 0
? `>>>${val}<<<`
: val
: isNumber(val)
? `#num ${val}`
: isDate(val)
? `#date ${val.toISOString()}`
: isTypedArray(val)
? `#base64 ${base64Encode(
new Uint8Array(x.buffer, x.byteOffset, x.byteLength)
new Uint8Array(val.buffer, val.byteOffset, val.byteLength)
)}`
: isArray(x) || isPlainObject(x)
? `#json ${JSON.stringify(x)}`
: x;
: isArray(val) || isPlainObject(val)
? `#json ${JSON.stringify(val)}`
: String(val);
91 changes: 91 additions & 0 deletions packages/egf/test/escape.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import * as assert from "assert";
import { ParseContext, parseString } from "../src";

const $ctx: Partial<ParseContext> = { opts: { prefixes: true } };

describe("escape", () => {
it("node id", () => {
assert.deepStrictEqual(parseString(`\\u0046oo`).nodes, {
Foo: { $id: "Foo" },
});
});

it("node id (qfn)", () => {
assert.deepStrictEqual(
parseString(`@prefix foo: FOO\n\\u0066oo:\\u0062ar`, $ctx).nodes,
{
FOObar: { $id: "FOObar" },
}
);
});

it("prefix decl", () => {
assert.strictEqual(
parseString(`@prefix \\u0066oo: \\u0046OO`, $ctx).prefixes.foo,
"FOO"
);
});

it("prop name (qfn)", () => {
assert.deepStrictEqual(
parseString(`@prefix a: foo\nx\n\t\\u0061:\\u0062ar baz`, $ctx)
.nodes,
{
x: { $id: "x", foobar: "baz" },
}
);
});

it("tag id", () => {
assert.deepStrictEqual(parseString(`a\n\tfoo #\\u006eum 42`).nodes, {
a: { $id: "a", foo: 42 },
});
});

it("string value", () => {
assert.deepStrictEqual(parseString(`a\n\tfoo \\u0062ar`).nodes, {
a: { $id: "a", foo: "bar" },
});
});

it("string multi-line value", () => {
assert.deepStrictEqual(
parseString(`a\n\tfoo >>>abc\\ndef\nghi<<<`).nodes,
{
a: { $id: "a", foo: "abc\ndef\nghi" },
}
);
});

it("#list multi-line value", () => {
assert.deepStrictEqual(
parseString(`a\n\tfoo #list >>>\nabc\\ndef\nghi<<<`).nodes,
{
a: { $id: "a", foo: ["abc\ndef", "ghi"] },
}
);
});

it("ref", () => {
assert.deepStrictEqual(
parseString(`a\n\tfoo -> \\u0062`, { opts: { resolve: true } })
.nodes,
{
a: { $id: "a", foo: { $id: "b" } },
b: { $id: "b" },
}
);
});

it("ref <>", () => {
assert.deepStrictEqual(
parseString(`a\n\tfoo -> <\\u0062:b>`, {
opts: { prefixes: true, resolve: true },
}).nodes,
{
a: { $id: "a", foo: { $id: "b:b" } },
"b:b": { $id: "b:b" },
}
);
});
});
21 changes: 12 additions & 9 deletions packages/egf/test/prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ describe("@prefix", () => {
assert.throws(() => parseString(`@prefix :`, $ctx), "1");
assert.throws(() => parseString(`@prefix : `, $ctx), "2");
assert.throws(() => parseString(`@prefix a&b: abc`, $ctx), "3");
assert.equal(parseString(`@prefix : abc`, $ctx).prefixes[""], "abc");
assert.strictEqual(
parseString(`@prefix : abc`, $ctx).prefixes[""],
"abc"
);
// prettier-ignore
assert.equal(parseString(`@prefix _: abc`, $ctx).prefixes["_"], "abc");
assert.strictEqual(parseString(`@prefix _: abc`, $ctx).prefixes["_"], "abc");
// prettier-ignore
assert.equal(parseString(`@prefix $1a-b_C: abc`, $ctx).prefixes["$1a-b_C"], "abc");
assert.strictEqual(parseString(`@prefix $1a-b_C: abc`, $ctx).prefixes["$1a-b_C"], "abc");
});

it("qfn", () => {
const qfn = (id: string) =>
qualifiedID({ "": "self/", thi: "thi.ng/" }, id);
assert.equal(qfn(":a"), "self/a");
assert.equal(qfn("thi:a"), "thi.ng/a");
assert.strictEqual(qfn(":a"), "self/a");
assert.strictEqual(qfn("thi:a"), "thi.ng/a");
assert.throws(() => qfn("foo:a"));
});

Expand All @@ -36,9 +39,9 @@ thi:b
`,
{ opts: { prefixes: true, resolve: true } }
);
assert.equal(prefixes[""], "self/");
assert.equal(prefixes["thi"], "thi.ng/");
assert.equal(nodes["self/a"].partof.$id, "thi.ng/b");
assert.equal(nodes["thi.ng/b"].parentof.$id, "self/a");
assert.strictEqual(prefixes[""], "self/");
assert.strictEqual(prefixes["thi"], "thi.ng/");
assert.strictEqual(nodes["self/a"].partof.$id, "thi.ng/b");
assert.strictEqual(nodes["thi.ng/b"].parentof.$id, "self/a");
});
});
10 changes: 5 additions & 5 deletions packages/egf/test/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ thi:c
`,
{ opts: { prefixes: true, resolve: true } }
).nodes;
assert.deepEqual(db["thi.ng/a"].partof, { $id: "thi.ng/b" });
assert.equal(db["thi.ng/a"].knows.$id, "alt.thi.ng/c");
assert.equal(db["alt.thi.ng/c"].diff.$id, "alt.thi.ng/a");
assert.deepStrictEqual(db["thi.ng/a"].partof, { $id: "thi.ng/b" });
assert.strictEqual(db["thi.ng/a"].knows.$id, "alt.thi.ng/c");
assert.strictEqual(db["alt.thi.ng/c"].diff.$id, "alt.thi.ng/a");
});

it("resolve circular", () => {
Expand All @@ -35,8 +35,8 @@ b
`,
{ opts: { resolve: true } }
).nodes;
assert.equal(db.a.knows.$id, "b");
assert.equal(db.b.knows.$id, "a");
assert.strictEqual(db.a.knows.$id, "b");
assert.strictEqual(db.b.knows.$id, "a");
});

it("ref array item (unresolved)", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/egf/test/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("serialize", () => {
}
);
writeFileSync("out.egf", res);
assert.equal(
assert.strictEqual(
res,
`@prefix thi: http://thi.ng/
@prefix schema: http://schema.org/
Expand Down

0 comments on commit 7691650

Please sign in to comment.