Skip to content

Commit 15b2530

Browse files
Merge pull request #13 from iolanta-tech/html-manifest-te004-failing
html manifest te004 failing
2 parents c72bc7e + 4059d0d commit 15b2530

File tree

6 files changed

+37
-7
lines changed

6 files changed

+37
-7
lines changed

ldtest/facets.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ def show(self) -> Iterable[TestCase]:
1717
rows = self.stored_query('tests.sparql', test_class=self.iri)
1818
for row in rows:
1919
test_url = URL(row['test'])
20+
21+
try:
22+
extract_all_scripts = row['extract_all_scripts'].value
23+
except KeyError:
24+
extract_all_scripts = False
25+
2026
yield TestCase(
2127
test=f'{test_url.name}#{test_url.fragment}',
2228
input=URL(row['input']),
2329
result=self._process_result(row['result']),
2430
req=(req := row.get('req')) and req.value,
31+
extract_all_scripts=extract_all_scripts,
2532
)
2633

2734
def _process_result(

ldtest/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class TestCase:
1010
input: Path
1111
result: Path | str | Exception # noqa: WPS110
1212
req: str
13+
extract_all_scripts: bool = False
1314

1415
@property
1516
def raw_document(self) -> bytes:

ldtest/sparql/tests.sparql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SELECT ?test ?result ?input ?req WHERE {
1+
SELECT ?test ?result ?input ?req ?extract_all_scripts WHERE {
22
?test
33
a $test_class ;
44
mf:result ?result ;
@@ -8,6 +8,10 @@ SELECT ?test ?result ?input ?req WHERE {
88
?test jld:req ?req .
99
}
1010

11+
OPTIONAL {
12+
?test jld:option / jld:extractAllScripts ?extract_all_scripts .
13+
}
14+
1115
# FIXME: We mimic how `pyld` tests work, skipping 1.0 tests. This is probably incorrect though.
1216
FILTER NOT EXISTS {
1317
?test jld:option / jld:specVersion "json-ld-1.0"^^xsd:string .

tests/test_specification.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def test_to_rdf(test_case: TestCase):
3939
def test_expand(test_case: TestCase):
4040
if isinstance(test_case.result, str):
4141
try:
42-
expanded_document = yaml_ld.expand(test_case.input)
42+
expanded_document = yaml_ld.expand(
43+
test_case.input,
44+
extract_all_scripts=test_case.extract_all_scripts,
45+
)
4346
except YAMLLDError as error:
4447
assert error.code == test_case.result
4548
else:
@@ -51,7 +54,10 @@ def test_expand(test_case: TestCase):
5154

5255
elif isinstance(test_case.result, Path):
5356
expected = yaml_ld.parse(test_case.result.read_text())
54-
actual = yaml_ld.expand(test_case.input)
57+
actual = yaml_ld.expand(
58+
test_case.input,
59+
extract_all_scripts=test_case.extract_all_scripts,
60+
)
5561
assert actual == expected
5662
else:
5763
raise ValueError(f'What to do with this test? {test_case}')

yaml_ld/expand.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def expand( # noqa: C901, WPS211
3636
if isinstance(document, Path) and base is None:
3737
base = f'file://{document.parent}/'
3838

39-
document = parse(document)
39+
document = parse(document, extract_all_scripts=extract_all_scripts)
4040

4141
options = ExpandOptions(
4242
base=base,

yaml_ld/parse.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
UndefinedAliasFound, MappingKeyError, InvalidEncoding,
1515
)
1616
from yaml_ld.loader import YAMLLDLoader
17-
from yaml_ld.models import Document, DocumentType
17+
from yaml_ld.models import Document, DocumentType, ExtractAllScripts
1818

1919

2020
def try_extracting_yaml_from_html(
@@ -34,7 +34,11 @@ def try_extracting_yaml_from_html(
3434
yield script.text
3535

3636

37-
def _parse_html(html_string: str, fragment: str | None) -> Document:
37+
def _parse_html(
38+
html_string: str,
39+
fragment: str | None,
40+
extract_all_scripts: ExtractAllScripts = False,
41+
) -> Document:
3842
"""Parse all YAML-LD scripts embedded into HTML."""
3943
html_yaml_scripts = list(try_extracting_yaml_from_html(
4044
html_string,
@@ -44,12 +48,16 @@ def _parse_html(html_string: str, fragment: str | None) -> Document:
4448
if not html_yaml_scripts:
4549
return {}
4650

51+
if extract_all_scripts:
52+
return [parse(script) for script in html_yaml_scripts]
53+
4754
first_script, *_other_scripts = html_yaml_scripts
4855
return parse(first_script)
4956

5057

5158
def parse( # noqa: WPS238, WPS231, C901
5259
raw_document: str | bytes | Path | URL,
60+
extract_all_scripts: ExtractAllScripts = False,
5361
) -> Document:
5462
"""Parse a YAML-LD document."""
5563
document_type = None
@@ -83,7 +91,11 @@ def parse( # noqa: WPS238, WPS231, C901
8391
)
8492
except ScannerError as err:
8593
if document_type != DocumentType.YAML:
86-
return _parse_html(raw_document, fragment=fragment)
94+
return _parse_html(
95+
raw_document,
96+
fragment=fragment,
97+
extract_all_scripts=extract_all_scripts,
98+
)
8799

88100
raise LoadingDocumentFailed() from err
89101

0 commit comments

Comments
 (0)