-
Notifications
You must be signed in to change notification settings - Fork 2
feat: TermWrapper.from static factory with intersection return type #83
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
Draft
jeswr
wants to merge
1
commit into
rdfjs:main
Choose a base branch
from
jeswr:feat/term-wrapper-from
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import assert from "node:assert" | ||
| import { describe, it } from "node:test" | ||
| import { DataFactory } from "n3" | ||
| import { TermWrapper } from "@rdfjs/wrapper" | ||
| import { Child } from "./model/Child.js" | ||
| import { datasetFromRdf } from "./util/datasetFromRdf.js" | ||
| import { Example } from "./vocabulary/Example.js" | ||
| import type { NamedNode, Quad_Subject, Term } from "@rdfjs/types" | ||
|
|
||
| const rdf = ` | ||
| prefix : <https://example.org/> | ||
|
|
||
| <x> :hasString "string 1" . | ||
| ` | ||
|
|
||
| await describe("Term Wrapper from", async () => { | ||
| const dataset = datasetFromRdf(rdf) | ||
|
|
||
| await describe("Runtime behaviour", async () => { | ||
| await it("creates an instance of the subclass it is invoked on", async () => { | ||
| const child = Child.from("x", dataset, DataFactory) | ||
|
|
||
| assert.equal(child instanceof Child, true) | ||
| assert.equal(child instanceof TermWrapper, true) | ||
| }) | ||
|
|
||
| await it("creates an instance of the base class when invoked on it", async () => { | ||
| const wrapper = TermWrapper.from("x", dataset, DataFactory) | ||
|
|
||
| assert.equal(wrapper instanceof TermWrapper, true) | ||
| }) | ||
|
|
||
| await it("wraps a string as a named node, like the constructor", async () => { | ||
| const child = Child.from("x", dataset, DataFactory) | ||
| const constructed = new Child("x", dataset, DataFactory) | ||
|
|
||
| assert.equal(child.termType, "NamedNode") | ||
| assert.equal(child.value, "x") | ||
| assert.equal(child.equals(constructed as Term), true) | ||
| assert.equal(child.equals(DataFactory.namedNode("x")), true) | ||
| }) | ||
|
|
||
| await it("wraps a given term instance, like the constructor", async () => { | ||
| const child = Child.from(DataFactory.blankNode("b1"), dataset, DataFactory) | ||
|
|
||
| assert.equal(child.termType, "BlankNode") | ||
| assert.equal(child.value, "b1") | ||
| }) | ||
|
|
||
| await it("exposes accessors of the subclass", async () => { | ||
| const child = Child.from("x", dataset, DataFactory) | ||
|
|
||
| assert.equal(child.hasString, "string 1") | ||
| }) | ||
|
|
||
| await it("keeps references to the dataset and factory", async () => { | ||
| const child = Child.from("x", dataset, DataFactory) | ||
|
|
||
| assert.equal(child.dataset, dataset) | ||
| assert.equal(child.factory, DataFactory) | ||
| }) | ||
| }) | ||
|
|
||
| await describe("Intersection return type", async () => { | ||
| await it("is assignable to term types without casts", async () => { | ||
| const child = Child.from("x", dataset, DataFactory) | ||
|
|
||
| // Compile-time assertions: no casts in any of the assignments below | ||
| const node: NamedNode = child | ||
| const subject: Quad_Subject = child | ||
|
|
||
| assert.equal(node.equals(subject), true) | ||
| }) | ||
|
|
||
| await it("can be used to create quads without casts", async () => { | ||
| const child = Child.from("x", dataset, DataFactory) | ||
| const quad = DataFactory.quad(child, DataFactory.namedNode(Example.hasString), DataFactory.literal("string 2")) | ||
|
|
||
| assert.equal(quad.subject.equals(child), true) | ||
| }) | ||
|
|
||
| await it("can be used to match quads in a dataset without casts", async () => { | ||
| const child = Child.from("x", dataset, DataFactory) | ||
| const matches = dataset.match(child) | ||
|
|
||
| assert.equal(matches.size, 1) | ||
| }) | ||
|
|
||
| await it("preserves the term type of the wrapped term", async () => { | ||
| const literal = DataFactory.literal("some value", "en") | ||
| const wrapped = Child.from(literal, dataset, DataFactory) | ||
|
|
||
| // Compile-time assertion: literal members are available without casts | ||
| assert.equal(wrapped.language, "en") | ||
| assert.equal(wrapped.datatype.value, "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString") | ||
| }) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can (and should) we do further type narrowing here such that the type of
termisTwhich extends string and the return type isNamedNode<T>