Skip to content

Commit 87c44c3

Browse files
committed
add tests for processor-graph outputs, close one TODO
1 parent d81a457 commit 87c44c3

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

html2rdf/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ struct LocalScope<'a> {
286286

287287
enum CurieError {
288288
EmptyCurie,
289+
InvalidBlankNodeSuffix(String),
289290
InvalidIRI(String),
290291
ExpansionError(curie::ExpansionError),
291292
}
@@ -399,7 +400,9 @@ impl<'b> LocalScope<'b> {
399400
return Ok(self.eval_context.empty_bnode.clone().into());
400401
}
401402

402-
return Ok(oxrdf::BlankNode::new(suffix).expect("TODO").into());
403+
return Ok(oxrdf::BlankNode::new(suffix)
404+
.map_err(|_| CurieError::InvalidBlankNodeSuffix(suffix.to_string()))?
405+
.into());
403406
}
404407

405408
Curie::new(Some(prefix), suffix)
@@ -433,6 +436,14 @@ impl<'b> LocalScope<'b> {
433436
match err {
434437
CurieError::EmptyCurie
435438
| CurieError::ExpansionError(ExpansionError::MissingDefault) => {}
439+
CurieError::InvalidBlankNodeSuffix(suffix) => {
440+
(self.emit_warning)(
441+
PGType::UnresolvedCurie,
442+
format!(
443+
"Invalid CURIE: {value} (invalid blank node suffix: `{suffix}`)",
444+
),
445+
);
446+
}
436447
CurieError::InvalidIRI(iri) => {
437448
(self.emit_warning)(
438449
PGType::UnresolvedCurie,

html2rdf/tests/processor-graph.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
use html2rdf::parse;
2+
use oxiri::Iri;
3+
use oxrdf::Graph;
4+
5+
mod utils;
6+
7+
fn base() -> Iri<String> {
8+
Iri::parse("http://example.test/".to_string()).unwrap()
9+
}
10+
11+
fn parse_html(html: &str) -> (Graph, Graph) {
12+
let mut output_graph = Graph::new();
13+
let mut processor_graph = Graph::new();
14+
parse(html, base(), &mut output_graph, &mut processor_graph).unwrap();
15+
(output_graph, processor_graph)
16+
}
17+
18+
#[test]
19+
fn invalid_blank_node_suffix_emits_warning() {
20+
let html = r#"<!DOCTYPE html>
21+
<html>
22+
<head><title>test</title></head>
23+
<body>
24+
<div about="[_:invalid blank]" property="http://example.test/p">value</div>
25+
</body>
26+
</html>"#;
27+
28+
let (_output, processor) = parse_html(html);
29+
insta::assert_snapshot!(utils::serialize_graph(processor, base().as_str()), @r#"
30+
@base <http://example.test/> .
31+
@prefix rdf: <//www.w3.org/1999/02/22-rdf-syntax-ns#> .
32+
@prefix rdfa: <//www.w3.org/ns/rdfa#> .
33+
@prefix dc: <//purl.org/dc/terms/> .
34+
_:c14n0 a rdfa:UnresolvedCurie ;
35+
dc:description "Invalid CURIE: [_:invalid blank] (invalid blank node suffix: `invalid blank`)" .
36+
"#);
37+
}
38+
39+
#[test]
40+
fn undefined_prefix_in_safe_curie_emits_warning() {
41+
let html = r#"<!DOCTYPE html>
42+
<html>
43+
<head><title>test</title></head>
44+
<body>
45+
<div about="[undefined:thing]" property="http://example.test/p">value</div>
46+
</body>
47+
</html>"#;
48+
49+
let (_output, processor) = parse_html(html);
50+
insta::assert_snapshot!(utils::serialize_graph(processor, base().as_str()), @r#"
51+
@base <http://example.test/> .
52+
@prefix rdf: <//www.w3.org/1999/02/22-rdf-syntax-ns#> .
53+
@prefix rdfa: <//www.w3.org/ns/rdfa#> .
54+
@prefix dc: <//purl.org/dc/terms/> .
55+
_:c14n0 a rdfa:UnresolvedCurie ;
56+
dc:description "Invalid CURIE: [undefined:thing] (no such prefix defined)" .
57+
"#);
58+
}
59+
60+
#[test]
61+
fn invalid_iri_from_curie_expansion_emits_warning() {
62+
let html = r#"<!DOCTYPE html>
63+
<html>
64+
<head><title>test</title></head>
65+
<body prefix="bad: http://example.test/invalid{iri}/">
66+
<div about="[bad:thing]" property="http://example.test/p">value</div>
67+
</body>
68+
</html>"#;
69+
70+
let (_output, processor) = parse_html(html);
71+
insta::assert_snapshot!(utils::serialize_graph(processor, base().as_str()), @r#"
72+
@base <http://example.test/> .
73+
@prefix rdf: <//www.w3.org/1999/02/22-rdf-syntax-ns#> .
74+
@prefix rdfa: <//www.w3.org/ns/rdfa#> .
75+
@prefix dc: <//purl.org/dc/terms/> .
76+
_:c14n0 a rdfa:UnresolvedCurie ;
77+
dc:description "Invalid CURIE: [bad:thing] (expanded to invalid IRI value <http://example.test/invalid{iri}/thing>)" .
78+
"#);
79+
}
80+
81+
#[test]
82+
fn empty_safe_curie_silently_ignored() {
83+
let html = r#"<!DOCTYPE html>
84+
<html>
85+
<head><title>test</title></head>
86+
<body>
87+
<div about="[]" property="http://example.test/p">value</div>
88+
</body>
89+
</html>"#;
90+
91+
let (_output, processor) = parse_html(html);
92+
insta::assert_snapshot!(utils::serialize_graph(processor, base().as_str()), @"");
93+
}
94+
95+
#[test]
96+
fn missing_default_prefix_in_safe_curie_silently_ignored() {
97+
let html = r#"<!DOCTYPE html>
98+
<html>
99+
<head><title>test</title></head>
100+
<body>
101+
<div about="[:thing]" property="http://example.test/p">value</div>
102+
</body>
103+
</html>"#;
104+
105+
let (_output, processor) = parse_html(html);
106+
insta::assert_snapshot!(utils::serialize_graph(processor, base().as_str()), @"");
107+
}

0 commit comments

Comments
 (0)